105
Lecture 3: Count Programs, While Programs and Recursively Defined Functions 虞虞虞 虞虞虞虞虞虞虞 虞虞虞虞虞 虞虞虞

Lecture 3: Count Programs, While Programs and Recursively Defined Functions

Embed Size (px)

DESCRIPTION

Lecture 3: Count Programs, While Programs and Recursively Defined Functions. 虞台文. 大同大學資工所 智慧型多媒體研究室. Content. Program Construction While Structure as a Normal Form for Register Programs The While Recursive Functions Primitive Recursive Functions & Partial Recursive Functions - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

虞台文大同大學資工所智慧型多媒體研究室

Page 2: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

ContentProgram ConstructionWhile Structure as a Normal Form

for Register ProgramsThe While Recursive FunctionsPrimitive Recursive Functions &

Partial Recursive FunctionsThe Turing Machine

Page 3: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

Program Construction

大同大學資工所智慧型多媒體研究室

Page 4: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Writing Programs

Program Building Blocks

Methods for Construction

Page 5: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Basic Programs

START HALT

START HALTF

The primitives

F F

Page 6: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Methods of Program Construction

1. Linear Concatenation

2. Condition Branching

3. Count Loop

4. While Loop

START HALT

START HALTF

Basic Programs

Page 7: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Linear Concatenation

START HALT

START HALTF

Basic Programs

START

HALT

1

START

HALT

2

START

1

HALT

2

LinearConcatenation

LinearConcatenation

Page 8: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Condition Branching

START HALT

START HALTF

Basic Programs

START

HALT

1

START

HALT

2ConditionBranching

ConditionBranching

START

P

HALT

1

true

2

false

Page 9: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Count Loop

START HALT

START HALTF

Basic Programs

START

HALT

CountLoop

CountLoop

START

y>0?

HALT

yy1

true

false

without using register y

Page 10: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Loop

START HALT

START HALTF

Basic Programs

START

HALT

WhileLoop

WhileLoop

START

P

HALT

true

false

Page 11: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Program Construction

START HALT

START HALTF

Basic Programs:

START

1

HALT

2

START

P

HALT

1

true

2

false

START

y>0?

HALT

yy1

true

false

START

P

HALT

true

false

Methods of Program Construction:

LinearConcatenation Condition

Branching

Count Loop While Loop

Page 12: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Count Loop vs. While Loop

START HALT

START HALTF

Basic Programs:

START

1

HALT

2

START

P

HALT

1

true

2

false

START

y>0?

HALT

yy1

true

false

START

P

HALT

true

false

Methods of Program Construction:

LinearConcatenation Condition

Branching

Count Loop While Loop

What is their main distinction?

Page 13: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Conditional Programs

START HALT

START HALTF

Basic Programs:

START

1

HALT

2

START

P

HALT

1

true

2

false

START

y>0?

HALT

yy1

true

false

START

P

HALT

true

false

Methods of Program Construction:

LinearConcatenation Condition

Branching

Count Loop While Loop

Conditional Programs Count Programs While Programs

Three classes of programs:

Page 14: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Count Programs

START HALT

START HALTF

Basic Programs:

START

1

HALT

2

START

P

HALT

1

true

2

false

START

y>0?

HALT

yy1

true

false

START

P

HALT

true

false

Methods of Program Construction:

LinearConcatenation Condition

Branching

Count Loop While Loop

Conditional Programs Count Programs While Programs

Three classes of programs:

Page 15: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Programs

START HALT

START HALTF

Basic Programs:

START

1

HALT

2

START

P

HALT

1

true

2

false

START

P

HALT

true

false

Methods of Program Construction:

LinearConcatenation Condition

Branching

START

y>0?

HALT

yy1

true

false

Count Loop While Loop

Conditional Programs Count Programs While Programs

Three classes of programs:

Page 16: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Three Classes of Programs

Conditional Programs– Basic Programs– Linear Concatenation + Condition Branching

