0% found this document useful (0 votes)
55 views

Recursion and Fixed-Point Theory: Programming Language Principles

This document discusses recursion and fixed-point theory in programming languages. It shows how recursion can be modeled using fixed points and the Y-combinator. Specifically: 1) Recursion is modeled as finding a fixed point of a function F, using an operator Y such that f = Y F. 2) This satisfies the fixed-point identity F(YF) = YF. 3) For a recursive function like factorial, YF represents the recursive call being replaced by the fixed point value.

Uploaded by

Mrunal Ruikar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Recursion and Fixed-Point Theory: Programming Language Principles

This document discusses recursion and fixed-point theory in programming languages. It shows how recursion can be modeled using fixed points and the Y-combinator. Specifically: 1) Recursion is modeled as finding a fixed point of a function F, using an operator Y such that f = Y F. 2) This satisfies the fixed-point identity F(YF) = YF. 3) For a recursive function like factorial, YF represents the recursive call being replaced by the fixed point value.

Uploaded by

Mrunal Ruikar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Recursion and Fixed-Point Theory

Programming Language Principles Lecture 14

Prepared by

Manuel E. Bermdez, Ph.D.


Associate Professor University of Florida

Recursion and Fixed-Point Theory


We know that the meaning of let x=P in Q is the value of (x.Q)P So, what's the meaning of (the de-sugarized version of) let rec f n = P in Q ?

Consider Factorial:
let rec f n = n eq 0 1 | n*f(n-1) in f 3 Without the 'rec', we'd have ( f.f 3) [ n.n eq 0 1 | n*f(n-1) ] Note: the last 'f' is free. The 'rec' makes the last 'f' bound to the [ n. ...] expression.

With 'rec', somehow


( f.f 3) [ n.n eq 0 1 | n*f(n-1) ] => ( n.n eq 0 1 | n*f(n-1)) 3 => 3 eq 0 1 | 3*f(3-1) => 3*f(2) =>magic 3*( n.n eq 0 1 | n*f(n-1)) 2 => 3*(2 eq 0 1 | 2*f(2-1)) => 3*2*f(1) =>magic 3*2*( n.n eq 0 1 | n*f(n-1)) 1 => 3*2*(1 eq 0 1 | 1*f(1-1)) => 3*2*1*f(0) =>magic 3*2*1*( n.n eq 0 1 | n*f(n-1)) 0 => 3*2*1*(0 eq 0 1 | 0*f(0-1)) => 3*2*1*1 => 6

To dispel the magic, need some math


Let F be a function. A value w is called a "fixed-point" of F if F w = w. Example: f = x.x ** 2 - 6 has two fixed points, 3 and -2, because ( x.x ** 2 - 6) 3 => 3 ** 2 - 6 => 3, and ( x.x ** 2 - 6) -2 => -2 ** 2 - 6 => -2 Only two fixed-points: solutions to x**2-x-6 = 0.

Fixed-points can be functions


T = f. ().(f nil)**2-6 has two fixed points, ().3 and ().-2, because ( f. ().(f nil)**2-6) ( ().3) => ().(( ().3) nil)**2-6 => ().3**2-6 => ().3 and ( f. ().(f nil)**2-6) ( ().-2) => ().(( ().-2) nil)**2-6 => ().-2**2-6 => ().-2

Let's Dispel the Magic


=> <= <= let let let let rec rec rec rec f n = P in Q f = n.P in Q (fcn_form) f = ( f. n.P) f in Q (abstraction) f = F f in Q where F = f. n.P

(abstraction). Now there are no free occurrences of f in F, and only one free occurrence of f overall.

Dispelling magic, (contd)


But, let rec f = F f in Q means we want f to be a fixed point of F ! So, re-phrase it as let f = A_fixed_point_of F in Q. The "A-fixed_point_of" operator is called a "fixed point finder". We call it "Y, or Ystar, or Y*.

Dispelling magic, (contd)


So, we have let f = Y F in Q => ( f.Q) (Y F) = ( f.Q) (Y ( f. n.P)) Note: No free occurrences of f ! So, explaining the purpose of "rec" is the same as finding a fixed point of F.

How do we find a suitable Y ?


WE DON'T NEED TO ! We only need to use some of its characteristics: 1. f = Y F 2. F f = f Substituting 1) in 2), we obtain F (Y F) = Y F This is the fixed point identity.

Let's extend some earlier definitions:


Definition: Axiom (rho): Y F => F (Y F). (Extended) Definition: An applicative expression M is not in normal form if it is of the form Y N. (Extended) Definition: A reduction sequence is in normal order if at every step we reduce the left-most Y or .

Let's Do the Factorial


This time, no magic. Just science. F = f. n.n eq 0 1 | n*f(n-1), so ( f.f 3) (Y F) YF3 F (Y F) 3 ( f. n.n eq 0 1 | n*f(n-1)) (Y F) 3 ( n.n eq 0 1 | n* Y F (n-1)) 3 3* Y F (2) 3* F (Y F) 2

=> => = =>


=> ,

=>

Let's Do the Factorial (contd)


= => => = => => = => 3* ( f. n.n eq 0 1 | n*f(n-1)) (Y F) 2 3* ( n.n eq 0 1 | n* Y F (n-1)) 2 3*2* F (Y F) 1 3*2* ( f. n.n eq 0 1 | n*f(n-1)) (Y F) 1 3*2* ( n.n eq 0 1 | n* Y F (n-1)) 1 3*2*1* F (Y F) 0 3*2*1* ( f. n.n eq 0 1 | n*f(n-1)) (Y F) 0 3*2*1* ( n.n eq 0 1 | n* Y F (n-1)) 0

=>, 3*2* Y F (1)

=>, 3*2*1* Y F (0)

=>, 3*2*1*1

Note:
Normal order is required. In PL order the evaluation would not terminate. Lemma: YF is in fact the factorial function. Proof (by induction, see notes).

Recursion via lambda calculus.


The following value of Y will work: Y = F.( f.f f) ( g. F (g g)) Lemma: Y satisfies the fixed-point identity. Proof: see notes.

Recursion in the CSE Machine


Alternatives: 1. Use Y = F.( f.f f) ( g.F(g g)). Pretty tedious. Won't work, because it requires normal order. 2. Delay evaluation of arguments in Y above, (as in Cond, earlier). Could use Z = F.( f.f f) ( h.F( x.h h x)). EXTREMELY tedious. 3. Our choice: a variation of the fixedpoint identity.

Subtree transformation for rec


Build AST for factorial, and standardize it. RPAL subtree transformation rule for rec. (remember we skipped it ?) Example: factorial (see diagram) rec => | = /\ X E = /\ X gamma / \ Ystar lambda / \ X E

Control Structures and CSE Machine Evaluation


See diagram. CSE Rule 12: Applying Y to F Results in in (Y F). We represent it using a single symbol : CSE Rule 13: Applying Y to F. Results in F (Y F)

Recursion and Fixed-Point Theory


Programming Language Principles Lecture 14

Prepared by

Manuel E. Bermdez, Ph.D.


Associate Professor University of Florida

You might also like