11. Computational Models - Computing at Northumbria

Download Report

Transcript 11. Computational Models - Computing at Northumbria

11. Computational Models
• The exam.
• Models of computation.
– The Turing machine.
– The Von Neumann machine.
– The l calculus.
– The predicate calculus.
• Turing machine equivalence.
• Referential transparency.
11.1
Computational Models
• Computability : the study of what can be
computed by a Totally Obedient Moron (TOM).
– TOM = computational model = programming
paradigm.
– Follows instructions correctly.
– Has no initiative.
• State manipulating TOMs :
– Turing machine.
– Von Neumann machine.
• State constant TOMs :
– l calculus.
– Predicate calculus.
11.2
Turing Machine
(S1,1)
(S1,0)
(S2,1)
(S2,0)
(S3,1)
(S3,0)
(S4,1)
(S4,0)
(S5,1)
• Tape is infinitely long.
• Read / write head can :
– Go left or right.
– Write 0 or 1.
– Change state.
• c.f. Programmable logic
arrays.
1
1
0
1
0









0
(0,L,S2)
(0,R,S3)
(1,R,S4)
(0,R,S4)
(0,R,S1)
(1,L,S2)
(1,L,S2)
(1,L,S5)
()
1
11.3
Von Neumann Machine
• Program and data held in (infinite)
memory.
• Processor reads instructions from
memory and executes them.
– Instructions modify memory.
Processor
MOVE adr1,adr2
ADD adr1,adr2
HALT
F1A0
1928
ABCD
786B
11.4
l Calculus
• Computation is reduction of a (possibly infinite) l expression
to normal form.
• Only one operation : function application (a.k.a. b reduction).
(l x . e) v  e[v / x]
• Two possible evaluation strategies :
– Applicative order : choose leftmost innermost redex.
– Normal order : choose leftmost outermost redex.
• When expression contains no redexs it is in normal form.
• If an expression can be reduced to normal form then using
normal order reduction will always succeed in doing so.
• NB : We also allow a (renaming) and d (arithmetical etc.)
reductions but they are not really required.
11.5
Predicate Calculus
• Computation is an attempt to prove a predicate (a.k.a. a
query) from a (possibly infinite) database of predicates
(a.k.a. facts and rules).
– Proof either succeeds with a set of variable bindings
or fails.
• Only one operation : unification.
P(A,fred,B,7)  P(X,Y,23,7)
succeeds with bindings
A  X, fred  Y, B  23, 7  7.
• If unification of two predicates fails then backtrack to a
“higher level” of the search tree and try something else.
– Only if all alternatives fail will the overall query fail.
11.6
In Practice
• Programming languages could be based on each model.
– Turing machine : none that I know of. Why?
– Von Neumann machine : imperative (C, C++, Pascal
etc.).
– l calculus : functional (ML {nearly}, Miranda, Haskell
etc.).
– Predicate calculus : relational (PROLOG {nearly}).
• Programming languages use sugared syntax to make life
easier for programmers.
– C++ translates directly to Von Neumann machine
instructions.
– Miranda translates directly to l calculus.
– PROLOG translates directly to predicate calculus.
11.7
In Practice II
• Almost all computers are Von Neumann machines.
– Most languages are imperative.
– Functional and relational languages must be translated
to Von Neumann machine instructions.
 Not easy, especially for relational languages.
• NB: Computer memory is finite so a computer is not a
pure Von Neumann machine but it’s as close as we’ll get.
• Some computers have been built which take l calculus or
predicate calculus as their “machine code”.
– Mainly research projects.
– Imperative languages must be translated to l calculus
or predicate calculus.
 Not easy, but possible.
11.8
Turing Machine Equivalence
• A.K.A Church’s thesis after Alonzo Church.
• Very informally : anything one model can do any other
model can do.
– All TOMs have equivalent computational power.
• Impossible to prove (why?) but intuitively seems to be
correct.
• Major consequence :
– Any program than can be written in a general purpose
(a.k.a. Turing machine equivalent) programming
language can be written in any other general purpose
language.
– Miranda == PROLOG == C++ == Java == Haskell etc.
• Which language you use is purely a matter of convenience.
11.9
State Manipulating vs. State Constant Models
• In state manipulating models (Turing and Von
Neumann machines) variables are (bound to)
store locations.
• Computation proceeds by state modifications.
– Writing 0 or 1 on the tape. Moving left or right.
– Changing values in memory.
• In state constant models (l and predicate
calculus) variables are bound to values.
• Computation proceeds by re-writing expressions.
– Reduction to normal form.
– Unification.
• Main consequence : referential transparency of
declarative programs.
11.10
Referential Transparency
• Within the same scope an expression always
means the same thing (since the value of
variables cannot be changed).
• Expressions obey simple rules :
f(x) + g(x)

g(x) + f(x)
• Can’t guarantee this in an imperative (or Turing
machine) language.
• Declarative language compilers use these rules
extensively to optimise code.
– Compilation by transformation.
• Programmers use these rules (sometimes) to
prove their programs correct.
11.11
Summary
• Four main models of computation.
– State manipulating : Turing machine and Von
Neumann machine.
– State constant : l calculus and predicate calculus.
• Turing machine equivalence : all models are equally
powerful (Church’s thesis).
• State constant models exhibit referential
transparency.
• Imperative programming languages based on Von
Neumann machine model.
• Functional programming languages based on l calculus
model.
• Relational programming languages based on predicate
11.12