formal methods 3 - languages and machines
DESCRIPTION
My course of Formal Methods at Santa Clara University, Winter 2014. automata, Turing machine, languages hierarchy (part 2)TRANSCRIPT
Formal Methods in Software
Lecture 3. Languages and Machines-2
Vlad PatryshevSCU2014
Machine with Stack
Stacktrait Stack[T] {
def push(T);
def pop:T;
def isEmpty;
}
Can Calculate Expressions val s = new Stack[String]
def doOp(y: Double, op: String, x: Double): String = {
"" + (op match {
case "+" => x + y
case "-" => x - y
case "*" => x * y
case "/" => x / y
})
}
def eval(expr:String): String = {
for (x <- expr.split(" ")) {
if (x == ")") s.push(doOp(s.pop.toDouble, s.pop, s.pop.toDouble))
else s.push(x)
}
s.pop
}
Context-Free Language
Given an alphabet A, a context-free grammar of a language L A*⊂ has the following form:
● A → w
where A is a nonterminal symbols, and w is a word formed by terminal and nonterminal symbols.
A context-free language is the one produced by a context-free grammar.
Regular languages are context-free.
CFL - samples
• Canonical example: anbn
o S → εo S → aSb
• Parentheses: ((()())())o S → εo S → SSo S → (S)
• Arithmetic expressions
Stack Machine aka PDA (push-down automaton)
FSM + Stack
E.g.Forth, JVM, Haskell...
(Deterministic) PDA, formally
• Alphabets:o In - input alphabet;o S - machine states;o Z - stack alphabet
• Transition function: (In ∪ {ε})×S×Z → S×(Z∪{ε})
meaning, it depends on top of stack, and the result may be pushed
More formally, states are S×Z*; transition is (In∪{ε})×S×Z* → S×Z*
(Nondeterministic) PDA, formally
• Alphabets:o In - input alphabet;o S - machine states (Sa - acceptable states);o Z - stack alphabet
• Transition function: (In∪{ε})×S×Z → P(S×(Z∪{ε}))
meaning, it depends on top of stack, and the result may be pushed
More formally, states are S×Z*; transition is (In∪{ε})×S×Z* → P(S×Z*)
Acceptable states are Sa×{λ}
DPDA vs NPDA
S → aSa
S → bSb
S → ε
Need a NPDA for this language.
Which means, DPDA and NPDA are not equivalent, unlike DFS and NFS.
PDA Example
Parse anbnε
Use deterministic
In={a,b,ε}
S={q0,good,bad}
Z={a}
transition t: t(q0,a,Z) → (q0,aZ)
t(q0,b,aZ) → (q0,Z)
t(q0,ε,<empty>) → (good,<empty>)
(all other transition go to bad)
Non-context-free languages
Example: anbncndn
In a context-sensitive grammar rules can look like
aXb → awb
In an arbitrary grammar there are no rules for rules
w1 → w2
Chomsky’s Language Hierarchy
The most General Tool
Either Lambda-calculus, or Turing machinethey are equivalent
and define recursive functions
Turing Machine
DFA + unlimited tape with symbols + commands:
• read symbol from tape, affecting its state
• write symbol to tape
• move tape left
• move tape right
• stop
Turing Machine - notation
In this example:
{A,B,C,H} - states
H - final state
0/P,L means: on ‘0’ Print ‘1’ and move tape Left
Example (Busy Beaver)
Busy Beaver, more
Busy Beaver machine is one that takes most steps, for a given number of states, using just 0 and 1.
We had a 4-state Busy BeaverS(n) grows faster than any recursive function
n (number of states) S(n) (number of steps)
1 1
2 6
3 21
4 107
5 >47176870
Halting Problem
Given a Turing machine, can we decide if it ever stops?
http://ro-che.info/ccc/03
Universal Turing Machine
• Models any other TM
• Can be used to calculate anything
Wolfram’s 2-state 3-symbol UTMA B
0 P1,R,B P2,L,A
1 P2,L,A P2,R,B
2 P1,L,A P0,L,A
Referenceshttp://www.liacs.nl/~hoogeboo/praatjes/tarragona/pda-intro.pdf
http://www.infor.uva.es/~teodoro/valladolid-beamer-version.pdf
http://www.vuse.vanderbilt.edu/~koutsoxd/www/Teaching/oldwww/CS252/CS252_lecture16.pdf
http://cstheory.stackexchange.com/questions/625/relationship-between-turing-machine-and-lambda-calculus
http://www.win.tue.nl/~wijers/shallit.pdf
Wikipediahttp://tinyurl.com/k45ngsc