Lecture 1: A Formal Model of Computation 虞虞虞 虞虞虞虞虞虞虞 虞虞虞虞虞 虞虞虞 體
Jan 04, 2016
Lecture 1: A Formal Model of Computation
虞台文大同大學資工所智慧型多媒體研究室
Content
Formalizing the Idea of Programmable Computer.
Program Defined Machine Defined Computation Defined A Simple Machine
Lecture 1: A Formal Model of Computation
Formalizing the Idea of Programmable
Computer
大同大學資工所智慧型多媒體研究室
A Simple Programmable Device PC
PC Pocket Calculator
What is inside?
Inside PC Memory Set
– Two registers with unbounded capacity
Machine Instructions– Operations & tests
, , 0m n mM n
, ,m n m n ne.g.,
Inside PC Memory Set
– Two registers with unbounded capacity
Machine Instructions– Operations & tests
, , 0m n mM n
, ,m n m n ne.g.,
How about if n = 0?How about if n = 0?
Undefined if n = 0.Undefined if n = 0.
Inside PC Memory Set
– Two registers with unbounded capacity
Machine Instructions– Operations & tests
, , 0m n mM n
, ,m n m n ne.g.,
Partial FunctionsPartial
FunctionsPartial
PredicatesPartial
Predicates
Partial Functions
Let A and B be two sets, and
( ,, )A B a b a A Bf b is a set of ordered pairs.
We say that f is a partial function if( , )
( , )
a bb b
a b
f
f
Hence, (a, b)f is written as:
If, aA, there is no bB such that f(a) = b, then we say that f(a) is undefined, and denote this condition as: ( )f a
( )f a b
Partial Functions is reserved to denote undefined object. f() = . ( ) ( ) and Domain a a A f af
( ) , ( )st Range b b B a A a bf f
There Cases:
1. Totally undefined
2. Partially defined
3. Totally defined
( )Domain f
( )Domain f
( )Domain f A
Operations for PC
Each operation of PC would be defined by some partial function over M.
Example: define the division operation of PC as a partial function as follows:
if
if
, 0,
0
m n n nm n
n
Partial Predicates
: , ,p M true false which does not change the information environment.
Example: | ?m n
if
if not
i
|
, |
0f
true m n
m n false m n
n
Computable Functions
By a computable function, we mean a function that can be algorithmically specified.
When a algorithm is applied to an element outside its domain, it may– not terminated; or– the result B.
Whenever an algorithm is such that it computation does not always terminate, then the algorithm defines a computable partial function.
Exercises
Define “algorithm”.
Show that ,
where f, g, and h are partial functions.
( ) ( )f g h f g h
Lecture 1: A Formal Model of Computation
Program Defined
大同大學資工所智慧型多媒體研究室
Programs vs. Flowcharts
Essentially, we take programs to be flowcharts constituting the following components.
START
HALT
Operation
Predicate (Test)
Flowchart
START
HALT
F0
P1
F1
F2truefalse
Labeled Statements
START
HALT
F0
P1
F1
F2truefalse
L0L1
L2
L3
L4
START: GOTO L3
L0: DO F2 GOTO L3
L1: IF P1 THEN GOTO L0 ELSE GOTO L3
L2: DO F1 GOTO L1
L3: DO F0 GOTO L2
L4: HALT
Instructions
Instructions are
made up from:
L : a set of labels;
F : a set of operations;
P : a set of predicates.
START: GOTO L3
L0: DO F2 GOTO L3
L1: IF P1 THEN GOTO L0 ELSE GOTO L3
L2: DO F1 GOTO L1
L3: DO F0 GOTO L2
L4: HALT
Four Types of Instructions
1. Start Instruction
2. Operation Instruction
3. Test Instruction
4. Halt Instruction
START: GOTO L
L: DO F GOTO L’
L: IF P1 THEN GOTO L’ ELSE GOTO L’’
L: HALT-Loop:
L: DO F GOTO L
Program Schema
A program schema is a set of instructions that contains:
Exactly one start instruction; No repeat on label.
Exercise
while P1 do begin W1; while P2 do
W2; end.
p1 := P1;p2 := false;while p1 or p2 do begin if p2 then W2 else W1; p2 := P2; p1 := P1; end.
Translate the above two Pascal Programs into labeled statements and prove their equivalence, using computation sequence in any convenient way.
Lecture 1: A Formal Model of Computation
Machine Defined
大同大學資工所智慧型多媒體研究室
Machines
M-Machine
A machine is a function, say, M defined on
• the instruction set F P
• for which there exists a memory set M
such that
• MF is a partial function over M for each FF
• MP is a partial predicate over M for each PP.
M-Program
An M-program or a program for M is a program
in which no instruction, except perhaps -loops,
makes use an operation or test name that is
interpreted as the totally undefined function by
M.
Without Syntax ErrorWithout Syntax Error
Lecture 1: A Formal Model of Computation
Computation Defined
大同大學資工所智慧型多媒體研究室
Complete Computation
A completed computation by a program on a machine M is a finite computation sequence: 0 0 1 1 1 1, , , , , , , , , , ,i i i i n nL m L m L m L m L m
Label ofstart instruction
Label of ahalt instruction
Complete Computation
A completed computation by a program on a machine M is a finite computation sequence: 0 0 1 1 1 1, , , , , , , , , , ,i i i i n nL m L m L m L m L m
Case 1:
Case 2:
: DO GO O TiL F L
: IF THEN GOT O ELSE GOTO iL P L L
1 1, ( )i i F iL L m m M
1 1
( ),
( )P i
i i iP i
L m truem m L
L m false
M
M
Without ambiguity at each step. Without ambiguity at each step.
Uncompletable Computation
The reasons for a computation being uncompletable:– Never reach halt instruction;
– Instruction Li: DO F GOTO L’ causes
– Instruction Li: IF P THEN GOTO L’ ELSE GOTO L’’
causes– Reaches a -loop.
( )F im M
( )P im M
Partial Function
The (partial) function
Computed by a program on a machine M is
such that
is a completed computation.
: M M M
0( ) nm m M
0 0 1 1, , , , , ,n nL m L m L m
0( ) nm completedm
othewise
M what a computer does.
Lecture 1: A Formal Model of Computation
A Simple Machine
大同大學資工所智慧型多媒體研究室
Definition of Machine PC
Memory set:
Instruction set: Operations & Predicates
N N
F P
Operations of PC
1x x
1x x
1y y
1y y
y x y
y x y
, 1,m n m n
, 1, if 0
, 0, if 0
m n m n m
m n n m
, , 1m n m n
, , 1 if 0
, ,0 if 0
m n m n n
m n m n
, ,m n m m n
, ,m n m mn
other
Predicates of PC
0?x if 0
,if 0
true mm n
false m
0?y if 0
,if 0
true nm n
false n
?x yif
,if
true m nm n
false m n
other
A PC-Program
START
HALTtrue
false
1x x
1y y
y x y
0?x
GOTO
DO GOTO
IF THEN GOTO E
START: L0
L0: 1 L1
L1: 0? L4 L2
L2: L3
L3:
LSE GOTO
DO GOTO
DO GOT1 L1
L4: HALT
O
y y
x
y x y
x x
L0
L1
L2
L3
L4
Computation Sequence
START
HALTtrue
false
1x x
1y y
y x y
0?x
GOTO
DO GOTO
IF THEN GOTO E
START: L0
L0: 1 L1
L1: 0? L4 L2
L2: L3
L3:
LSE GOTO
DO GOTO
DO GOT1 L1
L4: HALT
O
y y
x
y x y
x x
L0
L1
L2
L3
L4
0 3,0m
0 0, 3,L 1 ,1, 3L 2 ,1, 3L
3 ,3, 3L 1 ,3, 2L 2 ,3, 2L
3 ,6, 2L 1 ,6, 1L 2 ,6, 1L
3 ,6, 1L 1 ,6, 0L 4 ,6, 0L
PC=?
START
HALTtrue
false
1x x
1y y
y x y
0?x
L0
L1
L2
L3
L4
: , 0, ( 1) !m n n m PC
0 0, 3,L 1 ,1, 3L 2 ,1, 3L
3 ,3, 3L 1 ,3, 2L 2 ,3, 2L
3 ,6, 2L 1 ,6, 1L 2 ,6, 1L
3 ,6, 1L 1 ,6, 0L 4 ,6, 0L
Encoding/Decoding
: , 0, ( 1) !m n n m PC
Encoding
Decoding
:e X M
:d M Y
,0k k
,m n n
by
by
PC
k k!
0, !k,0ke d
Encoding/Decoding
: !d e k kPC
PC
k k!
0, !k,0ke d
Man-Machine Interaction
M a machine an M-program
:e X M:d M Y
an encoding function a decoding function
Me d
input output
What Computed?
Me d
input output
d eM A partial function