Count Programs– Basic Programs– Linear Concatenation + Condition Branching + Count

Loop

While Programs– Basic Programs– Linear Concatenation + Condition Branching + While

Loop

Page 17: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Programs

Three Classes of Programs

Count Programs

ConditionalPrograms

Problem: Count Programs = While Programs?Count Programs While Programs?

While Programs Count Programs?

Page 18: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Programs Total Functions

More on Count Programs

Count Programs

ConditionalPrograms

1. Count Programs While Programs.2. “Is a counter program?” algorithmically

checkable.3. Every count program implements a total function.

Page 19: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Programs Total Functions

More on Count Programs

Count Programs

ConditionalPrograms

1. Count Programs While Programs.2. “Is a counter program?” algorithmically

checkable.3. Every count program implements a total function.

Problem: Count Programs = Total Functions?Count Programs Total Functions?

Total Functions Count Programs?

Page 20: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

While Structure as a Normal Form for

Register Programs

大同大學資工所智慧型多媒體研究室

Page 21: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Program Equivalence

Definition.Two programs and ’ are M-equivalent if and only if

M MDefinition.Two programs are equivalent if and only if they are M-equivalent for every machine M.

Page 22: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 1

Every program is equivalent to one with just one halt instruction.

Pf) Case 1: without halt instruction

– Add one halt instruction.

Case 2: with multiple halt instructions– Condense to one by making their halt labels

identical.

Page 23: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

L1: IF P THEN GOTO L2 ELSE GOTO L3

L2: IF P THEN GOTO L4 ELSE GOTO L5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Theorem 2Every program is equivalent to one in which no test instruction leads directly to a test instruction involving the same predicate name.

P truefalse

true P false

L1

L2 L3

L4L5

Without such a code sequence

in a program.Without such a code sequence

in a program.

Page 24: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 2Every program is equivalent to one in which no test instruction leads directly to a test instruction involving the same predicate name.

P truefalse

true P false

L1

L2 L3

L4L5

P truefalse

true P false

L1

L2 L3

L4L5

Show that M M

Page 25: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 2Every program is equivalent to one in which no test instruction leads directly to a test instruction involving the same predicate name.

P truefalse

true P false

L1

L2 L3

L4L5

P truefalse

true P false

L1

L2 L3

L4L5

L1: IF P THEN GOTO L2 ELSE GOTO L3

L2: IF P THEN GOTO L4 ELSE GOTO L5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

L1: IF P THEN GOTO L4 ELSE GOTO L3

L2: IF P THEN GOTO L4 ELSE GOTO L5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

m m

Show that M MSuppose ( )p m trueM

1 2 4: ( , ) ( , ) ( , )L m L m L m

1 4: ( , ) ( , )L m L m

Page 26: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 3

Every loop-free program can be transformed into an equivalent conditional program, i.e.,loop-free program conditional program

Page 27: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 3loop-free program

conditional program

Pf)

The empty program is a conditional program.

n: #instructions (operations and test) in .

1. n = 0

2. n = k

3. n = k+1

Assumed true.

To be shown true.

(Induction)

Two Cases

START

1

END

P

2

true false

START

F

END

Page 28: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 3loop-free program

conditional program

Pf)

The empty program is a conditional program.

n: #instructions (operations and test) in .

1. n = 0

2. n = k

3. n = k+1

Assumed true.

To be shown true.

(Induction)

Two Cases

START

1

END

P

2

true false

START

F

END

The first Instruction is an operation.

The first Instruction is an operation.

The first Instruction is a predicate.

The first Instruction is a predicate.

Page 29: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 3loop-free program

conditional program

Pf)

The empty program is a conditional program.

n: #instructions (operations and test) in .

1. n = 0

2. n = k

3. n = k+1

Assumed true.

To be shown true.

(Induction)

Two Cases

START

END

P

1 2

true false

START

F

END

k instructions

Page 30: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 3loop-free program

