CSS Turing完成了吗?

本文翻译自:Is CSS Turing complete?

CSS isn't, insofar as I know, Turing complete. 就我所知,CSS并不是图灵完整的。 But my knowledge of CSS is very limited. 但是我对CSS的了解非常有限。

  • Is CSS Turing complete? CSS Turing完成了吗?
  • Are any of the existing draft or committees considering language features that might enable Turing completeness if it isn't right now? 现有的任何草案或委员会是否正在考虑可能不立即实现图灵完整性的语言功能?

#1楼

参考:https://stackoom.com/question/atCy/CSS-Turing完成了吗


#2楼

As per this article, it's not . 根据这篇文章,不是 。 The article also argues that it's not a good idea to make it one. 文章还指出,使其成为一个好主意。

To quote from one of the comments: 引用其中一项评论:

So, I do not believe that CSS is turing complete. 因此,我不认为CSS即将完成。 There is no capability to define a function in CSS. 没有在CSS中定义函数的功能。 In order for a system to be turing-complete it has to be possible to write an interpreter: a function that interprets expressions that denote programs to execute. 为了使系统图腾完整,必须有可能编写一个解释器:一个解释表示要执行程序的表达式的函数。 CSS has no variables that are directly accessible to the user; CSS没有用户可以直接访问的变量。 so you cannot even model the structure that represents the program to be interpreted in CSS. 因此,您甚至无法为表示要在CSS中解释的程序的结构建模。


#3楼

CSS is not a programming language, so the question of turing-completeness is a meaningless one. CSS不是一种编程语言,因此图灵完备性问题是毫无意义的。 If programming extensions are added to CSS such as was the case in IE6 then that new synthesis is a whole different thing. 如果像IE6中那样将编程扩展添加到CSS,那么新的综合就是完全不同的事情。

CSS is merely a description of styles; CSS只是样式的描述; it does not have any logic, and its structure is flat. 它没有任何逻辑,并且结构平坦。


#4楼

One aspect of Turing completeness is that its halting problem is undecidable . 图灵完整性的一个方面是它的暂停问题无法确定

This means there's no general algorithm for determining whether a CSS program will finish running or loop forever. 这意味着没有通用的算法来确定CSS程序将永远完成运行还是循环。

But we can derive such an algorithm for CSS! 但是我们可以为CSS推导这样的算法! Here it is: 这里是:

  • If the stylesheet doesn't declare any animations , then it will halt. 如果样式表未声明任何动画 ,则它将停止。

  • If it does have animations, then: 如果确实有动画,则:

    • If any animation-iteration-count is infinite , and the containing selector is matched in the HTML, then it will not halt. 如果任何animation-iteration-countinfinite ,并且包含的​​选择器在HTML中匹配,则它不会停止。

    • Otherwise, it will halt. 否则,它将停止。

That's it. 而已。 Since we just showed that the halting problem is decidable for CSS, it follows that CSS is not Turing complete . 由于我们只是表明暂停问题对于CSS是可以决定的,因此可以得出结论, CSS 并不是图灵完成的

(Other people have mentioned IE 6, which allows for embedding arbitrary JavaScript expressions in CSS; that will obviously add Turing completeness. But that feature is non-standard, and nobody in their right mind uses it anyway.) (其他人提到了IE 6,它允许在CSS中嵌入任意JavaScript表达式;这显然会增加Turing的完整性。但是该功能是非标准的,反正没有人使用它。)


Daniel Wagner brought up a point that I missed in the original answer. 丹尼尔·瓦格纳(Daniel Wagner)提出了我在原始答案中没有提到的观点。 He notes that while I've covered animations , other parts of the style engine such as selector matching or layout can lead to Turing completeness as well. 他指出,虽然我已经介绍了动画 ,但是样式引擎的其他部分(例如选择器匹配布局)也可以导致图灵完整性。 While it's difficult to make a formal argument about these, I'll try to outline why Turing completeness is still unlikely to happen. 尽管很难就这些问题进行正式辩论,但我将尝试概述为什么仍不太可能实现图灵完整性。

First: Turing complete languages have some way of feeding data back into itself , whether it be through recursion or looping. 第一:图灵完整的语言有某种方式可以将数据反馈回自身 ,无论是通过递归还是循环。 But the design of the CSS language is hostile to this feedback: 但是CSS语言的设计对此反馈意见表示反对:

  • @media queries can only check properties of the browser itself, such as viewport size or pixel resolution. @media查询只能检查浏览器本身的属性,例如视口大小或像素分辨率。 These properties can change via user interaction or JavaScript code (eg resizing the browser window), but not through CSS alone. 这些属性可以通过用户交互或JavaScript代码(例如,调整浏览器窗口的大小)来更改,但不能仅通过CSS来更改。

  • ::before and ::after pseudo-elements are not considered part of the DOM , and cannot be matched in any other way. ::before::after伪元素不被视为DOM的一部分 ,并且不能以任何其他方式进行匹配。

  • Selector combinators can only inspect elements above and before the current element, so they cannot be used to create dependency cycles. 选择器组合器只能检查当前元素之上之前的元素,因此不能用于创建依赖关系循环。

  • It's possible to shift an element away when you hover over it , but the position only updates when you move the mouse. 当您将鼠标悬停在某个元素上时,可以将其移开 ,但是只有当您移动鼠标时,位置才会更新。

