<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0"><id>tag:blogger.com,1999:blog-22587889</id><updated>2012-01-24T13:07:06.734+05:30</updated><category term="ruby" /><category term="couchdb" /><category term="amqp" /><category term="javascript" /><category term="clojure" /><category term="erlang" /><category term="web" /><category term="websocket" /><category term="redis" /><category term="monad" /><category term="data-structures" /><category term="actor" /><category term="lookback" /><category term="OCaml" /><category term="smullyan" /><category term="mina" /><category term="terracotta" /><category term="open source" /><category term="mustang" /><category term="RIA" /><category term="eventsourcing" /><category term="combinator" /><category term="guice" /><category term="agile" /><category term="dslsina" /><category term="yegge" /><category term="spring" /><category term="rails" /><category term="functional" /><category term="haskell" /><category term="api programming" /><category term="datatype-generic-programming" /><category term="cqrs" /><category term="nosql" /><category term="map-reduce" /><category term="database" /><category term="message-queue" /><category term="xml" /><category term="scala" /><category term="scouchdb" /><category term="type" /><category term="java" /><category term="scalability" /><category term="seam" /><category term="ajax" /><category term="patterns" /><category term="mixin" /><category term="programming" /><category term="jpa-gotcha-series" /><category term="aop" /><category term="rants" /><category term="demeter" /><category term="stm" /><category term="knuth" /><category term="lisp" /><category term="F#" /><category term="algorithm" /><category term="category_theory" /><category term="OO" /><category term="jvm" /><category term="joy" /><category term="concurrency" /><category term="lift" /><category term="DI" /><category term="rest" /><category term="oscon08" /><category term="potpouri" /><category term="algebra" /><category term="fixpoint" /><category term="ddd" /><category term="scalaz" /><category term="euler" /><category term="groovy" /><category term="jpa" /><category term="software" /><category term="orm" /><category term="closure" /><category term="dao" /><category term="dsl" /><category term="parser-combinator" /><category term="memcached" /><category term="akka" /><category term="design" /><category term="testing" /><category term="json" /><category term="potpourri" /><title type="text">Ruminations of a Programmer</title><subtitle type="html">A programmer's blog - will deal with everything that relates to a programmer. Occasionally, it will contain some humour, some politics and some sport news.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/posts/full" /><link rel="alternate" type="text/html" href="http://debasishg.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/posts/full?start-index=4&amp;max-results=3" /><author><name>Debasish Ghosh</name><uri>https://profiles.google.com/106871002817915335660</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-JUSCd5diPuU/AAAAAAAAAAI/AAAAAAAAA-c/RmE42S2JJQ8/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>288</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>3</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/RuminationsOfAProgrammer" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="ruminationsofaprogrammer" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry><id>tag:blogger.com,1999:blog-22587889.post-6817569772136922840</id><published>2012-01-24T11:57:00.000+05:30</published><updated>2012-01-24T11:57:53.976+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="category_theory" /><category scheme="http://www.blogger.com/atom/ns#" term="fixpoint" /><category scheme="http://www.blogger.com/atom/ns#" term="type" /><category scheme="http://www.blogger.com/atom/ns#" term="algebra" /><title type="text">List Algebras and the fixpoint combinator Mu</title><content type="html">In my last &lt;a HREF="http://debasishg.blogspot.com/2012/01/learning-type-level-fixpoint-combinator.html"&gt;post&lt;/A&gt; on recursive types and fixed point combinator, we saw how the type equations of the form &lt;code&gt;a = F(a)&lt;/code&gt;, where &lt;code&gt;F&lt;/code&gt; is the type constructor have solutions of the form &lt;code&gt;Mu a . F&lt;/code&gt; where &lt;code&gt;Mu&lt;/code&gt; is the fixed point combinator. Substituting the solution in the original equation, we get ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;Mu a . F = F {Mu a . F / a}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;where the rhs indicates substitution of all free a's in &lt;code&gt;F&lt;/code&gt; by &lt;code&gt;Mu a . F&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Using this we also got the type equation for &lt;code&gt;ListInt&lt;/code&gt; as ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;ListInt = Mu a . Unit + Int x a&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In this post we view the same problem from a category theory point of view. This post assumes understanding of quite a bit of category theory concepts. If you are unfamiliar with any of them you can refer to some &lt;a HREF="http://www.amazon.com/Category-Computer-Scientists-Foundations-Computing/dp/0262660717"&gt;basic text&lt;/A&gt; on the subject.&lt;br /&gt;&lt;br /&gt;We start with the definition of &lt;code&gt;ListInt&lt;/code&gt; as in the earlier post ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;// nil takes no arguments and returns a List data type&lt;br /&gt;nil : 1 -&gt; ListInt&lt;br /&gt;&lt;br /&gt;// cons takes 2 arguments and returns a List data type&lt;br /&gt;cons : (Int x ListInt) -&gt; ListInt&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Combining the two functions above, we get a single function as ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;in = [nil, cons] : 1 + (Int x ListInt) -&gt; ListInt&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We can say that this forms an algebra of the functor &lt;code&gt;F(X) = 1 + (Int x X)&lt;/code&gt;. Let's represent this algebra by &lt;code&gt;(Mu F, in)&lt;/code&gt; or &lt;code&gt;(Mu F, [nil, cons])&lt;/code&gt;, where &lt;code&gt;Mu F&lt;/code&gt; is &lt;code&gt;ListInt&lt;/code&gt; in the above combined function.&lt;br /&gt;&lt;br /&gt;As a next step we show that the algebra &lt;code&gt;(Mu F, [nil, cons])&lt;/code&gt; forms an &lt;a HREF="http://en.wikipedia.org/wiki/Initial_algebra"&gt;initial algebra&lt;/A&gt; representing the data type of Lists over a given set of integers. Here we are dealing with lists of integers though the same result can be shown for lists of any type &lt;code&gt;A&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;In order to show &lt;code&gt;(Mu F, [nil cons])&lt;/code&gt; form an initial &lt;a HREF="http://en.wikipedia.org/wiki/F-algebra"&gt;F-algebra&lt;/A&gt; we consider an arbitrary F-algebra &lt;code&gt;(C, phi)&lt;/code&gt;, where &lt;code&gt;phi&lt;/code&gt; is an arrow out of the sum type given by :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;C : 1 -&gt; C&lt;br /&gt;h : (Int x C) -&gt; C&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and the join given by &lt;code&gt;[c, h] : 1 + (Int x C) -&gt; C&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;By definition, if &lt;code&gt;(Mu F, [nil, cons])&lt;/code&gt; has to form an initial F-algebra, then for any arbitrary F-algebra &lt;code&gt;(C, phi)&lt;/code&gt; in that category, we need to find a function &lt;code&gt;f: Mu F -&gt; C&lt;/code&gt; which is a homomorphism and it should be unique. So for the algebra &lt;code&gt;[c, h]&lt;/code&gt; the following diagram must commute ..&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-pPYhvqZ7u7U/Tx2jVrrHDyI/AAAAAAAABI8/Lg2fqQfzTdg/s1600/cat.gif" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="300" width="400" src="http://1.bp.blogspot.com/-pPYhvqZ7u7U/Tx2jVrrHDyI/AAAAAAAABI8/Lg2fqQfzTdg/s400/cat.gif" /&gt;&lt;/a&gt;&lt;/div&gt;which means we must have a unique solution to the following 2 equations ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;f o nil = c&lt;br /&gt;f o cons = h o (id x f)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;From the &lt;a HREF="http://en.wikipedia.org/wiki/Universal_property"&gt;universal property&lt;/A&gt; of initial F-algebras it's easy to see that this system of equations has a unique solution which is &lt;code&gt;fold(c, h)&lt;/code&gt;. It's the catamorphism represented by ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;f: {[c, h]}: ListInt -&gt; C&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This proves that &lt;code&gt;(Mu F, [nil, cons])&lt;/code&gt; is an initial F-algebra over the endofunctor &lt;code&gt;F(X) = 1 + (Int x X)&lt;/code&gt;. And it can be shown that an initial algebra in: &lt;code&gt;F (Mu F) -&gt; Mu F&lt;/code&gt; is an isomorphism and the carrier of the initial algebra is (upto isomorphism) a fixed point of the functor. Well, that may sound a bit of a mouthful. But we can discuss this in more details in one of my subsequent posts. There's a well established lemma due to Lambek that proves this. I can't do it in this blog post, since it needs some more prerequisites to be established beforehand which would make this post a bit bloated. But it's really a fascinating proof and I promise to take this up in one of my upcoming posts. Also we will see many properties of initial algebras and how they can be combined to define many of the properties of recursive data types in a purely algebraic way.&lt;br /&gt;&lt;br /&gt;As I promised in my last post, here we have seen the other side of Mu - we started with the list definition, showed that it forms an initial algebra over the endofunctor &lt;code&gt;F(X) = 1 + (Int x X)&lt;/code&gt; and arrived at the same conclusion that &lt;code&gt;Mu F&lt;/code&gt; is a fixed point. Or &lt;code&gt;Mu&lt;/code&gt; is the fixed point combinator.&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/22587889-6817569772136922840?l=debasishg.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/6817569772136922840/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=22587889&amp;postID=6817569772136922840" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/22587889/posts/default/6817569772136922840" /><link rel="self" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/posts/default/6817569772136922840" /><link rel="alternate" type="text/html" href="http://debasishg.blogspot.com/2012/01/list-algebras-and-fixpoint-combinator.html" title="List Algebras and the fixpoint combinator Mu" /><author><name>Debasish Ghosh</name><uri>https://profiles.google.com/106871002817915335660</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-JUSCd5diPuU/AAAAAAAAAAI/AAAAAAAAA-c/RmE42S2JJQ8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-pPYhvqZ7u7U/Tx2jVrrHDyI/AAAAAAAABI8/Lg2fqQfzTdg/s72-c/cat.gif" height="72" width="72" /><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-22587889.post-7711822270811502612</id><published>2012-01-15T20:24:00.000+05:30</published><updated>2012-01-15T20:24:53.211+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="eventsourcing" /><category scheme="http://www.blogger.com/atom/ns#" term="akka" /><category scheme="http://www.blogger.com/atom/ns#" term="ddd" /><category scheme="http://www.blogger.com/atom/ns#" term="cqrs" /><category scheme="http://www.blogger.com/atom/ns#" term="scala" /><title type="text">Event Sourcing, Akka FSMs and functional domain models</title><content type="html">I blogged on &lt;a HREF="http://debasishg.blogspot.com/2011/01/cqrs-with-akka-actors-and-functional.html"&gt;Event Sourcing&lt;/A&gt; and functional domain models earlier. In this post I would like to share more of my thoughts on the same subject and how with a higher level of abstraction you can make your domain aggregate boundary more resilient and decoupled from external references.&lt;br /&gt;&lt;br /&gt;When we talk about a domain model, the Aggregate takes the centerstage. An aggregate is a core abstraction that represents the time invariant part of the domain. It's an embodiment of all states that the aggregate can be in throughout its lifecycle in the system. So, it's extremely important that we take every pain to distil the domain model and protect the aggregate from all unwanted external references. Maybe an example will make it clearer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Keeping the Aggregate pure&lt;/B&gt;&lt;br /&gt;&lt;br /&gt;Consider a Trade model as the aggregate. By Trade, I mean a security trade that takes place in the stock exchange where counterparties exchange securities and currencies for settlement. If you're a regular reader of my blog, you must be aware of this, since this is almost exclusively the domain that I talk of in my blog posts.&lt;br /&gt;&lt;br /&gt;A trade can be in various states like &lt;i&gt;newly entered&lt;/I&gt;, &lt;i&gt;value date added&lt;/I&gt;, &lt;i&gt;enriched with tax and fee information&lt;/I&gt;, &lt;i&gt;net trade value computed&lt;/I&gt; etc. In a trading application, as a trade passes through the processing pipeline, it moves from one state to another. The final state represents the complete Trade object which is ready to be settled between the counterparties. &lt;br /&gt;&lt;br /&gt;In the traditional model of processing we have the final snapshot of the aggregate - what we don't have is the audit log of the actual state transitions that happened in response to the events. With event sourcing we record the state transitions as a pipeline of events which can be replayed any time to rollback or roll-forward to any state of our choice. Event sourcing is coming up as one of the potent ways to model a system and there are lots of blog posts being written to discuss about the various architectural strategies to implement an event sourced application. &lt;br /&gt;&lt;br /&gt;That's ok. But whose responsibility is it to manage these state transitions and record the timeline of changes ? It's definitely not the responsibility of the aggregate. The aggregate is supposed to be a &lt;i&gt;pure&lt;/I&gt; abstraction. We must design it as an immutable object that can respond to events and transform itself into the new state. In fact the aggregate implementation should not be aware of whether it's serving an event sourced architecture or not.&lt;br /&gt;&lt;br /&gt;There are various ways you can model the states of an aggregate. One option that's frequently used involves algebraic data types. Model the various states as a sum type of products. In Scala we do this as case classes ..&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;sealed abstract class Trade {&lt;br /&gt;  def account: Account&lt;br /&gt;  def instrument: Instrument&lt;br /&gt;  //..&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;case class NewTrade(..) extends Trade {&lt;br /&gt;  //..&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;case class EnrichedTrade(..) extends Trade {&lt;br /&gt;  //..&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another option may be to have one data type to model the Trade and model states as immutable enumerations with changes being effected on the aggregate as functional updates. No in place mutation, but use functional data structures like zippers or type lenses to create the transformed object in the new state. Here's an example where we create an enriched trade out of a newly created one ..&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;// closure that enriches a trade&lt;br /&gt;val enrichTrade: Trade =&gt; Trade = {trade =&gt;&lt;br /&gt;  val taxes = for {&lt;br /&gt;    taxFeeIds      &lt;- forTrade // get the tax/fee ids for a trade&lt;br /&gt;    taxFeeValues   &lt;- taxFeeCalculate // calculate tax fee values&lt;br /&gt;  }&lt;br /&gt;  yield(taxFeeIds ° taxFeeValues)&lt;br /&gt;  val t = taxFeeLens.set(trade, taxes(trade))&lt;br /&gt;  netAmountLens.set(t, t.taxFees.map(_.foldl(principal(t))((a, b) =&gt; a + b._2)))&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;But then we come back to the same question - if the aggregate is distilled to model the core domain, who handles the events ? Someone needs to model the event changes, effect the state transitions and take the aggregate from one state to the next.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Enter Finite State Machines&lt;/B&gt;&lt;br /&gt;&lt;br /&gt;In one of my projects I used the domain service layer to do this. The domain logic for effecting the changes lies with the aggregate, but they are invoked from the domain service in response to events when the aggregate reaches specific states. In other words I model the domain service as a finite state machine that manages the lifecycle of the aggregate.&lt;br /&gt;&lt;br /&gt;In our example a Trading Service can be modeled as an FSM that controls the lifecycle of a &lt;code&gt;Trade&lt;/CODE&gt;. As the following ..&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: scala"&gt;import TradeModel._&lt;br /&gt;&lt;br /&gt;class TradeLifecycle(trade: Trade, timeout: Duration, log: Option[EventLog]) &lt;br /&gt;  extends Actor with FSM[TradeState, Trade] {&lt;br /&gt;  import FSM._&lt;br /&gt;&lt;br /&gt;  startWith(Created, trade)&lt;br /&gt;&lt;br /&gt;  when(Created) {&lt;br /&gt;    case Event(e@AddValueDate, data) =&gt;&lt;br /&gt;      log.map(_.appendAsync(data.refNo, Created, Some(data), e))&lt;br /&gt;      val trd = addValueDate(data)&lt;br /&gt;      notifyListeners(trd) &lt;br /&gt;      goto(ValueDateAdded) using trd forMax(timeout)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  when(ValueDateAdded) {&lt;br /&gt;    case Event(StateTimeout, _) =&gt;&lt;br /&gt;      stay&lt;br /&gt;&lt;br /&gt;    case Event(e@EnrichTrade, data) =&gt;&lt;br /&gt;      log.map(_.appendAsync(data.refNo, ValueDateAdded, None,  e))&lt;br /&gt;      val trd = enrichTrade(data)&lt;br /&gt;      notifyListeners(trd)&lt;br /&gt;      goto(Enriched) using trd forMax(timeout)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  when(Enriched) {&lt;br /&gt;    case Event(StateTimeout, _) =&gt;&lt;br /&gt;      stay&lt;br /&gt;&lt;br /&gt;    case Event(e@SendOutContractNote, data) =&gt;&lt;br /&gt;      log.map(_.appendAsync(data.refNo, Enriched, None,  e))&lt;br /&gt;      sender ! data&lt;br /&gt;      stop&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  initialize&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The snippet above contains a lot of other details which I did not have time to prune. It's actually part of the implementation of an event sourced trading application that uses asynchronous messaging (actors) as the backbone for event logging and reaching out to multiple consumers based on the CQRS paradigm.&lt;br /&gt;&lt;br /&gt;Note that the FSM model above makes it very explicit about the states that the &lt;code&gt;Trade&lt;/CODE&gt; model can reach and the events that it handles while in each of these states. Also we can use this FSM technique to log events (for event sourcing), notify listeners about the events (CQRS) in a very much declarative manner as implemented above.&lt;br /&gt;&lt;br /&gt;Let me know in the comments what are your views on this FSM approach towards handling state transitions in domain models. I think it helps keep aggregates pure and helps design domain services that focus on serving specific aggregate roots.&lt;br /&gt;&lt;br /&gt;I will be &lt;a HREF="http://phillyemergingtech.com/2012/sessions/building-applications-with-functional-domain-models-event-sourcing-and-actors"&gt;talking about&lt;/A&gt; similar stuff, Akka actor based event sourcing implementations and functional domain models in PhillyETE 2012. Please drop by if this interests you.&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/22587889-7711822270811502612?l=debasishg.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/7711822270811502612/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=22587889&amp;postID=7711822270811502612" title="7 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/22587889/posts/default/7711822270811502612" /><link rel="self" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/posts/default/7711822270811502612" /><link rel="alternate" type="text/html" href="http://debasishg.blogspot.com/2012/01/event-sourcing-akka-fsms-and-functional.html" title="Event Sourcing, Akka FSMs and functional domain models" /><author><name>Debasish Ghosh</name><uri>https://profiles.google.com/106871002817915335660</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-JUSCd5diPuU/AAAAAAAAAAI/AAAAAAAAA-c/RmE42S2JJQ8/s512-c/photo.jpg" /></author><thr:total>7</thr:total><georss:featurename>Kolkata, West Bengal, India</georss:featurename><georss:point>22.572646 88.363895</georss:point><georss:box>22.338053499999997 88.048038 22.8072385 88.679752</georss:box></entry><entry><id>tag:blogger.com,1999:blog-22587889.post-591560437586381904</id><published>2012-01-08T01:43:00.001+05:30</published><updated>2012-01-08T19:11:59.694+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="functional" /><category scheme="http://www.blogger.com/atom/ns#" term="fixpoint" /><category scheme="http://www.blogger.com/atom/ns#" term="type" /><category scheme="http://www.blogger.com/atom/ns#" term="datatype-generic-programming" /><title type="text">Learning the type level fixpoint combinator Mu</title><content type="html">I &lt;A HREF="http://debasishg.blogspot.com/2011/07/datatype-generic-programming-in-scala.html"&gt;blogged&lt;/A&gt; on Mu, type level fixpoint combinator some time back. I discussed how &lt;CODE&gt;Mu&lt;/CODE&gt; can be implemented in Scala and how you can use it to derive a generic model for catamorphism and some cool type level data structures. Recently I have been reading &lt;A HREF="http://www.amazon.com/Types-Programming-Languages-Benjamin-Pierce/dp/0262162091/ref=sr_1_sc_1?s=books&amp;ie=UTF8&amp;qid=1326026391&amp;sr=1-1-spell"&gt;TAPL&lt;/A&gt; by Benjamin Pierce that gives a very thorough treatment of the theories and implementation semantics of types in a programming language. &lt;br /&gt;&lt;br /&gt;And &lt;CODE&gt;Mu&lt;/CODE&gt; we meet again. Pierce does a very nice job of explaining how &lt;CODE&gt;Mu&lt;/CODE&gt; does for &lt;i&gt;types&lt;/i&gt; what &lt;CODE&gt;Y&lt;/CODE&gt; does for &lt;i&gt;values&lt;/i&gt;. In this post, I will discuss my understanding of &lt;CODE&gt;Mu&lt;/CODE&gt; from a type theory point of view much of what TAPL explains. &lt;br /&gt;&lt;br /&gt;As we know, the collection of types in a programming language forms a &lt;a href="http://en.wikipedia.org/wiki/Category_theory"&gt;category&lt;/a&gt; and any equation recursive in types can be converted to obtain an &lt;a href="http://en.wikipedia.org/wiki/Functor"&gt;endofunctor&lt;/a&gt; on the same category. In an upcoming post I will discuss how the fixed point that we get from &lt;CODE&gt;Mu&lt;/CODE&gt; translates to an isomoprhism in the diagram of categories.&lt;br /&gt;&lt;br /&gt;Let's have a look at the &lt;CODE&gt;Mu&lt;/CODE&gt; constructor - the fixed point for type constructor. What does it mean ? &lt;br /&gt;&lt;br /&gt;Here's the ordinary fixed point combinator for functions (from values to values) ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class="java_plain"&gt;Y&amp;nbsp;f&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;=&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;f&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;(&lt;/span&gt;&lt;span class="java_plain"&gt;Y&amp;nbsp;f&lt;/span&gt;&lt;span class="java_separator"&gt;)&lt;/span&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;and here's &lt;CODE&gt;Mu&lt;/CODE&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class="java_type"&gt;Mu&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;f&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;=&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;f&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;(&lt;/span&gt;&lt;span class="java_type"&gt;Mu&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;f&lt;/span&gt;&lt;span class="java_separator"&gt;)&lt;/span&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Quite similar in structure to &lt;CODE&gt;Y&lt;/CODE&gt;, the difference being that &lt;CODE&gt;Mu&lt;/CODE&gt; operates on type constructors. Here &lt;CODE&gt;f&lt;/CODE&gt; is a type constructor (one that takes a type as input and generates another type). List is the most commonly used type constructor. You give it a type &lt;CODE&gt;Int&lt;/CODE&gt; and you get a concrete type &lt;CODE&gt;ListInt&lt;/CODE&gt;.&lt;br /&gt;&lt;br /&gt;So, &lt;CODE&gt;Mu&lt;/CODE&gt; takes a type constructor &lt;CODE&gt;f&lt;/CODE&gt; and gives you a type &lt;CODE&gt;T&lt;/CODE&gt;. This &lt;CODE&gt;T&lt;/CODE&gt; is the fixed point of &lt;CODE&gt;f&lt;/CODE&gt;, i.e. &lt;CODE&gt;f T = T&lt;/CODE&gt;.&lt;br /&gt;&lt;br /&gt;Consider the following recursive definition of a List ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class="java_comment"&gt;//&amp;nbsp;nil&amp;nbsp;takes&amp;nbsp;no&amp;nbsp;arguments&amp;nbsp;and&amp;nbsp;returns&amp;nbsp;a&amp;nbsp;List&amp;nbsp;data&amp;nbsp;type&lt;/span&gt;&lt;br /&gt;&lt;span class="java_plain"&gt;nil&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;:&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_literal"&gt;1&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;ListInt&lt;/span&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="java_comment"&gt;//&amp;nbsp;cons&amp;nbsp;takes&amp;nbsp;2&amp;nbsp;arguments&amp;nbsp;and&amp;nbsp;returns&amp;nbsp;a&amp;nbsp;List&amp;nbsp;data&amp;nbsp;type&lt;/span&gt;&lt;br /&gt;&lt;span class="java_plain"&gt;cons&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;:&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;(&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;x&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;ListInt&lt;/span&gt;&lt;span class="java_separator"&gt;)&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;ListInt&lt;/span&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Taken together we would like to solve the following equation :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class="java_plain"&gt;a&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;=&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Unit&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;+&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;x&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class="java_comment"&gt;//&amp;nbsp;.....&amp;nbsp;(1)&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Now this is recursive and can be unfolded infinitely as &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class="java_plain"&gt;a&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;=&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Unit&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;+&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;x&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;(&lt;/span&gt;&lt;span class="java_type"&gt;Unit&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;+&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;x&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;a&lt;/span&gt;&lt;span class="java_separator"&gt;)&lt;/span&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;=&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Unit&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;+&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;x&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;(&lt;/span&gt;&lt;span class="java_type"&gt;Unit&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;+&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;x&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;(&lt;/span&gt;&lt;span class="java_type"&gt;Unit&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;+&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;x&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;a&lt;/span&gt;&lt;span class="java_separator"&gt;))&lt;/span&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;=&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;...&lt;/span&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="java_plain"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;TAPL shows that this equation can be represented in the form of an infinite labeled tree and calls this infinite type regular. So, generally speaking, we have an equation of the form &lt;CODE&gt;a = &amp;#964;&lt;/CODE&gt; where&lt;br /&gt;&lt;br /&gt;1. if &lt;CODE&gt;a&lt;/CODE&gt; does not occur in &lt;CODE&gt;&amp;#964;&lt;/CODE&gt;, then we have a finite solution which, in fact is &lt;CODE&gt;&amp;#964;&lt;/CODE&gt;&lt;br /&gt;2. if a occurs in &lt;CODE&gt;&amp;#964;&lt;/CODE&gt;, then we have an infinite solution represented by an infinite regular tree&lt;br /&gt;&lt;br /&gt;So the above equation is of the form &lt;CODE&gt;a = ... a ...&lt;/CODE&gt; or we can say &lt;CODE&gt;a = F(a)&lt;/CODE&gt; where &lt;CODE&gt;F&lt;/CODE&gt; is the type constructor. This highlights the recursion of types (not of values). Hence any solution to this equation will give us an object which will be the fixed point of the equation. We call this solution &lt;CODE&gt;Mu a . F&lt;/CODE&gt;. &lt;br /&gt;&lt;br /&gt;Since &lt;CODE&gt;Mu a . F&lt;/CODE&gt; is a solution to &lt;CODE&gt;a = F(a)&lt;/CODE&gt;, we have the following:&lt;br /&gt;&lt;br /&gt;&lt;CODE&gt;Mu a . F = F {Mu a . F / a}&lt;/CODE&gt;, where the rhs indicates substitution of all free &lt;CODE&gt;a&lt;/CODE&gt;'s in &lt;CODE&gt;F&lt;/CODE&gt; by &lt;CODE&gt;Mu a . F&lt;/CODE&gt;.&lt;br /&gt;&lt;br /&gt;Here &lt;CODE&gt;Mu&lt;/CODE&gt; is the fixed point combinator which takes the type constructor &lt;CODE&gt;F&lt;/CODE&gt; and gives us a type, which is the fixed point of &lt;CODE&gt;F&lt;/CODE&gt;. Using this idea, the above equation (1) has the solution &lt;CODE&gt;ListInt&lt;/CODE&gt;, which is the fixed point type ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class="java_type"&gt;ListInt&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;=&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Mu&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;a&amp;nbsp;&lt;/span&gt;&lt;span class="java_separator"&gt;.&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Unit&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_operator"&gt;+&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="java_type"&gt;Int&lt;/span&gt;&lt;span class="java_plain"&gt;&amp;nbsp;x&amp;nbsp;a&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;In summary, we express recursive types using the fix point type constructor &lt;CODE&gt;Mu&lt;/CODE&gt; and show that &lt;CODE&gt;Mu&lt;/CODE&gt; generates the fixed point for the type constructor just like &lt;CODE&gt;Y&lt;/CODE&gt; generates the same for functions on values.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/22587889-591560437586381904?l=debasishg.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/591560437586381904/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=22587889&amp;postID=591560437586381904" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/22587889/posts/default/591560437586381904" /><link rel="self" type="application/atom+xml" href="http://debasishg.blogspot.com/feeds/posts/default/591560437586381904" /><link rel="alternate" type="text/html" href="http://debasishg.blogspot.com/2012/01/learning-type-level-fixpoint-combinator.html" title="Learning the type level fixpoint combinator Mu" /><author><name>Debasish Ghosh</name><uri>https://profiles.google.com/106871002817915335660</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-JUSCd5diPuU/AAAAAAAAAAI/AAAAAAAAA-c/RmE42S2JJQ8/s512-c/photo.jpg" /></author><thr:total>0</thr:total><georss:featurename>Kolkata, West Bengal, India</georss:featurename><georss:point>22.572646 88.363895</georss:point><georss:box>22.338053499999997 88.048038 22.8072385 88.679752</georss:box></entry></feed>