conditional program

Pf)

The empty program is a conditional program.

n: #instructions (operations and test) in .

1. n = 0

2. n = k

3. n = k+1

Assumed true.

To be shown true.

(Induction)

Two Cases

START

END

P

1 2

true false

START

F

END

k instructions ’

START

F

END

Page 31: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 3loop-free program

conditional program

Pf)

The empty program is a conditional program.

n: #instructions (operations and test) in .

1. n = 0

2. n = k

3. n = k+1

Assumed true.

To be shown true.

(Induction)

Two Cases

START

END

P

1 2

true false

START

F

END

k instructions1 2

START

END

P

Page 32: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Exampleloop-free program

conditional program

START

P1

F1

F2

F3

P2

F4

F5

HALT

true false

true false

START

P1

F1

F2

F3

P2

F4

F5

HALT

true false

true false

F4

F5

Page 33: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Exampleloop-free program

conditional program

START

P1

F1

F2

F3

P2

F4

F5

HALT

true false

true false

START

P1

F1

F2

F3

P2

F4

F5

HALT

true false

true false

F4

F5

Page 34: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 4By making at most one extra variable, every program can be transformed into a while program which computes the same function over the set of registers used by .

program while program

Many methodsMany methods

Page 35: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 4program

while program

Pf) Assume that has n loops.Select a cut-point in each loop, and modify the code, such as:

Ii

cut-point

Ii

y i

ith loop

The program now becomes loop free.

Page 36: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 4

Ii

y i

Ii

cut-point

ith loop

program

while program

Ii

y iIn

Transform the loop-free program obtained above to a conditional program according to Theorem 3.

Page 37: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 4

Ii

y i

Ii

cut-point

ith loop

program

while program

y n+1

START

y>0?

y=i?

y=n?

Ii

y iIn

y 0

true

HALTfalse

false

false

true

true

Any halt instruction is replaced with this one.

Page 38: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Exercise

START

P1

F1

F2

P3

truefalse

truefalse

P2

HALT

true

false

HALT

Transform the program into an equivalent while program.

Page 39: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Corollary

Every register function is an associated function of a while

program.

Register Functions While Programs

Page 40: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Discussions

What can be computed?

What can be computed by SR (R)?

What can be computed by while programs?

What can be computed by:

1.Linear Concatenation2.Conditional Branching3.While-Loop

Page 41: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Gotoless Programming

12

if P then 1 else 2

while P do

START

1

HALT

2

START

P

HALT

1

true

2

false

START

P

HALT

true

false

Page 42: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

The While Recursive Functions

大同大學資工所智慧型多媒體研究室

Page 43: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

The Methods of Program Construction

1.Linear Concatenation

2.Conditional Branching

3.While-Loop

12

if P then 1 else 2

while P do

Page 44: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Linear Concatenation (=12 )

START

1

HALT

2

:

1 1 , , kh h k - adic associated f unctions of

1 2 , , kg g k - adic associated f unctions of

1 1, , k kx j x j

1( , , )i i kx h j j

