Resources: The slides of this lecture were derived from x = 1 [Järvi], with permission of the original author, by copy & let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook. x(1).!x(1) x.set(1) Programming Language Theory The Untyped Lambda Calculus Ralf Lämmel [Järvi] Slides by J. Järvi: Programming Languages, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002
What s the lambda calculus? It is the core of functional languages.... x x.x f. g. x.f (g x) The identity function Function composition 2
What s the lambda calculus? It is the core of functional languages. It is a mathematical system for studying programming languages. design, specification, implementation, type systems, et al. It comes in variations of typing: implicit/explicit/none. Formal systems built on top of simply typed lambda calculus: System F for studying polymorphism System F<: for studying subtyping... 3
Language constructs Abstract syntax: M ::= x M M λx.m M is a lambda term. An infinite set of variables x, y, z,... is assumed. M N is an application. Function M is applied to the argument N. λx.m is an abstraction. The resulting function maps x to M. Lambda functions are anonymous. 4
Computability theory Church s thesis: All intuitively computable functions are λ-definable. An established equivalence of notions of computability: Set of Lambda-definable functions = Set of Turing-computable functions 5
Syntax and semantics Syntax! t ::=x tt x.t Terms A term that cannot be reduced further.! Evaluation v ::= x.t Values (normal forms) t 1 0 t 1 t t 0 t 1 t 2 t 0 1 t 2 vt vt 0 ( x.t) v [v/x]t Reduce function position, then reduce argument position, then apply. 6
Syntactic sugar and conventions M N1...Nk means (...((M N1) N2)... Nk). Function application groups from left to right. λx.x y means (λx.(x y)). Function application has higher precedence. λx1x2... xk. M means λx1.(λx2.(...(λxk.(m))...))). 7
Variable binding λ is a binding operator: It binds a variable in the scope of the lambda abstraction. Examples: λx.m λx.x y x is bound (in the lambda abstraction) y is not bound (in the lambda abstraction). If a variable occurs in an expression without being bound, then it is called a free occurrence, or a free variable. Other occurrences of variables are called bound. A closed term is one without free variable occurrences. 8
Variable binding precise definition FV(M) defines the set of free variables in the term M FV(x) ={x} FV(MN) =FV(M) FV(N) FV( x.m) =FV(M) \{x} 9
Exercise: what are the free and bound variable occurrences in these terms? ( x.y)( y.y) x y xyy x.( y.x y)y 10
Substitution and β-equivalence Computation for the λ-calculus is based on substitution. Substitution is defined by the equational axiom: (λx.m)n = [N/x]M!! Redex direction = β-reduction The terms on both sides are also called β-equivalent. Think of substitution as invoking a function: (λx.m) is the function, N is the argument, Substitution takes care of parameter passing. 11
α-equivalence and conversion Names of bound variable are insignificant. λx.x defines the same function as λy.y Suppose two terms differ only on the names of bound variables. Then, they are said to be α-equivalent ( =α ). Equational axiom: λx.m = λy.[y/x]m where y does not appear in M Performing such renaming is also called α-conversion. and substitution applies to free occurrences only. 12
Reduction M N Computation ( ) with the lambda calculus is then a series of β-reductions, and ( implicit ) α-conversions. t 1 0 t 1 t t 0 t 1 t 2 t 0 1 t 2 vt vt 0 ( x.t) v [v/x]t Reflexive, transitive closure M * N means M reduces to N in zero or more steps. 13
Inductive definition of substitution [N/x]x = N [N/x]y = y, y any variable di erent from x [N/x](M 1 M 2 )=([N/x]M 1 )([N/x]M 2 ) [N/x]( x.m) = x.m If this condition is not met, then alpha conversion is needed. [N/x]( y.m) = y.([n/x]m), y not free in N Examples [z/x]x [z/x]( x.x x) [z/x]( y.y x) [z/x]( z.x z) z x.x x y.y z a.z a 14
Properties of reduction (i.e., semantics) How do we select redexes for reduction steps? Does the result depend on such a choice? Does reduction ultimately terminate with a normal form? 15
Illustration of different reductions (We assume natural numbers with +.) Option 1 Option 2 (λf.λx.f (f x)) (λy.y + 1) 2 (λf.λx.f (f x)) (λy.y + 1) 2 (λx.(λy.y +1) ((λy.y +1) x)) 2 (λx.(λy.y +1) ((λy.y +1) x)) 2 (λx.(λy.y +1) (x +1)) 2 (λx.(x+1+1)) 2 (2+1+1) 4 (λy.y +1) ((λy.y +1) 2)...... 4 16
Confluence Confluence: evaluation strategy is not significant for final value. That is: there is (at most) one normal form of a given expression. [http://en.wikipedia.org/wiki/church Rosser_theorem] 17
Confluence M * N means M reduces to N in zero or more steps. Confluence If M * N and M * N, then there exists some P such that N * P and N * P. 18
Strong normalization property of a calculus with reduction Definition: For every term M there is a normal form N such that M * N. Strong normalization properties for lambda calculi: Untyped lambda calculus: no ( x.x x)( x.x x) Simply-typed lambda calculus: yes 19
Evaluation/reduction strategies Full beta reduction Choice of strategy may impact termination behavior Reduce anywhere. Applicative order ( reduce the leftmost innermost redex ) Reduce argument before applying function. Normal order ( reduce the leftmost outermost redex ) Apply function before reducing argument. eager (strict) lazy (non-strict) http://en.wikipedia.org/wiki/evaluation_strategy 20
Extension vs. encoding Typical extensions (giving rise to so-called applied lambda calculi) Primitive types (numbers, Booleans,...) Type constructors (tuples, records,...) Recursive functions Effects (cell, exceptions,...)... Many extensions can be encoded in theory in terms of pure lambda calculus, except that such encoding is somewhat tedious. 21
Church Booleans Encodings of literals true = λt.λf.t false = λt.λf.f Conditional expression (if) Expectations test b v w * v, if b = true test b v w * w, if b = false Encoding test = λl.λm.λn.l m n = λl.(λm.(λn.((l m) n))) 22
Example reduction (λl.λm.λn.l m n) true v w (λm.λn.true m n) v w (λn.true v n) w true v w (λt.λf.t) v w (λf.v)w v 23
Church pairs A Boolean value picks either the 1st or the 2nd value of the pair. Construction and projections pair = λf.λs.λb.b f s first = λp.p true second = λp.p false 24
Church numerals Encodings of numbers c0 = λs.λz.z c1 = λs.λz.s z c2 = λs.λz.s (s z) c3 = λs.λz.s (s (s z)) A numeral n is a lambda abstraction that is parameterized by a case for zero, and a case for succ. In the body, the latter is applied n times to the former. This caters for primitive recursion.... Encodings of functions on numbers succ = λn.λs.λz.s (n s z) plus = λm.λn.λs.λz.m s (n s z) times = λm.λn.m (plus n) c0... 25
Recursive functions Let us define the factorial function. Suppose we had recursive function definitions. f λn. if n == 0 then 1 else n*f(n - 1) Let us do such recursion with anonymous functions. Fixed point combinators to the rescue! 26
Fixed points Consider a function f : X X. A fixed point of f is a value z such that z = f(z). A function may have none, one, or multiple fixed points. Examples (functions and sets of fixed points): f(x) = 2x f(x) = x f(x) = x + 1 {0} {0,1,...} 27
Fixed point combinators We call Y a fixed-point combinator if it satisfies the following definitional property: For all f : X X it holds that Y f = f (Y f) 100(M)$ Question: does such a Y exist? 28
Defining factorial as a fixed point Start from a recursive definition. f λn. if n == 0 then 1 else n*f(n - 1) Eliminate self-reference; receive function as argument. g λh.λn.if n == 0 then 1 else n*h(n - 1) g takes a function (h) and returns a function. Define f as a fixed point. f Y g 29
Fixed points cont d For example, apply definitional property to factorial g: (Y g) 2 =[Y def. prop.] g (Y g) 2 =[unfold g] (λ h.λ n.if n == 0 then 1 else n*h(n - 1)) (Y g) 2 =[beta reduce] (λ n.if n == 0 then 1 else n*((y g)(n - 1))) 2 =[beta reduce] if 2 == 0 then 1 else 2*((Y g)(2-1)) =[ - reduce] if 2 == 0 then 1 else 2*((Y g)(1)) =[ if reduce] 2*((Y g)(1)) =... = 2 This is as if we had extended evaluation. Apply these steps one more time. 30
A lambda term for Y One option: Y = λf.(λx.f (x x))(λx.f (x x)) Verification of the definitional property: Y g = g (Y g) Proof: Y g =[unfold Y] (λf.(λx.f (x x))(λx.f (x x))) g =[beta reduce] (λx.g(x x))(λx.g(x x))) =[beta reduce] g ((λx.g(x x)) (λx.g(x x))) =[fold Y] g (Y g) Not suitable for applicative order. 31
CBV fixed point combinator lambda(f,apply( lambda(x,apply(var(f),lambda(y,apply(apply(var(x),var(x)),var(y))))), lambda(x,apply(var(f),lambda(y,apply(apply(var(x),var(x)),var(y))))))) 32
Summary: The untyped lambda calculus A concise core of functional programming. A foundation of computability. A Prolog model is straightforward. Prepping: Types and Programming Languages Chapter 5 Outlook: The simply-typed lambda calculus Polymorphism 33
Resources: The slides of this lecture were derived from x = 1 [Järvi], with permission of the original author, by copy & let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook. x(1).!x(1) x.set(1) Programming Language Theory The Simply Typed Lambda Calculus Ralf Lämmel [Järvi] Slides by J. Järvi: Programming Languages, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002
Towards typed lambda calculus Now suppose you want to distinguish values of different types: Booleans Numbers Functions on Booleans Functions on functions on Booleans Products, sums of...... These types need to be... specified in the program, and checked to be correct. 35
Setting up the simply typed lambda calculus Define syntax for types including function types. Extend lambda abstractions for explicit types. Define typing rules. Revise reduction semantics. There is also a type system for lambda terms without explicit types, which we do not discuss in this Establish type safety. Consider extensions. 36
Revised lambda abstraction Lambda abstractions are annotated with types: λx : T.t Grammar of types: T ::= nat bool We only consider these simple types here for simplicity. T T 37
Examples What are the types of these terms? λx : bool.x λf : bool bool.f x Note that lambda variables are typed explicitly. Here are the same terms for the untyped calculus: λx.x λf.f x 38
Meaningless terms Some terms diverge.... unless we end up showing strong normalization. Some applications are ill-typed, e.g.: (λf : bool bool.f x) true Goal: a type system to reject ill-typed terms. 39
Typing relation with context t : T read as: Term t has type T in the typing context A typing context is a sequence of bindings. Each binding is a variable-type pair, e.g.: x : T. Contexts are composed as in Γ, x : T. All variable names are distinct for a given Γ. Γ can be omitted if it is empty. Γ can be empty for closed terms. 40
Typing rules for simply-typed lambda calculus x, y, z, f, g range over variables s, t, u range over terms S, T, U range over types T-Variable x : T x : T T-Abstraction, x : T u : U x : T.u : T U T-Application t : U T u : U tu: T 41
Rules for bool T-True true : bool T-False false : bool These typing rules illustrate one option to add specific types and their operations to a basic lambda calculus. Basically, we need to add one rule per operation. 42
Evaluation rules Syntax (terms, values, types)! t ::=x v tt v ::= x :T.t true false T ::=bool T T Evaluation rules!! t 1 t 1 t t t 1 t 2 t 1 t 2 vt vt ( x :T.t) v [v/x]t Evaluation rules do not bother with types. 43
Typing derivations Construct derivations as proofs of terms having a certain type. f : bool bool 2 f : bool bool f : bool bool ` f : bool bool f : bool bool ` false : bool g : bool 2 g : bool f : bool bool ` f false : bool g : bool ` g : bool ` ( f : bool bool.f false) :(bool bool) bool ` g : bool.g : bool bool ` ( f : bool bool.f false) g : bool.g : bool T-Variable x : T x : T T-Abstraction, x : T u : U x : T.u : T U T-Application t : U T u : U tu: T 44
Type safety = progress + preservation Progress: If t is a closed, well-typed term, then either t is a value, or there exists some u, such that t u. Preservation: If t : T and t u, then u : T Requires several trivial lemmas (properties) that are omitted here. 45
A few extensions Recursion (fixed point combinator) Type annotation (for documentation, abstraction) Pairs (as a simple form of type construction) Lists (another example of type construction) Records (as a first step towards objects) Mutable variables (not discussed in this lecture)... 46
Recursion A fixed point combinator is definable in the untyped calculus. It is not definable in the simply typed version. A special combinator fix is added to the formal system. Alternatively, a more powerful type system is needed. 47
Why is Y not typeable? Consider again the fixed-point combinator: Y = λf.(λx.f (x x))(λx.f (x x)) Let us simplify. Y contains self-application: λx. x x How could self-application be typeable? λx :?. x x The type of x would need to be obviously a function type and the argument type of the same type. 48
Illustration of fix iseven : nat bool iseven = fix iseven! iseven is a generator for the iseven function. Given a function f that equates with iseven for numbers up to n, iseven f equates with iseven an approximation up to n + 2. fix iseven extends this to all n. iseven : (nat bool) nat bool iseven = λ f:nat bool. λ x:nat. if iszero x then true else if iszero (pred x) then false else f (pred (pred x)) 49
Intuition We think of unfolding the recursive definition. fix iseven 0 true fix iseven 1 false fix iseven 2 true fix iseven 3 false fix iseven 4 true iseven? 0 true iseven? 1 false iseven? 2? iseven (iseven?) 2 true iseven (iseven?) 3 false iseven (iseven?) 4? 50
Recursion in the presence of types! Add a primitive fix. t ::=... fix t Typing rule t : T T fix t : T Evaluation rules fix ( x : T.t) t fix t t fix t [(fix ( x : T.t))/x] t 51
Type annotation (ascription) Syntax: t ::=... t as T Typing rule:! t : T t as T : T Evaluation rules: t t as T v as T u u as T v 52
Summary: The typed lambda calculus Typing relation carries argument for context. Recursion requires built-in Y combinator. Prepping: Types and Programming Languages Chapters 7 and 11 Outlook: Lambda calculi with polymorphism Object calculi Process calculi 53
Resources: The slides of this lecture were derived from x = 1 [Järvi], with permission of the original author, by copy & let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook. x(1).!x(1) x.set(1) Programming Language Theory System F Ralf Lämmel [Järvi] Slides by J. Järvi: Programming Languages, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002
Polymorphism -- Why? What s the identity function? In the simply typed lambda calculus, we need many! Examples λx:bool. x λx:nat. x In need of polymorphic functions, i.e., functions that accept many types of arguments. λx:bool bool. x λx:bool nat. x... 55
Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 56
System F -- Syntax tax Type variable t ::=x v tt t[t ] v ::= x :T.t X.t T ::=X T T X.T System F [Girard72,Reynolds74] = (simply-typed) lambda calculus + type abstraction & application Example: Type application Type abstraction Polymorphic type id : X.X X id =ΛX.λx :X.x 57
Examples Term id =ΛX.λx :X.x id[bool] id[bool] true id true Type : X.X X : bool bool : bool type error In actual programming languages, type application may be implicit. 58
System F -- Typing rules Type variables are held the context. T-Variable x : T x : T T-Abstraction, x : T u : U x : T.u : T U T-Application t : U T u : U tu: T Type variables are subject to alpha conversion. T-TypeAbstraction, X t : T X.t : X.T T-TypeApplication t : X.T t[t 1 ]:[T 1 /X ]T 59 Example: id : X.X X id =ΛX.λx :X.x
System F -- Evaluation rules E-AppFun t 1 t 1 t 1 t 2 t 1 t 2 E-TypeApp E-AppArg t t vt E-AppAbs ( x :T.t) v [v/x]t vt Example: id : X.X X id =ΛX.λx :X.x E-TypeApp t 1 t 1 t 1 [T ] t 1 [T ] E-TypeAppAbs ( X.t)[T ] [T /X ]t 60
The doubling function double: X.(X X) X X double=λx.λf :X X.λx :X.f (f x) Instantiated with nat double_nat = double [nat] : (nat nat) nat nat Instantiated with nat nat double_nat_arrow_nat = double [nat nat] : ((nat nat) nat nat) (nat nat) nat nat Invoking double double [nat] (λx : nat.succ (succ x)) 5 * 9 61
Functions on polymorphic functions Consider the polymorphic identity function: id : X. X X id = ΛX.λx : X.x Use id to construct a pair of Boolean and String: pairid : (Bool, String) pairid = (id true, id true ) Abstract over id: pairapply : ( X. X X) (Bool, String) pairapply = λf : X. X X. (f true, f true ) Type application left implicit. Argument must be polymorphic! 62
Self application Not typeable in the simply-typed lambda calculus λx :?. x x Typeable in System F selfapp : ( X.X X) ( X.X X) selfapp = λx : X.X X.x [ X.X X] x 63
The fix operator (Y) Not typeable in the simply-typed lambda calculus Extension required Typeable in System F. t : T T fix t : T fix : X.(X X) X Encodeable in System F with recursive types. fix =? See [TAPL] 64
Meaning of all types In the type X...., we quantify over all types. Predicative polymorphism X ranges over simple types. Polymorphic types are type schemes. Type inference is decidable. Impredicative polymorphism X also ranges over polymorphic types. Type inference is undecidable. type:type polymorphism X ranges over all types, including itself. Computations on types are expressible. Type checking is undecidable. Not covered by this lecture Generality is used for selfapp. 65
End of Lambda slides 66