74
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

Embed Size (px)

Citation preview

Page 1: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

11

CDT314

FABER

Formal Languages, Automata and Models of Computation

Lecture 14

School of Innovation, Design and Engineering Mälardalen University

2012

Page 2: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

2

Content

Recursive Functions

Primitive Recursion

Ackermann function & -recursive functions

Relations Among Function Classes

Page 3: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

3

Recursion

In computer programming, recursion is related to performing computations in a loop.

Page 4: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

Visualisation of a recursive structure

4

A Sierpinksi carpet is a famous fractal objects in mathematics. Creating one is an iterative procedure. Start with a square, divide it into nine equal squares and remove the central one. That leaves eight squares around a central square hole.  In the next iteration, repeat this process with each of the eight remaining squares and so on (see above). 

Page 5: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

5

Recursion used in Problem Modelling

Reducing the complexity by

Breaking up computational sequence into its simplest forms.

Synthesizing components into more complex objects by replicating simple component sequences over and over again.

Page 6: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

6

"A reduction is a way of converting one problem into another problem in such a way that a solution to the second problem can be used to solve the first problem."

Michael Sipser, Introduction to the Theory of Computation

Page 7: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

7

Self-referential definitions can be dangerous if we're not careful to avoid circularity.

This is why our definition of recursion includes the word well-defined.

(Recursion can be seen as a concept of well-defined self-reference.)

*Gertrude Stein: ''A rose is a rose is a rose''

Page 8: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

8

We can write a pseudocode to determine whether somebody is an immigrant:

FUNCTION isAnImmigrant(person): IF person immigrated herself, THEN:

return true ELSE:

return isAnImmigrant(person's parent) END IF

This is a recursive function, since it uses itself to compute its own value.

Recursive definition: an immigrant…

Page 9: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

9

Functions

From math classes, we have seen many ways of defining and combining numerical functions.

Inverse

Composition

Derivatives

Iteration

(iterated function is composed with itself)

…. etc.

1f

gf

),(),(),( xfxfxf

),(),(),( 321 xfxfxf

Page 10: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

10

Functions

Look at what happens when we use only some of these. - How can we define standard interesting functions? - How do these relate to e.g. TM computations? We have seen TMs as functions. They are awkward!

As alternative, look at a more intuitive definition of functions.

Page 11: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

11

Notation

For brevity, we will limit ourselves to functions on natural numbers

Notation will also use n-tuples of numbers

},2,1,0{ N

),,( 1 nmm

Page 12: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

12

Natural Numbers

Start with standard recursive definition of natural numbers (Peano axioms)

A natural number is either

0 or

successor(n), where n is a natural number.

Page 13: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

13

What is a recurrence?

A recurrence is a well-defined mathematical function written in terms of itself.

It is a mathematical function defined recursively.

Page 14: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

14

Fibonacci sequence

1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...

The first two numbers of the sequence are both 1, while each following number is the sum of the two numbers before it.

(We arrived at 55 as the tenth number, since it is the sum of 21 and 34, the eighth and ninth numbers.)

Page 15: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

15

F is called a recurrence, since it is defined in terms of itself evaluated at other values.

)2()1()(

)(1)1(1)0(

nFnFnF

casesbaseFF

Page 16: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

16

A recursive process is one in which objects are defined in terms of other objects of the same type.

Using some sort of recurrence relation*, the entire class of objects can then be built up from a few initial values and a small number of rules.

Recursion & Recurrence

(*Recurrence is a mathematical function defined recursively.)

Page 17: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

17

Computable Functions

Any computable function can be programmed using while-loops (i.e., "while something is true, do something else").

For-loops (which have a fixed iteration limit) are a special case of while-loops.

(Computable functions could of course also be coded using a combination of for- and while-loops. )

Page 18: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

18

Total Function

A function defined for all possible input values.

Primitive Recursive Function

A function which can be implemented using only for-loops.

Page 19: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

19

103

An example function

1)( 2 nnfDomain Range

10)3( f

Page 20: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

20

Building functions from basic ones

Based on C Busch, RPI, Models of Computation

Page 21: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

21

Zero function: 0)( xzero

Successor function: 1)( xxsucc

Projection functions: 1211 ),( xxxp

2212 ),( xxxp

Basic Primitive Recursive Functions

Page 22: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

22

Building functions

Composition

)),(),,((),( 21 yxgyxghyxf

Page 23: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

23

Composition, Generally

Given

NNh

NNf

NNg

NNg

k

m

km

k

:

:

:

...

:1

),,( 1 mggfh

)),,(,),,,((),,( 1111 kmkk nngnngfnnh

Page 24: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

24

Primitive Recursion “Template”

)),(),,(()1,( 2 yxfyxghyxf

)()0,( 1 xgxf

For primitive recursive functions recursion is in only one argument.

Page 25: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

25

Any function built from

the basic primitive recursive functions

is called Primitive Recursive Function.

Page 26: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

26

0)( xzero

)())(( xzeroxsucczero

Basic Primitive Zero function

(a constant function)

0)0()1()2()3( zerozerozerozero

Example

Page 27: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

27

Basic Primitive Identity function

...

xxidentity

xx

210)(

210

))(())((

0)0(

xidentsuccxsuccidentity

identity

Recursive definition

Page 28: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

28

Basic Primitive Successor function

...

1321)(

210

xxsucc

xx

Page 29: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

29

))(()( xzerosuccxone

Using Basic Primitive Zero function

and a Successor function we can construct Constant functions

etc..

))(()( xonesuccxtwo

))(()( xtwosuccxthree

Page 30: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

303

)2(

))1((

)))0(((

))))((((

)))(((

))(()(

succ

succsucc

succsuccsucc

xzerosuccsuccsucc

xonesuccsucc

xtwosuccxthree

Example

Page 31: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

31

A Primitive Recursive Function ),( yxadd

xxadd )0,( (projection)

)),(()1,( yxaddsuccyxadd (successor function)

Page 32: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

32

5

)4(

))3((

)))0,3(((

))1,3(()2,3(

succ

succsucc

addsuccsucc

addsuccadd

Example

Page 33: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

33

5

14

1)13(

1)1)0,3((

1))1,3(()2,3(

add

addadd

Example

Page 34: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

34

Basic Primitive Predecessor function

...

1100)(

210

xxpred

xx

Page 35: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

35

Predecessor

xxsuccpred

pred

))((

0)0(

1)( xxpred

)())((

0)0(

xGxsuccpred

pred

Predecessor is a primitive recursive function with no direct self-reference.

x) identity(G(x) templaterecursive primitive

Page 36: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

36

Subtraction

)),(())(,(

)0,(

xysubpredxsuccysub

yysub

xyxysub ),(

)1)()1(( xyxy

Page 37: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

37

1

)2(

))3((

)))0,3(((

))1,3(()2,3(

pred

predpred

subpredpred

subpredsub

Example

Page 38: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

38

0)0,( xmult

)),(,()1,( yxmultxaddyxmult

),( yxmult

Multiplication

))()1(( xxyyx

Page 39: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

39

x

xxadd

xxaddxadd

xxaddxaddxadd

xaddxaddxaddxadd

xmultxaddxaddxaddxadd

xmultxaddxaddxadd

xmultxaddxadd

xmultxaddxmult

4

)3,(

))2,(,(

))),(,(,(

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

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

))))1,(,(,(,(

)))2,(,(,(

))3,(,()4,(

Example

Page 40: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

40

1),0( xexp

)),,((),1( yyxexpmultyxexp

),( yxexpA Primitive Recursive Function,exponentiation

)( 1 yyy xx

Page 41: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

41

Example

4

)),(

)),,((

)),),,(((

)),),),,1((((

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

)),),),,1((((

)),),,2(((

)),,3((),4(

yyyyy

yyyymult

yyyymultmult

yyyymultmultmult

yyyymultmultmultmult

yyyyyexpmultmultmultmult

yyyyexpmultmultmult

yyyexpmultmult

yyexpmultyexp

Page 42: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

42

Primitive Recursion in Logic

A predicate (Boolean function) with output in the set {0,1} which is interpreted as {yes, no}, can be used to define standard functions.– Logical connectives , ,, , …– Numeric comparisons =, < ,, …– Bounded existential quantification in, f(i)– Bounded universal quantification in, f(i)– Bounded minimization min i in, f(i)

where result = 0 if f(i) never true within bounds.

Page 43: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

43

Recursive Predicates

and?zero ?_ zeronon

1110))?(?(?_

0001)),(()?(

3210

xzerozerozeronon

xxonesubxzero

x

Page 44: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

44

),( yxand ),( yxor ),( yxless )(xnon returns

1

0

00 yx 00 yx

00 yx00 yx

yx

yx

0x

0x

More Recursive Predicates

))),(?((),(

))),(?((),(

))),(?((),(

yxsubzerononyxless

yxaddzerononyxor

yxmultzerononyxand

Page 45: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

45

)),((),(_ yxequalnonyxequalnon

))),(()),,(((),( xylessnonyxlessnonandyxequal

More Recursive Predicates...

Page 46: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

46

Example

Recursive predicates can combine into powerful functions.

What does this compute?

Tests primality!

))1()1((,,)(? njinijnjinjnin

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Page 47: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

47

Function

0

0),,(

xify

xifzzyxif

if

Page 48: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

48

yzyxsuccif

zzyif

),),((

),,0(

)(),),((

)(),,0(

yGzyxsuccif

zBzyif

identityG Bwith

our construction

primitive recursive template

)),(),,(()1,( 2 yxfyxghyxf

)()0,( 1 xgxf

Page 49: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

49

Division example: x/4

rdqx quotient remainderx

0

1

2

3

4

5

6

7

8

0400

1401

2402

3403

0414

1415

2416

3417

0428

0

0

0

0

1

1

1

1

2

0

1

2

3

0

1

2

3

0

quotientq remainderr 4d

Page 50: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

50

Division as Primitive Recursion

))),,((

,

),,((),(

ddxsubremain

x

dxlessifdxremain

)))),,(((

,0

),,((),(

ddxsubquotsucc

dxlessifdxquot

Page 51: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

51

Division example: x/4

))),,((

,

),,((),(

ddxsubremain

x

dxlessifdxremain

rdqx quotient remainderx

0

1

2

3

4

5

6

7

8

0400

1401

2402

3403

0414

1415

2416

3417

0428

0

0

0

0

1

1

1

1

2

0

1

2

3

0

1

2

3

0

quotientq

remainderr

4d

)))),,(((

,0

),,((),(

ddxsubquotsucc

dxlessifdxquot

Page 52: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

52

Division as Primitive Recursion

)0

)),,(((

),)),,(((()),((

0),0(

dxsubremainsucc

ddxremainsucclessifdxsuccremain

dremain

)),()),),((?(()),((

0),0(

dxquotdxsuccremainzeroadddxsuccquot

dquot

Page 53: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

53

)),(?(),( dxremainzerodxdivisible

Recursive Predicate divisible

Page 54: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

54

)),((),(_ yxequalnonyxequalnon

Recursive Predicate

)),(?(),( dxremindzerodxdivisible

Page 55: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

55

Theorem The set of primitive recursive functions

is countable.

Proof Each primitive recursive function

can be encoded as a string.

Enumerate all strings in proper order.

Check if a string is a function.

Page 56: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

56

There is a function that is not primitive recursive.

Proof (by Cantor diagonal argument)Enumerate the primitive recursive functions

,,, 321 fff

Theorem

Page 57: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

57

Define function

1)()( ifig i

g differs from every if

g is not primitive recursive

END OF PROOF

Page 58: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

58

A specific function that is not

primitive recursive:

Ackermann’s function:

)),(,1()1,(

)1,1()0,(

1),0(

yxAxAyxA

xAxA

yyA

Grows very fast,

faster than any primitive recursive function

Page 59: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

59

The Ackermann function is the simplest example of a well defined total function which is computable but not primitive recursive, providing a counterexample to the belief in the early 1900s that every computable function was also primitive recursive.

Page 60: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

60

Recursive Functions

0),(such that smallest )),(( yxgyyxgy

Ackermann’s function is a

Recursive Function

Page 61: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

61

Primitive recursive functions

Recursive Functions

Page 62: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

62

Primitive Recursion: Extended Example

Needs following building blocks:– constants– addition– multiplication– exponentiation– subtraction

A polynomial function:27 73),( yxyxyxf

Page 63: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

63

Addition

add(0,n) =add(m+1,n) =

nsucc(add(m,n))

Multiplication:

mult(0,n) =mult(m+1,n) =

0add(mult(m,n),n)

nmnmadd ),(

nmnmmult ),(

Page 64: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

64

Exponentiation:

exp(0,n) =exp(m+1,n) =

1mult(exp(m,n),n)

= one(n)

Subtraction

sub(0,n) =sub(m+1,n) =

0 = zero(n)succ(sub(m,n))

nmnmsub ),(

mnnm ),(exp

Page 65: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

65

Primitive Recursion: Extended Example

f = sub ◦ (add ◦ (f1,f2), f3)

f1(x,y) = mult(3,exp(7,x)) f1 = mult ◦ (three, exp ◦ (seven))

f2(x,y) = mult(x,y) f2 = mult

f3(x,y) = mult(7,exp(2,y)) f3 = mult ◦ (seven, exp ◦ (two))

f(x,y) = sub(add(f1(x,y),f2(x,y)),f3(x,y))

27 73),( yxyxyxf

Page 66: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

66

Primitive Recursion

All primitive recursive functions are total.i.e., they are defined for all values.

Primitive recursion lack some interesting functions:“True” subtraction – when using natural numbers.“True” division – undefined when divisor is 0.Trigonometric functions – undefined for some values.…

Page 67: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

67

Partial Recursive vs. Recursive

A function is partial recursive it can be defined by the previous constructions.

A function is recursive it is partial recursive and total.

Page 68: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

68

Division:div(m,n) = m n

div(m,n) = min i, sub(succ(m),add(mult(i,n),n)) = 0

div(m,n) = minimum i such thati mnin m-(n-1)in+n m+1(m+1) – (in+n) 0(m+1) (in+n) = 0

Example

Page 69: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

69

Relations Among Function Classes

Functions TMs– Define TMs in terms of the

function formers.– Straightforward, but long.

TMs Functions– Define functions where

subcomputations encode TM behavior.

– Simple encoding scheme.– Straightforward, but very

messy.

partial recursive= recognizable

recursive= decidable

primitiverecursive

Page 70: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

70

otherwise 0,

)(

even isn if,1

neven

))(,1()1(

1)0(

kevensubkeven

even

Additional Examples of Primitive Recursion

A recursive function is a function that calls itself by using its own name within its function body.

Even

Page 71: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

71

))1(),(()1(

1)0(

xxfactmultxfact

fact

Factorials

1)1(! nnn

Page 72: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

72

),(),)),(((()),((

),0(),0(

),()?(

),()(

yxisSquareyxsuccsquareequaloryxsuccisSquare

yequalyisSquare

xxisSquarexsquare

xxmultxsquare

)),(,()),(,(),),,(((),(

)0,()?(

yymultxequalysuccxhxyymultlessifyxh

xhxsquare

Is a number a square?

Forward recursion (-recursion)

Page 73: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

73

number. naturalany of square anot is 5

0))5,0(

))5,0(.........................................................................

))5,1(..........................................................................

))5,2(..........................................................................

))))5,3(),5,4(((),5,5(((

)5,4(),5,5((()5,5(

)5,5()5?(

isSquare

isSquareetc

isSquareetc

isSquareetc

isSquaresquareequalorsquareequalor

isSquaresquareequalorisSquare

isSquaresquare

Page 74: 1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 14 School of Innovation, Design and Engineering Mälardalen University 2012

74

etc

multequalhmultlessifh

hsquare

...

))0,0(,5(),1,5(),5),0,0((()0,5(

)0,5()5?(