{"id":110,"date":"2006-08-10T14:33:25","date_gmt":"2006-08-10T14:33:25","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2006\/08\/10\/from-lambda-calculus-to-cartesian-closed-categories\/"},"modified":"2006-08-10T14:33:25","modified_gmt":"2006-08-10T14:33:25","slug":"from-lambda-calculus-to-cartesian-closed-categories","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2006\/08\/10\/from-lambda-calculus-to-cartesian-closed-categories\/","title":{"rendered":"From Lambda Calculus to Cartesian Closed Categories"},"content":{"rendered":"<p>This is one of the last posts in my series on category theory; and it&#8217;s a two parter. What I&#8217;m going to do in these two posts is show the correspondence between lambda calculus and the [cartesian closed categories][ccc]. If you&#8217;re not familiar with lambda calculus, you can take a look at my series of articles on it [here][lambda].  You might also want to follow the CCC link above, to remind yourself about the CCCs. (The short refresher is: a CCC is a category that has an exponent and a product, and is closed over both. The product is an abstract version of cartesian set product; the exponent is an abstraction of the idea of a function, with an &#8220;eval&#8221; arrow that does function evaluation.)<br \/>\nToday I&#8217;m going to show you one half of the correspondence: how the lambda-theory of any simply typed lambda calculus can be mapped onto a CCC. Tomorrow, I&#8217;ll show the other direction: how an arbitrary CCC can be mapped onto a lambda-theory.<br \/>\nFirst, let&#8217;s define the term &#8220;lambda theory&#8221;. In the simply typed lambda calculus, we always have a set of *base types* &#8211; the types of simple atomic values that can appear in lambda expressions. A *lambda theory* is a simply typed lambda calculus, plus a set of additional rules that define equivalences over the base types.<br \/>\nSo, for example, if one of the base types of a lambda calculus was the natural numbers, the lambda theory would need to include rules to define equality over the natural numbers:<br \/>\n1. x = y if x=0 and  y=0; and<br \/>\n2. x = y if x=s(x&#8217;) and y=s(y&#8217;) and x&#8217; = y&#8217;<br \/>\nSo. Suppose we have a lambda-theory **L**. We can construct a corresponding category C(**L**). The objects in C(**L**) are the *types* in **L**. The arrows in C(**L**) correspond to *families* of expressions in **L**; an arrow f : A &rarr; B corresponds to the set of expressions of type B that contain a single *free* variable of type A.<br \/>\nThe *semantics* of the lambda-theory can be defined by a *functor*; in particular, a *cartesian closed* functor F  that maps from C(**L**) to the closed cartesian category of Sets. (It&#8217;s worth noting that this is completely equivalent to the normal Kripke semantics for lambda calculus; but when you get into more complex lambda calculi, like Hindley-Milner variants, this categorical formulation is *much* simpler.)<br \/>\nWe describe how we build the category for the lambda theory in terms of a CCC using something called an *interpretation function*. It&#8217;s really just a notation that allows us to describe the translation recursively. The interpretation function is written using brackets: [A] is the categorical interpretation of the type A from lambda calculus.<br \/>\nSo, first, we define an object for each type in **L**, including &#8220;unit&#8221;, which is a special distinguished value used for an expression that doesn&#8217;t return anything:<br \/>\n* &forall; A &isin; basetypes(**L**), [A] = A<sub>C<\/sub> &isin; C(**L**)<br \/>\n* [unit] = 1<sub>C<\/sub><br \/>\nNext, we need to define the typing rules for complex types:<br \/>\n* [ A &times; B ] = [A] &times; [B]<br \/>\n* [ A &rarr; B ] = [B]<sup>[A]<\/sup><br \/>\nNow for the really interesting part. We need to look at type derivations  &#8211; that is, the type inference rules of the  lambda calculus &#8211; to show how to do the correspondences between more complicated expressions. Type derivations are done with a *context* containing a set of *type judgements*. Each type judgement assigns a type to a lambda term. We write type contexts as capital greek letters like &Gamma;. There are two translation rules for contexts:<br \/>\n* [&empty;] = 1<sub>C<\/sub><br \/>\n* [&Gamma;, x : A] = [&Gamma;] &times; [A]<br \/>\nWe also need to describe what to do with the values of the primitive types:<br \/>\n* For each value v : A, there is  an arrow v : 1 &rarr; A<sub>C<\/sub>.<br \/>\nAnd now the rest of the rules. Each of these is of the form [&Gamma; :- x : A] = arrow, where we&#8217;re saying that &Gamma; entails the type judgement &#8220;x : A&#8221;. What it means is the object corresponding to the type information covering a type inference for an expression corresponds to the arrow in C(**L**).<br \/>\n* [&Gamma; :- unit : Unit] = ! : [&Gamma;] &rarr; [Unit] *(A unit expression is a special arrow &#8220;!&#8221; to the Unit object)*<br \/>\n* [&Gamma; :- a : A<sub>C<\/sub>] = a &ordm; ! : [&Gamma;] &rarr; [A<sub>C<\/sub>] *(A simple value expression is an arrow composing with ! to form an arrow from &Gamma; to the type object of Cs type.)*<br \/>\n* [&Gamma; x : A :- x : A] = &pi;<sub>2<\/sub> : ([&Gamma; ] &times; [A]) &rarr; [A] *(A term which is a free variable of type A is an arrow from the product of &Gamma; and the type object A to A; That is, an unknown value of type A is some arrow whose start point will be inferred by the continued interpretation of gamma, and which ends at A. So this is going to be an arrow from either unit or a parameter type to A &#8211; which is a statement that this expression evaluates to a value of type A.)*<br \/>\n* [&Gamma;, x:A :- x&#8217; : A&#8217;],x&#8217; &ne; x = [&Gamma; :- x&#8217; : A&#8217;]  &ordm; &pi;<sub>1<\/sub> where &pi;<sub>1<\/sub> : ([&Gamma;] &times; [A]) &rarr; [A&#8217;]. *(If the type rules of &Gamma; plus the judgement x : A gives us x&#8217; : A&#8217;, then the term x&#8217; : A&#8217; is an arrow starting from the product of the interpretation of the full type context with A), and ending at A&#8217;. This is almost the same as the previous rule: it says that this will evaluate to an arrow for an expression that results in type A.)*<br \/>\n* [&Gamma; :- &lambda; x:A . M : A &rarr; B] = curry([&Gamma;, x:A :- M:B]) : [&Gamma;] &rarr; [B]<sup>[A]<\/sup> *(A function typing rule: the function maps to an arrow from the type context to an exponential [B]<sup>[A]<\/sup>, which is a function from A to B.)*<br \/>\n* [&Gamma; :- (M M&#8217;) : B] = eval<sub>C,B<\/sub> &ordm; ([&Gamma; :- M : C &rarr; B], [&Gamma; :- M&#8217; : C]) : [&Gamma;] &rarr; [B] *(A function evaluation rule; it takes the eval arrow from the categorical exponent, and uses it to evaluate out the function.)*<br \/>\nThere are also two projection rules for the decomposing categorical products, but they&#8217;re basically more of the same, and this is already dense enough.<br \/>\nThe intuition behind this is:<br \/>\n* *arrows* between types are families of values. A particular value is a particular arrow from unit to a type object.<br \/>\n* the categorical exponent *is* the same as a function type; a function *value* is an arrow to the type. Evaluating the function is using the categorical exponent&#8217;s eval arrow to &#8220;decompose&#8221; the exponent, and produce an arrow to the function&#8217;s result type; *that* arrow is the value that the function evaluates to.<br \/>\nAnd the semantics &#8211; called functorial semantics &#8211; maps from the objects in this category, C(**L**) to the category of Sets; function values to function arrows; type objects to sets; values to value objects and arrows. (For example, the natural number type would be an object like the NNO we showed yesterday in C(**L**), and the set of natural numbers is the sets category would be the target of the functor.)<br \/>\nAside from the fact that this is actually a very clean way of defining the semantics of a not-so-simply typed lambda calculus, it&#8217;s also very useful in practice. There is a way of executing lambda calculus expressions as programs that is based on this, called the [Categorical Abstract Machine][cam].  The best performing lambda-calculus based programming language (and my personal all-time-favorite programming language), [Objective-CAML][caml] is based on the CAM. (CAML stands for categorical abstract machine language.)<br \/>\n[cam]: http:\/\/portal.acm.org\/citation.cfm?id=34883&amp;dl=ACM&amp;coll=portal<br \/>\n[caml]: http:\/\/caml.inria.fr\/<br \/>\n[lambda]: http:\/\/goodmath.blogspot.com\/2006\/06\/lamda-calculus-index.html<br \/>\n[ccc]: http:\/\/scienceblogs.com\/goodmath\/2006\/06\/categories_products_exponentia_1.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is one of the last posts in my series on category theory; and it&#8217;s a two parter. What I&#8217;m going to do in these two posts is show the correspondence between lambda calculus and the [cartesian closed categories][ccc]. If you&#8217;re not familiar with lambda calculus, you can take a look at my series of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[76,24],"tags":[],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-category-theory","category-goodmath"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-1M","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}