160 likes | 322 Views
CAS810: WEEK 8. LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: exercises. LAMBDA CALCULUS. it is a notation for describing the behaviour of ALL COMPUTABLE FUNCTIONS. LAMBDA CALCULUS functions are the denotations in our semantic definition. What do THEY mean!?.
E N D
CAS810: WEEK 8 LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: exercises
LAMBDA CALCULUS • it is a notation for describing the behaviour of ALL COMPUTABLE FUNCTIONS. • LAMBDA CALCULUS functions are the denotations in our semantic definition. What do THEY mean!?
The Problem with Recursive Functions - what do they mean? Consider the meaning of the while loop (leaving aside the error conditions): Cmeans[[while B do C]] = f where f = ls.(Emeans[[B]]s -> f(Cmeans[[C]]s), s)
The Problem with SETS - Russell’s Paradox is related to recursive fns: • We can define SETS of things which are sets e.g. X = { {1,2}, {2,4} } is a set of sets. • We can define sets implicitly via properties e.g X ={x : x is a set containing 3 elements} • We can define sets of things that contain themselves: e.g. X = {x : x is a set of sets} X contains itself…! • Define Z = { x : x does not contain itself} does Z contain itself!!
Recursive Functions - The Fixpoint Equation Given: f = ls.(Emeans[[B]]s => f(Cmeans[[C]]s), s) Then: H = lg.ls.(Emeans[[B]]s => g(Cmeans[[C]]s), s) Hf = (lg.ls.(Emeans[[B]]s => g(Cmeans[[C]]s), s)) f = ls.(Emeans[[B]]s => f(Cmeans[[C]]s), s) = f SO WE HAVE Hf = f - the fixpoint equation!!
Recursive Functions - another example - the Factorial Function f = ln.(n=0 => 1, n*f(n-1)) Consider: H = lg.ln.(n=0 => 1, n*g(n-1)) H f = (lg.ln.(n=0 => 1, n*g(n-1))) f = ln.(n=0 => 1, n*f(n-1)) = f by the definition above.
Solution to the Fixed point Equation - background • Every iterative algorithm can be stated recursively • Every recursive algorithm is equivalent to a function which is the solution of the fixed point equation H f = f where H is constructed as shown above. • An approximation f’ of a function f is defined as If f’ s is defined then f’ s = f s Eg f’ 0 = 0, f’ 1 = 1, f’ n = undefined for n>1 Is an approximation of f n = n*n
BIG THEOREM (fixed point theorem): The static solution of the fixed point equation is: f = Hn(^), as n tends to infinity H ^, H H ^, HH H ^, are improving approximations of f Example: For the factorial: H = lg.ln.(n=0 => 1, n*g(n-1)) H ^ = ln.(n=0 => 1, ^) Graph(H ^) = (0,1), (1, ^), (2, ^), (3, ^), (4, ^)… H H ^ = lg.ln.(n=0 => 1, n*g(n-1)) (ln.(n=0 => 1, ^)) Graph(H H ^) = (0,1), (1, 1), (2, ^), (3, ^), (4, ^)…
l-CALCULUS: operational semantics An operational semantics gives us an abstract but precise way to execute functions (programs). A l-expression is in NORMAL FORM if it is a l-l-abstraction - i.e. It cannot be reduced. Operational Semantics: Repeatedly apply the conversion rules to an “application” until it is in normal form.
l-CALCULUS: order of application Two main ones: • Left-most innermost (call by value) ..basically reduce arguments of a function before reducing the function • Left-most outermost (call by name) (also called normal order reduction) ..basically reduce the outer-most function without reducing its arguments
l-CALCULUS: operational semantics Problem: the ORDER of application sometimes makes a difference! E.g. Try (lx. ly.y) ( (lv.vv)(lz.zz) )
l-CALCULUS: Church-Rosser Theorem (Paraphrase) If a l-Calculus Application can be reduced to a normal form then -- that normal form is UNIQUE up to naming -- the normal form can be reached using normal-order reduction Corollary: We now have a nice operational semantics for l-calculus and hence pure functional programming
LAMBDA CALCULUS - Fixed Point Semantics We have seen that all recursive functions f can be given a meaning as the fixed point of the functional H. Wouldn’t it be nice if the operational and fixed point semantics coincided?
LAMBDA CALCULUS - Fixed Point Semantics But consider the following function: f = lx. ly.( x=y => y+1, f x (f (x-1) (y-1)) ) In this case H has many fixed points !! E.g. lu. lv. u+1 lu. lv. (u=v => u+1, ^) are fixed points of H.
LAMBDA CALCULUS - Fixed Point Semantics Definition: Hn(^), as n tends to infinity, gives us the “LEAST DEFINED” fixed point of H. BIG THEOREM: The least defined fixed point of fIS OPERATIONALLY THE SAME AS f NB All above is paraphrased in that I have extracted all the maths/domain theory out to give you the gist.
Exercises (1) If f is the factorial function, and H f = f, derive H3(^) and H4(^). Hence derive graph(H3(^) ),graph(H4(^) ) and verify that they are improving approximations to f. (2) EXAM 2001: Derive part of the graph of f, by reducing the expressions (f 0 0), (f 1 1), (f 2 0), where f = lx. lv.(x=v => v+1, f x (f (x-1) (v+1)) ) Show, informally, that (f 0 1) and (f 1 0) do not terminate. Prove that ly. lz z+1 is a solution to the fixed point equation H f = f where H is H = l.f lx.lv.(x=v => v+1, f x (f (x-1) (v+1)) ) Show that ly. lz z+1 is not the least fixed point of H. (3) If H is the general function in the Cmeans of the while loop, derive H1(^) and H2(^) and their graphs. Are they approximations of the while-loop?