lecture 3: count programs, while programs and recursively defined functions

Post on 04-Jan-2016

33 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

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

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

Program Construction

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

Writing Programs

Program Building Blocks

Methods for Construction

Basic Programs

START HALT

START HALTF

The primitives

F F

Methods of Program Construction

1. Linear Concatenation

2. Condition Branching

3. Count Loop

4. While Loop

START HALT

START HALTF

Basic Programs

Linear Concatenation

START HALT

START HALTF

Basic Programs

START

HALT

1

START

HALT

2

START

1

HALT

2

LinearConcatenation

LinearConcatenation

Condition Branching

START HALT

START HALTF

Basic Programs

START

HALT

1

START

HALT

2ConditionBranching

ConditionBranching

START

P

HALT

1

true

2

false

Count Loop

START HALT

START HALTF

Basic Programs

START

HALT

CountLoop

CountLoop

START

y>0?

HALT

yy1

true

false

without using register y

While Loop

START HALT

START HALTF

Basic Programs

START

HALT

WhileLoop

WhileLoop

START

P

HALT

true

false

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

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?

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:

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:

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:

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

While Programs

Three Classes of Programs

Count Programs

ConditionalPrograms

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

While Programs Count Programs?

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.

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?

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

While Structure as a Normal Form for

Register Programs

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

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.

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.

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.

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

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

Theorem 3

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

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

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.

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

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

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

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

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

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

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.

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.

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.

Exercise

START

P1

F1

F2

P3

truefalse

truefalse

P2

HALT

true

false

HALT

Transform the program into an equivalent while program.

Corollary

Every register function is an associated function of a while

program.

Register Functions While Programs

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

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

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

The While Recursive 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

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

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

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

: 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

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

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

Writing Functions

Basic Functions

Methods to Construction 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

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.

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

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

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

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?

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

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.

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

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;}

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

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

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

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

Theorem

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

RegisterFunctions

While RecursiveFunctions

Exercises

Show that the following functions are while recursive

: ,mult x y xy

: , yexp x y x

if : ,

otherwise

zz y xlog x y

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

Primitive Recursive Functions &

Partial Recursive 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

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

Definition

1. Basic Functions

2. Generalized Composition

3. Primitive Recursion

4. -Operator

Primitive Recursive Functions

Partial Recursive 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?

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

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

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

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

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?

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

Theorem 5

Primitive Recursive Function Count Program

Pf)

1. Basic Functions

2. Generalized Composition

3. Primitive Recursion

Primitive Recursive Functions

Count Program

s

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

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

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

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

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

Theorem 5

Partial Recursive Function While Program

Pf)

1. Basic Functions

2. Generalized Composition

3. -Operator

Partial Recursive Functions

While Program

s

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

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.

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

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

While Programs

Discussion

Partial Recursive Functions

Count Programs

Primitive Recursive 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

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

Lecture 3:Count Programs, While Programs

and Recursively Defined Functions

The Turing Machine

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

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;

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

The TM() Operations F

PRINT s

MOVE LEFT

MOVE RIGHT

ERASE

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

Accessing TM()

Encoder

Decoder

,1:

,0instring

: ,outstring i

: * *outstring instring TM

By that, for any program ,

is a partial function over *.

Example

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

ˆ0110001 ˆ 01101001

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

Simple Turing Machines

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

abbreviated as TM, is called the

simple Turing machine.

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

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

Exercise

Stepwise simulation of TM using a register machine.

Lemmas & Theorem

TM() simulates TM where || 2.

TM simulates TM() where || 2.

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

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

top related