Other Recursive Definitions Concrete Syntax Recursive Definitions ::= if then end if f(n) = if n=0 then 1 else f(n–1) ::= | ; g(n) = if n=0 then 1 else g(n+1) or Lists of Numbers define f = &n . (if (zerop n) 1 (f (sub n 1))) List = {nil} ! (N x List) where nil represents the empty list define g = &n . (if (zerop n) 1 (g (succ n))) Model for Pure A satisfies a recursive definition iff V = set of variables it is a solution to an equation: D = V ! (D " D) f = &n . (if (zerop n) 1 (f (sub n 1))) g = &n . (if (zerop n) 1 (g (succ n))) Problem with Cardinality |D"D| ! |D| < |P(D)| ! |D"D| Similar to solving a mathematical equation: Modeling Nontermination x = x2 – 4x + 6.

Chapter 10 1 Chapter 10 2

Domains Definitions b%S is a lower bound of a subset A of S Sets with a -like structure. if b$x for all x%A. u S is an upper bound of a subset A of S Each domain contains a bottom element # % that is “less than” all other elements. if x$u for all x%A. A least upper bound of A, lub A, is For domains of functions, bottom represents an upper bound of A that is less than a computation that fails to complete normally. or equal every upper bound of A.

Example: Divides relation on { 1,2,4,5,8,10,20 } Partial Order $ on a Set S Hasse diagram A relation that is lub { 2, 5 } = 8 20 • reflexive lub { 2, 4, 5, 10 } = • transitive 4 10 lub { 1, 2, 4 } = • antisymmetric 2 5 lub { 8, 10 } =

lub { 20 } = 1

Chapter 10 3 Chapter 10 4 An ascending chain in a Elementary Domains S is a sequence of elements { x1, x2, x3, x4, … } with the property Natural numbers and Boolean values with a discrete partial order: x1 $ x2 $ x3 $ x4 $ …. for x,y%S, x $ y iff x = y or x = #. A (cpo) on a set S is a partial order $ with the two properties Elementary domains correspond to “answers”, the results produced by programs. a) There is an element #%S with # $ x for all x%S. true false b) Every ascending chain in S has a least upper bound in S. !

On domains, $ is thought of as approximates 0 1 2 3 4 … or is less defined than or equal to.

Any finite set with a partial order and ! a bottom element # is a cpo. Why? Proper and Improper values.

Also called flat domains.

Chapter 10 5 Chapter 10 6

Product Domains Example

If A with ordering $A and B with ordering $B Level = {# ,undergraduate,graduate,nondegree} are complete partial orders, the product L domain of A and B is AxB with the ordering and $AxB where Gender = {#G, female, male} AxB = { | a%A and b%B}, and $AxB iff a $A c and b $ B d. Gender x Level Thm: $AxB is a partial order on AxB. Proof: Exercise

Thm: $AxB is a complete partial order on AxB. Proof: #AxB = <#A,#B> acts as bottom for AxB, since #A $A a and #B $ B b for a%A and b%B. If $ $ $ … is an ascending chain in AxB, then a1 $A a2 $A a3 $A … is a chain in A with least upper bound, lub {ai|i"1}%A, and Imagine two processes to determine level and b1 $B b2 $B b3 $B … is a chain in B with gender of a student. least upper bound, lub {bi|i"1}%B. Therefore, %AxB is the least upper bound for original chain. !

Chapter 10 7 Chapter 10 8 Projection Functions Sum Domains

If A with ordering $A and B with ordering $B first : AxB"A are complete partial orders, the sum domain defined by first = a for any AxB % of A and B is A+B with the ordering $A+B and where second : AxB"B A+B = { | a%A}!{ | b%B}!{#A+B}, defined by second = b for any $A+B if a $A c, %AxB $A+B if b $B d,

#A+B $A+B for each a%A, Generalize to arbitrary product domains: #A+B $A+B for each b%B, and D1 x D2 x … x Dn #A+B $A+B #A+B.

Application: Calculator Semantics Thm: $A+B is a complete partial order on A+B. evaluate [[=]] (a,op,d,m) = (a, nop, op(a,d), m) Proof: #A+B $ x for any x%A+B by definition. is a more readable translation of An ascending chain x1 $ x2 $ x3 $ … in A+B may repeat #A+B forever or eventually climb into evaluate [[=]] st = either Ax{1} or Bx{2}. In first case, least upper (first(st), nop, bound will be #A+B, and in the other two cases second(st)(first(st),third(st)), fourth(st)). the least upper bound will exist in A or B. !

Chapter 10 9 Chapter 10 10

Functions on Sum Domains 3. Inspection (testing): Recall T = {true, false, #T}. Let S = A+B. isA : S"T is defined for s%S as isA s iff there exists a%A with s=. 1. Injection (creation): isB : S is defined for s S as inS : A S is defined for a A "T % " % isB s iff there exists b%B with s=. as inS a = %S In both cases, #S is mapped to #T. inS : B"S is defined for b%B as inS b = %S Signature Diagram 2. Projection (selection): outA outA : S"A is defined for s%S as A outA s = a A if s=, and % isA outA s = # %A if s= or s=# . inS A S S = A + B T inS isB outB : S"B is defined for s%S as outB s = b%B if s=, and B outB outB s = #B%B if s= or s=#S.

Observe that inS is an overloaded function.

Chapter 10 11 Chapter 10 12 Storable Values for Wren SV = Integer + Boolean = {| n%Integer}!{| b%Boolean}!{#SV} 0 1 … -1 2 … true false or SV = int(Integer) + bool(Boolean) = {int(n)| n%Integer}!{bool(b)| b%Boolean}!{#SV}

!Integer !Boolean Injection function inSV : Integer " SV where inSV n = int(n).

!SV The tags (constructors), int and bool, take the place of the overloaded injection function, inSV. Functions for SV int : Integer " SV inSV : Integer " SV bool : Boolean " SV inSV : Boolean " SV outInteger : SV " Integer Projection function isInteger : SV " T outInteger : SV " Integer where outInteger int(n) = n outBoolean : SV " Boolean outInteger bool(b) = # isBoolean : SV " T outInteger #SV = #.

Chapter 10 13 Chapter 10 14

Inspection handled by pattern matching Functions on D* execute [[if E then C]] sto = if p then execute [[C]] sto else sto Injection: inD* : Dk D* where bool(p) = evaluate [[E]] sto, " k k stands for Projection: outD : D*"D execute [[if E then C]] sto = if isBoolean(val) Functions on Lists: then if outBoolean(val) Let L%D* and e%D. then execute [[C]] sto Then L= for d%Dk for some k"0 else sto 0 else # where D = { nil }. where val = evaluate [[E]] sto 1. head : D*"D where head (L) = first (outDk(L)) if k>0, and Generalizations head () = #.

Finite sums: D1 + D2 + D3 + … + Dn Infinite sums: D + D + D + … = { | d%D } 1 2 3 i 2. tail : D*"D* where Domain of finite Sequences: tail (L) = inD*(<2nd (outDk(L)), k D* = { nil } + D + D2 + D3 + D4 + … 3rd (outD (L)), …, k where nil represents the empty sequence. kth (outD (L))>) if k>0, and tail () = #.

Chapter 10 15 Chapter 10 16 3. null : D*"T where Sets of Functions null () = true, and null (L) = false if L = with k>0. A function from a set A to a set B is total if f(x)%B is defined for every x%A. Therefore, null (L) = isD0(L)

If A with ordering $A and B with ordering $B * * are complete partial orders, define Fun(A,B) to 4. prefix : DxD "D where be the set of all total functions from A to B. prefix (e,L) = inD*() For f,g%Fun(A,B), f $ g if f(x) $B g(x) for all x%A. 5. affix : D*xD"D* where affix (L,e) = inD*(<1st (outDk(L)), 2nd (outDk(L)), …, kth (outDk(L)),e>) Lemma: $ is a partial order on Fun(A,B). Proof: Follow the definition to show reflexive, transitive, and antisymmetric. Each of these five functions map bottom to bottom. See text for complete proof. The binary functions prefix and affix produce # if either argument is bottom.

Chapter 10 17 Chapter 10 18

Restrictions on Fun(A,B) Thm: $ is a complete partial order on Fun(A,B). Proof: Define bottom for Fun(A,B) as the #(x) = #B for all x%A. A function f in Fun(A,B) is monotonic if Since (x) = f(x) for all x A # #B $B % x $A y implies f(x) $B f(y) for all x,y%A. and f%Fun(A,B), # $ f for all f%Fun(A,B). If $ means “approximates”, then when y has at Let f1 $ f2 $ f3 $ … be an ascending chain least as much information as x, it follows that in Fun(A,B). f(y) has at least as much information as f(x).

Then for any x%A, f1(x) $B f2(x) $B f3(x) $B … is a chain in B, which has a least upper bound, Continous yx%B. Note that yx is lub {fi(x) |i"1}. A function f%Fun(A,B) is continuous if it Define the function F(x) = yx for each x%A. preserves least upper bounds; that is, if X = x1 F serves as a least upper bound for the original $A x2 $A x3 $A … is an ascending chain in A, then f(lubAX) = lubB{f(x) | x%X}. chain. Set lub {fi|i"1} = F. ! Also written f(lubA{xi}) = lubB{f(xi)} Fun(A,B) contains some strange functions. or f(lubA{xi | i"1}) = lubB{f(xi) | i"1}. Consider F % Fun(N"N,N"N) defined by No surprises when taking the least upper F g = &n . if g(n)=# then 0 else 1, bounds (limits) of approximations for g%N"N

Chapter 10 19 Chapter 10 20 Lemma: If f%Fun(A,B) is continuous, then it is Lemma: The relation $ restricted to A"B is a monotonic. partial order. Proof: Suppose f is continuous and x $A y. Proof: The properties reflexive, transitive, and Then x $A y $A y $A y $A … is an ascending antisymmetric are inherited by a subset. ! chain in A, and since f is continuous, f(x) lub {f(x),f(y)} = f(lub {x,y}) = f(y). ! $B B A Lub Lemma: If x1 $ x2 $ x3 $ … is an ascending chain in a cpo A, and xi $ d%A for Function Domains each i"1, then lub {xi|i"1} $ d. Proof: By the definition of least upper bound, Define A"B to be the set of functions in if d is a bound for the chain, the least upper Fun(A,B) that are (monotonic and) continuous. bound, lub {x |i"1}, must be no larger than d. ! This set is ordered by the relation $ from i Fun(A,B). Limit Lemma: If x1 $ x2 $ x3 $ … and F % Fun(N"N,N"N) defined by y1 $ y2 $ y3 $ … are ascending chains in cpo F g = &n . if g(n)=# then 0 else 1, A, and xi $ yi for each i"1, for g%N"N then lub {xi|i"1} $ lub {yi|i"1}. is not monotonic. Proof: For each i"1, x $ y $ lub {y |i"1}. Proof by Counterexample: i i i Therefore lub {xi|i"1} $ lub {yi|i"1} by the Lub Let g = n . and g = n . 0. Then g g . 1 & # 2 & 1 $ 2 lemma (take d = lub {yi|i"1}). ! But F(g1) = &n . 0, F(g2) = &n . 1, and functions &n . 0 and &n . 1 are not related at all by $.

Chapter 10 21 Chapter 10 22

Thm: The relation $ on A"B is a complete Part 2: Let f1 $ f2 $ f3 $ … be an ascending partial order. chain in A"B, and let F = lub {fi|i"1} be its least Proof: Since $ is a partial order on A"B, two upper bound (in Fun(A,B)). Remember the de- properties need to be verified: finition of F, F(x) = lub {fi(x)|i"1} for each x%A. We need to show that F is monotonic and 1. The bottom element in Fun(A,B) is also in continuous so that we know F is a member of A"B; that is, the function #(x) = #B is A"B. monotonic and continuous. 2. For any ascending chain in A"B, its least Monotonic: upper bound, which is an element of If x $A y, fi(x) $B fi(y) $B lub {fi(y)|i"1} for Fun(A,B), is also in A"B, namely it is any i since each fi is monotonic. monotonic and continuous. Therefore, F(y) = lub {fi(y)|i"1} is an upper bound for each fi(x), and so the least upper Part 1: If x $A y for some x,y%A, bound of all the fi(x) satisfies then #(x) = #B = #(y), which means #(x) $B F(x) = lub {fi(x)|i"1} $ F(y) (Lub lemma), #(y), and so # is a monotonic function. and F is monotonic. If x1 $A x2 $A x3 $A … is an ascending chain in A, then its image under the function will be # Continuous: Let x1 $A x2 $A x3 $A … be an the ascending chain #B $B #B $B #B $B …, ascending chain in A. We need to show that whose least upper bound is . Therefore, #B F(lub {xj|j"1}) = lub {F(xj)|j"1} where #(lub {x |i"1}) = # = lub {#(x )|i"1}, and # is a A i B B i F(x) = lub {f (x)|i"1} for each x%A. . i

Chapter 10 23 Chapter 10 24 Note that “i” is used to index the ascending First Half chain of functions from A"B while “j” is used to index the ascending chains of elements in lub {lub {fi(xj)|j"1}|i"1} $ lub {lub {fi(xj)|i"1}|j"1} A and B. So F is continuous if For all k and j, fk(xj) $ lub {fi(xj)|i"1} by the F(lub {xj|j"1}) = lub {F(xj)|j"1}. definition of F (the rows of Figure 10.9). We have chains Recall these definitions and properties: fk(x1) $ fk(x2) $ fk(x3) $ … for each k 1. Each fi is continuous: and lub {f (x )|i"1} lub {f (x )|i"1} fi(lub {xj|j"1}) = lub {fi(xj)|j"1} i 1 $ i 2 lub {f (x )|i"1} … for each chain {xj|j"1} in A. $ i 3 $ 2. Definition of F: So for each k, lub {f (x )|j"1} lub {lub {f (x )|i"1}|j"1} F(x) = lub {fi(x)|i"1} for each x%A. k j $ i j by the Limit lemma. So This corresponds to the top row (remember F(lubj{xj|j"1}) = lub {fi(lub {xj|j"1})|i"1} by 2 each fk is continuous).

= lub {lub {fi(xj)|j"1}|i"1} by 1 Hence lub {lub {fk(xj)|j"1}|k"1} $ lub {lub {fi(xj)|i"1}|j"1} = lub {lub {fi(xj)|i"1}|j"1} ‡ by the Lub lemma. Now change k to i. = lub {F(xj)|j"1} by 2. Look at Figure 10.9.

Chapter 10 25 Chapter 10 26

Second Half Example 10 lub {lub {f (x )|i"1}|j"1} lub {lub {f (x )|j"1}|i"1} i j $ i j Student = { #, Autry, Bates }

For all i and k, Level = fi(xk) $ fi(lub {xj|j"1}) = lub {fi(xj)|j"1} by using { #, undergraduate, graduate, nondegree } the fact that each fi is monotonic and continuous (the columns of Figure 10.9). Fun(Student,Level) contains 64 (43) elements. We have chains Only 19 of these functions are monotonic and f1(xk) $ f2(xk) $ f3(xk) $ … for each k continuous.

and lub {f1(xj)|j"1} $ lub {f2(xj)|j"1} Which of these functions are monotonic? $ lub {f (x )|j"1} $ … 3 j f = { # |"#, Autry |"nondegree, Bates |"# } So for each k, lub {fi(xk)|i"1} $ lub {lub {fi(xj)|j"1}|i"1} by the Limit lemma. This corresponds to the rightmost column. g = { # |"grad, Autry |"grad, Bates |"# } Hence lub {lub {fi(xk)|i"1}|k"1} $ lub {lub {fi(xj)|j"1}|i"1} by the Lub lemma. Now change k to j. h = { # |"grad, Autry |"grad, Bates |"grad } Therefore F is continuous. !

Chapter 10 27 Chapter 10 28 Continuity of Functions on Domains Thm: If A and B are cpos, A is a finite set, and f%Fun(A,B) is monotonic, f is also continuous. Thm: These functions on domains and their Proof: Let x1 $A x2 $A x3 $A … be an analogs are continuous: ascending chain in A. 1. first : AxB"A

Since A is finite, for some k, xk = xk+1 = 2. inS : A"S where S = A+B xk+2 = …. 3. outA : A+B"A So the chain is a finite set, {x1, x2, x3, …, xk}, 4. isA : A+B"T whose least upper bound is xk. Proof: Since f is monotonic, 2. Let a a a … be an ascending f(x ) f(x ) f(x ) … f(x ) 1 $ 2 $ 3 $ 1 $B 2 $B 3 $B $B k chain in domain A. = f(x ) = f(x ) = … k+1 k+2 Observe that 1 $ 1 $ 1 $ 1 $ … is an is an ascending chain in B, which is also a finite ascending chain in N. set, namely {f(x1), f(x2), f(x3), …, f(xk)} Then $ $ $ … is with f(xk) as its least upper bound. an ascending chain in S. Therefore, f(lub {xi|i"1}) = f(xk) = lub {f(xi)|i"1}, So inS(lub {ai|i"1}) = and f is continuous. ! = = lub { | i"1} = lub {inS(ai) | i"1}.

Chapter 10 29 Chapter 10 30

3. An ascending chain s1 $ s2 $ s3 $ … in Case 3: For some k"1, si = for all i"k S = A + B may repeat #A+B forever or eventually climb into either Ax{1} or Bx{2}. where bi%B. In the first case, the least upper bound Then outA(lub{si|i"1}) = outA() will be #A+B, = #A and in the other two cases the lub will be and lub{outA(si)|i"1} = lub{#A|i"1} = #A. ! some a%A or some b%B. Thm: The composition of continuous functions Case 1: si = #A+B for all i"1. is continuous. Then outA(lubS{si|i"1}) = outA(#S) = #A, Proof: Suppose f : A " B and g : B " C are and lubA{outA(si)|i"1} = lubA{#A|i"1} = #A. continuous functions. Let x1 $ x2 $ x3 $ … be an ascending chain Case 2: in A. For some k"1, si = for all i"k Then f(x1) $ f(x2) $ f(x3) $ … is an ascending where ai%A. chain in B with f(lub{xi|i"1}) = lub{f(xi)|i"1} by Then outA(lub {si|i"1}) the continuity of f. = outA() Since g is continuous, = lub {ai|i"k} g(f(x1)) $ g(f(x2)) $ g(f(x3)) $ … is an and lub {outA(si)|i"1} = lub {ai|i"k}. ascending chain in C with g(lub{f(xi)|i"1}) = lub{g(f(xi))|i"1}.

Therefore g(f(lub{xi|i"1})) = g(lub{f(xi)|i"1}) = lub{g(f(xi))|i"1} and g ° f is continuous. !

Chapter 10 31 Chapter 10 32 Fixed Point Semantics Add an undefined element to the , C+ = {#,0,1,2}, and for symmetry, do likewise with the domain, D+ = {#,0,1,2}. Goal: Provide meaning for recursive definitions. Define the natural extension of f by having First Step: Transform partial functions into #D map to #C under f: total functions. f+ = {<#,#>,<0,#>,<1,2>,<2,1>}. Example f is a function with domain D = {0,1,2} and Define a relationship that orders functions and codomain C = {0,1,2} defined by: domains according to how “defined” they are, f(n) = 2/n putting a lattice-like structure on the elementary or domains: f = {<1,2>,<2,1>}. For x,y%D+, x$y if x=# or x=y. Note that f(0) is undefined; therefore f is a . Now extend f to make it a total function: This relation is read “x approximates y” or “x is less defined or equal to y”. f = {<1,2>,<2,1>,<0,?>}.

Chapter 10 33 Chapter 10 34

Thm: Let f+ be a natural extension of a function The natural extension of a function whose between two sets D and C so that f+ is a total domain is a Cartesian product, namely f : D +xD +x…xD +"C+, has the property that function from D+ to C+. 1 2 n f+(x ,x ,…,x ) = # whenever at least one x =#. Then f+ is monotonic and continuous. 1 2 n C i Proof: Let x $ x $ x $ … be an ascending Any function that satisfies this property 1 2 3 is known as a strict function. chain in the domain D+ = D!{#}. Two possibilities for the behavior of the chain: Thm: If f+: D +xD +x…xD + C+ is a natural Case 1: xi = #D for all i"1. 1 2 n " + + Then lub{xi|i"1} = #D, and extension where Di , 1!i!n, and C are + + + f (lub{xi|i"1}) = f (#D) elementary domains, then f is monotonic and + continuous. = #C = lub{#C} = lub{f (xi)|i"1}. Proof: Consider the case where n=2. Show f+ is Case 2: xi = #D for 1!i!k and xk+1 = xk+2 = xk+3 continuous. = …, since once the terms move above bottom, Let $ $ $ … be an the sequence is constant in a flat domain. + + + + ascending chain in D1 xD2 . Since D1 and D2 Then lub{xi|i"1} = xk+1, and are elementary domains, the chains {xi|i"1} and + + f (lub{xi|i"1}) = f (xk+1) {yi|i"1} must follow one of the two cases in the = lub{ ,f+(x )} = lub{f+(x )|i"1}. previous proof, namely all # or eventually a #C k+1 i + constant proper value in Di . If f+ is continuous, it is also monotonic. !

Chapter 10 35 Chapter 10 36 Case 1: lub{x |i"1} = # + or lub{y |i"1} = # + i D1 i D2 Example (or both). + Consider the natural extension of the Then f (lub{|i"1}) + conditional expression operation: = f () = #C+ because f+ is a natural extension and one of its (if a b c) = if a then b else c. arguments is #, and The natural extension unduly restricts the + meaning of the conditional expression. lub{f ()|i"1} = lub{#C+|i"1} = #C+. For example, we prefer that the following expression return 0 when x=1 and y=0: Case 2: lub{xi|i"1} = x%D1 if y>0 then x/y else 0. and lub{yi|i"1} = y%D2 + + Since D1 and D2 are both elementary domains, If we interpret the undefined operation 1/0 as #, there is an integer k when x=1 and y=0, such that xi = x and yi = y for all i"k. (if+ y>0 x/y 0) = (if+ false # 0) = # + So f (lub{|i"1}) + for a natural extension. = f () = f+()%C+ + + and lub{f ()|i"1} = lub{#C+,f ()} = f+(). !

Chapter 10 37 Chapter 10 38

A function, f : N"N, satisfies the original Second Step: Give meaning to recursive definition (†) if and only if it is a fixed point of definitions. the definition of F, (‡), F f n = f(n) for all n%N or just F f = f. Consider a recursively defined function f : N"N where N = {#,0,1,2,3,…} and Once more: f(n) = (†) Suppose f : D"C is a function defined if n=0 then 5 else if n=1 then f(n+2) else f(n-2) recursively by f(x) = '(x,f) for each x%D Two questions: where '(x,f) is some expression in x and f. 1. What function, if any, does this equation in Furthermore, let F : (D"C)"(D"C) be the f denote? functional defined by F f x = '(x,f). 2. Does it specify more than one function? Then F f = f if and only if F f x = f x for all x%D if and only if '(x,f) = f x for all x%D Define a functional F by which is the same as f(x) = '(x,f) for all x%D. F : (N"N)"(N"N) where Using lambda calculus notation: (F(f))(n) = (‡) F f = &n . if n=0 then 5 else if n=1 then f(n+2) if n=0 then 5 else if n=1 then f(n+2) else f(n-2) else f(n-2) Function application associates to the left; or omit the parentheses with multiple applications, F = &f . &n . if n=0 then 5 else if n=1 then f(n+2) writing F f n for (F(f))(n). else f(n-2)

Chapter 10 39 Chapter 10 40 Fixed Points in Special Fixed Point

Function Fixed Points Of the possible fixed points of a functional, g(n) = n2 – 6n 0 and 7 chose the one that is “least defined” according g(n) = n all n%N to $. g(n) = n + 5 none 1. Any fixed point of F embodies the g(n) = 2 2 information that can be deduced from F. 2. The includes no more Back to Functional F information than what must be deduced. The function g = &n . 5 is a fixed point of F: F g = &n . if n=0 then 5 else if n=1 then g(n+2) Define the meaning of a recursive definition of else g(n-2) a function to be the “least” fixed point, with = &n . if n=0 then 5 else if n=1 then 5 else 5 respect to $, of the corresponding functional F. = &n . 5 = g.

Problem Does a least fixed point always exist? g = &n . 5 does not agree with the operational behavior of the original recursive definition. Notation: Define fk for each k"0 inductively: f(1) = f(3) = f(1) = … does not produce a value, 0 whereas g(1) = 5. f (x) = x is the identity function and fn+1(x) = f(fn(x)) for n"0.

Chapter 10 41 Chapter 10 42

0 Thm: If D with $ is a complete partial order and Note that g (#) = # has no effect on the least i g : D"D (g is any monotonic and continuous upper bound of {g (#)|i"0}. function on D), then g has a least fixed point with respect to $ on D"D. Let v%D be another fixed point for g. 0 Proof: Since D is a cpo, g (#) = # $ g(#). Then # $ v and g(#) $ g(v) = v, the basis step for induction. Since g is monotonic, g(#) $ g(g(#)) = g2(#). i In general, since g is monotonic, Suppose g (#) $ v. gi(#) $ gi+1(#) implies Then since g is monotonic, gi+1(#) = g(gi(#)) $ g(gi+1(#)) = gi+2(#). gi+1(#) = g(gi(#)) $ g(v) = v, the induction step. So by induction, Therefore, by mathematical induction, gi(#) $ v # $ g(#) $ g2(#) $ g3(#) $ g4(#) $ … for all i"0. is an ascending chain in D, which must have a So v is an upper bound for {gi( ) | i"0}. least upper bound u = lub{gi( ) | i"0} D. # # % Hence u $ v, since u is the least upper bound But g(u) = g(lub{gi(#) | i"0}) for {gi(#) | i"0}. ! = lub{g(gi(#)) | i"0} since g is continuous = lub{gi+1(#) | i"0} Corollary: Every continuous functional = lub{gi(#) | i>0} = u F : (A"B)"(A"B), where A and B are That is, u is a fixed point for g. domains, has a least fixed point, Ffp : A"B, which can be taken as the meaning of the (recursive) definition corresponding to F.

Chapter 10 43 Chapter 10 44 0 Example g0(n) = G # n = #(n) = # for n%N, the everywhere undefined function. Consider the functional G: (N"N)"(N"N) where g1(n) = G # n = G g0 n G g n = if n=0 then 1 = if n=0 then 1 else if n=1 then g0(3)-12 else if n=1 then g(3)-12 else 4n+g0(n-2) else 4n+g(n-2) (‡) = if n=0 then 1 else if n=1 then #(3)-12 that corresponds to the recursive definition else 4n+#(n-2) g(n) = if n=0 then 1 = if n=0 then 1 else # else if n=1 then g(3)-12 else 4n+g(n-2) (†) 2 g2(n) = G # n = G g1 n Contemplate the ascending sequence = if n=0 then 1 else if n=1 then g1(3)-12 # $ G(#) $ G2(#) $ G3(#) $ G4(#) $ … else 4n+g1(n-2) = if n=0 then 1 else if n=1 then #-12 and its least upper bound. else 4n+(if n-2=0 then 1 else #) k Use the abbreviation gk = (G #) for k"0: = if n=0 then 1 else if n=1 then # 0 else (if n=2 then 4n+1 else ) g0(n) = G # n = #(n) # = if n=0 then 1 else if n=1 then g1(n) = G # n = G g0 n # else if n=2 then 9 else g2(n) = G (G #) n = G g1 n # 3 g3(n) = G # n = G g2 n Now calculate a few terms in the ascending chain g0 $ g1 $ g2 $ g3 $ ….

Chapter 10 45 Chapter 10 46

4 g4(n) = G # n = G g3 n Note Property = if n=0 then 1 else if n=1 then g3(3)-12 a + (if b then c else d) = else 4n+g3(n-2) = if b then a+c else a+d = if n=0 then 1 else if n=1 then #-12 else 4n+(if n-2=0 then 1 3 else if n-2=1 then # g3(n) = G # n = G g2 n else if n-2=2 then 9 = if n=0 then 1 else if n=1 then g2(3)-12 else if n-2=3 then # else 4n+g2(n-2) else if n-2=4 then 25 else #) = if n=0 then 1 else if n=1 then #-12 = if n=0 then 1 else if n=1 then else 4n+(if n-2=0 then 1 # else if n-2=1 then else (if n=2 then 4n+1 # else if n=3 then 4n+# else if n-2=2 then 9 else #) else if n=4 then 4n+9 = if n=0 then 1 else if n=1 then # else if n=5 then 4n+# else (if n=2 then 4n+1 else if n=6 then 4n+25 else if n=3 then 4n+# else 4n+#) else if n=4 then 4n+9 = if n=0 then 1 else 4n+#) else if n=1 then # = if n=0 then 1 else if n=2 then 9 else if n=1 then # else if n=3 then # else if n=2 then 9 else if n=4 then 25 else if n=3 then # else if n=5 then # else if n=4 then 25 else # else if n=6 then 49 else #

Chapter 10 47 Chapter 10 48 A pattern seems to be developing. = if n=0 then 1 else if n=1 then #-12 else 4n+(if n-2<2i Lemma: For all i"0, then (if even(n–2) then (n-1)2 gi(n) = else #) if n<2i then (if even(n) then (n+1)2 else #) else #) else #. = if n=0 then 1 Proof: The proof proceeds by induction on i. else if n=1 then # else (if n<2i+2 a) By the previous computations, for i = 0, then (if even(n–2) then 4n+(n-1)2 g0(n) = # = else 4n+#) else 4n+#) if n<2•0 then = if n=0 then 1 (if even(n) then (n+1)2 else #) else # else if n=1 then # else if n<2(i+1) 2 b) As the induction hypothesis, assume that then (if even(n) then (n+1) gi(n) = else #) else # if n<2i then = if n<2(i+1) 2 (if even(n) then (n+1)2 else #) else #, then (if even(n) then (n+1) else #) for any arbitrary i"0. else # Therefore our pattern for the gi is correct. ! Then gi+1(n) = G gi n = if n=0 then 1 else if n=1 then gi(3)-12 else 4n+gi(n-2)

Chapter 10 49 Chapter 10 50

fix The least upper bound of the ascending chain g0 $ g1 $ g2 $ g3 $ …, where The procedure for computing a least fixed point for a functional can be described as an 2 gi(n) = if n<2i then (if even(n) then (n+1) else #) operator on functions F : D"D: else #, must be defined (not #) for any n where some fix : (D"D)"D where i gi is defined, and must take the value (n+1)2 fix F = lub{F (#)|i"0} % D. there. The least fixed point of the functional Hence the least upper bound is F = &f . &n . if n=0 then 5 else if n=1 then f(n+2) Gfp(n) = (lub{gi | i"0}) n else f(n-2) can then be expressed as = (lub{Gi # | i"0}) n Ffp = fix F, an element of D = N"N. = if even(n) then (n+1)2 else # for all n%N, and this function can be taken as the meaning For F : (N"N)"(N"N), fix has type of the original recursive definition. fix : ((N"N)"(N"N))"(N"N). The fixed point operator fix provides a fixed Note that the function h n = (n+1)2 is also a point for any continuous functional, namely, fixed point for G. the least defined function with this fixed point property. It is more defined than Gfp.

In fact, Gfp $ h. Fixed Point Identity: F(fix F) = fix F.

Chapter 10 51 Chapter 10 52 Continuous Functionals (if true then b else c) = b for any b,c%D (if false then b else c) = c for any b,c%D Lemma: A constant function f : D"C, where (if # then b else c) = # for any b,c%D f(x) = k for some fixed k%C and for all x%D, is D continuous given either of the two extensions:

a) The natural extension where f(#D) = #C. Lemma: The uncurried “if” function as defined above is continuous. b) The “unnatural” extension where f(#D) = k. Proof: Consider three cases. Lemma: An identity function f : D"D, where f(x) = x for all x in a domain D, is continuous. Lemma: The composition of continuous functions is continuous, namely if f : Proof: If x1 $ x2 $ x3 $ … is an ascending chain in D, it follows that C1xC2x…xCn"C is continuous and gi : Di"Ci is continuous for each i, 1!i!n, then f f(lub{xi|i"1}) = lub{xi|i"1} = lub{f(xi)|i"1}. ! ° : D1xD2x…xDn"C, defined by Conditional Expression Function: f ° (g1,g2,…,gn) = f Natural extension of “if” is too restrictive. is also continuous. Lazy if Proof: Exercise. if(a,b,c) = if a then b else c. where if : TxDxD"D for some domain D and T = { #, true, false }

Chapter 10 53 Chapter 10 54

Composition Involving a Parameter Part 1: lub{F(fi)|i"1} $ F(lub{fi|i"1}). For each i"1, fi $ lub{fi|i"1}. F : (N"N)"(N"N) where Since F1 is monotonic, F1(fi) $ F1(lub{fi|i"1}), which means that F f n = n + if n=0 then 0 else f(f(n-1)). F1 fi d $ F1 lub{fi|i"1} d for each d%D. Since fi is monotonic, fi(F1 fi d) $ fi(F1 lub{fi|i"1} d). Lemma: If F1, F2, …, Fn are continuous But F fi d = fi < F1 fi d> and n n functionals, say Fi : (D "D)"(D "D) for each fi $ lub{fi|i"1} . i, 1!i!n, the functional F : (Dn"D)"(Dn"D) Therefore, F fi d $ lub{fi|i"1} for defined by F f d = f each i"1 and d%D. for all f%Dn"D and d%Dn is also continuous. So, lub{F(fi)|i"1} d = lub{F fi d|i"1} $ lub{fi|i"1} = F lub{fi|i"1} d for d%D. Proof: Consider the case where n=1. So F1 : (D"D)"(D"D), Part 2: F(lub{fi|i"1}) $ lub{F(fi)|i"1}. F : (D"D)"(D"D), and F f d = f . For any d%D, F lub{fi|i"1} d Let f1 $ f2 $ f3 $ … be a chain in D"D. = lub{fi|i"1} by defn of F, The proof shows that lub{F fi|i"1} = F(lub{fi|i"1}) = lub{f |i"1}(lub{F (f )|j"1} d) since F is cont, in two parts. i 1 j 1 = lub{lubi{fi|i"1} <{F1(fj)|j"1} d>|i"1} since lub{fi|i"1} is continuous = lub{lub{fi({F1(fj)|j"1} d)}|i"1} by definition of lub{fi|i"1}. †

Chapter 10 55 Chapter 10 56 If j!i, fj $ fi, Theorem: Any functional H defined by the F f $ F f since F is monotonic, composition of naturally extended functions on 1 j 1 i 1 elementary domains, constant functions, the F1 fj d $ F1 fi d for each d%D, and identity function, the if-then-else conditional fi $ fi since fi is monotonic. expression, and a function variable f, is If i $ fj for each d%D Proof: The proof follows by induction on the by the meaning of $. structure of the definition of the functional. The So, f lub{f (F f d)|n"1} basis is handled by the continuity of natural i 1 j $ n 1 n extensions, constant functions, and the identity for each i,j"1. function. The induction step relies on the But lubn{fn(F1 fn d)} = lubn{F fn d|i"1} lemmas which state that the composition of = lubn{F(fn)|i"1} d continuous functions, possibly involving f, is by the definition of F. continuous. So fi $ lub{F(fn)|n"1} d for each i,j"1, and lub{fi |i"1} $ lub{F(fn)|n"1}d for each j"1. Look at Example 14: Hence H : (N"N)"(N"N) where lub{lub{fi |i"1}|j"1} $ lub{F(fn)|i"1} d. H h n = n + if n=0 then 0 else h(h(n-1)) Combining with † gives F(lub{fi|i"1}) d $ lubn{F(fn)|i"1} d. = if n=0 then n else n+h(h(n-1)).

Chapter 10 57 Chapter 10 58

Fixed Points for Nonrecursive Functions Second Interpretation: Find the least fixed point for the function Think of h(n) = n3 - 3n as a rule defining a h(n) = n3 - 3n defined on the integers Z. “recursive” function that just has no actual recursive call of h. First Interpretation: The corresponding functional + The natural extension h of h is a continuous H : (Z"Z)"(Z"Z) is defined by the rule: function on the elementary domain Z!{#}. H h n = n3 - 3n. Then the least fixed point of h+ may be constructed as the least upper bound of the A function h satisfies definition h(n) = n3 - 3n if ascending sequence: and only if it is a fixed point of H, that is H h = h. + + + + + + # $ h (#) $ h (h (#)) $ h (h (h (#))) $ … The fixed point construction: But h+( ) = , # # 0 and so (h+)k(#) = h+((h+)k-1(#)) = h+(#) = # H # n = #(n) = # for any k"1. H1 # n = n3 - 3n + k 2 3 Therefore, lub {(h ) (#)|k"0} = lub {#|k"0} = # is H # n = n - 3n the least fixed point. H3 # n = n3 - 3n In fact, h+ has four fixed points in Z!{#}: : Hk # n = n3 - 3n h+(0) = 0 -2 0 2 : h+(2) = 2 Therefore, the least fixed point is lub{Hk( )|k"0} = n . n3 - 3n, which follows the + # & h (-2) = -2 ! same definition rule as the original function h. h+(#) = #

Chapter 10 59 Chapter 10 60 Revisiting We gain insight into both the while command and fixed point semantics by constructing a The recursive definition few terms in the ascending chain whose least execute [[while E do C]] sto = upper bound is fix W, if evaluate [[E]] sto = bool(true) 0 1 2 3 then execute [[while E do C]] (execute [[C]] sto) W # $ W # $ W # $ W # $ … i else sto where fix W = lub {W # | i"0}. violates the principle of compositionality. The fixed point construction for W: The function execute [[while E do C]] satisfies the recursive definition above if and only if it is W0 # s = # a fixed point of the functional W1 # s = W (W0 #) s W f s = if evaluate [[E]] s = bool(true) = if evaluate [[E]] s = bool(true) then f(execute [[C]] s) else s then #(execute [[C]] s) else s = if evaluate [[E]] s = bool(true) = if evaluate [[E]] s = bool(true) then (f °execute [[C]]) s else s. then # else s We obtain a nonrecursive and compositional definition of the meaning of a while command Let exC stand for the function execute [[C]]. by means of execute [[while E do C]] = fix W.

Chapter 10 61 Chapter 10 62

Then = if evaluate [[E]] s = bool(true) 2 1 then (if evaluate [[E]] (exC s) = bool(true) W # s = W (W #) s then (if evaluate [[E]] (exC2 s) = bool(true) = if evaluate [[E]] s = bool(true) 2 1 then # else (exC s)) then W # (exC s) else s else (exC s)) = if evaluate [[E]] s = bool(true) else s then (if evaluate [[E]] (exC s) = bool(true) then # else exC s) W4 # s = else s if evaluate [[E]] s = bool(true) then (if evaluate [[E]] (exC s) = bool(true) 2 W3 s = W (W2 ) s then (if evaluate [[E]] (exC s) = bool(true) # # 3 = if evaluate [[E]] s = bool(true) then (if evaluate [[E]] (exC s) = bool(true) 3 then W2 # (exC s) else s then # else (exC s)) = if evaluate [[E]] s = bool(true) else (exC2 s)) then (if evaluate [[E]] (exC s) = bool(true) else (exC s)) then else s (if evaluate [[E]] (exC (exC s)) = bool(true) then # else exC (exC s)) else (exC s)) else s

Chapter 10 63 Chapter 10 64 In general, The least upper bound of this ascending Wk+1 # s = W (Wk #) s sequence provides semantics for the while = if evaluate [[E]] s = bool(true) command: then (if evaluate [[E]] (exC s) = bool(true) execute [[while E do C]] = fix W = lub{Wi #|I"0}. then (if evaluate [[E]] (exC2 s) = bool(true) then (if evaluate [[E]] (exC3 s) = bool(true) : View the definition of execute [[while E do C]] then # else (exCk s)) in terms of the fixed point identity, else (exCk-1 s)) W(fix W) = fix W, where : W f s = if evaluate [[E]] s = bool(true) else (exC2 s)) then f(execute [[C]] s) else s. else (exC s)) In this context, else s execute [[while E do C]] = fix W The function Wk+1 # allows the body C of the Now define loop = fix W. Then while to execute up to k times. execute [[while E do C]] Thus this approximation to the meaning of the = loop while command can handle any instance of a where loop s = (W loop) s while with at most k iterations of the body. = loop where loop s = Any application of a while command will have if evaluate [[E]] s = bool(true) some finite number of iterations, say n. then loop(execute [[C]] s) else s. Therefore its meaning is subsumed in the approximation Wn+1 #. This approach produces the compositional definition of execute [[while E do C]] used in the specification of Wren, Figure 9.11.

Chapter 10 65 Chapter 10 66

Fixed Point Induction Theorem: Any finite conjunction of inequalities of the form '(F) $ )(F), where ' and ) are con- Induction on the construction of the least fixed tinuous functionals, is an admissible predicate. i point lub {F # | i"0}. This includes terms of the form '(F) = )(F). Let ((f) be a predicate that describes a property Proof: See [Manna72]. ! for an arbitrary function f defined recursively. To show ( holds for the least fixed point Ffp of Mathematical induction is used to verify the the functional F corresponding to a recursive condition in Part 1: definition of f, two conditions are needed: Given a functional F : (D"D)"(D"D) for some Part 1: Show by induction that ( holds for each domain D and admissible predicate ((f), show: element in the ascending chain a) ( ) holds where : D D, and # $ F # $ F2 # $ F3 # $ … and ( # # " i i+1 Part 2: Show that ( remains true when the b) for any i"0, if ((F (#)), then ((F (#)). least upper bound is taken. An alternate version of condition b) is: b') for any f : D"D, if ((f), then ((F(f)). Part 2 is handled by defining a class of predi- cates with the necessary property. Either formulation is sufficient to infer that the predicate ( holds for every function in the A predicate is called admissible if it has the ascending chain {Fi # | i"0}. property that whenever the predicate hold for an ascending chain of functions, it also must hold for the least upper bound of that chain.

Chapter 10 67 Chapter 10 68 Example Paradoxical Combinator An implementation of the fixed-point operator H h n = if n=0 then 0 else (2n-1)+h(n-1) with fix in the (untyped) lambda calculus: least fixed point Hfp. define = 2 Y &f . (&x . f (x x)) (&x . f (x x)) Prove that Hfp $ &n . n . or in the lambda calculus evaluator Let (f) be the predicate f n . n2. ( $ & define Y = (L f ((L x (f (x x))) (L x (f (x x))))). 2 a) Since # $ &n . n , ((#) holds. Reduction proves Y satisfies fixed-point identity. 2 b') Suppose ((h), that is, h $ &n . n . Y E = (&f . (&x . f (x x)) (&x . f (x x))) E Then H h n= if n=0 then 0 else (2n-1)+h(n-1) * (&x . E (x x)) (&x . E (x x)) $ if n=0 then 0 else (2n-1)+(n-1)2 * E ((&x . E (x x)) (&x . E (x x))) = if n=0 then 0 else n2 * E (&h . (&x . h (x x)) (&x . h (x x)) E) = n2 for n"0. = E (Y E). Therefore, ((H(h)) holds, and by fixed point Calculation follows normal order reduction. 2 induction Hfp $ &n . n . ! Applicative order strategy leads to a nonterminating reduction: Y E = (&f . (&x . f (x x)) (&x . f (x x))) E * (&f . f ((&x . f (x x)) (&x . f (x x)))) E * (&f . f (f ((&x . f (x x)) (&x . f (x x))))) E * …

Chapter 10 69 Chapter 10 70

Fixed-Point Identity The replication of the function encoded in the fix operator enables a reduction to create as many copies of the original function as it F(fix F) = fix F needs.

Add a reduction rule that carries out effect of (fix Two) 4 fixed-point identity from right to left to replicate the functional F—namely, fix F * F(fix F). * (Two (fix Two)) 4

Consider this definition of a function involving * (&h . &n . if n=0 then 1 powers of 2 with its associated functional: else 2•h(n-1)+1) (fix Two) 4 two n = if n=0 then 1 else 2•two(n-1)+1 * (&n . if n=0 then 1 and else 2•(fix Two)(n-1)+1) 4 Two = &h . &n . if n=0 then 1 else 2•h(n-1)+1. * if 4=0 then 1 else 2•(fix Two)(4-1)+1

The least fixed point of Two, (fix Two), serves * 2•((fix Two) 3)+1 as the definition of the two function. The function (fix Two) is not recursive and can * 2•((Two (fix Two)) 3)+1 be “reduced” using the fixed-point identity * 2•((&h . &n . if n=0 then 1 fix Two * Two (fix Two). else 2•h(n-1)+1) (fix Two) 3)+1

Chapter 10 71 Chapter 10 72 * 2•(2•(2•((&h . &n . if n=0 then 1 * 2•((&n . if n=0 then 1 else 2•h(n-1)+1) (fix Two) 1)+1)+1)+1 else 2•(fix Two)(n-1)+1) 3)+1 * 2•(2•(2•((&n . if n=0 then 1 * 2•((if 3=0 then 1 else 2•(fix Two)(3-1))+1)+1 else 2•(fix Two)(n-1)+1) 1)+1)+1)+1

* 2•(2•((fix Two) 2)+1)+1 * 2•(2•(2•(if 1=0 then 1 else 2•((fix Two)(1-1))+1)+1)+1)+1 * 2•(2•((Two (fix Two)) 2)+1)+1 * 2•(2•(2•( 2•((fix Two) 0)+1)+1)+1)+1 * 2•(2•((&h . &n . if n=0 then 1 else 2•h(n-1)+1) (fix Two) 2)+1)+1 * 2•(2•(2•( 2•((Two (fix Two)) 0)+1)+1)+1)+1

* 2•(2•((&n . if n=0 then 1 * 2•(2•(2•(2•((&h . &n . if n=0 then 1 else 2•(fix Two)(n-1)+1) 2)+1)+1 else 2•h(n-1)+1) (fix Two) 0)+1)+1)+1)+1

* 2•(2•(if 2=0 then 1 * 2•(2•(2•(2•((&n . if n=0 then 1 else 2•((fix Two) (2-1))+1)+1)+1 else 2•((fix Two) (n-1))+1) 0)+1)+1)+1)+1

* 2•(2•( 2•((fix Two) 1))+1)+1)+1 * 2•(2•(2•(2•(if 0=0 then 1 else 2•((fix Two) (0-1))+1)+1)+1)+1)+1 * 2•(2•( 2•((Two (fix Two)) 1)+1)+1)+1 * 2•(2•(2•(2•1+1)+1)+1)+1 = 31

Chapter 10 73 Chapter 10 74