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 SML, \x:t.e is like (fn x:t => e) can have more than one ground type, but we won't for simplicity -------------------- ABSTRACT SYNTAX t,s in Type-Expr e in Expr x in Identifier t ::= o | (t -> t) e ::= x | (\ x : t . e) | (e e) -------------------- New parsing rule: o -> o -> o == (o -> (o -> o)) Otherwise the parsing rules, substitution, free and bound variables, and reductions are the same as in the untyped language. ** 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 E: s [-> Intro] --------------------- (\x: t . 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 empty-type-env : TypeEnv bind : Identifier * Type-Expr -> TypeEnv overlay : TypeEnv * TypeEnv -> TypeEnv find : TypeEnv * Identifier -> Type-Expr [ :-> ] : TypeEnv * Identifier * Type-Expr -> TypeEnv empty-type-env = { } bind(I,T) = { I :-> T } find(H, I) = H(I) find(overlay(H1, H2), I) = if I in dom(H1) then H1(I) else H2(I) H[I :-> T] = overlay(bind(I,T), H) ------------------------ Here T stands for a Type-Expr, I for and Identifier Think of a function as a set of bindings. Q: so what is find(overlay(bind(i,int), bind(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 find(H,x) = t [var] ---------------- H |> x : t H |> e:t [add hyp] --------------- x not in dom(H) H[x:->s] |> e:t 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 *** 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 dom(H) then eta(x:t) 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:t) [[H |> (e e') : t]]eta = ([[H |> e:s->t]]eta) ([[H |> e':s]]eta) [[H |> (\x:s.e) : s->t]]eta = y |-> [[H[x:->s] |> e:t ]]eta' where eta' is an H,x:s-envirionment such that eta'(x) = y and eta'(z) = eta(z) if not(z == x) -------------------- eta, eta' are H-environments