ChatGPT解决这个技术问题 Extra ChatGPT

Is CSS Turing complete?

CSS isn't, insofar as I know, Turing complete. But my knowledge of CSS is very limited.

Is CSS Turing complete?

Are any of the existing draft or committees considering language features that might enable Turing completeness if it isn't right now?

It's already been done, if you use ie6. They're called CSS expressions, and consensus is they are horribly broken and dangerous. JS embedded in CSS...
@Kibibu - Yikes! Please erase that idea from my brain before it folds on itself!
How could CSS possibly be Turing-complete?
@DVK: you could actually do some cool things with them - particularly with regards to resolution independent layout - that are still tricky or quirky in CSS without resorting to tables. I think if they'd limited it to be strictly a declarative expression language with no side-effects instead of allowing full access to the script engine it would have been better received (and maybe also if webkit had come up with it first)
@SLaks: Don't underestimate the power of HTML5/CSS3 :)

9
9 revs, 8 users 46%

You can encode Rule 110 in CSS3, so it's Turing-complete so long as you consider an appropriate accompanying HTML file and user interactions to be part of the “execution” of CSS. A pretty good implementation is available, and another implementation is included here:

body { -webkit-animation: bugfix infinite 1s; margin: 0.5em 1em; } @-webkit-keyframes bugfix { from { padding: 0; } to { padding: 0; } } /* * 111 110 101 100 011 010 001 000 * 0 1 1 0 1 1 1 0 */ body > input { -webkit-appearance: none; display: block; float: left; border-right: 1px solid #ddd; border-bottom: 1px solid #ddd; padding: 0px 3px; margin: 0; font-family: Consolas, "Courier New", monospace; font-size: 7pt; } body > input::before { content: "0"; } p { font-family: Verdana, sans-serif; font-size: 9pt; margin-bottom: 0.5em; } body > input:nth-of-type(-n+30) { border-top: 1px solid #ddd; } body > input:nth-of-type(30n+1) { border-left: 1px solid #ddd; clear: left; } body > input::before { content: "0"; } body > input:checked::before { content: "1"; } body > input:checked { background: #afa !important; } input:not(:checked) + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input::before { content: "1"; } input:not(:checked) + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input { background: #fa0; } input:checked + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input::before { content: "1"; } input:checked + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input { background: #fa0; } input:checked + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input::before { content: "1"; } input:checked + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input { background: #fa0; } input:checked + input:checked + input:checked + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input::before { content: "0"; } input:checked + input:checked + input:checked + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input { background: #fff; } input:not(:checked) + input:not(:checked) + input:not(:checked) + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input::before { content: "0"; } input:not(:checked) + input:not(:checked) + input:not(:checked) + *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ input { background: #fff; } body > input:nth-child(30n) { display: none !important; } body > input:nth-child(30n) + label { display: none !important; }

Rule 110 in (webkit) CSS, proving Turing-completeness.


The formal definition (simplest) of Turing Machine is simply a tuple of states set, symbol set, initial state, accepting states set and a transition function. There is no crank in it. By computation we mean somebody needs to apply the transition function faithfully on the tape which is exactly like the clicking in this case. More formally, a model of computation can be viewed as a set of rules somebody needs to follow to do the computation. In that sense, I think CSS is Turing-Complete.
This snippet doesn't appear to work (Firefox 61). I see a grid of empty checkboxes - nothing happens if you check them.
@John "CSS is turing complete" makes me uneasy, but I have no problem with "CSS is as turing complete as a couple of rocks on the beach". Would the latter statement be correct?
@R.Schmitz no, in that case the human is choosing where to put the rocks, so it is the combined system of human+rocks that is turing complete. In the CSS example above, the tab+space keypresses is a simple repeating process, similar to say, a feedback circuit. So if C++ is turing complete using computer hardware to execute instructions, then it's not a stretch to say CSS is turing complete using repeating keypresses to execute instructions
Say that “[CSS] is Turing-complete so long as you consider an appropriate accompanying HTML file and user interactions to be part of the “execution” of CSS” more or less sounds like “it's Turing-complete so long as you consider it to mean it can display a red border”.
L
Lambda Fairy

One aspect of Turing completeness is the halting problem.

This means that, if CSS is Turing complete, then there's no general algorithm for determining whether a CSS program will finish running or loop forever.

But we can derive such an algorithm for 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. Otherwise, it will halt.

If any animation-iteration-count is infinite, and the containing selector is matched in the HTML, then it will not halt.

Otherwise, it will halt.

That's it. Since we just solved the halting problem for CSS, it follows that CSS is not Turing complete.

(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.)

Daniel Wagner brought up a point that I missed in the original answer. 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:

@media queries can only check properties of the browser itself, such as viewport size or pixel resolution. These properties can change via user interaction or JavaScript code (e.g. resizing the browser window), but not through CSS alone.

::before and ::after pseudo-elements are not considered part of the DOM, and cannot be matched in any other way.

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. 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. 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. 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. If there were a way to write an infinite loop using CSS, then they probably would have found it already 😉


When people say "CSS is turing complete" they mean "CSS, which supports animations is Turing Complete". You can restrict programming languages and conclude they are not Turing Complete using your logic, but you would have to specify the restrictions.
I think this answer is using a funny definition of "halt" -- certainly not the one I would want if I were asking the question, at least. You seem to use "halt" to mean "there is a time at which the computed properties for each element stop changing"; but I would want "halt" to mean "the algorithm which transforms declarative CSS into the computed properties of all elements finishes running". With the latter definition, it seems there needs to be significantly more argument than just "there are no animations".
Your logic is backwards here. Turing completeness implies that halting is undecidable doesn't mean that halting is undecidable implies Turing completeness.
@LambdaFairy what is the halting problem for CSS? I don't believe such a thing exists. The halting problem is only relevant to machines. The most powerful computational model we have today are Turing Machines. Your computer is as powerful as a Turing Machine (w/o infinite memory). CSS alone can not be defined as a machine. Perhaps you can define the CSS engine of a browser as a machine, but even then it can only be as powerful as a TM. The statement "halting problem for CSS" just simply doesn't make any sense, unless CSS has become a new automaton in Chomsky's hierarchy.
@LambdaFairy You're misunderstanding what a proof of turing incompleteness would look like. We know that the halting problem is undecidable by a TM. The original proof seems to make the claim because CSS can solve the halting problem it is not turing complete. If CSS could truly solve the halting problem, then CSS is stronger than a Turing Machine because it could compute something a TM can't. We know (by the Church-Turing Thesis), that a machine can't be built that's stronger than a TM/The Lambda Calculus. Therefore, this is a contradiction, and your original statement isn't correct.
D
DVK

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. There is no capability to define a function in 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; so you cannot even model the structure that represents the program to be interpreted in CSS.


CSS isn't executable in any way. The person who wrote the quoted comment doesn't seem to understand that. :-\
CSS is a set of instructions to a processor (layout engine). What's so not "executable" about it?
Turing-completeness is not just about if you can write programs the way you want or a belief. It is a mathematical property about calculability. So you cannot believe or not that CSS is Turing-complete, you need a proof. In this case, because of the rule 110, CSS is Turing-complete.
@MikaëlMayer - as noted in many comments in the "110" answer, that requires user to perform an action. If user actions are requred, CSS without the user is NOT Turing complete
@DVK the repeating keypresses required by the CSS 110 example are not quite a user "action", they can be performed by a repeating digital circuit. Actual turing machines require some sort of electrical hardware to drive execution, so I don't see how this is any different
M
Maurício Szabo

Turing-completeness is not only about "defining functions" or "have ifs/loops/etc". For example, Haskell doesn't have "loop", lambda-calculus don't have "ifs", etc...

For example, this site: http://experthuman.com/programming-with-nothing. The author uses Ruby and create a "FizzBuzz" program with only closures (no strings, numbers, or anything like that)...

There are examples when people compute some arithmetical functions on Scala using only the type system

So, yes, in my opinion, CSS3+HTML is turing-complete (even if you can't exactly do any real computation with then without becoming crazy)


Haskell and lambda-caclculus have recursion. Does CSS? You have to be crazy to do any real computation in Malbolge; in CSS, it doesn't matter how crazy you are: it won't work, CSS is not turing-complete, and that's not a matter of opinion.
Sorry, I couldn't find any other comment of yours in this page. But as noted in the accepted answer it is Turing-complete " (...) so long as you consider (...) user interactions to be part of the “execution” of CSS". And I don't.
Without intending to be offensive, turing completeness does not respond to anyone's opinion.
@MaurícioSzabo No, CSS3 plus HTML plus a human being continually stepping the simulation is Turing complete.
@LambdaFairy That's also true of every physical turing machine too.
H
Henrik Sommerland

The fundamental issue here is that any machine written in HTML+CSS cannot evaluate infinitely many steps (i.e there can be no "real" recursion) unless the code is infinitely long. And the question will this machine reach configuration H in n steps or less is always answerable if n is finite.


I don't see where in the Turing Machine requirements the ability to process infinite loops is specified. Your second point doesn't appear to be valid. While Turing used his Turing Machine to prove issues of computability, these rules, like the halting problem, do not determine whether a machine is a turing machine or not. If the Turing Machine included these hypothesis as requirements he couldn't have used the turing machine to prove them.
@AdamDavis It's a totally valid argument: if an algorithm exists that solves the halting problem for a Turing-complete formalism, that's equivalent to solving the halting problem in general. Of course, this has been proven impossible, which means that if the halting problem can be solved for a given formalism, then that formalism must not be Turing-complete. Therefore, all formalisms that can only evaluate a finite number of steps cannot be Turing-complete, CSS included.
It's not if B then A! It's if not B then not A! The former is affirming the consequent, which you correctly point out is wrong. The latter, argument by contrapositive, is what I use and is valid. Note the careful use of negation in my last comment -- I constructed it specifically to avoid that fallacy.
@woojoo666 No. Having being able to transform state according to a set of rules is not the same as being Turing complete. Nothing that can only for a finite number of steps can ever be turing complete. If the set of transformations rules can only be applied a finite number of times the question "Will the system ever reach state H?" is always decidable and it is thus not Turing complete. An implementation of a cellular automaton that can only do a finite number of iterations can never be turing complete.
“and, yes, also CSS are turing complete” [citation needed]
Y
Yankes

This answer is not accurate because it mix description of UTM and UTM itself (Universal Turing Machine).

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. 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. Act of clicking can be UTM, because you do not click at random but only in specific places.

Instead of CSS I could use this text (Rule 110):

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? No this is only input (description) that other UTM (human or computer) can read and run. Clicking is enough to run any 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. 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.


Rule 110 is a UTM. Your text is a (probably slightly insufficient) description of Rule 110, so yes that text is a (representation of a) UTM.
"If CSS could generate clicks" I use a lot of AVR microcontrollers in my projects. You can either use an internal clock, which is limited to 8MHz. Alternatively, you could connect an external crystal and go up to 20MHz. Alternatively I could also just supply a completely external clock so it's not even cpu hardware that's actually driving the crystal. Meaning that I can actually wire up a switch and a piece of circuit to debounce it, and quite literally use that as a clock with me pressing the button. Does that mean it stops being turing complete if I do that?
@CedricMamo But your clicks change state, and you need click only correct places other wise it would not work. Look exactly on eli.fox-epste.in/rule110-full.html I can click ANY cell, if CSS is disabled I still click correct cells and have UTM. If all you do would be clicking one button "next" then indeed CSS would be UTM, but to have something like that you would need have some JS that is call per button click, and as we know JS IS UTM. For CSS you need have something that will correctly interpreted result and update state.
@CedricMamo any link to it? Right now I look on couple of examples from there but it do not work for me. Overall if I understand correctly CSS change visibility of some check boxes and you use tabs to navigate to next one, and here we have again hidden UTM that is not CSS, because when you tab you ask browser to calculate next valid position to navigate, and to od thins it do LOT of complex code, this use CSS but its lot more. This mean you prove that your browser is UTM not CSS.
@CedricMamo But Cellular automata have in its own definition a loop, when state transition is done then its run again, and again etc. If we remove it and leave only this state transition then this automata will not be UTM any more. This will be exactly same situation as CSS. We can define two steps in CA, R - read state, and W - write state, normal CA do infinite sequence RWRWRWRW... in case of CSS we have only R, and we do not have W because it modify things that it can't read, only if we add B - browser action then we could have RBRBRBR... but then BBBBBB is on its own UTM.
R
Ryan Prior

CSS is not a programming language, so the question of turing-completeness is a meaningless one. If programming extensions are added to CSS such as was the case in IE6 then that new synthesis is a whole different thing.

CSS is merely a description of styles; it does not have any logic, and its structure is flat.


Also (IIRC), there is some ambiguity about which styles take precedence when multiple conflicting (duplicate) styles are used. And then there is the slightly different ways that different browsers implement/interpret markup styles to deal with.
"CSS is not a programming language, so the question of turing-completeness is a meaningless one." Tautological sentences are tautological.
Take a look at the programming language Prolog if you wonder why your answer is downvoted so much.