You are on page 1of 20

Application of Category Theory to Computer Science This is a list of miscellaneous applications of Category Theory to Computer Science, mostly based

on examples given during the lecture [7] but also on some ideas I found elsewhere. The references that I used to explain or imagine these applications are given at the end of the document. 1. Concurrency ([7]) We have seen an overview of the category of transition systems TS in exercise A3. A transition system can be used to describe concurrency of parallel programs: the states of the set of processes (states) and the way it may evolve (transitions). Another example has been mentioned during the lecture: the synchronization trees ST. It is a subcategory of TS, where we restrict ourself to graphs of State/Transition that are trees. This is a very specific kind of concurrency, where the set of processes may move to a new state but never come back to an old one.

A synchronization tree

One of a very widespread model for concurrency is given by Petri Nets. They can be represented by an oriented graph, where we distinguish two kinds of nodes, square and circle shaped. There are small balls that can move in the circle nodes and split/merge with respect to some particular rules. The position of the balls corresponds to the states of the system and the restriction on their movements prevent conflicts between the concurrent processes.

A petri net that describes parallel execution of B and C

Again, Petri Nets are objects of a category and we may find appropriate morphisms between Petri Nets as arrows. There are several other concurrency models with their own characteristics (scope of application, semantics...). Category Theory provides a uniform framework that is very convenient. 2. Functional programming ([6]) Suppose we have a pipeline of functions, each one using two operations: return that puts a value into a container and bind that extracts the value and sends it to the successor in the pipeline. In functionnal programming, there exists an abstract data structure used to transmit information between the functions. This structure is called a "monad" because the two operations roughly correspond to the unit and the counit of a monad in Category Theory. As an example, consider the category Set and the powerset functor defined by ( f ) ( X

) = f ( x )

x X . One can verify that this functor defines a monad on Set with unit and counit:

S :

S ( S )

x { x }

and

S :

( ( S ) ) ( S )

x X x

From t ese functions we get t e two operations mentioned above: return: A PA x A(x)

bind: PA (A PB) PB (X , f ) B({fx, xX}) Here, t e return operation allows to pack an element x of "type" A in a set {x}. T e bind operation unpacks a set of elements of type A, applies an operation f to t ese objects and transmits a new set of elements of "type" B. T e c oice of t e function f and t e generalization to ot er monad functors t an t e powerset provides a wide range of computation met ods for functionnal programming. 3. Adjoint functors and Monads ([7]) Adjoint functors and Monads are two fundamental concepts of Category T eory t at ave many applications in Computer Science. We ave already seen an application of monad in t e previous section, so let us look at t e related concept of adjunction. Consider t e categories Set and Mon. We ave an obvious "forgetful" functor G from Mon to Set.

Set

Mon

In Computer Science, we often ave a set of symbols and we build the language of all the words over this alphabet

* . Doing so, we get a monod with the empty word as th id ntity and concat nation as int rnal law. This transformation F : * is actually a functor which is adjoint to G. Anoth r xampl is giv n by th cat gory T and T of th first s ction. Th functor inclusion and th "unfolding" of transition syst ms form an adjunction! 4. Compl t Partial Ord r ([7])

d 0 d 1 d 2 . . . has a l ast upp r bound

sup i

d i . We can endo the class of complete partial order ith a structure of

A partial ord r is said to b compl t if s qu nc of l m nts

v ry incr asing

category as follo s: the objects are the CPO's and the arro s are the functions that are increasing and continuous (in the sense that f (

sup i

d i

) = sup i f ( d i ) ). CPO are very common in Computer Science and Category Theory gives a nice picture that permits to understand and solve many problems. For instance, one often considers a structure of CPO ( D , ) and ants to find a fixed point of a given function f from D to itself.

If f is an arro for the category previously defined and D additionnaly has a bottom element then it's easy to sho that fixf =

sup i

( f i ( ) ) is such a fixed point.

