CS 641 Lecture -*- Outline -*- * The simply typed lambda calculus theory of application and substitution governed by types ** expressions ground type o, x in Variables --------------- t ::= o | t -> t | (t) e ::= x | \x: t . e | e e | (e) --------------- t is a type expression, e is a term expression (term) *** parsing rules ------------- \x:o. e e' == (\x:o. (e e')) x y z == (x y) z o -> o -> o == o -> (o -> o) ------------- *** free variables, Fv ---------- Fv(x) = {x} Fv(\x:t.e) = Fv(e) - {x} Fv(e e') = Fv(e) union Fv(e') ---------- *** bound variables Def: variable is bound if it is not free bound variable names unimportant: treat raw terms as equivalence classes of terms under alpha-cnv ------------ (\x:t.x) == (\y:t.y) syntactic identity ------------ *** 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](\y:t.e') == \y:t . [e/x]e' where not(y == x), 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)) ** typing rules *** type context a list x1:t1, ..., xn:tn where the xi are distinct usually written H, think of it as an environment *** 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 ----------------- [proj] H, x:t, H' |- x : t [abs] H, x:s |- e:t ==> H |- (\x:s.e) : s->t [appl] H|-e:(s->t), H|-e':s ==> H |- (e e') : t ----------------- by abuse of notation, identify the "has type" relation with the forms H |- x : t *** type derivation demonstration of H |- e : t from the rules derivation tree: node is a judgement + rule name pair, branches (up) to other nodes on which the rule depends, branches of a node not ordered. exercise: show |- (\x:o \f:o->o . f(x)) : o -> (o->o) -> o how to proceed: write desired result at bottom of board look at form of expression in question, that gives rule only interested in terms e and env H such that H |- e:s for some s Def: e has a type (in H) if there is such a s exercise: show that if H |- e : s and H |- e : t, then s == t. show that if a derivation of H |- e : t exists it is unique. HW exercise: if H |- e:s and x is not in domain(H), then H,x:t |- e:s. converse? ** equational rules, equational theory an equation is a 4-tuple H |- e = e' : s where terms e and e', a type env H, a type expr s such that H |- e : s and H |- e' : s equations are typed, including H also tells what variables the equation ranges over. equational theory is generated by the following rules ------------------- {refl} H |- x = x : s {trans} H |- e = e' : s, H |- e' = e'' : s ==> H |- e = e'' : s {sym} H |- e = e' : s ==> H |- e' = e : s H |- e0 = e0' : s->t H |- e1 = e1' : s {cong} __________________________________________ H |- e0(e1) = e0'(e1') : t {xi} H,x:s |- e = e' : t ==> H |- (\x:s.e) = (\x:s.e') : s->t {beta} H |- (\x:s.e)(e') = ([e'/x]e) : t {eta} H |- \x:s.e(x) = e : s->t, where x not in Fv(e) ------------------- equivalence rules: [refl], [trans], [sym] used to prove that equality is an equivalence relation congruence rules: [cong], [xi] state that application and abstraction are congruences with respect to equality. basic axioms: [beta], [eta] a theory is a set of sequents (of form H |- e = e' : s). the above theory is consistent and decideable Def: the \-theory generated by a theory T is the least relation that includes T and satisfies the rules of the \-calculus HW exercise: show that equality is preserved under a renaming of free vars