Presentation is loading. Please wait.

Presentation is loading. Please wait.

TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation.

Similar presentations


Presentation on theme: "TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation."— Presentation transcript:

1 TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation

2 Your Questions? Previous class days' material Reading Assignments HW 12 problems Anything else I have included some slides online that we will not have time to do in class, but may be helpful to you anyway.

3 Recap: Turing Machines At each step, the machine must: ● choose its next state, ● write on the current square, and ● move left or right.

4 A Formal Definition A (deterministic) Turing machine M is (K, , , , s, H): ● K is a finite set of states; ●  is the input alphabet, which does not contain  ; ●  is the tape alphabet, which must contain  and have  as a subset. ● s  K is the initial state; ● H  K is the set of halting states; ●  is the transition function: (K - H)   to K    { ,  } non-halting  tape  state  tape  direction to move state char char (R or L)

5 An Example M takes as input a string in the language: { a i b j, 0  j  i}, and adds b ’s as required to make the number of b ’s equal the number of a ’s. The input to M will look like this: The output should be:

6 The Details K = {1, 2, 3, 4, 5, 6},  = { a, b },  = { a, b, , $, #}, s = 1,H = {6},  =

7 Notes on Programming The machine has a strong procedural feel, with one phase coming after another. There are common idioms, such as "scan left until you find a blank" There are some common ways to scan back and forth marking things off. Often there is a final phase to fix up the output. Even a very simple machine is a nuisance to write.

8 Halting ● A DFSM M, on input w, is guaranteed to halt in |w| steps. ● A PDA M, on input w, is not guaranteed to halt. To see why, consider again M = But there exists an algorithm to construct an equivalent PDA M that is guaranteed to halt. A TM M, on input w, is not guaranteed to halt. And there is no algorithm to construct an equivalent TM that is guaranteed to halt.

9 Formalizing the Operation A configuration of a Turing machine M = (K, , , s, H) is an element of: K  ((  - {  })  *)  {  }    (  * (  - {  }))  {  } state up current after to current square current square square

10 Example Configurations (1)(q, ab, b, b )written more simply as (q, abbb ) (2)(q, , , abbb ) written more simply as (q,  abbb ) Initial configuration is (s,  w). b

11 Yields (q 1, w 1 ) |- M (q 2, w 2 ) iff (q 2, w 2 ) is derivable, via , in one step. For any TM M, let |- M * be the reflexive, transitive closure of |- M. Configuration C 1 yields configuration C 2 if: C 1 |- M * C 2. A path through M is a sequence of configurations C 0, C 1, …, C n for some n  0 such that C 0 is the initial configuration and: C 0 |- M C 1 |- M C 2 |- M … |- M C n. A computation by M is a path that halts. If a computation is of length n (has n steps), we write: C 0 |- M n C n

12 Exercise If n and m are non-negative integers, monus(n, m) is defined to be n – m if n > m, and 0 if n ≤ m. Draw the diagram for a TM M whose input is 1 n ;1 m and whose output is 1 monus(n, m). When M halts, the read/write head should be positioned on the blank before the first 1.

13 (Hidden, not used 2014)Exercise A TM to recognize { ww R : w  {a, b}* }. If the input string is in the language, the machine should halt with y as its current tape symbol If not, it should halt with n as its current tape symbol. The final symbols on the rest of the tape may be anything.

14 Hidden: Exercise A TM to recognize ww R, where w  {a, b}* (beginning of a solution) New tape symbols: A, B: (1,  )  (2, ,   ) (2,  )  (3, ,   ) 3 is a halting state (2, a)  (4,  A,   ) (4, a)  (4,  a,   ) (4, b)  (4,  b,   ) (4,  )  (5, ,   ) (4, A)  (5, A,  )

15 TMs are complicated … and low-level! We need higher-level "abbreviations". –Macros –Subroutines

16 A Macro language for Turing Machines (1) Define some basic machines ● Symbol writing machines For each x  , define M x, written as just x, to be a machine that writes x. Read-write head ends up in original position. ● Head moving machines R:for each x  ,  (s, x) = (h, x,  ) L:for each x  ,  (s, x) = (h, x,  ) ● Machines that simply halt: h, which simply halts (don't care whether it accepts). n, which halts and rejects. y, which halts and accepts. You need to learn this simple language. I will use it and I expect you to use it on HW and tests.

17 Next we need to describe how to: ● Check the tape and branch based on what character we see, and ● Combine the basic machines to form larger ones. To do this, we need two forms: ● M 1 M 2 Checking Inputs and Combining Machines

18 Turing Machines Macros Cont'd Example: >M 1 a M 2 b M 3 ● Start in the start state of M 1. ● Compute until M 1 reaches a halt state. ● Examine the tape and take the appropriate transition. ● Start in the start state of the next machine, etc. ● Halt if any component reaches a halt state and has no place to go. ● If any component fails to halt, then the entire machine may fail to halt.

19 a M 1 M 2 becomesM 1 a, b M 2 b M 1 all elems of  M 2 becomesM 1 M 2 or M 1 M 2 Variables M 1 all elems of  M 2 becomesM 1 x   a M 2 except a and x takes on the value of the current square M 1 a, b M 2 becomesM 1 x  a, b M 2 and x takes on the value of the current square M 1 x = y M 2 if x = y then take the transition e.g., > x    Rx if the current square is not blank, go right and copy it. More macros

20 Blank/Non-blank Search Machines Find the first blank square to the right of the current square. Find the first blank square to the left of the current square. Find the first nonblank square to the right of the current square. Find the first nonblank square to the left of the current square RR LL RR LL

21 More Search Machines L a Find the first occurrence of a to the left of the current square. R a, b Find the first occurrence of a or b to the right of the current square. L a, b a M 1 Find the first occurrence of a or b to the left of the current square, b then go to M 1 if the detected character is a ; go to M 2 if the M 2 detected character is b. L x  a, b Find the first occurrence of a or b to the left of the current square and set x to the value found. L x  a, b RxFind the first occurrence of a or b to the left of the current square, set x to the value found, move one square to the right, and write x ( a or b ).

22 An Example Input:  w w  {1}* Output:  w 3 Example:  111 

23 What does this machine do?

24 Exercise Initial input on the tape is a non-negative integer written in binary, most significant bit first (100 represents 6). Design a TM that replaces the binary representation of n by the binary representation of n+1.

25 Two Useful Kinds of TMs 1.Recognize a language 2.Compute a function

26 Turing Machines as Language Recognizers Let M = (K, , , , s, {y, n}). ● M accepts a string w iff (s,  w) |- M * (y, w) for some string w. ● M rejects a string w iff (s,  w) |- M * (n, w) for some string w. M decides a language L   * iff: For any string w   * it is true that: if w  L then M accepts w, and if w  L then M rejects w. A language L is decidable iff there is a Turing machine M that decides it. In this case, we will say that L is in D.

27 A Deciding Example A n B n C n = { a n b n c n : n  0} Example:  aabbcc  Example:  aaccb 

28 Semideciding a Language Let  M be the input alphabet to a TM M. Let L   M *. M semidecides L iff, for any string w   M *: ● w  L  M accepts w ● w  L  M does not accept w. M may either: reject or fail to halt. A language L is semidecidable iff there is a Turing machine that semidecides it. We define the set SD to be the set of all semidecidable languages.

29 Example of Semideciding Let L = b * a ( a  b )* We can build M to semidecide L: 1. Loop 1.1 Move one square to the right. If the character under the read head is an a, halt and accept. In our macro language, M is:

30 Example of Semideciding L = b * a ( a  b )*. We can also decide L: Loop: 1.1 Move one square to the right. 1.2 If the character under the read/write head is an a, halt and accept. 1.3 If it is , halt and reject. In our macro language, M is:

31 Computing Functions Let M = (K, , , , s, {h}). Define M(w) = z iff (s,  w) |- M * (h,  z). Let    be M’s output alphabet. Let f be any function from  * to  *. M computes f iff, for all w   *: ● If w is an input on which f is defined: M(w) = f(w). ● Otherwise M(w) does not halt. A function f is recursive or computable iff there is a Turing machine M that computes it and that always halts.

32 Example of Computing a Function Let  = { a, b }. Let f(w) = ww. Input:  w  Output:  ww  Define the copy machine C:  w    w  w  Also use the S  machine:  u  w    uw  Then the machine to compute f is just >C S  L 

33 Example of Computing a Function Let  = { a, b }. Let f(w) = ww. Input:  w  Output:  ww  Define the copy machine C:  w    w  w  Then use the the S  machine:  u  w    uw  Then the machine to compute f is just >C S  L  We skip the details in class; you can look at them later.

34 Computing Numeric Functions For any positive integer k, value k (n) returns the nonnegative integer that is encoded, base k, by the string n. For example: ● value 2 ( 101 ) = 5. ● value 8 ( 101 ) = 65. TM M computes a function f from ℕ m to ℕ iff, for some k: value k (M(n 1 ;n 2 ;…n m )) = f(value k (n 1 ), … value k (n m )).

35 Computing Numeric Functions Example: succ(n) = n + 1 We will represent n in binary. So n  0  1 { 0, 1 }* Input:  n  Output:  n+1   1111  Output:  10000 

36 Why Are We Working with Our Hands Tied Behind Our Backs? Turing machinesAre more powerful than any of the other formalisms we have studied so far. Turing machinesAre a lot harder to work with than all the real computers we have available.  Why bother? The very simplicity that makes it hard to program Turing machines makes it possible to reason formally about what they can do. If we can, once, show that anything a real computer can do can be done (albeit clumsily) on a Turing machine, then we have a way to reason about what real computers can do.


Download ppt "TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation."

Similar presentations


Ads by Google