# Is there an algorithm for converting Turing machines into equivalent Lambda expressions?

We know that Turing machines and Lambda Calculus are equivalent in power. And There are proofs for that, I'm sure.

But is there an algorithm, a systematic way for us to convert a Turing machine into a Lambda Calculus expression? Is it impossible to have such algorithm? (meaning does it go down to undecidability or NP-Completeness?) if not, are there any papers or algorithms for doing so?

## 3 Answers

Sure there is. I'm going to assume you can figure out how to convert Haskell into the lambda calculus; for a reference, look at the GHC implementation.

Now just to be clear: a Turing Machine is a (finite) map from (State, Token) pairs to (State, Token, Direction) triples. We'll represent the states as integers (this is okay by the finiteness of the map) and represent the tokens by the values True and False. The directions will be L and R.

The state of the machine is represented by a four-tuple (State, LeftTape, Head, RightTape), where LeftTape and RightTape are lists of tokens and Head is a token.

The initial state for input n is (1, [], True, replicate (n-1) True). The machine halts if it enters state 0. The result is the number of symbols on the tape.

Now it is easy to see that the following defines an interpreter:

data Direction = L | R
type Configuration = (Integer, [Bool], Bool, [Bool])
type TM = Map (Integer, Bool) (Integer, Bool, Direction)

tail' :: [Bool] -> [Bool]
tail' (_:xs) = xs
tail' [] = []

head' :: [Bool] -> Bool
head' (x:_) = x
head' [] = False

step :: TM -> Configuration -> Configuration
step tm (s, lt, h, rt) = (s', lt', h', rt')
where (s', t, d) = tm ! (s, h)
(lt', h', rt') = case d of
L -> (tail' lt, head' lt, t:rt)
R -> (t:lt, head' rt, tail' rt)

run :: TM -> Int -> Int
run tm n = go (1, [], True, replicate (n-1) True)
where go (0, lt, h, rt) = length . filter id $lt ++ [h] ++ rt go c = go (step tm c)  (This implementation may have bugs, but a correct implementation is not far.) Now simply take your favourite Turing Machine, partially apply the run function to it, and convert the resulting Int -> Int term into the lambda calculus. • This is only half an answer. The other conversion is missing: how to simulate$\lambda$-calculus with Turing machines. – Andrej Bauer Oct 24 '15 at 8:57 • @AndrejBauer: Indeed, I'm only answering the question in the title. The other direction is left as an exercise to the reader. – Anton Golov Oct 24 '15 at 9:40 • @AndrejBauer actually I think the answer is complete here, since I did not ask for the proof of equivalency between turing machines and lambda calculus, but only the algorithm f converting turing machines into lambda calculus, and this answer successfully satisfies the latter. – Ashkan Kazemi Oct 24 '15 at 11:55 • You are correct. – Andrej Bauer Oct 26 '15 at 8:00 All proofs of the equivalence of these two models of computation are constructive, that is they describe an algorithm for converting a program from one model of computation to the other. However, I caution you that these proofs are probably rather informal, and may not satisfy you. You may get luckier if you consult original work by computing pioneers (Turing, Church, Post, Kleene and their ilk). • The answer to your original question is: Yes, there is an algorithms, a systematic way to convert a Turing machine into a$\lambda$calculus expression. – Yuval Filmus Oct 23 '15 at 13:08 • well, what is it? – Ashkan Kazemi Oct 23 '15 at 13:32 • I'm not going to spell it out. For me it's enough to know that it exists in principle. If you're interested, you might be able to find it in textbooks or early papers. – Yuval Filmus Oct 23 '15 at 13:34 • Its not popular to try to "find" these models. If there exists one, then it is trivial to prove there are infinite algorithms (just add any number of unused predicates into the system, and you'll get a new algorithm). Thus, we'd have to pick one. Ideally we want the simplest one, but Turing machine/lambda calculus has never been about being the simplest. Thus, every variant and flavor of turing complete language will have a different "best" algorithm. Computer Scientists are generally content with proving that one must exist, because that is enough to do the kinds of proofs... – Cort Ammon Oct 23 '15 at 19:51 • .. they typically want to do. I am reminded of when they proved Rule 110 was Turing complete using cyclic tag codes. It's not the same as proving two fundamental models are equivalent, but the result was still messy enough to make me glad I didn't have to actually find the algorithm myself. – Cort Ammon Oct 23 '15 at 19:53 Since you did not like Yuval's answer, you deserve this one: The equivalence of Church's$\lambda\$-calculus and Turing machines is proved in the Appendix of Alan Turing's 1937 paper On computable numbers, with an application to the Entscheidungsproblem.

• I most respectfully suggest that we are unable nor entirely willing to satisfy your further demands. I can highly recommend the Google search engine through which I also found the above reference, although, being an expert in this area, I knew of course what to look for. – Andrej Bauer Oct 23 '15 at 13:42
• @AshkanKzme, please don't use comments to promote other questions of yours. Comments should only be used to suggest clarifications or improvements to the post you are commenting on. See cs.stackexchange.com/help/privileges/comment for more details. Note that this is not a discussion forum, so please refrain from chatty or tangential comments. – D.W. Oct 23 '15 at 16:30
• Point well taken. Which policy describes how to respond to people who treat us like we are their servants? Servilely? – Andrej Bauer Oct 24 '15 at 8:54
{"ft_lang_label":"__label__en","ft_lang_prob":0.8041114,"math_prob":0.9195942,"size":1683,"snap":"2021-04-2021-17","text_gpt3_token_len":490,"char_repetition_ratio":0.09350804,"word_repetition_ratio":0.012269938,"special_character_ratio":0.3184789,"punctuation_ratio":0.1890411,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9836916,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-16T23:32:02Z\",\"WARC-Record-ID\":\"<urn:uuid:7fa6b600-5b83-44a5-b833-7bb4336d7ab7>\",\"Content-Length\":\"196302\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f2aa19e3-14e6-4b65-bfa3-e20735007431>\",\"WARC-Concurrent-To\":\"<urn:uuid:c021e184-b6ea-4248-8d1d-0198d88654fb>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/48622/is-there-an-algorithm-for-converting-turing-machines-into-equivalent-lambda-expr\",\"WARC-Payload-Digest\":\"sha1:AH45WNNUGYLNFAPJ666VTS3IOZGIXOCI\",\"WARC-Block-Digest\":\"sha1:GIEW4N5OJMG3W63VP45W3HSLJUGACPT4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038092961.47_warc_CC-MAIN-20210416221552-20210417011552-00086.warc.gz\"}"}