Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 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 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
Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 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 3
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... 4
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. 5
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 6
Syntax and semantics Syntax t ::=x tt x.t Terms A term that cannot be reduced further. v ::= x.t Values (normal forms) Evaluation 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 Aka Call by value : Reduce function position, then reduce argument position, then apply. 7
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))...))). 8
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. An open term is one with free variable occurrences. A closed term is one without free variable occurrences. 9 preferred
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} 10
Exercise: what are the free and bound variable occurrences in these terms? ( x.y)( y.y) x y xyy x.( y.x y)y 11
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 [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 If this condition is not met, then a fresh variable is needed. x.x x y.y z a.z a 12
α-equivalence and conversion Names of bound variable are insignificant. λx.x defines the same function as λy.y Performing such renaming is also called α-conversion. Suppose two terms differ only on the names of bound variables. Then, they are said to be α-equivalent ( =α ). 13
Computation based on the lambda calculus Computation ( ) with the lambda calculus is then a series of Substitutions (so-called β-reductions), and ( Implicit ) α-conversions. Reflexive, transitive closure 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 M * N means M reduces to N in zero or more steps. 14
Extension / encoding of the untyped Lambda calculus 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 terms of the pure lambda calculus maybe such encoding is somewhat tedious. 15
Church Booleans Encodings of literals true = λt.λf.t false = λt.λf.f Conditional expression (if) Expectations ifthenelse b v w * v, if b = true ifthenelse b v w * w, if b = false Encoding ifthenelse = λb.λv.λw.b v w 16
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 17
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)) Church numerals... 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... 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. 18
Encoding of recursive functions in the untyped Lambda calculus Let us define the factorial function. Suppose we had recursive function definitions. f λn. if n == 0 then 1 else n*f(n - 1) But we don t have recursive functions! 19
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 20
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? 21
A lambda term for Y One option: Y = λf.(λx.f (x x))(λx.f (x x)) Verification of the definitional property: Proof: =[unfold Y] =[beta reduce] =[beta reduce] Y g = g (Y g) Y g =[fold Y] g (Y g) (λf.(λx.f (x x))(λx.f (x x))) g (λx.g(x x))(λx.g(x x))) g ((λx.g(x x)) (λx.g(x x))) Not suitable for applicative order. 22
Summary: The untyped lambda calculus A concise core of functional programming. A foundation of computability. Textbook support: Types and Programming Languages Chapter 5 Outlook: The simply typed lambda calculus Polymorphism 23
Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 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. 25
The simply typed lambda calculus Define syntax for types including function types. Extend lambda abstractions for explicit types. Define typing rules. Revise reduction semantics. Establish type safety. 26
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 27
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 28
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. 29
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. 30
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 31
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. 32
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. 33
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 34
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. 35
A few extensions of the simply typed Lambda calculus 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 here)... 36
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. See this and other extension on the slides with extra material towards the end of the slide deck! 37
Summary: The typed lambda calculus Typing relation carries argument for context. Recursion requires built-in Y combinator. Textbook support: Types and Programming Languages Chapters 7 and 11 Outlook: Lambda calculi with polymorphism Object calculi Process calculi 38
Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 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 Polymorphic Lambda Calculus (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 λx:bool bool. x λx:bool nat. x... In need of polymorphic functions, i.e., functions that accept many types of arguments. 40
Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes / OO) Ad-hoc polymorphism ( some types ) We focus on parametric polymorphism. Existential types ( exists as opposed to for all ) 41
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 Type application Type abstraction Polymorphic type Example: id : X.X X id =ΛX.λx :X.x 42
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. 43
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 44 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 45
The doubling function double: X.(X X) X X Instantiated with nat double=λx.λf :X X.λx :X.f (f x) 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 46
Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 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 Extra material on the 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
Properties of reduction 48
Properties of reduction How do we select redexes for reduction steps? The given semantics first reduces function, then argument. The order could be inverted. The argument may not need to be reduced at all. Does the result depend on such a choice? Does reduction ultimately terminate with a normal form? 49
Illustration of different reductions (We assume natural numbers with +.) Option 1 (λf.λx.f (f x)) (λy.y + 1) 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 Option 2 (λf.λx.f (f x)) (λy.y + 1) 2 (λx.(λy.y +1) ((λy.y +1) x)) 2 (λy.y +1) ((λy.y +1) 2)...... 4 50
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] 51
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. 52
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 53
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 54
A few extensions of the simply typed Lambda calculus 55
A few extensions of the simply typed Lambda calculus 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 here)... 56
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. 57
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. 58
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)) 59
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? 60
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 61
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 62
Typing of pairs: Pairs New syntax: t ::=... {t, t} t.1 t.2 New types: T ::=... T x T { } t : T u : U {t, u} : T U t : T t.1 : T U t : T t.2 : U U Evaluation rules: {v 1, v 2 }.1 v 1 {v 1, v 2 }.2 v 2 t t {t, u} {t, u} u u {v, u} {v, u } t t t.1 t.1 t t t.2 t.2 63
Lists New type:... List T New syntax:... nil[t] cons[t] t t isnil[t] t head[t] t tail[t] t New congruence rules, e.g.: New computation rules, e.g.: t 1 t 1 cons[t ] t 1 t 2 cons[t ] t 1 t 2 head[s] (cons[t ] v 1 v 2 ) v 1 New typing rules, e.g.: t : List T head[t ] t : T 64
Records Pairs generalize to tuples. Tuples further generalize to records. Records generalize to extensible records. Extensible records generalize to objects. We use the syntax: {age=44, name="smith"} {age=44, name="smith"}.name and write the types as: // record value // field access {age=44, name="smith"} : {age:int, name:string} 65
Records New syntax: t ::=... {l i = t i i21...n } t.l New values: v ::=... {l i = v i i21...n } New types: T ::=... {l i : T i i21...n } Typing of records: for each i, t i : T i {l i = t i i21...n } : {l i : T i i21...n } t : {l i : T i i21...n } t.l j : T j Evaluation rules: {l i = v i i21...n }.l j v j t t 0 t j t j 0 t.l t 0.l {l i = v i i21...j 1, l j = t j, l k = t k k2j+1...n } {l i = v i i21...j 1, l j = t j 0, l k = t k k2j+1...n } 66
More details on parametric polymorphism 67
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! 68
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 69
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] 70
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. 71
End of Lambda slides 72