tm design universal tm ma/csse 474 theory of computation

39
TM Design Universal TM MA/CSSE 474 Theory of Computation

Upload: eunice-martin

Post on 31-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: TM Design Universal TM MA/CSSE 474 Theory of Computation

TM DesignUniversal TM

MA/CSSE 474Theory of Computation

Page 2: TM Design Universal TM MA/CSSE 474 Theory of Computation

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.

Page 3: TM Design Universal TM MA/CSSE 474 Theory of Computation

The CFL Hierarchy

Page 4: TM Design Universal TM MA/CSSE 474 Theory of Computation

Context-Free Languages Over a Single-Letter Alphabet

Theorem: Any context-free language over a single-letter alphabet is regular.

Proof: Requires Parikh’s Theorem, which we are skipping

Page 5: TM Design Universal TM MA/CSSE 474 Theory of Computation

Algorithms and Decision Procedures for

Context-Free Languages

Chapter 14

Page 6: TM Design Universal TM MA/CSSE 474 Theory of Computation

Decision Procedures for CFLs

Membership: Given a language L and a string w, is w in L?

Two approaches: ● If L is context-free, then there exists some context-free grammar G that generates it. Try derivations in G and see whether any of them generates w.

Problem (later slide):

● If L is context-free, then there exists some PDA M that accepts it. Run M on w.

Problem (later slide):

Page 7: TM Design Universal TM MA/CSSE 474 Theory of Computation

Decision Procedures for CFLs

Membership: Given a language L and a string w, is w in L?

Two approaches: ● If L is context-free, then there exists some context-free grammar G that generates it. Try derivations in G and see whether any of them generates w.

S S T | a Try to derive aaa

S

S T

S T

Page 8: TM Design Universal TM MA/CSSE 474 Theory of Computation

Decision Procedures for CFLs

Membership: Given a language L and a string w, is w in L?

● If L is context-free, then there exists some PDA M that accepts it. Run M on w.

Problem:

Page 9: TM Design Universal TM MA/CSSE 474 Theory of Computation

Using a Grammar

decideCFLusingGrammar(L: CFL, w: string) =

1. If given a PDA, build G so that L(G) = L(M).

2. If w = then if SG is nullable then accept, else reject.

3. If w then: 3.1 Construct G in Chomsky normal form such that L(G) = L(G) – {}.

3.2 If G derives w, it does so in 2|w| - 1 steps. Try all derivations in G of 2|w| - 1 steps. If one of them derives w, accept. Otherwise reject.

Alternative O(n3) algorithm: CKY

Page 10: TM Design Universal TM MA/CSSE 474 Theory of Computation

Recall CFGtoPDAtopdown, which built:

M = ({p, q}, , V, , p, {q}), where contains: ● The start-up transition ((p, , ), (q, S)).

● For each rule X s1s2…sn. in R, the transition ((q, , X), (q, s1s2…sn)).

● For each character c , the transition ((q, c, c), (q, )).

Can we make this work so there are no -transitions? If every transition consumes an input character then M would have to halt after |w| steps.

Membership Using a PDA

Put the grammar into Greibach Normal form:All rules are of the following form: ● X a A, where a and A (V - )*.We can combime pushing the RHS of the production withmatching the first character a. Details on p 316-317.

Page 11: TM Design Universal TM MA/CSSE 474 Theory of Computation

Greibach Normal Form All rules are of the following form:

● X a A, where a and A (V - )*.

No need to push the a and then immediately pop it.

So M = ({p, q}, , V, , p, {q}), where contains:

1. The start-up transitions: For each rule S cs2…sn, the transition:

((p, c, ), (q, s2…sn)).

2. For each rule X cs2…sn (where c and s2 through sn are elements of V - ), the

transition:

((q, c, X), (q, s2…sn))

Page 12: TM Design Universal TM MA/CSSE 474 Theory of Computation

A PDA Without -Transitions Must Halt