1 1 1( , , ), , ( , ), )(i k k kix h j j h jg j

Page 45: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Linear Concatenation (=12 )

START

1

HALT

2

:1 1, , k kx j x j

1, , kf f k - adic associated f unctions of

1 1 1( , , ), , ( , ), )(i k k kix h j j h jg j

1 1 1 1: , , ( , , ), , ( ,( , ))ki k k kif x x h x x h x xg

Defined as function composition:

1 1 , , kh h k - adic associated f unctions of

1 2 , , kg g k - adic associated f unctions of

1, ,( )ki jf j

Page 46: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Condition Branching

: START

P

HALT

1

true

2

false

1 1 , , kh h k - adic associated f unctions of

1 2 , , kg g k - adic associated f unctions of

'sih 'sig

1 1, , k kx j x j

1

1(

( , , ) if

if , ) n t, oi

ii

k

k

g j

h j j Px

Pj

Page 47: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

: START

P

HALT

1

true

2

false

'sih 'sig

1 1, , k kx j x j

1

1(

( , , ) if

if , ) n t, oi

ii

k

k

g j

h j j Px

Pj

Condition Branching

1 1 , , kh h k - adic associated f unctions of

1 2 , , kg g k - adic associated f unctions of

1, ,

kf fk

- adic associated

f unctions of

Defined by part:

1, ,( )ki jf j

11

1

( , , ) if , ,

if n( , t, o) : i k

ii k

k g x

h x x

xf

Px x

P

Page 48: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Loop1 1 , , kh h k - adic associated f unctions of

1, , kx x:

11 1 1

if not :

(, ,

( , , ), , ( , , ) if )i

ki

k kikf

x Px x

h x x h x xf P

START

P

HALT

1

true

false

1 1 1( , , ), , ( , , )k k kh x x h x x

Defined by while recursion:'sih

Page 49: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Loop1 1 , , kh h k - adic associated f unctions of

1, , kx x:

11 1 1

if not :

(, ,

( , , ), , ( , , ) if )i

ki

k kikf

x Px x

h x x h x xf P

START

P

HALT

1

true

false

1 1 1( , , ), , ( , , )k k kh x x h x x

Defined by while recursion:'sih 1, ,

kf fk

- adic associated

f unctions of

Page 50: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Writing Functions

Basic Functions

Methods to Construction Functions

Page 51: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Basic Functions

1:S x x

1 if:

0

0 if 0

x xP x

x

1, ,: ik kip x x x

Successor

Predecessor

Projector

e.g.,13

23

33

(2,3,5) 2

(2,3,5) 3

(2,3,5) 5

p

p

p

Page 52: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Forms of Function Definition

1:S x x 1 if

: 0

0 if 0

x xP x

x

1, ,: ik kip x x x

Successor Predecessor Projector

1. Generalize Composition

2. Conditional Definition

3. While Recursion

1 1 1 1, , ( , , ), , ( , , ): ( )k k m kgx x h x x h xf x

1

11

( , , ) if 0, ,

if 0( , , ):

k

k ik

i

h x x xx x

xg xf

x

11

1 1 1

( , , ) if 0, ,

( , , ), , ( , , ) if 0:

( )k i

kk k k i

g x x xx x

h xf

x h x x xf

m k>=<

1 ( , , ) 'sk ix hf x if any of is und efi ned.1 ( , , ) 'sk ix hf x if any of is und efi ned.

Page 53: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Recursive Functions

1:S x x 1 if

: 0

0 if 0

x xP x

x

1, ,: ik kip x x x

Successor Predecessor Projector

1, ,

:

i

k

ki

p xx

x

1

:Sx

x

1 if: 00

if 0

xx

P x

x

1

11

1

:, ,

( , , ),, ( , ,

))

(k

k

m

k

g

xx

h xx

xx

f

h

11

1 11

( , , )if 0

, ,( , , ), , ( , , ) if 0

:(

)

k

ik

k k k i

g x xx

x xh x

f

x h x x xf

1

11

( , , ) if 0

, , if 0( , , ): k

k i

k i

h x x x

x x xg xf x

Page 54: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Recursive Functions

The class of functions which includes

• all the basic functions ; and

• are closed under

1:S x x 1 if

: 0

0 if 0

x xP x

x

1, ,: ik kip x x x

Successor Predecessor Projector

, ikS P pand

1. Generalize Composition

2. Conditional Definition

3. While Recursion

Page 55: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is : ,add x y x y while recursive?

It will be true if we can define this function by applying

• Generalize Composition

• Conditional Definition

• While Recursion

onto

• Successor

• Predecessor

• Projector

Page 56: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is : ,add x y x y while recursive?

Analysis

if 0

: ,1, 1 if 0

x yadd x y

add x y y

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h

1

11

( , , ) if 0, ,

if 0( , , ):

k

k ik

i

h x x xx x

xg xf

x

11

1 1 1

( , , ) if 0, ,

( , , ), , ( , , ) if 0:

( )k i

kk k k i

g x x xx x

h xf

x h x x xf

1:S x x 1 if

: 0

0 if 0

x xP x

x

1, ,: ik kip x x x

Successor Predecessor Projector

Which functional form(s) will be used?

Which basic functions will be used?

Page 57: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is : ,add x y x y while recursive?

if 0

: ,1, 1 if 0

x yadd x y

add x y y

11

1 1 1

( , , ) if 0, ,

( , , ), , ( , , ) if 0:

( )k i

kk k k i

g x x xx x

h xf

x h x x xf

1 2

( , ) if 0: ,

, if 0( , ) ( , )h

g ya

x y h x ydd x

xy

add y

y

while recursion

1

2

( , )

(

)

, )