That should be enough to convince you that selector matching, on its own, cannot be Turing complete . 这足以说服您选择器匹配本身不能完成图灵完成 But what about layout? 但是布局呢?

The modern CSS layout algorithm is very complex, with features such as Flexbox and Grid muddying the waters. 现代的CSS布局算法非常复杂,具有诸如Flexbox和Grid之类的功能。 But even if it were possible to trigger an infinite loop with layout, it would be hard to leverage this to perform useful computation. 但是,即使有可能触发布局无限循环,也很难利用它来执行有用的计算。 That's because CSS selectors inspect only the internal structure of the DOM, not how these elements are laid out on the screen. 这是因为CSS选择器仅检查DOM的内部结构,而不检查这些元素在屏幕上的布局。 So any Turing completeness proof using the layout system must depend on layout alone . 因此, 任何使用布局系统的图灵完整性证明都必须仅依赖于布局

Finally – and this is perhaps the most important reason – browser vendors have an interest in keeping CSS not Turing complete . 最后-这也许是最重要的原因- 浏览器供应商对保持CSS 完整的Turing感兴趣 By restricting the language, vendors allow for clever optimizations that make the web faster for everyone. 通过限制语言,供应商可以进行巧妙的优化 ,使每个人都能更快地上网。 Moreover, Google dedicates a whole server farm to searching for bugs in Chrome. 此外,Google致力于在整个服务器场中搜索Chrome中的错误。 If there were a way to write an infinite loop using CSS, then they probably would have found it already 如果有一种使用CSS编写无限循环的方法,那么他们可能已经发现它了。


#5楼

The fundamental issue here is that any machine written in HTML+CSS cannot evaluate infinitely many steps (ie there can be no "real" recursion) unless the code is infinitely long. 这里的根本问题是,除非代码是无限长的,否则任何用HTML + CSS编写的机器都不能评估无限多的步骤(即,不可能有“真实的”递归)。 And the question will this machine reach configuration H in n steps or less is always answerable if n is finite. 如果n是有限的,那么该机器将以n步或更短的时间达到配置H的问题总是可以回答的。


#6楼

This answer is not accurate because it mix description of UTM and UTM itself (Universal Turing Machine). 该答案不准确,因为它混合了对UTM和UTM本身(通用图灵机)的描述。

We have good answer but from different perspective and it do not show directly flaws in current top answer. 我们有很好的答案,但是从不同的角度来看,它并没有直接显示当前最佳答案中的缺陷。


First of all we can agree that human can work as UTM. 首先,我们可以同意人类可以作为UTM。 This mean if we do 这意味着如果我们这样做

CSS + Human == UTM

Then CSS part is useless because all work can be done by Human who will do UTM part. 那么CSS部分就没有用了,因为所有工作都可以由将执行UTM部分的Human完成。 Act of clicking can be UTM, because you do not click at random but only in specific places. 单击行为可以是UTM,因为您不是随机单击而是仅在特定位置单击。

Instead of CSS I could use this text ( Rule 110 ): 我可以使用以下文本( 规则110 )代替CSS:

000 -> 0
001 -> 1
010 -> 1
011 -> 1
100 -> 0
101 -> 1
110 -> 1
111 -> 0

To guide my actions and result will be same. 指导我的行动和结果将是相同的。 This mean this text UTM? 这意味着此文本是UTM吗? No this is only input (description) that other UTM (human or computer) can read and run. 不,这仅仅是其他UTM(人或计算机)可以读取和运行的输入(说明)。 Clicking is enough to run any UTM. 单击足以运行任何UTM。


Critical part that CSS lack is ability to change of it own state in arbitrary way, if CSS could generate clicks then it would be UTM. CSS缺乏的关键部分是以任意方式更改其自身状态的能力,如果CSS可以产生点击,那么它将是UTM。 Argument that your clicks are "crank" for CSS is not accurate because real "crank" for CSS is Layout Engine that run it and it should be enough to prove that CSS is UTM. 关于CSS的点击是“曲柄”的说法不正确,因为CSS的真正“曲柄”是运行它的Layout Engine,并且足以证明CSS是UTM。

你可能感兴趣的:(css,turing-complete)