Consider the execution of M on input w:

● Each individual path of M must halt within |w| steps.

● The total number of paths pursued by M must be less than or equal to P = B|w|, where B is the maximum number of competing transitions from any state in M.

● The total number of steps that will be executed by all paths of M is bounded by P |w|.

So all paths must eventually halt.

Page 13: TM Design Universal TM MA/CSSE 474 Theory of Computation

Emptiness

Given a context-free language L, is L = ?

decideCFLempty(G: context-free grammar) =

1. Let G = removeunproductive(G).

2. If S is not present in G then return True else return False.

Page 14: TM Design Universal TM MA/CSSE 474 Theory of Computation

Finiteness

Given a context-free language L, is L infinite?

decideCFLinfinite(G: context-free grammar) =

1. Lexicographically enumerate all strings in * of length greater than bn and less than or equal to bn+1 + bn.

2. If, for any such string w, decideCFL(L, w) returns True then return True. L is infinite.

3. If, for all such strings w, decideCFL(L, w) returns False then return False. L is not infinite.

Why these bounds?

Page 15: TM Design Universal TM MA/CSSE 474 Theory of Computation

Some Undecidable Questions about CFLs

● Is L = *?

● Is the complement of L context-free?

● Is L regular?

● Is L1 = L2?

● Is L1 L2?

● Is L1 L2 = ?

● Is L inherently ambiguous?

● Is G ambiguous?

Page 16: TM Design Universal TM MA/CSSE 474 Theory of Computation

Regular and CF Languages

Regular Languages Context-Free Languages

● regular exprs. ● context-free grammars ● or● regular grammars● = DFSMs ● = NDPDAs● recognize ● parse● minimize FSMs ● try to find unambiguous grammars

● try to reduce nondeterminism in PDAs

● find efficient parsers● closed under: ● closed under: ♦ concatenation ♦ concatenation ♦ union ♦ union ♦ Kleene star ♦ Kleene star ♦ complement ♦ intersection ♦ intersection w/ reg. langs● pumping theorem ● pumping theorem● D = ND ● D ND

Page 17: TM Design Universal TM MA/CSSE 474 Theory of Computation

Languages and MachinesSD

D

Context-FreeLanguages

RegularLanguagesreg exps

FSMs

cfgs PDAs

unrestricted grammarsTuring Machines

Page 18: TM Design Universal TM MA/CSSE 474 Theory of Computation

SD Language

Unrestricted Grammar

Turing Machine

L

Accepts

Grammars, SD Languages, and Turing Machines

Page 19: TM Design Universal TM MA/CSSE 474 Theory of Computation

Turing Machines

We want a new kind of automaton:

● powerful enough to describe all computable things

unlike FSMs and PDAs.

● simple enough that we can reason formally about itlike FSMs and PDAs,unlike real computers.

Goal: Be able to prove things about what can and cannot be computed.

Page 20: TM Design Universal TM MA/CSSE 474 Theory of Computation

Turing Machines

At each step, the machine must:

● choose its next state, ● write on the current square, and ● move left or right.

Page 21: TM Design Universal TM MA/CSSE 474 Theory of Computation

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 q; ● is the tape alphabet, which must contain q 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)

Page 22: TM Design Universal TM MA/CSSE 474 Theory of Computation

Notes on the Definition

1. The input tape is infinite in both directions.

2. is a function, not a relation. So this is a definition for deterministic Turing machines.

3. must be defined for all (state, tape symbol) pairs unless the state is a halting state.