( ,

g x y

h x y

h x y = ?

12: ( , ) ( , )g x y p x y basic function

11 2

22 2

: ( , ) ( ( , ))

: ( , ) ( ( , ))

h x y S p x y

h x y P p x y

composition

composition

Page 58: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is : ,add x y x y while recursive?

1 2

( , ) if 0: ,

, if 0( , ) ( , )h

g ya

x y h x ydd x

xy

add y

y

12: ( , ) ( , )g x y p x y

11 2: ( , ) ( ( , ))h x y S p x y

22 2: ( , ) ( ( , ))h x y P p x y

This shows that

: ,add x y x yis while recursive.

if 0

: ,1, 1 if 0

x yadd x y

add x y y

For convenience, we allow using the above syntax.

Page 59: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is : ,sub x y x y while recursive?

1 2

( , ) if 0: ,

, if 0( , ) ( , )h

g ys

x y h x yub x

xy

sub y

y

12: ( , ) ( , )g x y p x y

11 2: ( , ) ( ( , ))h x y P p x y

22 2: ( , ) ( ( , ))h x y P p x y

This shows that

: ,sub x y x yis while recursive.

if 0

: ,1, 1 if 0

x ysub x y

sub x y y

Page 60: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is / if 0: ,

if 0

x y ydiv x y

y

while recursive?

int div(int x, int y){ if( x < y) return 0; return div(x - y, y) + 1;}

int div(int x, int y){ if( x < y) return 0; return div(x - y, y) + 1;}

Page 61: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is / if 0: ,

if 0

x y ydiv x y

y

while recursive?

int div(int x, int y){ return div1(x, y, 0, x < y);}

int div(int x, int y){ return div1(x, y, 0, x < y);}

int div1(int x, int y, int q, int x_less_y){ if(x_less_y) return q; return div1(x - y, y, q + 1, x - y < y);}

int div1(int x, int y, int q, int x_less_y){ if(x_less_y) return q; return div1(x - y, y, q + 1, x - y < y);}

: , 1( , ,0, )div x y div x y x y

Page 62: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is / if 0: ,

if 0

x y ydiv x y

y

while recursive?

: , 1( , ,0, )div x y div x y x y

1 22 2( , ) ( , ): , 1( , , ( , ) (, ), )p x y p x y zero x y lessdi xv y div yx

Formally, div must be defined as:

x y zero(x) 0: x < y1: x y0

: , 1( , , , )( ) ( , )x y zero x lessdiv x y v xi yd

For convenience, it is now defined as:

composition

Page 63: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example : , 1( , ,0, )div x y div x y x y

: , 1( , , , )( ) ( , )x y zero x lessdiv x y v xi yd composition

( , ), , ( , )

if 01: , , ,

1( ,, )( if1 , ) 0sub x y y z sub x y

z wdiv x y z w

div e yl s ws

while recursionif 0

:( 1) if 0

x xzero x

zero x x

while recursion

: , ( ( , ))less x y pos sub y x composition

( ( )) if 0:

( ) if 0

S zero x xpos x

zero x x

condition

Page 64: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

Is / if 0: ,

if 0

x y ydiv x y

y

while recursive?

: , 1( , , , )( ) ( , )x y zero x lessdiv x y v xi yd

( , ), , ( , )

if 01: , , ,

1( ,, )( if1 , ) 0sub x y y z sub x y

z wdiv x y z w

div e yl s ws

if 0:

( 1) if 0

x xzero x

zero x x

: , ( ( , ))less x y pos sub y x

( ( )) if 0:

( ) if 0

S zero x xpos x

zero x x

This shows that

is while recursive.

/ if 0: ,

if 0

x y ydiv x y

y

Page 65: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem

The register functions are precisely the while recursive functions, i.e.,

RegisterFunctions

While RecursiveFunctions

Page 66: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Exercises

Show that the following functions are while recursive

: ,mult x y xy

: , yexp x y x

if : ,

otherwise

zz y xlog x y

Page 67: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

Primitive Recursive Functions &

Partial Recursive Functions

大同大學資工所智慧型多媒體研究室

Page 68: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Basic Functions

1:S x x

1 if:

0

0 if 0

x xP x

x

1, ,: ik kip x x x

Successor

Predecessor

Projector

e.g.,13

23

33

(2,3,5) 2

(2,3,5) 3

(2,3,5) 5

p

p

p

Page 69: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Forms of Function Definition

1:S x x 1 if

: 0

0 if 0

x xP x

x

1, ,: ik kip x x x

Successor Predecessor Projector

1. Generalize Composition

2. Primitive Recursion

3. -Operator

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h

11

1 1

( , , ) if 0, , ,

( , , , 1, ( , , , 1)) if 0:

k

kk k

ff

g x x yx x y

xh x y x x y y

1 1, ,: : ( , , , ) 0k kx x z h x x zf

m k>=<

1 ( , , ) 'sk ix hf x if any of is und efi ned.1 ( , , ) 'sk ix hf x if any of is und efi ned.

Value of f (x1,…,xk) is the smallest z such that 1( , , , ) 0,kh x x z

and 1, ( , , , ) .kz z h x x z

Page 70: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Definition

1. Basic Functions

2. Generalized Composition

3. Primitive Recursion

4. -Operator

Primitive Recursive Functions

Partial Recursive Functions

Page 71: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example : ,add x y x y

if 0: ,

( , 1) 1 if 0

x yadd x y

add x y y

Analysis

Primitive Recursive?

Partial Recursive?

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h

11

1 1

( , , ) if 0, , ,

( , , , 1, ( , , , 1)) if 0:

k

kk k

ff

g x x yx x y

xh x y x x y y

1 1, ,: : ( , , , ) 0k kx x z h x x zf

Which forms are applicable?

Page 72: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example : ,add x y x y

Primitive Recursive?

Partial Recursive?

11 ( ) if 0

: ,( , 1, ( , 1)) if 0

p x yadd x y

x yh add x y y

33: , , ( ( , , ))x y z S p x y zh

: ,add x y x y

is primitive recursive.

if 0: ,

( , 1) 1 if 0

x yadd x y

add x y y

Analysis

Page 73: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example : ,mult x y xyPrimitive Recursive?

Partial Recursive?

( ) if 0: ,

( , 1, ( , 1)) if 0

zero x ymult x y

x y mh ult x y y

1 33 3: , , ( ( , , ), ( , , ))x y z add p x y z p x y zh

: ,mult x y y

is primitive recursive.

0 if 0: ,

( , ( , 1)) if 0

ymult x y

add x mult x y y

Analysis

Page 74: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example Primitive Recursive?

Partial Recursive?

11 ( ) if 0

: ,( , 1, ( , 1)) if 0

p x ysub x y

x yh sub x y y

33: , , ( ( , , ))x y z P p x y zh

: ,sub x y x y is primitive recursive.

if 0: ,

( , 1) 1 if 0

x ysub x y

sub x y y

Analysis

: ,sub x y x y

Page 75: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example / 0

: ,0

x y ydiv x y

y

Primitive Recursive?

Partial Recursive?

: , : ( , ( , 1)) 0div x y z less x mult y z Analysis

: , : ( , , ) 0div x y z x yh z

( , , ) ( , ( , 1 ): )x y z less x m lt y zh u

: ( , ) ( ( , ))less x y pos sub y x…

To continue until reaching the lowest level.

/ 0: ,

0

x y ydiv x y

y

is partial recursive.

1 if 0:

0 if 0

xpos x

x

Page 76: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Question / 0

: ,0

x y ydiv x y

y

Primitive Recursive?

Partial Recursive?

: , : ( , ( , 1)) 0div x y z less x mult y z Analysis

Is div primitive recursive?

Is div primitive recursive?

Page 77: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

1. Every primitive recursive function is an associated function of a count program.

2. Every partial recursive function is an associated function of a while program.

Primitive Recursive Function Count Program

Partial Recursive Function While Program

Page 78: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

Primitive Recursive Function Count Program

Pf)

