Mercurial > dylan
view categorical/morphisms.org @ 9:23db8b1f0ee7
Softened tone in science minus science.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Sat, 29 Oct 2011 21:18:54 -0500 |
parents | b4de894a1e2e |
children |
line wrap: on
line source
1 #+TITLE: Categorical Programs2 #+AUTHOR: Dylan Holmes4 #+STYLE: <link rel="stylesheet" type="text/css" href="../css/aurellem.css" />6 #+BEGIN_HTML7 <h1>{{{title}}}</h1>8 #+END_HTML10 * The Category of Types11 Every computer language with data types (such as Integers,12 Floats, or13 Strings) corresponds with a certain category; the objects of the category are the data types14 of the language, and there is one arrow between data types $A$ and $B$15 for every function in the language whose argument has type $A$ and whose return value16 has type $B$.18 #+begin_src clojure :results silent19 (ns categorical.morphisms)20 #+end_src22 ** Building new data types out of existing ones24 We will now discuss two processes which combine existing25 data types to yield composite data types. These higher-order processes will be26 /functorial/, which means that they will recombine not only the objects of27 our category (the data types), but the arrows as well (functions with28 typed input and output).30 The =product= functor combines two data types, $A$ and $B$, producing31 a new type, denoted $A\times B$. Conceptually, values with the data type $A\times32 B$ have two components, the first of which is a value of type $A$, the33 second of which is a value of type $B$. Concretely, the data type $A\times B$34 can be implemented as pairs of values, the first of which is35 of type $A$ and the second of which is of type $B$. With this36 implementation in mind, you can see that the type $A$ and type $B$37 constituents of type $A\times B$ value can be recovered; the so-called38 projection functors =first= and =second= recover them. A third39 functor, the =diagonal= functor, completes our toolkit; given40 value $a$ of type $A$, it produces the value $(a,a)$ of type $A\times A$.42 #+begin_src clojure :results silent43 (defn product[a b] (list a b))44 (def projections (list first second))45 (defn diagonal[a] (product a a))46 #+end_src48 The =coproduct= functor combines two data types, $A$ and $B$,49 producing a new type, denoted $A+B$. Conceptually, you construct the50 $A+B$ datatype by creating labelled versions of the data types $A$ and51 $B$ \mdash{} we'll call them black-$A$ and white-$B$ \mdash{} and52 then creating a data type $A+B$ which contains all the black-$A$53 values and all the white-$B$ values. Concretely, you can implement the54 $A+B$ data type using label-value pairs: the first value in the pair55 is either the label `black' or the label `white'; the second56 value must have type $A$ if the label is black, or type $B$ if the57 label is white.59 Now, the =product= functor came with projection functors =first=60 and =second= that take $A\times B$ values and return (respectively) $A$ values and61 $B$ values. The =coproduct= functor, conversely, comes with two62 injection functors =make-black= and =make-white= that perform the63 opposite function: they take (respectively) $A$ values and $B$ values,64 promoting them to the $A+B$ datatype by making the $A$'s into65 black-$A$'s and the $B$'s into white-$B$'s.67 #the =coproduct= functor comes with two injection functors =make-black=68 #and =make-white= that take (respectively) $A$ values and $B$ values,69 #promoting them to the $A+B$ datatype by making them into black-$A$'s70 #or white-$B$'s.73 #The =coproduct= functor combines two data types, $A$ and $B$,74 #producing a new type, denoted $A+B$. Conceptually, values with the75 #data type $A+B$ can be either values with type $A$ that are designated /of the first76 #kind/, or values with type $B$ that are designated /of the second77 #kind/. Concretely, the data type $A+B$ can be implemented using pairs78 #of values in conjunction with a labelling convention; the first value79 #in the pair is of either type $A$ or $B$, and the second value is a label80 #indicating whether it is of the first or second kind. Our labelling81 #scheme uses the keywords =:black= and =:white= for this purpose.