5. Construction of a model of -ca cu us ([2], [7]) In -ca cu us, objects behave both as functions and arguments. Hence in order to construct a mode D of -ca cu us, we need a way to "identify" e ements of D with functions from D to itse f. If D has severa e ements, then there is no one-to-one map from D onto D D (by Cantor's theorem). However, on y a imited subset of the functions from D to itse f is rea y interesting in the context of Computer Science. In 1969, Scott was ab e to produce such an "identification" by restricting to continuous functions in the category of comp ete attices. Another approach is to choose the continuous function for the CPO category described above. In any case, we get a map : D D D

x , y D , x ( y ) can be understood as ( x ) ( y ) . 6. The Presheaf Category ([7]) We saw that it is possib e to define a category [ C op , Set ] with functor as objects and natura transformations as arrows. Let us first consider the case where Set is rep aced by the category 2 = ( { 0 , 1 } , {

. Then, for a

0 0 , 0 1 , 1 1 } ) . An object of [ C op , 2 ] is a functor t at assigns to eac element c C a value 0 or 1, so can be viewed as t e c aracteristic function of a subset of C. Note t at suc a functor must "preserve" arrows in C op , so we ave an additional control on t e subsets of C. Moreover, we can replace t e boolean values { 0 , 1 } by any ot er category so t at we can refine t e description of "appartenance" to a subset. Consider for example "fuzzy logic" w ere we would replace { 0 , 1 }

by t e interval [ 0 , 1] to get a probability of "appartenance".

aa

ab

ba

bb

aa a

To illustrat how this g n ralization can b us d in comput r sci nc , l t

C = { a , b } *

of th ways to "g n rat " it. A word w ' that xt nds anoth r on

w ' to w

F : w S w is an object of [ C op , Set

. But this

xactly m ans that

w is going to us th sam ways to g n rat so w should hav a function from

and probably additional on s,

with arrows that indicat ach word w th s t

xt nsion of words. Th n w

can assign to

] ! 7. Categorical Logic ([5], [7]) Logic is an essential tool of Computer Science in various fields suc t at formal met ods, automated t eorem proving or artificial intelligence. T e different applications may not ave all t e same requirements and consequently several kinds of logic ave been invented. One benefit of categorical logic is to permit dealing wit all t ese logical systems in a unified fas ion. In order to give an example w ere Category T eory is useful in logic, let's consider t e definition of Kripke semantics. One can find t is kind of representation in modal logics, intuitionistic logic or even in t e met od of Forcing in Set T eory. We ave a category w ere t e objects and t e arrows are respectively "t e worlds" and "t e accessibility relation" in t e terminology of Kripke semantics. To eac world, we may assign t e set of formulae that are true in this world. The accessibility relation implies certain kinds o relation between the sets assigned to a world. Hence Category Theory gives an interpretation similar to the one we saw in previous section: the "words" are replaced by the "worlds" and the "ways to generate a word" by the " ormulae that hold in a world". 8. B-method ([1]) B-method is a ormal method or so tware development generally used or sa ety-critical system (such that the Ligne 14 du mtro de Paris, with a ully automated driving). The idea is to design an abstract machine with variables and operations so that it is possible to generate certi ied code (i.e. program whose correctness with respect to the speci ication is proved ormally). Below is an example o a simple B machine that deals with booking o seats. Note that the INITIALISATION makes the INVARIANT true. Also, each operation has PREconditions that ensure that the INVARIANT remains true. MACHINE booking(nb_seats) VARIABLES available_seats INVARIANT available_seats[1;max_seats] INITIALISATION available_seats := [1;max_seats] OPERATIONS book i = PRE iavailable_seats THEN available_seats := available_seats \ {i} END; cancel i = PRE i([1;max_seats]\available_seats) THEN available_seats := available_seats {i} END; The implementation o this machine does not seem straight orward. One o the main eature o the B-method is that it allows re inement o machine rom the really abstract speci ication until something very close to actual programming languages, so that automated generators o code can be used. For instance, consider the re inement below. The available seats are now represented by a boolean unction that can direcly set its values to true or alse. This is

more similar to tables o booleans (that exist in programming languages) than the abstract notion o set o seat numbers. Note that the "glue" invariant gives a relation between old and new variables and remains true during the execution o the machine. REFINEMENT booking2 REFINES booking(nb_seats) VARIABLES available_seats2 INVARIANT i[1;max_seats] available_seats2(i) = (i available_seats) INITIALISATION available_seats2 := [1;max_seats]{TRUE} OPERATIONS book i = PRE available_seats2(i) THEN available_seats2(i) := FALSE END; cancel i = PRE available_seats2(i) THEN available_seats2(i) := TRUE END; Where is Category Theory involved? First, the B-method uses sets to represent objects. This can be handle as the Set category but one can imagine an extension o the B-method where we use another kind o category. Next, the B-method requires a lot o logics. We have to ensure that the INVARIANTs are satis ied a ter INITIALISATION and preserved by each operations, ensure that the semantics o the machines are preserved by re inement... O course in this simple example it is really trivial to check these conditions but or complex machines we use automated proo s. It is also necessary to de ine semantics or ormulae and machines. As we have seen in section 7, Category Theory is really help ul when working with logic. Finally, B-method can be understood in a categoric ramework, using the same techniques as we saw in section 1. The objects are the B-machines and we can de ine arrows as re inements (actually the B-method contains other kinds o relation such that those given by the keywords SEES or INCLUDES but we ignore them or the sake o simplicity).

(Note: In this section, we assume amiliarity with the XML syntax and related ormats. Also, we simpli y the structure o XML documents, or instance by ignoring attributes or text nodes) XML is a generic ormat o documents that can be represented as trees. One de ines a amily o XML documents by restricting the syntax allowed (and generally adding a semantic). For example, XHTML is a amily o XML documents used or Web pages: it contains speci ic nodes such that <a/> to describes hyperlinks, <table/>, <tr/>, <td/> to describe tables etc. There exists a lot o XML ormats used or various applications. It is possible to trans orm an XML document to another one. For example, consider the ollowing XML document that gives the price o di erent items A, B, C. <?xml version="1.0" encoding="UTF-8"?> <root>

9. XML

amilies [[3], [4]]

<item> <name>A</name> <price>10 </price> </item> <item> <name>B</name> <price>5 </price> </item> <item> <name>C</name> <price>18 </price> </item> </root> This XML document is use ul as a storage ormat but i one wants to display the document in a browser, then a conversion into an XHTML document is needed (we do not give all the mandatory XHTML elements: <body/>...) <?xml version="1.0" encoding="UTF-8"?> <html> ... <table> <tr><td>Name</td><td>Price</td></tr> <tr><td>A</td><td>10 </td></tr> <tr><td>B</td><td>5 </td></tr> <tr><td>C</td><td>18 </td></tr> </table> ... </html> This trans ormation converts a amily o XML documents (here the one that describes prices o items) into another amily (here a sub amily o XHTML). Hence we can de ine a category with XML amilies as objects and XSLT trans ormations as arrows (XSLT is a amily o XML document that represent trans ormations, so the sets o arrows in our category is actually itsel an object o the category!). We have not described precisely how an XML amily is de ined. There are several schema languages to do this, such that DTD, XML Schema and RELAX NG. XML amily describable by RELAX NG is always trans ormed by XSLT into another amily describable by RELAX NG. Hence The sets o XML amilies describable by RELAX NG is a subcategory (I have not checked whether this also holds or DTD and XML Schema). In order to study these di erent schemas, Murata et al. introduce a generalization o context- ree grammars or trees. For example, the initial amily o items can be described by the grammar (see the paper or more details): NonTerminalSymbols = {Root, Item, Name, Price, Pcdata} TerminalSymbols = {root, item, name, price, pcdata} StartSymbols = {Root} Rules = { Rootroot[Item*], Itemitem[NamePrice], Namename[Pcdata], Priceprice[Pcdata], Pcdatapcdata[] } A grammar d scribing th transform d docum nts is (again, without d tails): NonT rminal ymbols = {Html, ... Tabl , Tr, Td1, Td2, Nam , Pric , ...} T rminal ymbols = {html, ... tabl , tr, td, ...} tart ymbols = {Html} Rul s = { Htmlhtml[...], ......[Tabl *] Tabl tabl [Tr*], Trnam [Td1 Td2], Td1td[Nam ], Td2td[Pric ]... }

Th n w d fin an arrow b tw n th s two grammars, such that th function that assigns to ach grammar th corr sponding family is a functor (w could probably d fin arrows in th grammar cat gory intrinsically, but again I'v not thought much about that). Two grammars may d fin th sam family. Not all XML famili s can b d scrib d by a grammar but according to Murata t al., all RELAX NG XML famili s can. H nc , if w r strict ours lv s to th subcat gori s of XML famili s d scribabl by RELAX NG and if w us a quoti nt cat gory for th grammars (with two grammars quival nt iff th y d fin th sam XML family) th n th (quoti nt d) functor F abov is a bij ction. Finally F , F 1 form an adjunction b tw n th quoti nt s t of grammars and th RELAX NG subcat gory. 10. R f r nc s J.R. Abrial Th B Book. Cambridg Univ rsity. Pr ss, 1996. H.P. Bar ndr gt Th Lambda Calculus: Its yntax and mantics. North Holland, Amst rdam (1984). http://mathgat .info/c brown/not s/bar ndr gt.php#5 M. Murata t al. Taxonomy of XML sch ma languag s using formal languag th ory. A. Mll r Advanc d W b T chnology. L ctur at th Univ rsity of Aarhus (Nov mb r D c mb r 2009). J. van Oost n Basic Cat gory Th ory. BRIC L ctur ri s (January 1995) wikibooks.org Hask ll/Cat gory th ory

http:// n.wikibooks.org/wiki/Hask ll/Cat gory_th ory G. Winsk l Cat gory Th ory for Comput r ci nc . L ctur at th Univ rsity of Aarhus (August pt mb r 2009).

You might also like