1. Basic Functions

2. Generalized Composition

3. Primitive Recursion

Primitive Recursive Functions

Count Program

s

Page 79: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

Primitive Recursive Function Count Program

Pf)

1. Basic Functions

2. Generalized Composition

3. Primitive Recursion

, , ikS P p

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h

Count Program

Count Program

11

1 1

( , , ) if 0, , ,

( , , , 1, ( , , , 1)) if 0:

k

kk k

ff

g x x yx x y

xh x y x x y y

Count

Program

Page 80: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

Primitive Recursive Function Count Program

Pf)

1. Basic Functions

2. Generalized Composition

3. Primitive Recursion

, , ikS P p

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h

Count Program

Count Program

11

1 1

( , , ) if 0, , ,

( , , , 1, ( , , , 1)) if 0:

k

kk k

ff

g x x yx x y

xh x y x x y y

Count

Program

trivialtrivial

Page 81: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h Count Program

Generalized Composition

START

1i iz x

i k

1h

1 1y r

1i ix z

i k

2h

2 2y r

1i ix z

i k

hm

m my r

1i ix y

i m

g

HALT

Page 82: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

Primitive Recursion Count Program

11

1 1

( , , ) if 0, , ,

( , , , 1, ( , , , 1)) if 0:

k

kk k

