Upload
others
View
165
Download
0
Embed Size (px)
Citation preview
A Haskell Implementation of Turing Machines
Lim Shao En
Zhang Licheng
Computer Science
Computability Theory
A Haskell Implementation of Turing Machines
Computability Theory
A Haskell Implementation of Turing Machines
• Turing Machine
• Recursion theory
• Lambda-calculus
• Post system
A Haskell Implementation of Turing Machines
Aim and HypothesisBridging the theory – practice gap
Aim and Hypothesis
Computability
Theory
A Haskell Implementation of Turing Machines
Abstract
Descriptions
Computer
programs
Our Aim
TuringMachines
HaskellCodes
A Haskell Implementation of Turing Machines
Turing Machine
Tape (Infinite Length)
Cells
A Haskell Implementation of Turing Machines
Turing Machine
1 0 0 0 00 1 1 1 0 1
Symbols (0,1 or Blank)
A Haskell Implementation of Turing Machines
Turing Machine
1 0 0 0 00 1 1 1 0 1
q1Head
A Haskell Implementation of Turing Machines
Turing Machine
1 0 0 0 00 1 1 1 0 1
q1 q2 q3 q4
0 0 0
q5
1
State
Read and Write
Symbols
A Haskell Implementation of Turing Machines
Turing Machine -- -- Transitions
A Haskell Implementation of Turing Machines
Haskell
A Haskell Implementation of Turing Machines
Haskell
• Typed , Functional Programming Language
• Typed - Data types in haskell are built up from
the basic data types: Int, Bool, (a,b) and X->Y
• Functional - functions can be passed as data
types, data is passed recursively from function
to function
A Haskell Implementation of Turing Machines
Methods
5
Major
Stages
Learn Haskell1
2 Create ‘Types’
3 Construct ‘UTM’ Prototype
4 Test - Functions
5 Refine & Debug
A Haskell Implementation of Turing Machines
Results and Discussion
Translating knowledge of Turing Machines into Haskell
Types Construction
type Dir = Int-- {-1,0,1}
type State = Int-- state numbers-- start at q(0);-- halts at q(-1)
type Sym = Int-- {0,1,2} cell symbols;-- 2 represents Blank
type Hdp = Int-- head position
type Tape = [Sym]-- strings on tape
type Tmac = State -> Sym ->(State,Sym,Dir)
-- generic TM-- (a set of transitions)
Results and Discussion:Translating knowledge of Turing Machines into Haskell
Main Program
of UTM
prototype
turr :: Tmac -> Tape -> Tapeturr m t = fst (auxt m 0 (t,0))
auxt :: Tmac -> State -> (Tape,Hdp) -> (Tape,Hdp)auxt m (-1) (t,i) = (t,i)auxt m q (t,i) = let t2 = edit (i,mysnd(m q (t!!i)),t) in
auxt m (myfst(m q (t!!i))) (t2,i + mythd(m q (t!!i)))
edit :: (Hdp,Sym,Tape) -> Tapeedit (i,x,t) = (take i t) ++ [x] ++ (drop (i+1) t) ++ [2]
myfst :: (State,Sym,Dir) -> Statemyfst (q,x,d) = q
mysnd :: (State,Sym,Dir) -> Symmysnd (q,x,d) = x
mythd :: (State,Sym,Dir) -> Dirmythd (q,x,d) = d
Results and Discussion:Translating knowledge of Turing Machines into Haskell
Test – Function: Addition
add :: Tmacadd 0 2 = (1,2,1)add 1 1 = (1,1,1)add 1 0 = (2,1,1)add 2 1 = (2,1,1)add 2 2 = (3,2,-1)add 3 1 = (4,2,-1)add 4 1 = (4,1,-1)add 4 2 = (-1,2,0)
3 + 2 = 5
Test – Function: Multiplication
mult :: Tmacmult 0 0 = (7,0,1)mult 0 2 = (0,2,1)mult 1 1 = (2,2,1)mult 1 0 = (6,0,-1)mult 2 0 = (3,0,1)mult 2 1 = (2,1,1)mult 3 1 = (3,1,1)mult 3 2 = (4,1,-1)mult 4 0 = (5,0,-1)mult 4 1 = (4,1,-1)mult 5 1 = (5,1,-1)mult 5 2 = (1,2,1)mult 6 2 = (6,1,-1)mult 6 0 = (9,0,-1)
mult 7 0 = (10,2,1)mult 7 1 = (8,2,1)mult 8 0 = (1,0,1)mult 8 1 = (8,1,1)mult 9 1 = (9,1,-1)mult 9 2 = (7,2,1)--mult 10 1 = (10,2,1)mult 10 0 = (11,2,-1)mult 11 2 = (11,2,-1)mult 11 0 = (-1,2,0)
3 x 2 = 6
UTM Prototype
Results and Discussion:Translating knowledge of Turing Machines into Haskell
Successful !
A Haskell Implementation of Turing Machines
Conclusion
1
2
3
State-transition
table / diagramHaskell
Codes
Prototype of UTM
Theory-Practice Gap
A Haskell Implementation of Turing Machines
Future Work
1
2
3
More Functions (e.g. Logarithm, Factorial)
Visualise operation of Turing Machine
Multi-tape Turing Machine
Thank You !