Brief Notes on the Category Theoretic Semantics of Simply Typed Lambda Calculus
Total Page:16
File Type:pdf, Size:1020Kb
University of Cambridge 2017 MPhil ACS / CST Part III Category Theory and Logic (L108) Brief Notes on the Category Theoretic Semantics of Simply Typed Lambda Calculus Andrew Pitts Notation: comma-separated snoc lists When presenting logical systems and type theories, it is common to write finite lists of things using a comma to indicate the cons-operation and with the head of the list at the right. With this convention there is no common notation for the empty list; we will use the symbol “”. Thus ML-style list notation nil a :: nil b :: a :: nil etc becomes , a , a, b etc For non-empty lists, it is very common to leave the initial part “,” of the above notation implicit, for example just writing a, b instead of , a, b. Write X∗ for the set of such finite lists with elements from the set X. 1 Syntax of the simply typed l-calculus Fix a countably infinite set V whose elements are called variables and are typically written x, y, z,... The simple types (with product types) A over a set Gnd of ground types are given by the following grammar, where G ranges over Gnd: A ::= G j unit j A x A j A -> A Write ST(Gnd) for the set of simple types over Gnd. The syntax trees t of the simply typed l-calculus (STLC) over Gnd with constants drawn from a set Con are given by the following grammar, where c ranges over Con, x over V and A over ST(Gnd): t ::= c j x j () j (t , t) j fst t j snd t j lx : A. t j t t 1 We identify such syntax trees modulo remaning of l-bound variables. More formally a simply typed l-term is an equivalence class of syntax trees for the following, inductively defined relation of a-equivalence =a 0 0 0 t1 =a t1 t2 =a t2 t =a t 0 0 0 c =a c x =a x () =a () (t1 , t2) =a (t1 , t2) fst t =a fst t 0 0 0 t =a t t1 =a t1 t2 =a t2 0 0 0 snd t =a snd t t1 t2 =a t1 t2 0 0 0 0 (y x) · t =a (y x ) · t y does not occur in fx, x , t, t g 0 0 lx : A. t =a lx : A. t In the last rule (y x) · t indicates the syntax tree obtained from t by swapping occurrences of y and x; given the condition that y does not occur in t, this is the same as replacing all occurrences of x in t by y. Thus the last rule says that lx : A. t and lx0 : A. t0 are a-equivalent if t and t0 become a-equivalent once we replace all occurrences of x in t and all occurrences of x0 in t0 by some common “fresh” variable y. It is conventional to not make a notational distinction between a tree t and the a-equivalence class that it determines. That convention can be made mathematically precise via the use of nominal sets; see for example Pitts[2013, Chapter 8]. An alternative to working with l-terms as a-equivalence classes of abstract syntax trees is to use a nameless representation due to de Bruijn[1972] instead of explicitly named bound variables. For typed l-calculi, especially when using systems like Agda [wiki.portal.chalmers.se/agda/agda.php] or Coq [coq.inria.fr], so-called well-scoped de Bruijn indices are very convenient (if not very humam-readable); see for example Keller and Altenkirch[2010, Section 2]. 2 Typing relation We assume that the set Con comes with a function mapping each constant c 2 Con to its type A 2 ST(Gnd). We some times write c as cA to indicate that A is its type. In order to extend this typing function from constants to compound simply typed l-terms we have to assign types to (free) variables. We do so via typing environments G: G ::= j G, x : A (where x 2 V, A 2 ST(Gnd)) Thus the set of typing environments is in bijection with (V × ST(Gnd))∗, the set of finite lists of (variable,type)-pairs. The domain dom G of a typing environment G is the finite set of variables occurring in it: dom = Æ dom(G, x : A) = dom G [ fxg We only use the G that are well-formed G ok in the sense that no variable occurs more than once in the list: G ok x 2/ dom G ok G, x : A ok 2 Then the typing relation G ` t : A for assigning types A to terms t in a given typing environment G is inductively defined by: G ok x 2/ dom G G ` x : A x0 2/ dom G (var) (var’) G, x : A ` x : A G, x0 : A0 ` x : A G ok G ok (const) (unit) G ` cA : A G ` () : unit G ` t : A G ` t0 : A0 G ` t : A x A0 G ` t : A x A0 (pair) (fst) (snd) G ` (t , t0) : A x A0 G ` fst t : A G ` snd t : A0 G, x : A ` t : A0 G ` t : A -> A0 G ` t0 : A (l) (app) G ` lx : A. t : A -> A0 G ` t t0 : A0 Here are some simple properties of the typing relation G ` t : A, proved by induction on its derivation. The second property makes use of the finite set fv t of free variables of a term t, which is well-defined by: fv c = fv () = Æ fv (t , t0) = fv t t0 = fv t [ fv t0 fv x = fxg fv lx : A. t = fx0 2 fv t j x0 6= xg Lemma 2.1. 1. If G ` t : A, then G ok. 2. If G ` t : A, then fv t ⊆ dom G. 3. If G ` t : A and G ` t : A0, then A = A0. Property3 says that terms have at most one type in any (well-formed) typing environment. Of course some terms have no type; for example ` () () : A is not derivable from the rules for any type A (why?). Because we have formulated typing environments as ordered lists (rather than, say, fi- nite maps from variables to types), the important property of the typing relation that it is preserved under weakening typing environments (that is, adding extra (variable, type)-pairs while preserving the property of being well-formed) has to be formulated care- 0 fully. Here is a particular inductive definition of a weakening relation w : G B G (where w ::= i j w p j w x), inspired by Chapman[2009, Section 4.5], that interacts well with the typing relation: 0 0 0 0 G ok w : G B G x 2/ dom G w : G B G x 2/ dom G 0 0 i : G B G w p : (G , x : A) B G w x : (G , x : A) B G, x : A 0 0 Lemma 2.2. 1. If w : G B G and G ok, then G ok. 0 0 2. If G ` t : A and w : G B G, then G ` t : A. 3 0 Proof. Property1 is proved by induction on the derivation of w : G B G. For property2, which is the desired weakening property of the typing relation, one pro- ceeds by induction on the derivation of G ` t : A. For the base case when t is a variable, one proves 0 0 G ` x : A and w : G B G implies G ` x : A 0 by induction on the derivation of w : G B G, using part1; for the induction step when t is a l-abstraction one uses the fact that l-terms are a-equivalence classes of syntax trees, so that a representative l-bound variable can chosen to not be in dom G0, allowing the third 0 rule for the w : G B G relation to be applied. 3 Cartesian closed categories Recall that a category C is cartesian closed if it has A terminal object: a C-object > with the property that for every Z 2 obj C there is a unique morphism hi 2 C(Z, >). The uniqueness part of this property is: f 2 C(Z, >) ) f = hi Binary products: for all X, Y 2 obj C there is a C-object X × Y and morphisms p1 2 C(X × Y, X), p2 2 C(X × Y, Y) with the property that for every Z 2 obj C, f 2 C(Z, X) and g 2 C(Z, Y), there is a unique morphism h f , gi 2 C(Z, X × Y) satisfying p1 ◦ h f , gi = f and p2 ◦ h f , gi = g. The uniqueness part of this property is equivalent to requiring: h 2 C(Z, X × Y) ) h = hp1 ◦ h, p2 ◦ hi As a matter of notation, if f 2 C(Z, X) and g 2 C(W, Y) we define f × g 2 C(Z × W, X × Y) to be f × g , h f ◦ p1, g ◦ p2i. Exponentials: for all X, Y 2 obj C there is a C-object YX and a morphism app 2 C(YX × X, Y) with the property that for every Z 2 obj C and f 2 C(Z × X, Y) there is a unique X morphism cur f 2 C(Z, Y ) satisfying app ◦ (cur f × idX) = f . The uniqueness part of this property is equivalent to requiring: X h 2 C(Z, Y ) ) h = cur(app ◦ (h × idX)) 4 Semantics in a cartesian closed category Let C be a cartesian closed category.