User:Michiexile/MATH198/Lecture 5

From HaskellWiki
Jump to navigation Jump to search

IMPORTANT NOTE: THESE NOTES ARE STILL UNDER DEVELOPMENT. PLEASE WAIT UNTIL AFTER THE LECTURE WITH HANDING ANYTHING IN, OR TREATING THE NOTES AS READY TO READ.


Cartesian Closed Categories and typed lambda-calculus

A category is said to have pairwise products if for any objects , there is a product object .

A category is said to have pairwise coproducts if for any objects , there is a coproduct object .

Recall when we talked about internal homs in Lecture 2. We can now define what we mean, formally, by the concept:

Definition An object in a category is an internal hom object or an exponential object or 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 .

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 is a Cartesian Closed Category or a CCC if:

  1. has a terminal object
  2. Each pair of objects has a product and projections , .
  3. For every pair of objects, there is an exponential object with an evaluation map .

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

This function really lives in the Haskell type a -> (a -> a), and thus is an element in the repeated exponential object . 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

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.


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 has a type associated to it, and we write or . The system is subject to a sequence of rules:

  1. There is a type . Hence, the empty lambda calculus is excluded.
  2. If are types, then so are and . These are, initially, just additional symbols, not imbued with the associations we usually give the symbols used.
  3. There is a term . Hence, the lambda calculus without any terms is excluded.
  4. For each type , there is an infinite (countable) supply of terms .
  5. If are terms, then there is a term .
  6. If then there are terms .
  7. If And , then there is a term .
  8. If is a variable and is a term, then there is a . Note that here, is a meta-expression, meaning we have SOME lambda-calculus expression that may include the variable .
  9. There is a relation for each set of variables that occur freely in either or . 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.
  10. If then . In other words, up to lambda-calculus equality, there is only one value of type .
  11. If , then implies . Binding more variables gives less freedom, not more, and thus cannot suddenly make equal expressions differ.
  12. implies .
  13. implies . So equality plays nice with function application.
  14. implies . Equality behaves well with respect to binding variables.
  15. , , for all .
  16. if is substitutable for in and is what we get by substituting each occurrence of by in . A term is substitutable for another if by performing the substitution, no occurrence of any variable in the term becomes bound,
  17. .
  18. if is substitutable for in and each variable is not free in the other expression.

Note that 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, 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 and 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:

CCC to Lambda

Given a typed lambda calculus , we can define a CCC . Its objects are the types of . An arrow from to is an equivalence class (under ) of terms of type , free in a single variable .

We need the equivalence classes because for any variable , 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.

Lambda to CCC

More on this subject can be found in:

  • Lambek & Scott: Aspects of higher order categorical logic and Introduction to higher order categorical logic

As it turns out, this is exactly what we need for -calculus. Any typed -calculus gives rise to a CCC in a natural manner, and any CCC has an internal language which satisfies, by the axioms for the CCC, all requirements to be a typed -calculus.

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.


Limits and colimits

  • Generalizing these constructions
  • Diagram and universal object mapping to (from) the diagram
  • Express product/coproduct as limit/colimit
  • Issues with Haskell
    • No dependent types
    • No compiler-enforced equational conditions
    • Can be simulated but not enforced, e.g. using QuickCheck.

Useful limits and colimits

Equalizer, coequalizer
  • Kernels, cokernels, images, coimages
    • connect to linear algebra: null spaces et.c.
Pushout and pullback squares
  • Computer science applications


Homework

  1. Prove that currying/uncurrying are isomorphisms in a CCC. Hint: the map is a map .
  2. Prove that in a CCC, the composition is .
  3. * Implement a typed lambda calculus as an EDSL in Haskell.