CS 541 Lecture -*- Outline -*- * The simply typed lambda calculus "the pure typed lambda calculus with function types" -- Mitchell or \^{->}. advert: simpler than the untyped version (less powerful) easier to see what's going on aware: give examples of everything. give reasons for everything ** syntax ground type o, x in Variables --------------- TYPED LANGUAGE CONCRETE SYNTAX t,s in Type-Expr e in Expr x in Identifier t ::= o | t -> t | (t) e ::= x | \ x : t . e | e e | (e) --------------- t is a type expression, e is a term expression (term), in Haskell, \x:t.e is like (\ x::t -> e) in SML, \x:t.e is like (fn x:t => e) can have more than one ground type, but we won't for simplicity Microsyntax (microsyntax not standard) x ::= non-blank non-blank* non-blank ::= any character except blank, ".", ":", "(", and ")" -------------------- ABSTRACT SYNTAX t,s in Type-Expr e in Expr x in Identifier t ::= o | (t -> t) e ::= x | (\ x : t . e) | (e e) -------------------- *** parsing rules above grammar is ambiguous. This is the way it is disambiguated ------------- PARSING RULES parse(\x:o. e e') == (\x:o. (e e')) parse(x y z) == ((x y) z) parse(o -> o -> o) == (o -> (o -> o)) ------------- (may work problem 1 of the typed-quiz here) ** substitution (quickly if have done this before) examples: (\x:o . (+ x x)) 2 ==> (+ 2 2) (\+:o->o->o . (+ 2 2)) * ==> (* 2 2) (\f:o->o . \y:o . f y) (\y:o.y) ==> ? -no problem- (\x:o->o . \y:o . x y) y ==> ? -be careful- *** free occurrences -------------- FREE AND BOUND VARIABLES def: x occurs free in (i) x, (ii) (U V) if x occurs free in U or V, (iii) (\y:t.U) if x =/= y, and x occurs free in U. ------------- *** free variables, Fv ---------- Fv: Expr -> (Set of Identifier) Fv(x) = {x} Fv(\x:t.e) = Fv(e) - {x} Fv(e e') = Fv(e) union Fv(e') ---------- *** bound variables ------------- def: x is bound in (i) \x:t.U, if x in Fv(U) (ii) (\y:t.U), if x is not y and if x is bound in U (iii) (U V), if x is bound in U or in V ------------- note: x can be free and bound in the same term: (x (\x:o.x)) (do problem 2 of the typed quiz here) *** syntactic substitution ------------ SYNTACTIC SUBSTITUTION [e/x]x == e [e/x]y == y if not(y == x) [e/x](e1 e2) == ([e/x]e1) ([e/x]e2) [e/x](\x:t.e') == (\x:t.e') [e/x](\y:t.e') == (\y:t . [e/x]e') where not(y == x), and not(y in Fv(e)) ------------ last restriction necessary to preserve lexical scoping e.g. [y/x](\y:o . (x y)) is not (\y:o . (y y)) rather (\z:o. (y z)) (do problem 3 of the typed quiz here) ** typing rules ref: Mitchell, Handbook of TCS, chapter 8 don't emphasize the formalism *** examples ---------- TYPING RULE EXAMPLES what is the type of: (\x:o . x) (\x: o -> o . x) (\y:o.y) z (\x:o . z) ---------- -------------------------- THE TYPE CHECKING PROBLEM & NOTATION H[x:->s] |- e:t [-> Intro] -------------------- H |- (\x:s.e) : s->t --------------------------- This rule means to show that the thing under the line checks, have to prove that the thing on the type checks. *** type environments (contexts) Q: what if E involves x? What if E involves some y? How to keep track of this information to use? introduce the notion of a type environment map from names to types that embodies the assmumptions you can make. ------------------------ TYPE ENVIRONMENTS H \in TypeEnv = Identifier -> Type-Expr We represent these as their graphs: so x:o, y:(o->o) maps x to o y to (o -> o) {} : TypeEnv _:_ : Identifier * Type-Expr -> TypeEnv overlay : TypeEnv * TypeEnv -> TypeEnv find : TypeEnv * Identifier -> Type-Expr _,_:_ : TypeEnv * Identifier * Type-Expr -> TypeEnv find(x:t, x) = t find(overlay(H1, H2), x) = if x in dom(H1) then H1(x) else H2(x) dom({}) = {} dom(x:t) = {x} dom(overlay(H1, H2) = dom(H1) \union dom(H2) H,x:t = overlay(bind(x,t), H) Thus find((H,x:t), x) = t find((H,x:t), y) = find(H, y), if not(x == y) ------------------------ Here t stands for a Type-Expr, x for and Identifier Think of a function as a set of bindings. Q: so what is find(overlay(i:int, i:real), i)? Q: so how do we formalize the notation for the rule above? *** type judgements, has type relation "has type" is a ternary relation, written H |- e : t such that Fv(e) subset-of domain(H) the least relation satisfying ------------------------------------------ TYPE CHECKING AXIOMS AND RULES [var] H |- x : t if find(H,x) = t H |- e:t [add hyp] --------------- if x not in H,x:s |- e:t dom(H) H,x:s |- e:t [-> Intro] -------------------- H |- (\x:s.e) : s->t H |- e:(s->t), H |- e':s [-> Elim] ------------------------ H |- (e e') : t ------------------------------------------ conventions in the above: x is a variable, e is an expression s, t are type expressions H is an environment stuff above horizontal line is hypothesis, below is conclusion by abuse of notation, identify the "has type" relation with the forms H |- x : t (do problem 4 of the typed-quiz here) ** models (can skip) *** full type frame ref: Carl Gunter, Semantics of Programming Languages (MIT Press, 1993) ---------------------- FULL TYPE FRAME MODEL meaning of a type: [[.]]: Type-Expr -> Value [[o]] = Nat [[(s -> t)]] = { f: [[s]] -> [[t]] } H-environments: (H a type context) eta in Env(H) = Identifier -> Value such that if x:t in H then eta(x) in [[t]] Meaning of an expression: [[.|>.:.]] : (H:TypeEnv) * Expr * Type-Expr -> Env(H) -> Value such that if H |> e:t then [[H |> e : t]](eta) in [[t]] [[H |> x : t]]eta = eta(x) [[H |> (e e') : t]]eta = ([[H |> e:s->t]]eta) ([[H |> e':s]]eta) [[H |> (\x:s.e) : s->t]]eta = f where f(y) = [[H[x:->s] |> e:t ]]eta' and eta' = \z.if z==x then y else eta(z) -------------------- eta, eta' are H-environments See the file denotational-calculations.txt for some examples See the Haskell code for this in lambda-in-haskell/SimplyTypedModel.lhs