ff

g x x yx x y

xh x y x x y y

START

1i iz x

i k

h

HALT

g

2k gx r

1 0kx

y>0?

1i ix z

i k

2k hx r

1

1 1k

k

x

x

1y y

true

false

Page 83: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

1. Every primitive recursive function is an associated function of a count program.

2. Every partial recursive function is an associated function of a while program.

Primitive Recursive Function Count Program

Partial Recursive Function While Program

Page 84: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

Partial Recursive Function While Program

Pf)

1. Basic Functions

2. Generalized Composition

3. -Operator

Partial Recursive Functions

While Program

s

Page 85: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

Partial Recursive Function While Program

Pf)

1. Basic Functions

2. Generalized Composition

3. -Operator

, , ikS P p

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h

While Program

While Program

1 1, ,: : ( , , , ) 0k kx x z h x x zf While Program

Page 86: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5

Partial Recursive Function While Program

Pf)

1. Basic Functions

2. Generalized Composition

3. -Operator

, , ikS P p

1 1 1 1: , , ( , , ), , ( , , ))(k k m kgx x h x x x xf h

While Program

While Program

1 1, ,: : ( , , , ) 0k kx x z h x x zf While Program

As primitive recursive functions.

Page 87: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 5 While Program-Operator 1 1, ,: : ( , , , ) 0k kx x z h x x zf

START

0z

h

rh>0?

true

HALT

false

1z z

h

Page 88: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Theorem 6

1. Every associated function of a count program is primitive recursive.

2. Every associated function of a while program is partial recursive.

Count Program Primitive Recursive Function

While Program Partial Recursive Function

Page 89: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

While Programs

Discussion

Partial Recursive Functions

Count Programs

Primitive Recursive Functions

Page 90: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

ExercisesShow that the following functions are primitive recursive:

, yexp x y x

if ,

0 otherwise

zz y xlog x y

0 is a prime number

1 otherwise

xisprime x

gcd , the greatest common divisor of ,x y x y

Page 91: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

ExercisesShow that the following functions are partial recursive:

if ,

otherwise

zz y xlog x y

0 0 and divides

, 1 0 and does not divide

otherwise

x x y

divides x y x x y

% 0,

otherwise

y x xrem x y

Page 92: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

The Turing Machine

大同大學資工所智慧型多媒體研究室

Page 93: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

The Configuration of TM()

Symbol String

S1 S2 Si1 Si Si+1 Sn1 Sn… … Blank TapeBlank Tape

Head

Notations:

: Empty String;{0,1} : Alphabet;

0

* ,0,1,00,01,10,11,i

i

The set of all finitestrings of symbol;

Page 94: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

The TM()

Memory Set: *,0 | |; 0 iff , i iM i

Instruction Set: F P

: Containing at least two symbols;

,0 : Completely blank tape ; ̂1 2 1 2 ˆ,i k i ks s s s i s s s s

Page 95: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

The TM() Operations F

PRINT s

MOVE LEFT

MOVE RIGHT

ERASE

Page 96: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

The TM() Predicates P

s?

LEFT END?

RIGHT END?

1 2

ˆ

ˆ ii k

i

false

true s ss s s s

false s s

ˆ

1,

1 | |

true

true ii

false i

ˆ

1,

1 | |

true

true ii

false i

Page 97: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Accessing TM()

Encoder

Decoder

,1:

,0instring

: ,outstring i

: * *outstring instring TM

By that, for any program ,

is a partial function over *.

Page 98: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example

1: , 1 ,1 , | |,insert i i

ˆ0110001 ˆ 01101001

Page 99: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Example1: , 1 ,1insert i

START

EmptyTape?

0?

Print 1

leftend?

Move left Move left

0?

Print 0 Print 0

Print 0

Print 1

leftend?

Move leftMove left

0?

Print 1Print 1

Print 1

Print 1 HALT

HALT

true

true

true true

true true

falsefalse

false false

false false

Page 100: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Simple Turing Machines

Let {0, 1}. Then, TM(),

abbreviated as TM, is called the

simple Turing machine.

Page 101: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Stepwise Simulation of SR2 by TM

Step 1:

Step 2:

Step 3:

Define encoding function g as:

1, 0 0 0,11: m ng m n

Simulate operations of SR2 on TM.

1x x 1x x

1y y 1y y trivial

next page

Simulate predicates of SR2 on TM.

Exercise

Page 102: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Simulate Operations of SR2 on TM

1y y START

MoveRight

0?

insert1

HALT

true

false

1y y START

MoveRight

0?

delete1

HALT

true

false

MoveRight

0? true

false

Page 103: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Exercise

Stepwise simulation of TM using a register machine.

Page 104: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Lemmas & Theorem

TM() simulates TM where || 2.

TM simulates TM() where || 2.

TheoremThe machines TM(), for varying alphabets constitute a family of equivalent machines.

Page 105: Lecture 3: Count Programs, While Programs and  Recursively Defined Functions

Church Thesis:– Any computable function (algorithmic process) is a

partial recursive function (can be computed on register machines).

Turing's Thesis (weak form): – A Turing machine can compute anything that can be

computed by a general-purpose digital computer. Turing's Thesis (strong form):

– A Turing machine can compute anything that can be computed.

Church-Turing Thesis:– Any computable function can be computed on register

machines or Turing Machines.

Church-Turing Thesis