Title: Recursion and Fixed-Point Theory
1Recursion and Fixed-Point Theory
Programming Language Principles Lecture 14
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida
2Recursion and Fixed-Point Theory
- We know that the meaning of
- let xP 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 ?
3Consider Factorial
- let rec f n n eq 0 ? 1 nf(n-1) in f 3
- Without the 'rec', we'd have
- (? f.f 3) ? n.n eq 0 ? 1 nf(n-1)
- Note the last 'f' is free.
- The 'rec' makes the last 'f' bound to the ? n.
... expression.
4With 'rec', somehow
- (? f.f 3) ? n.n eq 0 ? 1
nf(n-1) - gt? (? n.n eq 0 ? 1 nf(n-1)) 3
- gt? 3 eq 0 ? 1 3f(3-1)
- gt? 3f(2)
- gtmagic 3(? n.n eq 0 ? 1 nf(n-1)) 2
- gt? 3(2 eq 0 ? 1 2f(2-1))
- gt? 32f(1)
- gtmagic 32(? n.n eq 0 ? 1 nf(n-1)) 1
- gt? 32(1 eq 0 ? 1 1f(1-1))
- gt? 321f(0)
- gtmagic 321(? n.n eq 0 ? 1 nf(n-1)) 0
- gt? 321(0 eq 0 ? 1 0f(0-1))
- gt? 3211
- gt? 6
5To 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 gt? 3 2 - 6 gt? 3, and
- (? x.x 2 - 6) -2 gt? -2 2 - 6 gt? -2
- Only two fixed-points
- solutions to x2-x-6 0.
6Fixed-points can be functions
- T ? f. ? ().(f nil)2-6 has two fixed
points, - ? ().3 and ? ().-2, because
- (? f. ? ().(f nil)2-6) (? ().3)
- gt? ? ().((? ().3) nil)2-6
- gt? ? ().32-6
- gt? ? ().3
- and
- (? f. ? ().(f nil)2-6) (? ().-2)
- gt? ? ().((? ().-2) nil)2-6
- gt? ? ().-22-6
- gt? ? ().-2
7Let's Dispel the Magic
- let rec f n P in Q
- gt let rec f ? n.P in Q (fcn_form)
- lt let rec f (? f. ? n.P) f in Q
(abstraction) - lt let rec 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.
8Dispelling 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.
9Dispelling magic, (contd)
- So, we have
- let f Y F in Q
- gt (? 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.
10How do we find a suitable Y ?
- WE DON'T NEED TO !
- We only need to use some of its characteristics
- f Y F
- F f f
- Substituting 1) in 2), we obtain
- F (Y F) Y F
- This is the fixed point identity.
11Let's extend some earlier definitions
- Definition
- Axiom ? (rho) Y F gt? 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 ?.
12Let's Do the Factorial
?
- This time, no magic. Just science.
- F ? f. ? n.n eq 0 ? 1 nf(n-1), so
- (? f.f 3) (Y F)
- gt? Y F 3
- gt? F (Y F) 3
- (? f. ? n.n eq 0 ? 1 nf(n-1)) (Y F) 3
- gt? (? n.n eq 0 ? 1 n Y F (n-1)) 3
- gt? ,? 3 Y F (2)
- gt? 3 F (Y F) 2
13Let's Do the Factorial (contd)
- 3 (? f. ? n.n eq 0 ? 1 nf(n-1)) (Y
F) 2 - gt? 3 (? n.n eq 0 ? 1 n Y F (n-1)) 2
- gt?,? 32 Y F (1)
- gt? 32 F (Y F) 1
- 32 (? f. ? n.n eq 0 ? 1 nf(n-1)) (Y
F) 1 - gt? 32 (? n.n eq 0 ? 1 n Y F (n-1)) 1
- gt?,? 321 Y F (0)
- gt? 321 F (Y F) 0
- 321 (? f. ? n.n eq 0 ? 1 nf(n-1))
(Y F) 0 - gt? 321 (? n.n eq 0 ? 1 n Y F (n-1)) 0
- gt?,? 3211
- gt? 6
14Note
- 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).
15Recursion 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.
16Recursion in the CSE Machine
- Alternatives
- Use Y ? F.(? f.f f) (? g.F(g g)).
- Pretty tedious. Won't work, because it requires
normal order. - 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.
- Our choice a variation of the fixed-point
identity.
17Subtree 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 gt
- / \
- X gamma
- / \ / \
- X E Ystar lambda
- / \
- X E
18Control 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)
19(No Transcript)
20(No Transcript)
21(No Transcript)
22(No Transcript)
23(No Transcript)
24(No Transcript)
25(No Transcript)
26Recursion and Fixed-Point Theory
Programming Language Principles Lecture 14
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida