Modèles des langages de programmation Domaines, catégories, jeux Programme de cette seconde séance: Modèle ensembliste du lambda-calcul ; Catégories cartésiennes fermées 1
Synopsis 1 the simply-typed λ-calculus, 2 the set-theoretic model of the λ-calculus, 3 categories and functors 4 cartesian categories 5 cartesian closed categories 6 interprétation of the simply-typed λ-calculus in a ccc 2
The simply-typed λ-calculus 3
The pure λ-calculus Terms M ::= x M N łx. M The β-reduction: (λx.m) N M [x := N] The η-expansion: M λx. (M x) Remark: every term is considered up to renaming α of the bound variables, typically: λx.λy.x α λz.λy.z 4
The simply-typed λ-calculus The simple types A, B are constructed by the grammar: A, B ::= α A B. A typing context Γ is a finite sequence Γ = (x 1 : A 1,..., x n : A n ) where each x i is a variable and each A i is a simple type. A sequent is a triple where x 1 : A 1,..., x n : A n P : B x 1 : A 1,..., x n : A n is a typing context, P is a λ-term and B is a simple type. 5
The simply-typed λ-calculus Variable Abstraction Application Weakening Contraction Exchange x : A x : A Γ, x : A P : B Γ λx.p : A B Γ P : A B Γ, PQ : B Γ P : B Γ, x : A P : B Q : A Γ, x : A, y : A P : B Γ, z : A P[x, y z] : B Γ, x : A, y : B, P : C Γ, y : B, x : A, P : C 6
Subject reduction A λ-term P is simply typed when there exists a sequent Γ P : A which may be obtained by a derivation tree. One establishes that the set of simply typed λ-terms is closed under β-réduction: Subject Reduction: If Γ P : A and P β Q, then Γ Q : A. 7
The set-theoretic interpretation of the λ-calculus 8
Interprétation ensembliste To each atomic type α is associated a set X α Then, one extends the interpretation to every type: [[α]] = X α [[A B]] = [[B]] [[A]] = Set ([[A]], [[B]]) A sequent x 1 : A 1,..., x n : A n M : B is interpreted as a function [[A 1 ]] [[A n ]] [[B]] 9
Soundness theorem Theorem. The interpretation provides an invariant of λ-terms modulo β and η. if Γ (λx.m) : A B and N : A, then [[Γ, (λx.m)n : B]] = [[Γ, M[x := N] : B]] if Γ M : A B then [[Γ (λx.mx) : A B]] = [[Γ M : A B]] 10
Categories and functors 11
Categories A category C is given by [0] a class of objects [1] a set Hom(A, B) of morphisms f : A B for every pair of objects (A, B) [2] a composition law : Hom(B, C) Hom(A, B) Hom(A, C) [2] an identity morphism for every object A, id A : A A 12
Categories satisfying the following properties: [3] the composition law is associative: f Hom(A, B) g Hom(B, C) h Hom(C, D) f (g h) = ( f g) h [3] the morphisms id are neutral elements f Hom(A, B) f id A = f = id B f 13
Examples 1. the category Set of sets and functions 2. the category Ord of partial orders and monotone functions 3. the category Dom of domains and continuous functions 4. the category Coh of coherence spaces and linear maps 5. every partial order 6. every monoid 14
Opposite category The opposite of a category C is the category noted C op whose morphisms f : A B are defined as the morphisms f : B A of the original category C. 15
Product category The product A B of two categories is the category A B whose objects are the pairs (A, B) of objects of A and B, whose morphisms (A, B) (A, B ) are the pairs of morphisms f : A A g : B B 16
Product category with composition and identities defined as expected: (A, B) id (A,B) (A, B) = (A, B) (id A,id B ) (A, B) (A, B) ( f,g) (A, B ) ( f,g ) (A, B ) = (A, B) ( f f,g g) (A, B ) 17
Functors A functor between categories is defined as the following data: F : C D [0] an object FA of D for every object A of C, [1] a function F A,B : Hom C (A, B) Hom D (FA, FB) for every pair of objects (A, B) of the category C. 18
Functors One requires moreover [2] that F preserves composition FA F f FB Fg FC = FA F(g f ) FC [2] that F preserves the identities FA Fid A FA = FA id FA FA 19
Illustration [ orders ] Every ordered set (X, ) defines a category [X, ] whose objects are the elements of X whose hom-sets are defined as { Hom(x, y) = { } if x y otherwise In this category, there exists at most one map between two objects 20
Illustration [ orders ] Exercise: given two ordered sets (X, ) (Y, ) a functor F : [X, ] [Y, ] is the same thing as a monotonic function F : (X, ) (Y, ) between the underlying ordered sets. 21
Illustration [ monoids ] A monoid (M,, e) is a set M equipped with a binary operation : M M M and a neutral element e : { } M satisfying the two properties below: Associativity law x, y, z M, (x y) z = x (y z) Unit law x M, x e = x = e x. 22
Illustration [ monoids ] Key observation: between there is a one-to-one relationship (M,, e) Σ (M,, e) monoids categories with one object obtained by defining Σ(M,, e) as the category with unique hom-set Σ(M,, e) (, ) = M and composition law and unit defined as g f = g f id = e 23
Illustration [ monoids ] Key observation: given two monoids (M,, e) (N,, u) a functor F : Σ (M,, e) Σ (N,, u) is the same thing as a homomorphism f : (M,, e) (N,, u) between the underlying monoids. Recall that a homomorphism is a function f such that x, y M, f (x y) = f (x) f (y) f (e) = u 24
Transformations A transformation θ : F G between two functors F, G : A B is a family of morphisms (θ A : FA GA) A Obj(A) of the category B indexed by the objects of the category A. 25
Natural transformations A transformation θ : F G : A B is natural when the diagram F f FA FB θ A θ B GA G f GB commutes for every morphism f : A B. 26
Isomorphism In a category C, a morphism f : A B is called an isomorphism when there exists a morphism satisfying g : B A g f = id A et f g = id B. Exercise. Show that g f : A C is an isomorphism when f : A B and g : B C are isomorphisms. Exercise. Show that every functor F : C D transports an isomorphism of C into an isomorphism of D. 27
Bifunctors A bifunctor F from C and D to E is given by: a functor F(A, ) : D E for every object A of the category C a functor F(, B) : C E for every object B of the category D such that the diagram F(A, B) F(A,g) F(A, B ) F( f,b) F( f,b ) F(A, B) F(A,g) F(A, B ) commutes for all morphisms f : A A in C and g : B B in D. 28
Cartesian categories 29
Products The product of two objects A and B in a category C is an object A B equipped with two morphisms π 1 : A B A π 2 : A B B such that for every diagram X f π 1 A B A g there exists a unique morphism h : X A B making the diagram X h f π 2 π 1 A B B A commute. g π 2 B 30
Illustrations 1. The cartesian product in the category Set, 2. The lub a b of two elements a and b in an ordered set (X, ), 3. The cartesian product in the category Dom, 31
Terminal object An object 1 is terminal in a category C when Hom(A, 1) is a singleton for all objects A. One may consider 1 as the nullary product in C. Example 1. the singleton { } in the categories Set and Dom, Example 2. the maximum of an ordered set (X, ) 32
Cartesian category A cartesian category is a category C equipped with a product A B for all pairs A, B of objects, and of a terminal object 1 33
Cartesian categories In every cartesian category, one finds weakening maps ɛ A : A 1, diagonal maps δ A : A A A obtained as id A A A δ A A A π 1 id A π 2 A symmetry maps γ A,B : A B B A obtained as π 2 B A B γ A,B B A π 1 π 1 π 2 A Exercise: Show that ( ) defines a bifunctor C C C. 34
Cartesian closed categories First definition 35
Ccc A cartesian closed category is a cartesian category (C,, 1) together with the following data for all objects A and B: of an object A B of a morphism eval A,B : A (A B) B such that for every object X and morphism f : A X B there exists a unique morphism h : X A B making the diagram A (A B) eval A,B B commute. A h A X f 36
Cartesian closed category Second definition 37
Adjunction An adjunction is a triple consisting of two functors and a family of bijections L : A B R : B A φ A,B : Hom B (LA, B) Hom A (A, RB) natural in A and B, for all pairs of objects A, B of A and B. LA B B A A RB φ A,B One writes L R and one says that L is left adjoint to R. 38
Example: the free vector space Set L R Vect where A = Set : the category of sets and functions B = Vect : the category of vector spaces on a field k R : the «forgetful» functor V U(V) L : the «free vector space» functor X kx kx := { x X λ x x λ x k null almost everywhere. } 39
Illustration: the free monoid Set L R Mon where A = Set : the category of sets and functions B = Mon : the category of monoids and homomorphisms, R : the «forgetful» functor M U(M). L : the «free monoid» functor A A. A := n N A n 40
What does natural bijection φ exactly mean? By φ natural, one means that the two families of sets Hom B (LA, B) Hom A (A, RB) define functors Hom B (L, ) : A op B Set Hom A (, R ) : A op B Set and that the family of bijections φ defines a natural transformation φ : Hom B (L, ) Hom A (, R ) : A op B Set between them. 41
What does natural bijection φ exactly mean? Natural in A and B thus means that every commutative diagram LA g B Lh A h B LA is transformed into a commutative diagram f B A φ A,B (g) RB h A A φ A,B ( f ) RB Rh B 42
Cartesian exponentiation Consider an object A in a cartesian category(c,, 1). A cartesian exponentiation of A is a pair consisting of a functor (A ) : C C and of a family of bijections φ A,B,C : Hom(A B, C) Hom(B, A C) natural in B and C. In other words, it is an adjunction between the functors A A 43
What natural bijection means in that case Naturality in B and C means that the family of bijections φ A,B,C : Hom(A B, C) Hom(B, A C) transforms every commutative diagram A B g C A h B A B into a commutative diagram f C h C B φ A,B,C (g) A C h B B φ A,B,C ( f ) A h C A C 44
Cartesian closed category Definition. A cartesian closed category (ccc) is a cartesian category (C,, 1) equipped with a cartesian exponentation A B C B A C for every object A of the category. φ A,B,C 45
Parameter theorem We have seen that the cartesian product defines a bifoncteur A, B A B : C C C in every cartesian category. In the same way, Parameter theorem [MacLane] The family of cartesian exponentiations (A ) A : C C defines a unique bifunctor A, B A B : C op C C such that the bijections φ A,B,C are natural in A, B, C. 46
Parameter theorem Here, natural in A, B, C means that the family of bijections (φ A,B,C ) A,B,C : Hom(A B, C) Hom(B, A C) transforms every commutative diagram A B g C h A h B A B in a commutative diagram: f C h C B φ A,B,C (g) A C h B B φ A,B,C ( f ) h A h C A C 47
Interpretation of the simply-typed λ-calculus in a CCC 48
The simply-typed λ-calculus The simple types A, B are constructed by the grammar: A, B ::= α A B. A typing context Γ is a finite sequence Γ = (x 1 : A 1,..., x n : A n ) where each x i is a variable and each A i is a simple type. A sequent is a triple where x 1 : A 1,..., x n : A n P : B x 1 : A 1,..., x n : A n is a typing context, P is a λ-term and B is a simple type. 49
The simply-typed λ-calculus Variable Abstraction Application Weakening Contraction Exchange x : A x : A Γ, x : A P : B Γ λx.p : A B Γ P : A B Γ, PQ : B Γ P : B Γ, x : A P : B Q : A Γ, x : A, y : A P : B Γ, z : A P[x, y z] : B Γ, x : A, y : B, P : C Γ, y : B, x : A, P : C 50
Interpretation of the λ-calculus Step 1. We suppose given a function ξ : α ξ(α) which associates an object ξ(α) to every type variable α. Step 2. Every type A is then interpreted as an object [[A]] of the cartesian closed category by structural induction: [[α]] = ξ(α) [[A B]] = [[A]] [[B]] [[A B]] = [[A]] [[B]] 51
Interpretation of the λ-calculus Step 3. Every sequent x 1 : A 1,..., x n : A n t : B is interpreted as a morphism [[t]] : [[A 1 ]] [[A n ]] [[B]] by structural induction on the derivation tree which produced it. 52
The logical rules Variable: [[A]] id [[A]] [[A]] Lambda: A Γ f B becomes Γ φ A,Γ,B ( f ) A B Application: Γ f A and g A B become Γ f g A (A B) eval A,B B 53
The structural rules Contraction: A A Γ f B becomes A Γ δ A Γ A A Γ f B Weakening: Γ f B becomes A Γ ε A Γ 1 Γ Γ f B Permutation: Γ A B becomes Γ B A Γ γ A,B Γ A B f B f B 54
Soundness theorem Theorem. In every cartesian closed category C, the interpretation [[ ]] is an invariant modulo β, η. If Γ (λx.m) : A B and N : A, then [[Γ, (λx.m)n : B]] = [[Γ, M[x := N] : B]] If Γ M : A B then [[Γ (λx.mx) : A B]] = [[Γ M : A B]] Exercise. Establish the soundness theorem. 55