Lambda Calculus Type Systems, Lectures 3 Jevgeni Kabanov Tartu, 13.02.2006
PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety = Progress + Preservation 1
OVERVIEW Motivation Lambda Calculus Putting λ to work Simple Types for Simple Calculus 2
Motivation
WHAT IS THE CALCULUS? Toy language Simple, Powerful and Formal Allows to play with advanced features formally Saves headache of dealing with the Real World Usually typable 3
EXISTING CALCULI Record calculus Pi-calculus Lambda-calculus Object calculus µabc calculus... 4
Lambda Calculus
LAMBDA OVERVIEW Everything is a function Very simple evaluation Very simple basic typing Structural types are easy Object-oriented types are harder 5
LAMBDA SYNTAX t ::= x λx.t t t terms: variables abstractions applications Where x Var, a countable set of variables 6
BRACKET AND SHORTHAND EXAMPLES Shorthand notation on the left: λx.x = (λx.x) x y z = ((x y) z) λx.x y = (λx.(x y)) (λx.x) y = ((λx.x) y) outer brackets left associate λ is greedy λ is greedy (λx.x x)λy.y y = ((λx.(x x))(λy.(y y))) 7
REDEX & BETA REDUCTION Redex is an application of an abstraction: (λx.x) y x ((λx.x) y) ((λx.x) y) ((λx.x) y) ((λx.x) y) ((λx.x) y) Redex is evaluated by substitution: (λx.t 1 ) t 2 t 1 [x t 2 ] 8
SUBSTITUTION Search & Replace (aka Tretjakov isomorphism): (λx.x) y = x[x y] = y x ((λx.x) y) = x (x[x y]) = x y (λx.x y) λz.z = (x y)[x λz.z] = (λz.z) y = z[z y] = y (λx.y) λz.z = y[x λz.z] = y 9
THE FEMALE INTUITION Lambda abstraction captures functions (not a valid definition): factorial = λn.if n = 0 then 1 else n factorial(n 1) Some common functions: id = λx.x const = λx.λy.x Further on we will use macros for brevity. 10
TERMINATION What does Ω evaluate to? ω = λx.x x Ω = ω ω 11
TERMINATION What does Ω evaluate to? ω = λx.x x Ω = ω ω = (λx.x x) ω x x[x ω] = ω ω = Ω Ladies and Gentlemen the halting problem 11
EVALUATION ORDER What to do if the term has several redexes? Full beta reduction Reduce any redex Normal order Evaluate functions before values Call-by-name Normal order, where evaluation under λ is forbidden Call-by-value Evaluate values before functions Further on we use call-by-value as in most real languages. 12
LAMBDA SEMANTICS Values (also a syntactical category): v ::= λx.t Evaluation rules: E-APP1 t 1 t 1 t 1 t 2 t 1 t 2 E-APP2 t 2 t 2 v 1 t 2 v 1 t 2 values: abstraction value E-APPABS (λx.t) v [x v]t No evaluation under λ-abstraction Redex reduction only over values Left-to-right order of evaluation 13
FREE VARIABLES Free variables are those not bound by λ-abstraction: FV(x) = x FV(λx.t 1 ) = FV(t 1 ) \ {x} FV(t 1 t 2 ) = FV(t 1 ) FV(t 2 ) Further on we suppose that only free variables matter if there are colliding non-free variables they get magically renamed to avoid collision. We will call terms without free variables closed. 14
SUBSTITUTION REVISITED The formal definition of substitution on terms: [x s]x = s [x s]y = y, if x y [x s](t 1 t 2 ) = [x s]t 1 [x s]t 2 [x s](λy.t 1 ) = λy.[x s]t 1 if x y, where y / FV(s) The last rule implies that magic renaming (called alpha-conversion) takes place. 15
Putting λ to work
HIGHER ORDER FUNCTIONS Functions with multiple arguments: add = λn.λm.n + m Higher order functions Simulate multiple arguments Allow partial application (currying) Allow passing functions to functions 16
BOOLEANS Can we incorporate Booleans into λ-calculus? true = λt.λf.t false = λt.λf.f ifthenelse = λb.λl.λr.b l r Yes, we can: ifthenelse true x y = (λt.λf.t) x y = x ifthenelse false x y = (λt.λf.f) x y = y 17
NUMERALS What about arithmetical expressions? c 0 = λs.λz.z c n = λs.λz.s n (z) succ = λn.λs.λz.s (n s z) iszero = λm.m(λx.false)true c n is interpreted as number n, it is also possible to define addition, multiplication,... 18
RECURSION To define recursion we only have to define recursion (still not valid): factorial = λn.if n = 0 then 1 else n factorial(n 1) Fixpoints to the rescue: factstep = λf.λn.if n = 0 then 1 else n f(n 1) factorial = fix factstep 19
FIXPOINTS, MATHEMATICALLY A fixpoint (also fixed point) for function f is such x that f(x) = x In our case factstep(factorial) = factorial Often a fixpoint can be found using some operator fix and in such a case is denoted x = fix f 20
LAMBDA FIXPOINTS In call-by-value λ-calculus we can define the fix operator as fix = λf.(λx.f(λy.x x y)) (λx.f(λy.x x y)) fix f f (fix f) factorial = fix factstep factstep(fix factstep) factstep(factstep( (0) )) The latter shows that factorial definition indeed makes sense. 21
Simple Types for Simple Calculus
FUNCTION TYPES Following our interpretation of λ-abstractions as higher order functions we first introduce function types: T ::= T n T T types: type variables types of functions A lambda term then would have type according to the number of λ-abstractions, e.g. T 1 (T 2 T 3 ). 22
EXPLICIT VERSUS IMPLICIT TYPING There are two main ways of typing λ-calculus: Annotate the λ-abstraction arguments with their types. Deduce the λ-abstraction arguments types from the λ-abstraction body (type inference). Further we use the first solution: λx:t 1.t 23
TYPING CONTEXT How do we type λx:t 1.x y We can choose to type only closed terms, but such a case can occur as a subterm: λy :T 2.(λx:T 1.x y) We won t our type relation to be compositional, therefore we introduce the notion of a typing context. 24
TYPING CONTEXT Γ ::= contexts: empty context Γ, x:t term variable binding Typing context is a set of variable and type pairs that connect the free variables with their types. Lambda typing relation involves contexts, terms and types: Γ t : T 25
LAMBDA SIMPLE TYPING Finally we can present the Lambda calculus simple typing: T-VAR x:t Γ Γ x : T T-ABS Γ, x:t 1 t 2 : T 2 Γ λx:t 1.t 2 : T 1 T 2 T-APP Γ t 1 : T 1 T 2 Γ t 2 : T 1 Γ t 1 t 2 : T 2 Further on we refer to typed Lambda calculus as λ. 26
LAMBDA SIMPLE TYPING EXAMPLE Lets derive the type for the constant function (λx.λy.x): T-ABS T-ABS T-VAR therefore x:t 1 {x:t 1, y :T 2 } x:t 1, y :T 2 x : Φ 2 = T 1 x:t 1 λy :T 2.x : Φ 1 = T 2 Φ 2 λx:t 1.λy :T 2.x : T 1 Φ 1, λx:t 1.λy :T 2.x : T 1 (T 2 T 1 ) 27
LAMBDA SAFETY Progress proof outline: Straightforward induction on typing derivation Preservation proof outline: Prove that substitution preserves types Induction on typing derivation Both proofs are relatively simple. 28
LAMBDA SAFETY, ADVANCED Uniqueness of typing Every lambda term has one unique type. Normalization Every lambda term evaluation halts after a finite number of reductions. The first property is trivial to prove. The second property proof uses the property that with every reduction either the maximal depth of redexes decreases or the number of redexes with maximal depth decreases. 29
NORMALIZATION AND TERMINATION Why is normalization possible? 30
NORMALIZATION AND TERMINATION Why is normalization possible? ω = λx.x x Ω = ω ω What s ω type? T-APP T-ABS T-VAR x:φ 1 x : Φ 1 Φ 2 x:φ 1 x : Φ 1 x:φ 1 x x : Φ 2 ω = λx:φ 1.x x : Φ 2 30
NORMALIZATION AND TERMINATION Fixpoint operator is not typable in λ. Nor is any other kind of recursion. Some type systems are reducible to λ. Most of the languages we examine lack normalization. Some type systems have normalization as their goal. 31