Live data from Hacker News

A proof that Unix utility sed is Turing complete

catonmat.net

61–70 of 109 posts

Re: A proof that Unix utility sed is Turing complete

#61
post #5

sed really is a powerful utility. I once wrote a tool in sed that strips HTML tags leaving just plain text as a fun exercise. Naturally it can't handle complicated cases but for many simple use cases it works. The code, though, is basically unreadable.

Given that sed is Turing complete, I'm sure if you tried harder you would have come up with a general solution ;)

Absolutely. One approach would be to take this TM implementation and program it to parse HTML.

Re: A proof that Unix utility sed is Turing complete

#62
post #23
post #13

In a course on algorithms I took in Moscow State Uni (Russia), we were told there are two ways to describe a Turing machine: - One with a tape & state transition table - A string & a list of string replacements Then we proved this two definitions being equal. And programmed for each ‘system’ a little. I must admit, programming with ‘string replacements’ is much much more fun than doing ‘tape & table’. Is this just so…

In my formal languages course in Germany we covered various computational models and showed the equivalences to Turing Machines. We never built complex TMs by hand, only describing how they would work. I did, however, build larger TMs by hand in a high school course.

Uh in a logic class, I had to build a really large TM that computes the Collatz Function (3n+1 or //2 depending on arity) and it was a very menial task. To debug my TM, I built a simple TM smulator in python and wrote unittests.

Re: A proof that Unix utility sed is Turing complete

#64
post #58
post #5

sed really is a powerful utility. I once wrote a tool in sed that strips HTML tags leaving just plain text as a fun exercise. Naturally it can't handle complicated cases but for many simple use cases it works. The code, though, is basically unreadable.

Isn't it more or less a cliche that using sed etc for stripping HTML tags is unreliable and potentially hazardous? I've tried it. I could get it to apparently "work". But then I'd get some input that hosed it. Now I just use "w3m -dump". I mean, it's a browser.

The unreliability follows from it being "basically unreadable", which leads to it probably not doing what you intend. Whether it is hazardous depends on what you use it for.

Re: A proof that Unix utility sed is Turing complete

#65
post #58
post #5

sed really is a powerful utility. I once wrote a tool in sed that strips HTML tags leaving just plain text as a fun exercise. Naturally it can't handle complicated cases but for many simple use cases it works. The code, though, is basically unreadable.

Isn't it more or less a cliche that using sed etc for stripping HTML tags is unreliable and potentially hazardous? I've tried it. I could get it to apparently "work". But then I'd get some input that hosed it. Now I just use "w3m -dump". I mean, it's a browser.

There's the classic Stack Overflow answer[0] about matching HTML tags. If you have a small subset of HTML, it can work out pretty well.

[0]: https://stackoverflow.com/questions/1732348/regex-match-open...

Re: A proof that Unix utility sed is Turing complete

#66
post #63

And that's a bad thing. It means not only that some sed programs never complete, but it's not even possible to tell whether any given sed program hangs or not. Not something you want from a text substitution utility.

If you just want a text substitution utility, then this is proof that you can implement that by using sed in a controlled manner. Why bemoan something for doing more things than you need it to right now? This is what general-purpose computing is all about.

Re: A proof that Unix utility sed is Turing complete

#67
post #63

And that's a bad thing. It means not only that some sed programs never complete, but it's not even possible to tell whether any given sed program hangs or not. Not something you want from a text substitution utility.

If you just want a text substitution utility, then this is proof that you can implement that by using sed in a controlled manner. Why bemoan something for doing more things than you need it to right now? This is what general-purpose computing is all about.

I do like the thought experiment that I might not want Turing completeness from a language in a specific use case.

Re: A proof that Unix utility sed is Turing complete

#68
post #63

And that's a bad thing. It means not only that some sed programs never complete, but it's not even possible to tell whether any given sed program hangs or not. Not something you want from a text substitution utility.

No, it's not possible to tell if arbitrary programs hang. But it is possible to tell if a program is of a restricted form that is known to terminate. This is how we know the many standard algorithms we frequently use terminate.

Re: A proof that Unix utility sed is Turing complete

#69
post #63

And that's a bad thing. It means not only that some sed programs never complete, but it's not even possible to tell whether any given sed program hangs or not. Not something you want from a text substitution utility.

Never in my life have I been more concerned about whether a simple text substitution is going to halt than whether I can write it down reasonably concisely without having to resort to a more powerful scripting/programming language.

Re: A proof that Unix utility sed is Turing complete

#70
post #21

Earlier quoted context omitted.

You're basically describing compilers. Microsoft Word probably wouldn't run on sed because it requires things outside of the capabilities of a Turing machine, like being able to produce a GUI.

Considering a GUI runs on a turing machine, how is it outside the capabilities of one? Isn't everything we do on computers exactly within the capabilities of a turing machine and just about the ingenuity to create a program that does what we want and about its efficiency? In other words, if sed is turing complete, this means you could run any other turing complete program on it. Producing a GUI is also just telling y…

Note that Turing completeness says nothing about efficiency. It is possible that some models experience exponential slowdown or worse.
Post reply on HN