User:Michiexile/MATH198/Lecture 5
From HaskellWiki
Michiexile (Talk | contribs) |
Michiexile (Talk | contribs) |
||
(14 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | + | ===Cartesian Closed Categories and typed lambda-calculus=== | |
+ | |||
+ | A category is said to ''have pairwise products'' if for any objects <math>A,B</math>, there is a product object <math>A\times B</math>. | ||
+ | |||
+ | A category is said to ''have pairwise coproducts'' if for any objects <math>A,B</math>, there is a coproduct object <math>A+B</math>. | ||
+ | |||
+ | Recall when we talked about internal homs in [[User:Michiexile/MATH198/Lecture 2|Lecture 2]]. We can now define what we mean, formally, by the concept: | ||
+ | |||
+ | ''Definition'' An object <math>C</math> in a category <math>D</math> is an ''internal hom object'' or an ''exponential object'' <math>[A\to B]</math> or <math>B^A</math> if it comes equipped with an arrow <math>ev: [A\to B] \times A \to B</math>, called the ''evaluation arrow'', such that for any other arrow <math>f: C\times A\to B</math>, there is a unique arrow <math>\lambda f: C\to [A\to B]</math> such that the composite | ||
+ | |||
+ | <math>C\times A\to^{\lambda f\times 1_A} [A\to B]\times A\to^{ev} B</math> | ||
+ | |||
+ | is <math>f</math>. | ||
+ | |||
+ | The idea here is that with something in an exponential object, and something in the source of the arrows we imagine live inside the exponential, we can produce the evaluation of the arrow at the source to produce something in the target. Using global elements, this reasoning comes through in a more natural manner: given <math>f: 1\to [A\to B]</math> and <math>x: 1\to A</math> we can produce the global element <math>f(x) = ev \circ f\times x: 1\to B</math>. Furthermore, we can always produce something in the exponential whenever we have something that looks as if it should be there. | ||
+ | |||
+ | And with this we can define | ||
+ | |||
+ | '''Definition''' A category <math>C</math> is a ''Cartesian Closed Category'' or a CCC if: | ||
+ | # <math>C</math> has a terminal object <math>1</math> | ||
+ | # Each pair of objects <math>A, B\in C_0</math> has a product <math>A\times B</math> and projections <math>p_1:A\times B\to A</math>, <math>p_2:A\times B\to B</math>. | ||
+ | # For every pair <math>A, B\in C_0</math> of objects, there is an exponential object <math>[A\to B]</math> with an evaluation map <math>[A\to B]\times A\to B</math>. | ||
+ | |||
+ | ====Currying==== | ||
+ | |||
+ | Note that the exponential as described here is exactly what we need in order to discuss the Haskell concept of multi-parameter functions. If we consider the type of a binary function in Haskell: | ||
+ | <haskell> | ||
+ | binFunction :: a -> a -> a | ||
+ | </haskell> | ||
+ | This function really lives in the Haskell type <hask>a -> (a -> a)</hask>, and thus is an element in the repeated exponential object <math>[A \to [A\to A]]</math>. Evaluating once gives us a single-parameter function, the first parameter consumed by the first evaluation, and we can evaluate a second time, feeding in the second parameter to get an end result from the function. | ||
+ | |||
+ | On the other hand, we can feed in both values at once, and get | ||
+ | <haskell> | ||
+ | binFunction' :: (a,a) -> a | ||
+ | </haskell> | ||
+ | which lives in the exponential object <math>[A\times A\to A]</math>. | ||
+ | |||
+ | These are genuinely different objects, but they seem to do the same thing: consume two distinct values to produce a third value. The resolution of the difference lies, again, in a recognition from Set theory: there is an isomorphism | ||
+ | |||
+ | <math>Hom(S, Hom(T, V)) = Hom(S\times T, V)</math> | ||
+ | |||
+ | which we can use as inspiration for an isomorphism <math>Hom(S,[T\to V]) = Hom(S\times T, V)</math> valid in Cartesian Closed Categories. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====Typed lambda-calculus==== | ||
+ | |||
+ | The lambda-calculus, and later the ''typed'' lambda-calculus both act as foundational bases for computer science, and computer programming in particular. The idea in both is that everything is a function, and we can reduce the act of programming to function application; which in turn can be analyzed using expression rewriting rules that encapsulate the act of computation in a sequence of formal rewrites. | ||
+ | |||
+ | ''Definition'' A ''typed lambda-calculus'' is a formal theory with ''types'', ''terms'', ''variables'' and ''equations''. Each term <math>a</math> has a type <math>A</math> associated to it, and we write <math>a:A</math> or <math>a\in A</math>. The system is subject to a sequence of rules: | ||
+ | # There is a type <math>1</math>. ''Hence, the empty lambda calculus is excluded''. | ||
+ | # If <math>A, B</math> are types, then so are <math>A\times B</math> and <math>[A\to B]</math>. ''These are, initially, just additional symbols, not imbued with the associations we usually give the symbols used.'' | ||
+ | # There is a term <math>*:1</math>. ''Hence, the lambda calculus without any terms is excluded''. | ||
+ | # For each type <math>A</math>, there is an infinite (countable) supply of terms <math>x_A^i:A</math>. | ||
+ | # If <math>a:A, b:B</math> are terms, then there is a term <math>(a,b):A\times B</math>. | ||
+ | # If <math>c:A\times B</math> then there are terms <math>proj_1(c):A, proj_2(c):B</math>. | ||
+ | # If <math>a:A</math> And <math>f:[A\to B]</math>, then there is a term <math>f a:B</math>. | ||
+ | # If <math>x:A</math> is a variable and <math>\phi(x):B</math> is a term, then there is a <math>\lambda_{x\in A}\phi(x):[A\to B]</math>. ''Note that here, <math>\phi(x)</math> is a meta-expression, meaning we have SOME lambda-calculus expression that may include the variable <math>x</math>.'' | ||
+ | # There is a relation <math>a=_Xa'</math> for each set of variables <math>X</math> that occur freely in either <math>a</math> or <math>a'</math>. This relation is reflexive, symmetric and transitive. ''Recall that a variable is free in a term if it is not in the scope of a <math>\lambda</math>-expression naming that variable.'' | ||
+ | # If <math>a:1</math> then <math>a=_{\{\}}*</math>. ''In other words, up to lambda-calculus equality, there is only one value of type <math>*</math>.'' | ||
+ | # If <math>X\subseteq Y</math>, then <math>a=_Xa'</math> implies <math>a=_Ya'</math>. ''Binding more variables gives less freedom, not more, and thus cannot suddenly make equal expressions differ.'' | ||
+ | # <math>a=_Xa'</math> implies <math>f a=_Xf a'</math>. | ||
+ | # <math>f=_Xf'</math> implies <math>f a=_Xf' a</math>. ''So equality plays nice with function application.'' | ||
+ | # <math>\phi(x) =_{X\cup \{x\}} \phi'(x)</math> implies <math>\lambda_x \phi(x) =_X \lambda_x \phi'(x)</math>. ''Equality behaves well with respect to binding variables.'' | ||
+ | # <math>proj_1(a,b) =_X a</math>, <math>proj_2(a,b) =_Xb</math>, <math>c=_X(proj_1(c),proj_2(c))</math> for all <math>a,b,c,X</math>. | ||
+ | # <math>\lambda_x\phi(x) a =_X \phi(a)</math> if <math>a</math> is substitutable for <math>x</math> in <math>\phi(x)</math> and <math>\phi(a)</math> is what we get by substituting each occurrence of <math>x</math> by<math>a</math> in <math>\phi(x)</math>. ''A term is '''substitutable''' for another if by performing the substitution, no occurrence of any variable in the term becomes bound,'' | ||
+ | # <math>\lambda_{x\in A}f x =_X f</math>, provided <math>x\not\in X</math>. | ||
+ | # <math>\lambda_{x\in A}\phi(x) =_X \lambda_{x'\in A}\phi(x')</math> if <math>x'</math> is substitutable for <math>x</math> in <math>\phi(x)</math> and each variable is not free in the other expression. | ||
+ | |||
+ | Note that <math>=_X</math> is ''just a symbol''. The axioms above give it properties that work a lot like equality, but two lambda calculus-equal terms are not equal unless they are identical. However, <math>a=_Xb</math> tells us that in any ''model'' of this lambda calculus - where terms, types, et.c. are replaced with actual things (mathematical objects, say, or a programming language semantics embedding typed lambda calculus) - ''then'' the things given by translating <math>a</math> and <math>b</math> into the model should end up being equal. | ||
+ | |||
+ | Any actual realization of typed lambda calculus is bound to have more rules and equalities than the ones listed here. | ||
+ | |||
+ | With these axioms in front of us, however, we can see how lambda calculus and Cartesian Closed Categories fit together: We can go back and forth between the wo concepts in a natural manner: | ||
+ | |||
+ | =====Lambda to CCC===== | ||
+ | |||
+ | Given a typed lambda calculus <math>L</math>, we can define a CCC <math>C(L)</math>. Its objects are the types of <math>L</math>. An arrow from <math>A</math> to <math>B</math> is an equivalence class (under <math>=_{\{x\}}</math>) of terms of type <math>B</math>, free in a single variable <math>x:A</math>. | ||
+ | |||
+ | We need the equivalence classes because for any variable <math>x:A</math>, we want <math>\lambda_xx: 1\to [A\to A]</math> to be the global element of <math>[A\to A]</math> corresponding to the identity arrow. Hence, that variable must itself correspond to an identity arrow. | ||
+ | |||
+ | And then the rules for the various constructions enumerated in the axioms correspond closely to what we need to prove the resulting category to be cartesian closed. | ||
+ | |||
+ | =====CCC to Lambda===== | ||
+ | |||
+ | To go in the other direction, starting out with a Cartesian Closed Category and finding a typed lambda calculus corresponding to it, we construct its ''internal language''. | ||
+ | |||
+ | Given a CCC <math>C</math>, we can assume that we have chosen, somehow, one actual product for each finite set of factors. Thus, both all products and all projections are well defined entities, with no remaining choice to determine them. | ||
+ | |||
+ | The ''types'' of the internal language <math>L(C)</math> are just the objects of <math>C</math>. The existence of products, exponentials and terminal object covers axioms 1-2. We can assume the existence of variables for each type, and the remaining axioms correspond to definition and behaviour of the terms available. | ||
+ | |||
+ | Using the properties of a CCC, it is at this point possible to prove a resulting equivalence of categories <math>C(L(C)) = C</math>, and similarly, with suitable definitions for what it means for formal languages to be equivalent, one can also prove for a typed lambda-calculus <math>L</math> that <math>L(C(L)) = L</math>. | ||
+ | |||
+ | ---- | ||
+ | More on this subject can be found in: | ||
+ | * Lambek & Scott: ''Aspects of higher order categorical logic'' and ''Introduction to higher order categorical logic'' | ||
+ | |||
+ | More importantly, by stating <math>\lambda</math>-calculus in terms of a CCC instead of in terms of ''terms'' and ''rewriting rules'' is that you can escape worrying about variable clashes, alpha reductions and composability - the categorical translation ignores, at least superficially, the variables, reduces terms with morphisms that have equality built in, and provides associative composition ''for free''. | ||
+ | |||
+ | At this point, I'd recommend reading more on Wikipedia [http://en.wikipedia.org/wiki/Lambda_calculus] and [http://en.wikipedia.org/wiki/Cartesian_closed_category], as well as in Lambek & Scott: Introduction to Higher Order Categorical Logic. The book by Lambek & Scott goes into great depth on these issues, but may be less than friendly to a novice. | ||
===Limits and colimits=== | ===Limits and colimits=== | ||
− | + | One ''design pattern'', as it were, that we have seen occur over and over in the definitions we've seen so far is for there to be some object, such that for every other object around, certain morphisms have unique existence. | |
− | + | ||
− | + | We saw it in terminal and initial objects, where there's a unique map from or to '''every''' other object. And in products/coproducts where a wellbehaved map, capturing any pair of maps has unique existence. And finally, above, in the CCC characterization of the internal hom, we had a similar uniqueness requirement for the lambda map. | |
− | + | ||
− | + | One thing we can notice is that the isomorphisms theorems for all these cases look very similar to each other: in each isomorphism proof, we produce the uniquely existing morphisms, and prove that their uniqueness and their other properties force the maps to really be isomorphisms. | |
− | + | ||
− | + | Now, category theory has a philosophy slightly similar to design patterns - if we see something happening over and over, we'll want to generalize it. And there are generalizations available for these! | |
+ | |||
+ | ====Diagrams, cones and limits==== | ||
+ | |||
+ | '''Definition''' A ''diagram'' <math>D</math> of the shape of an index category <math>J</math> (often finite or countable), in a category <math>C</math> is just a functor <math>D:J\to C</math>. Objects in <math>J</math> will be denoted by <math>i,j,k,...</math> and their images in <math>C</math> by <math>D_i,D_j,D_k,...</math>. | ||
+ | |||
+ | This underlines that when we talk about diagrams, we tend to think of them less as just functors, and more as their images - the important part of a diagram <math>D</math> is the objects and their layout in <math>C</math>, and not the process of going to <math>C</math> from <math>D</math>. | ||
+ | |||
+ | '''Definition''' A ''cone'' over a diagram <math>D</math> in a category <math>C</math> is some object <math>C</math> equipped with a family <math>c_i:C\to D_i</math> of arrows, one for each object in <math>J</math>, such that for each arrow <math>\alpha:i\to j</math> in <math>J</math>, the following diagram | ||
+ | |||
+ | [[Image:ConeDefDiagram.png]] | ||
+ | |||
+ | commutes, or in equations, <math>D_\alpha c_i = c_j</math>. | ||
+ | |||
+ | A ''morphism'' <math>f:(C,c_i)\to(C',c'_i)</math> of cones is an arrow <math>f:C\to C'</math> such that each triangle | ||
+ | |||
+ | [[Image:ConeMorphismDiagram.png]] | ||
+ | |||
+ | commutes, or in equations, such that <math>c_j = c'_j f</math>. | ||
+ | |||
+ | This defins a category of cones, that we shall denote by <math>Cone(D)</math>. And we define, hereby: | ||
+ | |||
+ | '''Definition''' The ''limit'' of a diagram <math>D</math> in a category <math>C</math> is a terminal object in <math>Cone(D)</math>. We often denote a limit by | ||
+ | |||
+ | <math>p_i:\lim_{\leftarrow_j} D_j \to D_i</math> | ||
+ | |||
+ | so that the map from the limit object <math>\lim_{\leftarrow_j} D_j</math> to one of the diagram objects <math>D_i</math> is denoted by <math>p_i</math>. | ||
+ | |||
+ | The limit being terminal in the category of cones nails down once and for all the uniqueness of any map into it, and the isomorphism of any two terminal objects carries over to a proof once and for all for the limit case. | ||
+ | |||
+ | Specifically, since the morphisms of cones are morphisms in <math>C</math>, and composition is carried straight over, so proving a map is an isomorphism in the cone category implies it is one in the target category as well. | ||
+ | |||
+ | '''Definition''' A category <math>C</math> ''has all (finite) limits'' if all diagrams (of finite shape) have limit objects defined for them. | ||
+ | |||
+ | ====Limits we've already seen==== | ||
+ | |||
+ | The terminal object of a category is the limit object of an empty diagram. Indeed, it is an object, with ''no'' specified maps to ''no'' other objects, such that every other object that also maps to the same empty set of objects - which is to say all other objects - have a uniquely determined map to the limit object. | ||
+ | |||
+ | The product of some set of objects it she limit object of the diagram containing all these objects and no arrows; a diagram of the shape of a discrete category. The condition here becomes the requirement of maps to all factors so any other cone factors through these maps. | ||
+ | |||
+ | To express the exponential as a limit, we need to go to a different category than the one we started in. Take the category with objects given by morphisms <math>X\times Y\to Z</math> for fixed objects <math>Y,Z</math>, and morphisms given by morphisms <math>X\times Y\to X'\times Y</math> commuting with the 'objects' they run between and fixing <math>Y</math>. The exponential is a terminal object in this category. | ||
+ | |||
+ | Adding further arrows to diagrams amounts to adding further conditions on the products, as the maps from the product to the diagram objects need to factor through any arrows present in the diagram. | ||
+ | |||
+ | These added relations, however, is exactly what trips things up in Haskell. The idealized Haskell category does not have even all finite limits. At the core of the issue here is the lack of dependent types: there is no way for the type system to guarantee equations, and hence only the trivial limits - the products - can be guaranteed by the Haskell type checked. | ||
+ | |||
+ | In order to get that kind of guarantees, the type checker would need an implementation of [[Dependent type]], something that can be simulated in several ways, but is not (yet) an actual part of Haskell. Other languages, however, cover this - most notably Epigram, Agda and Cayenne - which the latter is much stronger influenced by constructve type theory and category theory even than Haskell. | ||
+ | |||
+ | The kind of equations that show up in a limit, however, could be thought of as invariants for the type - and thus something that can be tested for. The resulting equations can be plugged into a testing framework - such as [[Introduction to QuickCheck|QuickCheck]] to verify that the invariants hold under the functions applied. | ||
+ | |||
+ | ====Colimits==== | ||
+ | |||
+ | The dual concept to a limit is defined using the dual to the cones: | ||
+ | |||
+ | '''Definition''' A ''cocone'' over a diagram <math>D:J\to C</math> is an object <math>C</math> with arrows <math>c_j:D_j\to C</math> such that for each arrow <math>\alpha:i\to j</math> in <math>J</math>, the following diagram | ||
+ | |||
+ | [[Image:CoConeDefDiagram.png]] | ||
+ | |||
+ | commutes, or in equations, such that <math>c_jD_\alpha=c_i</math>. | ||
+ | |||
+ | A ''morphism'' <math>f:(C,c_i)\to (C',c'_i)</math> of cocones is an arrow <math>f:C\to C'</math> such that each triangle | ||
+ | |||
+ | [[Image:CoConeMorphismDiagram.png]] | ||
+ | |||
+ | commutes, or in equations, such that <math>c_j=c'_jf</math>. | ||
+ | |||
+ | Just as with the category of cones, this yields a category of cocones, that we denote by <math>Cocone(D)</math>, and with this we define: | ||
+ | |||
+ | '''Definition''' The ''colimit'' of a diagram <math>D:J\to C</math> is an initial object in <math>Cocone(D)</math>. | ||
+ | |||
+ | We denote the colimit by | ||
+ | |||
+ | <math>i_i:D_i\to\lim_{\rightarrow_j}D_j</math> | ||
+ | |||
+ | so that the map from one of the diagram objects <math>D_i</math> to the colimit object <math>\lim_{\rightarrow_j}D_j</math> is denoted by <math>i_i</math>. | ||
+ | |||
+ | Again, the isomorphism results for coproducts and initial objects follow from that for the colimit, and the same proof ends up working for all colimits. | ||
+ | |||
+ | And again, we say that a category ''has (finite) colimits'' if every (finite) diagram admits a colimit. | ||
+ | |||
+ | ====Colimits we've already seen==== | ||
− | + | The initial object is the colimit of the empty diagram. | |
− | + | The coproduct is the colimit of the discrete diagram. | |
− | + | For both of these, the argument is almost identical to the one in [[User:Michiexile/MATH198/Lecture 5#Limits we've already seen|the limits section]] above. | |
− | + | ||
− | |||
− | * | + | ===Homework=== |
+ | Credit will be given for up to 4 of the 6 exercises. | ||
+ | # Prove that currying/uncurrying are isomorphisms in a CCC. Hint: the map <math>f\mapsto\lambda f</math> is a map <math>Hom(C\times A, B)\to Hom(C,[A\to B])</math>. | ||
+ | # Prove that in a CCC <math>\lambda ev</math> is <math>\lambda ev = 1_{[A\to B]}: [A\to B] \to [A\to B]</math>. | ||
+ | # What is the limit of a diagram of the shape of the category 2? | ||
+ | # Is the category of Sets a CCC? Prove it. | ||
+ | # Is the category of vector spaces a CCC? Prove it. | ||
+ | # * Implement a typed lambda calculus as an EDSL in Haskell. |
Latest revision as of 06:32, 28 October 2009
Contents |
[edit] 1 Cartesian Closed Categories and typed lambda-calculus
A category is said to have pairwise products if for any objects A,B, there is a product object .
A category is said to have pairwise coproducts if for any objects A,B, there is a coproduct object A + B.
Recall when we talked about internal homs in Lecture 2. We can now define what we mean, formally, by the concept:
Definition An object C in a category D is an internal hom object or an exponential object or B^{A} if it comes equipped with an arrow , called the evaluation arrow, such that for any other arrow , there is a unique arrow such that the composite
is f.
The idea here is that with something in an exponential object, and something in the source of the arrows we imagine live inside the exponential, we can produce the evaluation of the arrow at the source to produce something in the target. Using global elements, this reasoning comes through in a more natural manner: given and we can produce the global element . Furthermore, we can always produce something in the exponential whenever we have something that looks as if it should be there.
And with this we can define
Definition A category C is a Cartesian Closed Category or a CCC if:
- C has a terminal object 1
- Each pair of objects has a product and projections , .
- For every pair of objects, there is an exponential object with an evaluation map .
[edit] 1.1 Currying
Note that the exponential as described here is exactly what we need in order to discuss the Haskell concept of multi-parameter functions. If we consider the type of a binary function in Haskell:
binFunction :: a -> a -> a
On the other hand, we can feed in both values at once, and get
binFunction' :: (a,a) -> a
which lives in the exponential object .
These are genuinely different objects, but they seem to do the same thing: consume two distinct values to produce a third value. The resolution of the difference lies, again, in a recognition from Set theory: there is an isomorphism
which we can use as inspiration for an isomorphism valid in Cartesian Closed Categories.
[edit] 1.2 Typed lambda-calculus
The lambda-calculus, and later the typed lambda-calculus both act as foundational bases for computer science, and computer programming in particular. The idea in both is that everything is a function, and we can reduce the act of programming to function application; which in turn can be analyzed using expression rewriting rules that encapsulate the act of computation in a sequence of formal rewrites.
Definition A typed lambda-calculus is a formal theory with types, terms, variables and equations. Each term a has a type A associated to it, and we write a:A or . The system is subject to a sequence of rules:
- There is a type 1. Hence, the empty lambda calculus is excluded.
- If A,B are types, then so are and . These are, initially, just additional symbols, not imbued with the associations we usually give the symbols used.
- There is a term * :1. Hence, the lambda calculus without any terms is excluded.
- For each type A, there is an infinite (countable) supply of terms .
- If a:A,b:B are terms, then there is a term .
- If then there are terms proj_{1}(c):A,proj_{2}(c):B.
- If a:A And , then there is a term fa:B.
- If x:A is a variable and φ(x):B is a term, then there is a . Note that here, φ(x) is a meta-expression, meaning we have SOME lambda-calculus expression that may include the variable x.
- There is a relation a = _{X}a' for each set of variables X that occur freely in either a or a'. This relation is reflexive, symmetric and transitive. Recall that a variable is free in a term if it is not in the scope of a λ-expression naming that variable.
- If a:1 then a = _{{}} * . In other words, up to lambda-calculus equality, there is only one value of type * .
- If , then a = _{X}a' implies a = _{Y}a'. Binding more variables gives less freedom, not more, and thus cannot suddenly make equal expressions differ.
- a = _{X}a' implies fa = _{X}fa'.
- f = _{X}f' implies fa = _{X}f'a. So equality plays nice with function application.
- implies λ_{x}φ(x) = _{X}λ_{x}φ'(x). Equality behaves well with respect to binding variables.
- proj_{1}(a,b) = _{X}a, proj_{2}(a,b) = _{X}b, c = _{X}(proj_{1}(c),proj_{2}(c)) for all a,b,c,X.
- λ_{x}φ(x)a = _{X}φ(a) if a is substitutable for x in φ(x) and φ(a) is what we get by substituting each occurrence of x bya in φ(x). A term is substitutable for another if by performing the substitution, no occurrence of any variable in the term becomes bound,
- , provided .
- if x' is substitutable for x in φ(x) and each variable is not free in the other expression.
Note that = _{X} is just a symbol. The axioms above give it properties that work a lot like equality, but two lambda calculus-equal terms are not equal unless they are identical. However, a = _{X}b tells us that in any model of this lambda calculus - where terms, types, et.c. are replaced with actual things (mathematical objects, say, or a programming language semantics embedding typed lambda calculus) - then the things given by translating a and b into the model should end up being equal.
Any actual realization of typed lambda calculus is bound to have more rules and equalities than the ones listed here.
With these axioms in front of us, however, we can see how lambda calculus and Cartesian Closed Categories fit together: We can go back and forth between the wo concepts in a natural manner:
[edit] 1.2.1 Lambda to CCC
Given a typed lambda calculus L, we can define a CCC C(L). Its objects are the types of L. An arrow from A to B is an equivalence class (under = _{{x}}) of terms of type B, free in a single variable x:A.
We need the equivalence classes because for any variable x:A, we want to be the global element of corresponding to the identity arrow. Hence, that variable must itself correspond to an identity arrow.
And then the rules for the various constructions enumerated in the axioms correspond closely to what we need to prove the resulting category to be cartesian closed.
[edit] 1.2.2 CCC to Lambda
To go in the other direction, starting out with a Cartesian Closed Category and finding a typed lambda calculus corresponding to it, we construct its internal language.
Given a CCC C, we can assume that we have chosen, somehow, one actual product for each finite set of factors. Thus, both all products and all projections are well defined entities, with no remaining choice to determine them.
The types of the internal language L(C) are just the objects of C. The existence of products, exponentials and terminal object covers axioms 1-2. We can assume the existence of variables for each type, and the remaining axioms correspond to definition and behaviour of the terms available.
Using the properties of a CCC, it is at this point possible to prove a resulting equivalence of categories C(L(C)) = C, and similarly, with suitable definitions for what it means for formal languages to be equivalent, one can also prove for a typed lambda-calculus L that L(C(L)) = L.
More on this subject can be found in:
- Lambek & Scott: Aspects of higher order categorical logic and Introduction to higher order categorical logic
More importantly, by stating λ-calculus in terms of a CCC instead of in terms of terms and rewriting rules is that you can escape worrying about variable clashes, alpha reductions and composability - the categorical translation ignores, at least superficially, the variables, reduces terms with morphisms that have equality built in, and provides associative composition for free.
At this point, I'd recommend reading more on Wikipedia [1] and [2], as well as in Lambek & Scott: Introduction to Higher Order Categorical Logic. The book by Lambek & Scott goes into great depth on these issues, but may be less than friendly to a novice.
[edit] 2 Limits and colimits
One design pattern, as it were, that we have seen occur over and over in the definitions we've seen so far is for there to be some object, such that for every other object around, certain morphisms have unique existence.
We saw it in terminal and initial objects, where there's a unique map from or to every other object. And in products/coproducts where a wellbehaved map, capturing any pair of maps has unique existence. And finally, above, in the CCC characterization of the internal hom, we had a similar uniqueness requirement for the lambda map.
One thing we can notice is that the isomorphisms theorems for all these cases look very similar to each other: in each isomorphism proof, we produce the uniquely existing morphisms, and prove that their uniqueness and their other properties force the maps to really be isomorphisms.
Now, category theory has a philosophy slightly similar to design patterns - if we see something happening over and over, we'll want to generalize it. And there are generalizations available for these!
[edit] 2.1 Diagrams, cones and limits
Definition A diagram D of the shape of an index category J (often finite or countable), in a category C is just a functor . Objects in J will be denoted by i,j,k,... and their images in C by D_{i},D_{j},D_{k},....
This underlines that when we talk about diagrams, we tend to think of them less as just functors, and more as their images - the important part of a diagram D is the objects and their layout in C, and not the process of going to C from D.
Definition A cone over a diagram D in a category C is some object C equipped with a family of arrows, one for each object in J, such that for each arrow in J, the following diagram
commutes, or in equations, D_{α}c_{i} = c_{j}.
A morphism of cones is an arrow such that each triangle
commutes, or in equations, such that c_{j} = c'_{j}f.
This defins a category of cones, that we shall denote by Cone(D). And we define, hereby:
Definition The limit of a diagram D in a category C is a terminal object in Cone(D). We often denote a limit by
so that the map from the limit object to one of the diagram objects D_{i} is denoted by p_{i}.
The limit being terminal in the category of cones nails down once and for all the uniqueness of any map into it, and the isomorphism of any two terminal objects carries over to a proof once and for all for the limit case.
Specifically, since the morphisms of cones are morphisms in C, and composition is carried straight over, so proving a map is an isomorphism in the cone category implies it is one in the target category as well.
Definition A category C has all (finite) limits if all diagrams (of finite shape) have limit objects defined for them.
[edit] 2.2 Limits we've already seen
The terminal object of a category is the limit object of an empty diagram. Indeed, it is an object, with no specified maps to no other objects, such that every other object that also maps to the same empty set of objects - which is to say all other objects - have a uniquely determined map to the limit object.
The product of some set of objects it she limit object of the diagram containing all these objects and no arrows; a diagram of the shape of a discrete category. The condition here becomes the requirement of maps to all factors so any other cone factors through these maps.
To express the exponential as a limit, we need to go to a different category than the one we started in. Take the category with objects given by morphisms for fixed objects Y,Z, and morphisms given by morphisms commuting with the 'objects' they run between and fixing Y. The exponential is a terminal object in this category.
Adding further arrows to diagrams amounts to adding further conditions on the products, as the maps from the product to the diagram objects need to factor through any arrows present in the diagram.
These added relations, however, is exactly what trips things up in Haskell. The idealized Haskell category does not have even all finite limits. At the core of the issue here is the lack of dependent types: there is no way for the type system to guarantee equations, and hence only the trivial limits - the products - can be guaranteed by the Haskell type checked.
In order to get that kind of guarantees, the type checker would need an implementation of Dependent type, something that can be simulated in several ways, but is not (yet) an actual part of Haskell. Other languages, however, cover this - most notably Epigram, Agda and Cayenne - which the latter is much stronger influenced by constructve type theory and category theory even than Haskell.
The kind of equations that show up in a limit, however, could be thought of as invariants for the type - and thus something that can be tested for. The resulting equations can be plugged into a testing framework - such as QuickCheck to verify that the invariants hold under the functions applied.
[edit] 2.3 Colimits
The dual concept to a limit is defined using the dual to the cones:
Definition A cocone over a diagram is an object C with arrows such that for each arrow in J, the following diagram
commutes, or in equations, such that c_{j}D_{α} = c_{i}.
A morphism of cocones is an arrow such that each triangle
commutes, or in equations, such that c_{j} = c'_{j}f.
Just as with the category of cones, this yields a category of cocones, that we denote by Cocone(D), and with this we define:
Definition The colimit of a diagram is an initial object in Cocone(D).
We denote the colimit by
so that the map from one of the diagram objects D_{i} to the colimit object is denoted by i_{i}.
Again, the isomorphism results for coproducts and initial objects follow from that for the colimit, and the same proof ends up working for all colimits.
And again, we say that a category has (finite) colimits if every (finite) diagram admits a colimit.
[edit] 2.4 Colimits we've already seen
The initial object is the colimit of the empty diagram.
The coproduct is the colimit of the discrete diagram.
For both of these, the argument is almost identical to the one in the limits section above.
[edit] 3 Homework
Credit will be given for up to 4 of the 6 exercises.
- Prove that currying/uncurrying are isomorphisms in a CCC. Hint: the map is a map .
- Prove that in a CCC λev is .
- What is the limit of a diagram of the shape of the category 2?
- Is the category of Sets a CCC? Prove it.
- Is the category of vector spaces a CCC? Prove it.
- * Implement a typed lambda calculus as an EDSL in Haskell.