4. Turing machines do not necessarily halt (unlike FSM's and most PDAs). Why? To halt, they must enter a halting state. Otherwise they loop.

5. Turing machines generate output, so they can compute functions.

Page 23: TM Design Universal TM MA/CSSE 474 Theory of Computation

An ExampleM takes as input a string in the language:

{aibj, 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:

Page 24: TM Design Universal TM MA/CSSE 474 Theory of Computation

The Details

K = {1, 2, 3, 4, 5, 6}, = {a, b}, = {a, b, q, $, #}, s = 1, H = {6}, =

Page 25: TM Design Universal TM MA/CSSE 474 Theory of Computation

Notes on Programming

The machine has a strong procedural feel, with one phase coming after another.

There are common idioms, like scan left until you find a blank

There are two 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.

Page 26: TM Design Universal TM MA/CSSE 474 Theory of Computation

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.

Page 27: TM Design Universal TM MA/CSSE 474 Theory of Computation

Formalizing the Operation

A configuration of a Turing machine

M = (K, , , s, H) is an element of:

K ((- {q}) *) {} (* (- {q})) {}

state up current after to current square current square square

Page 28: TM Design Universal TM MA/CSSE 474 Theory of Computation

Example Configurations

(1) (q, ab, b, b) = (q, abbb) (2) (q, , q, aabb) = (q, qaabb)

Initial configuration is (s, qw).

b

Page 29: TM Design Universal TM MA/CSSE 474 Theory of Computation

Yields (q1, w1) |-M (q2, w2) iff (q2, w2) is derivable, via , in one step.

For any TM M, let |-M* be the reflexive, transitive closure of |-M.

Configuration C1 yields configuration C2 if: C1 |-M* C2.

A path through M is a sequence of configurations C0, C1, …, Cn for some n 0 such that C0 is the initial configuration and:

C0 |-M C1 |-M C2 |-M … |-M Cn.

A computation by M is a path that halts.

If a computation is of length n (has n steps), we write:

C0 |-Mn Cn

Page 30: TM Design Universal TM MA/CSSE 474 Theory of Computation

Exercise

A TM to recognize { wwR : 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.

Page 31: TM Design Universal TM MA/CSSE 474 Theory of Computation

TMs are complicated

• … and low-level!• We need higher-level "abbreviations".

– Macros– Subroutines

Page 32: TM Design Universal TM MA/CSSE 474 Theory of Computation

A Macro language for Turing Machines

(1) Define some basic machines

● Symbol writing machines

For each x , define Mx, written just x, to be a machine that writes x.

● 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.

Page 33: TM Design Universal TM MA/CSSE 474 Theory of Computation

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:

● M1 M2

● M1 <condition> M2

Checking Inputs and Combining Machines

Page 34: TM Design Universal TM MA/CSSE 474 Theory of Computation

Turing Machines Macros Cont'd

Example:

>M1 a M2

b M3

● Start in the start state of M1.● Compute until M1 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.

Page 35: TM Design Universal TM MA/CSSE 474 Theory of Computation

aM1 M2 becomes M1 a, b M2

b M1 all elems of M2 becomes M1 M2

or M1M2

Variables M1 all elems of M2 becomes M1 x a M2

except a and x takes on the value of the current square

M1 a, b M2 becomes M1 x a, b M2

and x takes on the value of the current square

M1 x = y M2

if x = y then take the transition

e.g., > x q Rx if the current square is not blank, go right and copy it.

More macros

Page 36: TM Design Universal TM MA/CSSE 474 Theory of Computation

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

Rq

Lq

Rq

Lq

Page 37: TM Design Universal TM MA/CSSE 474 Theory of Computation

More Search Machines La Find the first occurrence of a to

the left of the current square.

Ra,b Find the first occurrence of a or b to the right of the current square.

La,b a M1 Find the first occurrence of a or b

to the left of the current square, b then go to M1 if the detected

character is a; go to M2 if the M2 detected character is b.

Lxa,b Find the first occurrence of a or b to the left of the current square and set x to the value found.

Lxa,bRx Find 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).

Page 38: TM Design Universal TM MA/CSSE 474 Theory of Computation

An Example Input: qw w {1}*Output: qw3

Example: q111qqqqqqqqqqqqqq

Page 39: TM Design Universal TM MA/CSSE 474 Theory of Computation

What does this machine do?