<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/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-3857512306503018420</id><updated>2011-07-08T01:04:45.628+01:00</updated><title type='text'>Nattermorphisms</title><subtitle type='html'>Ramblings about Functional Programming</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://nattermorphisms.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://nattermorphisms.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ben</name><uri>http://www.blogger.com/profile/05535385464025706733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3857512306503018420.post-1613371941756742937</id><published>2009-01-18T15:32:00.006Z</published><updated>2009-01-18T21:38:22.242Z</updated><title type='text'>The Category Theory of Appendages</title><content type='html'>There has been much discussion recently on haskell-cafe about Monoids, much (too much!) focusing on the possibility of renaming of the existing monoid typeclass to "Appendable"... which is a daft idea, not least because then we'd need to rename Monad to be "Appendage".&lt;br /&gt;&lt;br /&gt;Anyway, I wanted to focus a bit on the link between the two concepts (monoid and monad), and how that's treated from the Category Theoretic perspective. (I'm not aiming at any kind of rigour ... simply trying to gain some basic intuitions). In terms of assumptions, I assume familiarity with the definitions of Category, Functor and ideally categorical products / co-products, initiality and finality.&lt;br /&gt;&lt;br /&gt;This article aims to cover similar ground to one of sigfpe's &lt;a href="http://sigfpe.blogspot.com/2008/11/from-monoids-to-monads.html"&gt;here&lt;/a&gt;, but at a slightly higher (hopefully simpler) level.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Traditional Monoids&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The basic concept of Monoid is treated from the standard mathematical (abstract algebra) perspective &lt;a href="http://en.wikipedia.org/wiki/Monoid"&gt;here&lt;/a&gt;, and from the practical (Haskell) perspective sigfpe has a nice article &lt;a href="http://sigfpe.blogspot.com/2009/01/haskell-monoids-and-their-uses.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So, from a traditional point of view, a monoid is:&lt;br /&gt;&lt;br /&gt;* A set, along with...&lt;br /&gt;* an associative binary function over that set, accompanied by ...&lt;br /&gt;* an identity element&lt;br /&gt;&lt;br /&gt;The basic (non-category theoretic) view of monoids in Haskell is pretty much the standard one, viewing Haskell types as sets. So in Haskell common examples of monoids are: &lt;tt&gt;(String, (++), [])&lt;/tt&gt; and &lt;tt&gt;(Int, (+), 0)&lt;/tt&gt; and &lt;tt&gt;(Int, (*), 1)&lt;/tt&gt; and &lt;tt&gt;(Bool, (&amp;&amp;), True)&lt;/tt&gt; and &lt;tt&gt;(Bool, (||), False)&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;In the Haskell setting this means that the function has both arguments and result with the same type - ie the function's type is "a -&gt; a -&gt; a", and the identity element is a distinguished value of type "a". And, of course, it is exactly this which is embodied in the &lt;a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html"&gt;Data.Monoid&lt;/a&gt; typeclass.&lt;br /&gt;&lt;br /&gt;Category takes this standard view of monoids and generalises it somewhat. This generalisation has two parts - not only does it generalise so that we can talk about monoids in different categories, but it also generalises so that we can have multiple ways of identifying monoids &lt;i&gt;within&lt;/i&gt; a single category.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Categories which can contain Monoids&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;From the category theory perspective, a category can only "play host" to monoids under the following conditions:&lt;br /&gt;&lt;br /&gt;* It must be equipped with a &lt;b&gt;functor&lt;/b&gt; over the category which maps any pair of objects to another object, and any pair of arrows to another arrow. (This type of functor is similar to a normal functor apart from operating on pairs - and is known as a Bifunctor). Let us call this functor "&lt;b&gt;B&lt;/b&gt;".&lt;br /&gt;&lt;br /&gt;* It must have a distinguished &lt;b&gt;object&lt;/b&gt; (which we'll call "&lt;b&gt;e&lt;/b&gt;") which acts as an identity for this functor.&lt;br /&gt;&lt;br /&gt;This then gives us what is known as a &lt;a href="http://en.wikipedia.org/wiki/Monoidal_category"&gt;Monoidal Category&lt;/a&gt;. (Note that by making different choices for the (bi)functor and distinguished object we may have several different ways to view an underlying category as monoidal).&lt;br /&gt;&lt;br /&gt;The simplest example is on Set - we take the standard product as the (bi)functor (which maps any pair of sets to the set which is their product), and a single-element set (ie terminal object) as the distinguished object. &lt;!-- Slightly more generally, any category with products and a terminal object is monoidal using product as the (bi)functor and the terminal object as the distinguished object, and any category with co-products and an initial object is monoidal using co-product as the (bi)functor and the initial object as the distinguished object. --&gt;&lt;br /&gt;&lt;br /&gt;In the context of Haskell, we take as the (bi)functor, the product (bi)functor (which maps any pair of types, say 'a' and 'b', to their product type "(a,b)", and any pair of functions, say 'a-&gt;c' and 'b-&gt;d' to the function '(a,b)-&gt;(c,d)'). As the distinguished object we take "()" - the unit type whose sole value "()" is written syntactically identically to its type.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Category Theoretical Monoids&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Once we have those two components, we have a category in which it is possible to identify categorical monoids, but we don't actually have the definition of a categorical monoid itself. A &lt;a href="http://en.wikipedia.org/wiki/Monoid_(category_theory)"&gt;monoid within a monoidal category&lt;/a&gt; is defined to be:&lt;br /&gt;&lt;br /&gt;* An object (which we'll call "c")&lt;br /&gt;* An arrow from &lt;b&gt;B&lt;/b&gt; (c,c) to c.&lt;br /&gt;* An arrow from &lt;b&gt;e&lt;/b&gt; to c.&lt;br /&gt;&lt;br /&gt;...such that &lt;a href="http://en.wikipedia.org/wiki/Monoid_(category_theory)"&gt;some basic diagrams commute&lt;/a&gt;. Now, recall that "&lt;b&gt;B&lt;/b&gt;" is the (bi)functor which we selected when creating our monoidal category, so "&lt;b&gt;B&lt;/b&gt; (c,c)" is just another object.&lt;br /&gt;&lt;br /&gt;So, to make this concrete - if we consider Haskell as a monoidal category as above, then we can take "c" to be "String", our first arrow to be "(++) :: (String,String) -&gt; String", and our second arrow to be the function ":: () -&gt; String" which takes "()" as its argument and returns "[]".&lt;br /&gt;&lt;br /&gt;If we compare this with our first, non-categorical, definition of (String, (++), []) as a monoid above, the parallels are quite clear. The differences are firstly that we now need to pretend that (++) has type "(String,String) -&gt; String" rather than "String -&gt; String -&gt; String", which we can do simply by viewing it as an uncurried function, and secondly that we're representing "[]" by a function rather than a value. This extra baggage only really becomes useful when we look at other categorical monoids (which aren't plain, normal monoids).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Monads&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;So far, when discussing Haskell, we've implicitly had in mind the category "Hask" which has types as its objects, and Haskell functions between those types as its arrows. What we can now do, is to consider another category which is closely related to "Hask" - namely the category formed by taking as objects all Haskell functors ("Maybe", "[]", etc...) and as arrows all functions from one functor type to another ("listToMaybe :: [a] -&gt; Maybe [a]", "maybeToList :: Maybe a -&gt; [a]", etc...). This is the "endofunctor" category over Hask, and these arrows are natural transformations.&lt;br /&gt;&lt;br /&gt;Next we're going to go looking for monoids in this "endofunctor" category using our above definitions. We need to keep a slightly clear head at this point, because we need to remember that the objects of this category are functors over another category - hence when we just say "functor" we need to be clear whether we're talking about one of these objects (ie a functor &lt;b&gt;in the underlying category&lt;/b&gt;), or about a functor &lt;b&gt;over this category&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;First we need to choose a (bi)functor &lt;b&gt;over this category&lt;/b&gt; - we'll choose composition of functors (so this takes a pair of objects - say "Maybe" and "[]" and maps them to their composition "[Maybe]"). Secondly we need to choose a distinguished object - we'll choose the identity functor "Id".&lt;br /&gt;&lt;br /&gt;Finally, using our second definition, we can see that a monoid in this category must be an object along with two suitable arrows (ie this will be a functor and two natural transformations in Hask). We can take 'Maybe' as the object, 'join :: Maybe (Maybe a) -&gt; Maybe a' as the first arrow, and 'Just :: a -&gt; Maybe a' as our second arrow.&lt;br /&gt;&lt;br /&gt;Thus equipped, "Maybe" can be seen as a monoid in the endofunctor category over Hask. And that, of course, is what a monad is - a monad over a category 'C' is a (categorical) monoid in the endofunctor category over 'C'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Back to Set (...and Hask?) &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;We can now go back and take another look at the Set category. This time we can look for some monoids which are monoids from the category theoretic perspective, but &lt;b&gt;&lt;i&gt;not&lt;/i&gt;&lt;/b&gt; from the traditional perspective.&lt;br /&gt;&lt;br /&gt;We can do this by viewing Set as a monoidal category using &lt;i&gt;co-product&lt;/i&gt; as the (bi)functor (ie disjoint union rather than product) and taking the initial object (the empty set) as the distinguished object. Under this definition, "monoids" would be objects (ie Sets - as before), equipped with (a) a function to the set from the disjoint union of the set with itself, and (b) a function to the set from the empty set - ie the empty function.&lt;br /&gt;&lt;br /&gt;Now, I have to confess I don't remotely understand the implications of this. I haven't ever seen any reference to what such objects would be called in "Set". In Hask, the coproduct of 'a' and 'b' is 'Either a b', and on functions takes 'a-&gt;c' and 'b-&gt;d' to 'Either a b -&gt; Either c d'.  Also, I'm not sure how much sense it makes to talk about an empty type in Hask, or an empty function in Hask.&lt;br /&gt;&lt;br /&gt;It seems like there ought to be something useful (back in Haskell land) to drop out of this... I'd be very interested in anyone has any ideas / pointers...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3857512306503018420-1613371941756742937?l=nattermorphisms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nattermorphisms.blogspot.com/feeds/1613371941756742937/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3857512306503018420&amp;postID=1613371941756742937' title='48 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/1613371941756742937'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/1613371941756742937'/><link rel='alternate' type='text/html' href='http://nattermorphisms.blogspot.com/2009/01/category-theory-of-appendages.html' title='The Category Theory of Appendages'/><author><name>Ben</name><uri>http://www.blogger.com/profile/05535385464025706733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>48</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3857512306503018420.post-5728890438230881759</id><published>2008-12-24T15:50:00.002Z</published><updated>2008-12-24T15:56:00.632Z</updated><title type='text'>Christmas Profiling Tip</title><content type='html'>If you're profiling some code and you get something like this from &lt;tt&gt;hp2ps&lt;/tt&gt; ...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;c:/ws/fpfdev/depot/QA/EDG/EDG_priv/FPF_Dev.br/src $ hp2ps -c&lt;br /&gt;GenBarriers.hp&lt;br /&gt;c:\ws\fpfdev\depot\QA\EDG\EDG_priv\FPF_Dev.br\ThirdParty\ghc\ghc-6.10.1\&lt;br /&gt;bin\hp2ps.exe:  Disaster! (bucket out of range)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;...then the chances are it's because you have a partially truncated &lt;tt&gt;.hp&lt;/tt&gt; file (eg if your program didn't exit cleanly). This is easily fixed, by searching for the last &lt;tt&gt;BEGIN_SAMPLE&lt;/tt&gt; line in the file, and deleting everything from there onwards.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3857512306503018420-5728890438230881759?l=nattermorphisms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nattermorphisms.blogspot.com/feeds/5728890438230881759/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3857512306503018420&amp;postID=5728890438230881759' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/5728890438230881759'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/5728890438230881759'/><link rel='alternate' type='text/html' href='http://nattermorphisms.blogspot.com/2008/12/christmas-profiling-tip.html' title='Christmas Profiling Tip'/><author><name>Ben</name><uri>http://www.blogger.com/profile/05535385464025706733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3857512306503018420.post-7953418964243514393</id><published>2008-11-09T20:15:00.002Z</published><updated>2008-11-09T20:33:10.012Z</updated><title type='text'>Why does Functional Programming matter?</title><content type='html'>Of course we all know &lt;a href="http://www.md.chalmers.se/~rjmh/Papers/whyfp.html"&gt;the answer&lt;/a&gt;...&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;...but recently I've been wondering how to explain what I feel is important about FP in a pithy, succinct way. (I've frequently found myself failing to explain it well).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I think in the future I'm going to say this:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;blockquote&gt;"Functional Programming makes programs &lt;span class="Apple-style-span" style="font-style: italic;"&gt;easier to understand&lt;/span&gt;. And that means they're less likely to go wrong."&lt;/blockquote&gt;&lt;/div&gt;&lt;div&gt;For my money this is why functional programming is vitally important - because the biggest problem we have right now isn't concurrency ... it's the fact that we can't even write single-threaded programs that work properly. (Of course "go wrong" is used above in the standard rather than the &lt;a href="http://books.google.com/books?id=ti6zoAC9Ph8C&amp;amp;pg=PA111&amp;amp;lpg=PA111&amp;amp;dq=well+typed+programs+don't+go+wrong&amp;amp;source=web&amp;amp;ots=EzGfuHkYVy&amp;amp;sig=d3wef7W5R22Kz_jna_YFnon1IY8&amp;amp;hl=en&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;resnum=10&amp;amp;ct=result"&gt;Milner&lt;/a&gt; sense).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;One other comment I'd make is that I think the "easier to understand" bit becomes more apparent with bigger programs.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I'd be interested in hearing if anyone's got a better way of describing it.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3857512306503018420-7953418964243514393?l=nattermorphisms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nattermorphisms.blogspot.com/feeds/7953418964243514393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3857512306503018420&amp;postID=7953418964243514393' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/7953418964243514393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/7953418964243514393'/><link rel='alternate' type='text/html' href='http://nattermorphisms.blogspot.com/2008/11/why-does-functional-programming-matter.html' title='Why does Functional Programming matter?'/><author><name>Ben</name><uri>http://www.blogger.com/profile/05535385464025706733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3857512306503018420.post-7668587104450418342</id><published>2008-10-30T09:16:00.005Z</published><updated>2008-10-30T11:38:29.141Z</updated><title type='text'>2 Minute intro to Associated Types / Type Families</title><content type='html'>Type Families are an important recent addition to GHC which have been developed over the past couple of years (and indeed are still being developed). They promise to address some of the same issues addressed by functional dependencies whilst avoiding some of the nastier corner cases and allowing for a more solid theoretical underpinning and implementation.&lt;br /&gt;&lt;br /&gt;There is a lot of material available on Type Families, in fact it seemed somewhat bewildering to me initially. There doesn't seem to be an "idiot's guide" overview - so that's what this post attempts to do - to provide a just enough background to help you work out which papers etc you want to read next.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;Terminology and Syntax&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;The thing to note is that there are essentially four different concepts, each of which have a couple of different terms for them:&lt;br /&gt;&lt;br /&gt;&lt;table border="1"&gt;&lt;tr&gt; &lt;td&gt;&lt;b&gt;Associated (Data) Type&lt;/b&gt; &lt;pre&gt;class ArrayElem e where&lt;br /&gt;    data Array e&lt;br /&gt;    index :: Array e -&gt; Int -&gt; e&lt;br /&gt;&lt;br /&gt;instance ArrayElem Int where&lt;br /&gt;    data Array Int = IntArray UIntArr&lt;br /&gt;    index (IntArray a) i = ...&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Associated (Type) Synonym &lt;/span&gt;&lt;span class="Apple-style-span" style=""&gt; &lt;pre&gt;class Collection c where&lt;br /&gt;    type Elem c&lt;br /&gt;    insert :: Elem c -&gt; c -&gt; c&lt;br /&gt;&lt;br /&gt;instance Eq e =&gt; Collection [e] where&lt;br /&gt;    type Elem [e] = e&lt;br /&gt;    ...&lt;/pre&gt;&lt;/span&gt;&lt;/td&gt;&lt;tr&gt; &lt;tr&gt;&lt;td&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Data (Type) Family&lt;/span&gt; &lt;pre&gt;data family Array e&lt;br /&gt;&lt;br /&gt;data instance Array Int = IntArray UIntArr&lt;br /&gt;&lt;br /&gt;data instance Array Char = MyCharArray a b    &lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt; &lt;td&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;(Type) Synonym Family&lt;/span&gt; &lt;pre&gt;type family Elem c&lt;br /&gt;&lt;br /&gt;type instance Elem [e] = e&lt;br /&gt;&lt;br /&gt;type instance Elem BitSet = Char&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;Associated or Family?&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;The first "axis" of categorization is "Associated" vs "Family". The "Associated ..." variants (which were invented first) are those which are declared inside a standard typeclass declaration, the "... Family" variants are stand-alone, top-level, use the "family" keyword and were invented a year or so later. The Family variants (collectively known as Type Families) are strict generalizations of the associated ones, and the associated ones are simple syntactic sugar for the family variants.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;Data or Type Synonym?&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;The other "axis" of categorization is "Data" vs "Type Synonym". This distinction mirrors that of normal Haskell "data" and "type" declarations. The key point is that associated &lt;b&gt;data&lt;/b&gt; types and &lt;b&gt;data&lt;/b&gt; families let you create a &lt;b&gt;&lt;i&gt;bijection&lt;/i&gt;&lt;/b&gt; (ie one-to-one mapping) from source type to destination type. Associated type &lt;b&gt;synonyms&lt;/b&gt; and &lt;b&gt;synonym&lt;/b&gt; families on the other hand allow you to map two different souce types onto the same destination type. The best reference for more details on the difference is the first part of section 7 of the "Associated Type Synonyms" paper.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;Equality Constraints&lt;/b&gt;&lt;br /&gt;The final piece of syntax allows us to assert type-level equalities using the above:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;sumCollection :: (Collection c, Elem c ~ Int) =&gt; c -&gt; Int&lt;br /&gt;sumCollection c = sum (toList c)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This (as with the other examples) is slightly modified from one of the papers - in this case the "Associated Type Synonyms" paper where the syntax uses "=" rather than the "~" which was ultimately used.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;Compatability&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Many of the above features are actually available in the GHC 6.8 branch but they are only really supported under 6.10 onwards. (I understand that the main reason the code is in 6.8 at all is to facilitate merging of other, unrelated fixes between those branches). I've had success with both kinds of associated types under 6.8 but ran into problems using equality constraints. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;b&gt;References&lt;/b&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.haskell.org/haskellwiki/GHC/Type_families"&gt;http://www.haskell.org/haskellwiki/GHC/Type_families&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cse.unsw.edu.au/~chak/papers/CKPM05.html"&gt;Associated Types with Class&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cse.unsw.edu.au/~chak/papers/CKP05.html"&gt;Associated Type Synonyms&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cse.unsw.edu.au/~chak/papers/SCPD07.html"&gt;System F with Type Equality Coercions&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cse.unsw.edu.au/~chak/papers/SPCS08.html"&gt;Type Checking with Open Type Functions&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3857512306503018420-7668587104450418342?l=nattermorphisms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nattermorphisms.blogspot.com/feeds/7668587104450418342/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3857512306503018420&amp;postID=7668587104450418342' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/7668587104450418342'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/7668587104450418342'/><link rel='alternate' type='text/html' href='http://nattermorphisms.blogspot.com/2008/10/2-minute-intro-to-associated-types-type.html' title='2 Minute intro to Associated Types / Type Families'/><author><name>Ben</name><uri>http://www.blogger.com/profile/05535385464025706733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3857512306503018420.post-6093465227919956076</id><published>2008-08-03T21:05:00.000+01:00</published><updated>2008-08-04T21:04:04.559+01:00</updated><title type='text'>Join-ing the Blogosphere</title><content type='html'>I'd like to share a little Haskell snippet (I wanted to say "idiom" but actually I'm not sure if I've seen it used anywhere else...) that I quite like:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;&lt;br /&gt;join (,) 9&lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;What does this do? On its own, not much - in fact you just get:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;&lt;br /&gt;(9,9)&lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;...which you'd have been much better off typing in yourself. Where it can come-in handy though is when writing code in a pointfree style if you have a need to duplicate an anonymous value into both parts of a pair:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;&lt;br /&gt;functionWhichTakesAPair . join (,) . functionWhichProducesASingleValue&lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;Anyway, how / why does this construct work....?&lt;br /&gt;&lt;br /&gt;The &lt;tt&gt;(,)&lt;/tt&gt; is just the standard data constructor for a 2-tuple, a pair. (This constructor has type &lt;tt&gt;a -&gt; b -&gt; (a,b)&lt;/tt&gt; so you can always write &lt;tt&gt;(,) 9 9&lt;/tt&gt; as an alternative notation for &lt;tt&gt;(9,9)&lt;/tt&gt; if you wish).&lt;br /&gt;&lt;br /&gt;The &lt;tt&gt;join&lt;/tt&gt; is the standard function &lt;tt&gt;:: Monad m =&gt; m (m a) -&gt; m a&lt;/tt&gt; we know from monadic programming (Control.Monad). But this just raises the question of how it can make sense to apply it to &lt;tt&gt;(,)&lt;/tt&gt; which as we've seen is a function of type &lt;tt&gt;a -&gt; b -&gt; (a,b)&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;The answer is of course that this function may be considered to be of type &lt;tt&gt;m (m a)&lt;/tt&gt;. This is because &lt;tt&gt;Control.Monad.Instances&lt;/tt&gt; declares any partially applied function (eg &lt;tt&gt;a -&gt; ...&lt;/tt&gt;) to be a monad (the type &lt;tt&gt;a&lt;/tt&gt; can be seen as representing an encapsulated environment which is threaded through by the monadic bind). This then means that a function of two arguments (such as &lt;tt&gt;(,)&lt;/tt&gt;) can be seen as having type &lt;tt&gt;m (m a)&lt;/tt&gt; which is just what we need to be able to apply &lt;tt&gt;join&lt;/tt&gt;. The net result is that the single argument of the resulting function (&lt;tt&gt;join (,)&lt;/tt&gt;) is passed in twice as desired.&lt;br /&gt;&lt;br /&gt;This of course generalises to functions other than &lt;tt&gt;(,)&lt;/tt&gt; - whenever we use &lt;tt&gt;join f&lt;/tt&gt; where &lt;tt&gt;f&lt;/tt&gt; is a function of two arguments, we end up with a function of one argument which is passed into &lt;tt&gt;f&lt;/tt&gt; twice.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3857512306503018420-6093465227919956076?l=nattermorphisms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nattermorphisms.blogspot.com/feeds/6093465227919956076/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3857512306503018420&amp;postID=6093465227919956076' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/6093465227919956076'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3857512306503018420/posts/default/6093465227919956076'/><link rel='alternate' type='text/html' href='http://nattermorphisms.blogspot.com/2008/08/join-ing-blogosphere.html' title='Join-ing the Blogosphere'/><author><name>Ben</name><uri>http://www.blogger.com/profile/05535385464025706733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
