Turing Machines Part Two
Total Page:16
File Type:pdf, Size:1020Kb
Turing Machines Part Two Recap from Last Time Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ☐ → ☐, R ThisThis is is the the Turing Turing machine’s machine’s finite state control. It issues finite state control. It issues qrej commandscommands that that drive drive the the operationoperation of of the the machine. machine. Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ☐ → ☐, R ThisThis is is the the TM’s TM’s infinite infinite tape tape. Each tape cell holds a tape Each tape cell holds a tape qrej symbolsymbol. .Initially, Initially, all all (infinitely (infinitely many)many) tape tape symbols symbols are are blank. blank. … … Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R TheThe machine machine is is started started with with the the ☐ → ☐, R inputinput string string written written somewhere somewhere onon the the tape. tape. The The tape tape head head initially points to the first symbol initially points to the first symbol qrej ofof the the input input string. string. … a a a a … Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ☐ → ☐, R LikeLike DFAs DFAs and and NFAs,NFAs, TMs TMs begin begin executionexecution in in their their q startstart state state. rej … a a a a … Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ☐ → ☐, R AtAt each each step, step, the the TMTM only only looks looks at at thethe symbol symbol q immediatelyimmediately under under rej thethe tape tape head head. … a a a a … Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ☐ → ☐, R TheseThese two two transitions transitions originateoriginate at at the the current current state.state. We’re We’re going going to to q choosechoose one one of of them them to to rej follow.follow. … a a a a … Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a Each transition →has ☐ the, R form Each transition has the form ☐ → ☐, R read → write, dir read → write, dir andand means means “if “if symbol symbol read read is is under under the the tape tape head, head,q replace replace it it with with write write and and ☐ rej movemove the the tape tape head head in in direction direction dir dir (L (L or or R). R). The The ☐ symbol symbol denotes denotes a a blank blank cell. cell. … a a a a … Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ☐ → ☐, R UnlikeUnlike aa DFADFA oror NFA,NFA, aa TMTM doesn’tdoesn’t stopstop afterafter readingreading allall thethe inputinput characters.characters. WeWe keepkeep qrej runningrunning untiluntil thethe machinemachine explicitlyexplicitly sayssays toto stop.stop. Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ThisThis specialspecial statestate isis anan ☐ → ☐, R acceptingaccepting statestate.. WhenWhen aa TMTM entersenters anan acceptingaccepting state,state, itit immediatelyimmediately stopsstops runningrunning qrej andand acceptsaccepts whateverwhatever thethe originaloriginal inputinput stringstring waswas (in(in thisthis case,case, aaaaaaaa).). Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 a → ☐, R ThisThis specialspecial state state isis aa ☐ → ☐, R rejectingrejecting statestate.. WhenWhen aa TMTM entersenters a a rejectingrejecting state,state, itit immediatelyimmediately stopsstops runningrunning qrej andand rejectsrejects whateverwhatever thethe originaloriginal inputinput stringstring waswas (in(in thisthis case,case, aaaaaaaaaa).). Our First Turing Machine qacc ☐ → ☐, R a → ☐, R start q0 q1 If the TM is started on the a → ☐, R If the TM is started on the ☐ → ☐, R emptyempty stringstring ε,ε, thethe entireentire tapetape isis blankblank andand thethe tapetape headhead isis positionedpositioned atat somesome arbitraryarbitrary q locationlocation onon thethe tape.tape. rej … … Input and Tape Alphabets ● A Turing machine has two alphabets: – An input alphabet Σ. All input strings are written in the input alphabet. – A tape alphabet Γ, where Σ ⊆ Γ. The tape alphabet contains all symbols that can be written onto the tape. ● The tape alphabet Γ can contain any number of symbols, but always contains at least one blank symbol, denoted ☐. You are guaranteed ☐ ∉ Σ. ● At startup, the Turing machine begins with an infinite tape of☐ symbols with the input written at some location. The tape head is positioned at the start of the input. ☐ → ☐, R 0 → 0, R 0 → 0, L Go to 1 → ☐, L Clear a 1 → 1, L start 1 s ☐ → ☐, R ☐ → ☐, L ta rt 1 → ☐, R 0 → ☐, R qq Check Go to 0 → 0, R accrej for 0 end 1 → 1, R ☐ → ☐, R qacc s ta rt ☐ → ☐, R Unmark × → 0, L Edge Case × → ×, R 0 → 0, R ☐ → ☐, L 1 → 1, R 0 → 0, R 1 → 1, L × → ×, R Check 1 → 1, L Back ☐ → ☐, R Next 0 → ×, R To End 0 → 0, R m ≟ 0 home 0 ◻ → ◻, R 0 → 0, L ◻ → ◻, R ◻ → ◻, L × → ×, L ☐ → ☐, R Back 1 → ☐, L Cross Accept! 0 → 0, L home off 1 1 → 1, L New Stuff! Main Question for Today: Just how powerful are Turing machines? Another TM Design ● Last time, we designed a TM for this language over Σ = {0, 1}: L = { w ∈ Σ* | w has the same number of 0s and 1s } ● Let's do a quick review of how it worked. A Different Idea A Different Strategy … 0 0 0 1 1 1 1 0 … LastLast time,time, wewe builtbuilt aa machinemachine thatthat checkschecks n n whetherwhether aa stringstring hashas thethe formform 00n11n.. ThatThat machinemachine almostalmost solvessolves thisthis problem,problem, butbut requiresrequires thatthat thethe characterscharacters havehave toto bebe inin order.order. WhatWhat ifif wewe sortedsorted thethe input?input? A Different Strategy … 0 0 0 1 1 1 1 0 … ObservationObservation 1:1: AA stringstring ofof 0s0s andand 1s1s isis sortedsorted ifif itit matchesmatches thethe regexregex * * 00*11*.. A Different Strategy … 0 0 0 1 1 1 1 0 … ObservationObservation 2:2: AA stringstring ofof 0s0s andand 1s1s isis notnot sortedsorted ifif itit containscontains 1010 asas aa substring.substring. A Different Strategy … 0 0 0 1 1 1 0 1 … Idea:Idea: RepeatedlyRepeatedly findfind aa copycopy ofof 1010 andand replacereplace itit withwith 01.01. Let's Build It! 0 → 0, L To ◻ → ◻, R Start 1 → 1, L Start 0n1n ◻ → ◻, L ◻ → ◻, L st ar t 1 → 1, R 0 → 0, R 0* 0*1* 1 → 1, R ◻ → ◻, R 0 → 1, L 1 → 0, L 0 → 0, L Go Fix 1 → 1, L Home 01 TM Subroutines ● A TM subroutine is a Turing machine that, instead of accepting or rejecting an input, does some sort of processing job. ● TM subroutines let us compose larger TMs out of smaller TMs, just as you'd write a larger program using lots of smaller helper functions. ● Here, we saw a TM subroutine that sorts a sequence of 0s and 1s into ascending order. TM Subroutines ● Typically, when a subroutine is done running, you have it enter a state marked “done” with a dashed line around it. ● When we're composing multiple subroutines together – which we'll do in a bit – the idea is that we'll snap in some real state for the “done” state. What other subroutines can we make? TM Arithmetic ● Let's design a TM that, given a tape that looks like this: … 1 3 7 4 2 … ends up having the tape look like this: … 1 7 9 0 0 … ● In other words, we want to build a TM that can add two numbers. TM Arithmetic ● There are many ways we could in principle design this TM. ● We're going to take the following approach: – First, we'll build a TM that increments a number. – Next, we'll build a TM that decrements a number. – Then, we'll combine them together, repeatedly decrementing the second number and adding one to the first number. Incrementing Numbers ● Let's begin by building a TM that increments a number. ● We'll assume that – the tape head points at the start of a number, – there is are at least two blanks to the left of the number, and – that there's at least one blank at the start of the number. ● The tape head will end at the start of the number after incrementing it. … 9 9 8 Incrementing Numbers gogo toto thethe endend ofof thethe number;number; whilewhile (the(the currentcurrent digitdigit isis 9)9) {{ setset thethe currentcurrent digitdigit toto 0;0; backback upup oneone digit;digit; }} incrementincrement thethe currentcurrent digit;digit; gogo toto thethe startstart ofof thethe number;number; st ar t 0 → 0, R ◻ → ◻, L 1 → 1, R To Wrap 9 → 0, L … End Nines 9 → 9, R 0 → 1, L 1 → 2, L 2 → 3, L … 8 → 9, L ◻ → 1, L 0 → 0, L ◻ → ◻, R Back 1 → 1, L done! Home … 9 → 9, L Decrementing Numbers ● Now, let's build a TM that decrements a number. ● We'll assume that – the tape head points at the start of a number, – there is at least one blank on each side of the number. ● The tape head will end at the start of the number after decrementing it. ● If the number is 0, then the subroutine should somehow signal this rather than making the number negative. 1 0 2 0 → 0, R ◻ → ◻, L 1 → 1, R To Wrap 0 → 9, L … End Zeros 9 → 9, R 1 → 0, L 1 → 1, R 2 → 1, L 2 → 2, R 3 → 2, L … … 9 → 9, R 9 → 8, L st ar t 0 → 0, L ◻ → ◻, L ◻ → ◻, R Non- Back 1 → 1, L 0 → 0, R n = 0 done! zero? Home … 9 → 9, L TM Subroutines ● Sometimes, a subroutine needs to report back some information about what happened. ● Just as a function can return multiple different values, we'll allow subroutines to have different “done” states.