a haskell implementation of turing machines...a haskell implementation of turing machines haskell...

22
A Haskell Implementation of Turing Machines Lim Shao En Zhang Licheng

Upload: others

Post on 17-Jul-2020

139 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Lim Shao En

Zhang Licheng

Page 2: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

Computer Science

Computability Theory

A Haskell Implementation of Turing Machines

Page 3: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

Computability Theory

A Haskell Implementation of Turing Machines

• Turing Machine

• Recursion theory

• Lambda-calculus

• Post system

Page 4: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Aim and HypothesisBridging the theory – practice gap

Page 5: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

Aim and Hypothesis

Computability

Theory

A Haskell Implementation of Turing Machines

Abstract

Descriptions

Computer

programs

Our Aim

TuringMachines

HaskellCodes

Page 6: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Turing Machine

Tape (Infinite Length)

Cells

Page 7: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Turing Machine

1 0 0 0 00 1 1 1 0 1

Symbols (0,1 or Blank)

Page 8: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Turing Machine

1 0 0 0 00 1 1 1 0 1

q1Head

Page 9: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 10: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Turing Machine -- -- Transitions

Page 11: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Haskell

Page 12: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 13: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 14: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Results and Discussion

Translating knowledge of Turing Machines into Haskell

Page 15: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 16: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 17: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 18: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 19: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

UTM Prototype

Results and Discussion:Translating knowledge of Turing Machines into Haskell

Successful !

Page 20: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

A Haskell Implementation of Turing Machines

Conclusion

1

2

3

State-transition

table / diagramHaskell

Codes

Prototype of UTM

Theory-Practice Gap

Page 21: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

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

Page 22: A Haskell Implementation of Turing Machines...A Haskell Implementation of Turing Machines Haskell •Typed , Functional Programming Language • Typed - Data types in haskell are built

Thank You !