[null, {"document_url": "http://debasishg.blogspot.com/2012/07/does-category-theory-make-you-better.html?showComment=1497812050340", "unformatted_src": "http://2.bp.blogspot.com/-T6hzH0mzEHA/UBQKau21pKI/AAAAAAAABtA/kFuslRfV6PI/s400/product.jpg", "src": "http://2.bp.blogspot.com/-T6hzH0mzEHA/UBQKau21pKI/AAAAAAAABtA/kFuslRfV6PI/s400/product.jpg", "formatted_filename": "product", "rendered_width": 400, "rendered_height": 300}, null, {"document_url": "http://debasishg.blogspot.com/2012/07/does-category-theory-make-you-better.html?showComment=1497812050340", "unformatted_src": "http://3.bp.blogspot.com/-BzgwAsMi3J8/UBQnb9Z9ZbI/AAAAAAAABtU/249F5FVqXhk/s400/sum.jpg", "src": "http://3.bp.blogspot.com/-BzgwAsMi3J8/UBQnb9Z9ZbI/AAAAAAAABtU/249F5FVqXhk/s400/sum.jpg", "formatted_filename": "sum", "rendered_width": 400, "rendered_height": 300}, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, {"document_url": "http://debasishg.blogspot.com/2012/07/does-category-theory-make-you-better.html?showComment=1497812050340", "unformatted_src": "//lh4.googleusercontent.com/-JUSCd5diPuU/AAAAAAAAAAI/AAAAAAAABuo/RF-pdPiR7gE/s80-c/photo.jpg", "src": "http://lh4.googleusercontent.com/-JUSCd5diPuU/AAAAAAAAAAI/AAAAAAAABuo/RF-pdPiR7gE/s80-c/photo.jpg", "formatted_filename": "photo", "alt_text": "My photo", "rendered_width": 80, "rendered_height": 80}, null, null, null, null, null, {"document_url": "http://debasishg.blogspot.com/2012/07/does-category-theory-make-you-better.html?showComment=1497812050340", "unformatted_src": "http://manning.com/ghosh/ghosh_cover150.jpg", "src": "http://manning.com/ghosh/ghosh_cover150.jpg", "formatted_filename": "ghosh cover", "alt_text": "Now Available in Print", "rendered_width": 150, "rendered_height": 186}, null, null, null]

Does category theory make you a better programmer ?

Translating to Scala

Category theory & programming languages 

Interface driven modeling

Thinking generically 
Hi there, You have done an excellent job. I'll certainly digg it and personally recommend to my friends. I'm confident they'll be benefited from this site.
My web blog - scripting vs programming language

Monday, July 30, 2012 at 5:54:00 PM GMT+5:30 
Hi Debasish, great post, thanks!

Monday, July 30, 2012 at 6:21:00 PM GMT+5:30 
Thank you for this Debashish, it's really enlightening. However, I'm having a little trouble working out what the category 'C' denotes in your sum and product examples - the types of 'f' and 'g' in the scala example of a product seem to suggest that C is the type Integer (and is therefore the same as A?) - but I'm having a hard time seeing the significance of this (or indeed working out what the types of f, g, and [f,g] should be for the 'sum' example. Might you be able to shed some light on this?



Monday, July 30, 2012 at 7:19:00 PM GMT+5:30 
Hi Tim -

Consider the definition of a Cartesian product between 2 Sets. In category theory, we define it as follows:
For all sets C, if there exists a morphism f: C -> A and g: C -> B, then there exists a *unique* h given by h: C -> A × B (typically written ) such that π1 ◦ h = f and π2 ◦ h = g.
The object C exists to show that the morphism h: C -> A x B is unique upto isomorphism. In other words, if we have 2 such objects C1 and C2, such that both C1 and C2 morph to A x B (and either of them can be called the Product of A and B), then C1 is isomorphic to C2.
In terms of the programming model, if we had C1 as Int and C2 as XInt (some other type), but both map to the product of Int x String, then we can say that Int is isomorphic to XInt. This goes to show that for every pair of Scala types, the Product or Tuple2 is uniquely defined.
Does this clear things a bit ?

Monday, July 30, 2012 at 8:59:00 PM GMT+5:30 j2kun said...

Strictly speaking, products are not defined as sets of tuples. The tuples are just a realization of a product in a specific category (the category of sets is the simplest example). In fact, in pure category theory there is no such thing as a set or an element. In this way, you can define things like the category of types, which has absolutely nothing to do with sets.

It's an important fact that not all categories have products. A category with products is a strong assumption, and if you talk about "elements" of sets in your category, then you're probably working under the assumption that your category is abelian. At least, this is the main content of the Freyd-Mitchell embedding theorem, which says that every abelian category can be thought of as a category of R-modules (and hence, of sets).

Tuesday, July 31, 2012 at 3:18:00 AM GMT+5:30 
Some good discussions on proggit .. http://www.reddit.com/r/programming/comments/xdz76/does_category_theory_make_you_a_better_programmer/ and Google+ https://plus.google.com/101021359296728801638/posts/GzNEMhGRcpY

Tuesday, July 31, 2012 at 8:56:00 AM GMT+5:30 Adam Warski said...


nice article!
One thing I don't understand, why: "... the Product type fails miserably in presence of the Bottom data type (_|_) represented by Nothing ..."?


Wednesday, August 1, 2012 at 11:41:00 PM GMT+5:30 
For a Product type we need to satisfy the following rules (easier to explain in Haskell):

fst (a, b) = a // _1 in Scala
snd (a, b) = b // _2 in Scala

In order to be a categorical product it also has to satisfy the following rule for a product type p:

(fst p, snd p) = p

Now if you substitute _|_ for p, then you get

(_|_, _|_) = _|_

which fails in Haskell, since the above is false.

A categorical product for Haskell would be unlifted, and would be considered non-bottom if either component were non-bottom, but bottom if both were. But we don't have those available.

The above explanation is from Dan Doyle's comment in James Iry's blog post that I referred to in the article (http://james-iry.blogspot.in/2011/05/why-eager-languages-dont-have-products.html#comment-201436254).

Hope this helps ..

Thursday, August 2, 2012 at 10:09:00 AM GMT+5:30 
Hmm aren't some levels mixed here?

For a categorical product we need the diagram to commute and the product object to be unique up to isomorphism, that is ;pi_1 = f etc., there's no notion of "elements" of the objects, as the (fst p, snd p) = p formula could suggest.

The data type _|_ (Nothing in Scala) in uninhabited, that is there are no instances of this type. So we can take "a" p, as there is none.

Thursday, August 2, 2012 at 2:09:00 PM GMT+5:30 
I am not sure I get your question ..

However in categorical domain, we don't have the bottom. Hence the equations hold good. While in the programming model, unless we assume a strong functional model, the bottom is the spoilsport.

But I think you are pointing to something else ..

Thursday, August 2, 2012 at 2:24:00 PM GMT+5:30 
Ah, got it!

There are no *instances* of type Nothing, but there are *expressions* of type nothing (a diverging computation). And then, indeed, there are no products, if as your element you take such a computation.


Thursday, August 2, 2012 at 3:31:00 PM GMT+5:30 

Thursday, August 2, 2012 at 3:39:00 PM GMT+5:30 
The first sentence following the third snippet looks incorrect. Aren't the last two expressions typewise equal to f and g respectively (rather than to each other)?

Monday, August 6, 2012 at 3:20:00 PM GMT+5:30 
Rob - Thanks for pointing out .. fixed.

Monday, August 6, 2012 at 3:41:00 PM GMT+5:30 seanbell said...

It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thank you and giving great information about how to make better programmer.

Friday, September 7, 2012 at 10:56:00 AM GMT+5:30 Jan Kammerath said...

I think providing good and fast software solutions for normal people's problems makes you a good dev. This one: Scala programming pretty much describes some Scala things. I find it quite important to ensure people are able to use your software. To be honest I never found a client who was really interested in what type of language is in the background. Most users don't care...

Tuesday, September 18, 2012 at 11:30:00 PM GMT+5:30 Best Training said...


Wednesday, December 12, 2012 at 9:03:00 AM GMT+5:30 piyushmahesh said...

Can you recommend any good introductory books on category theory from programmers ?

Thursday, June 4, 2015 at 12:27:00 PM GMT+5:30 
I started with Conceptual Mathematics (http://www.amazon.com/Conceptual-Mathematics-First-Introduction-Categories/dp/052171916X), which does a great job teaching the basics.

Thursday, June 4, 2015 at 12:33:00 PM GMT+5:30 Akram Ahmad said...

Ashish, your book DSLs in Action (Manning) is one of my favorites! I recently reviewed on my blog the best Scala books in print (Programming Digressions). Given how your fine book has fantastic portions dealing with Scala in the DSL world, I made sure to mention some details about your book in my comments that accompany that particular blog - Keep up your great work!

Wednesday, July 29, 2015 at 6:31:00 PM GMT+5:30 
Hi Debasish - As I returned to re-read this fantastic post, I glanced at my prior comment and cringed upon realizing that I had inadvertently mixed up your name with the name of a good friend of mine... My apologies!

BTW, I'm looking forward to buying your book Functional and Reactive Domain Modeling as soon as it's published - Another friend of mine here in Austin (Texas) is, in fact, currently reviewing your upcoming book through Manning Publications' MEAP process, and has a lot of good things to say about it!


Friday, August 7, 2015 at 7:06:00 PM GMT+5:30 
Hello Akram -

Thanks for the kind words. Also it feels good to hear that you find my latest book interesting enough.

Saturday, August 8, 2015 at 12:00:00 AM GMT+5:30 Philippe Derome said...

In section "Thinking generically" I don't understand references to 1 (unit of multiplication) and 0 (unit of addition) and what it brings to the paragraph (multiplication with 1 form a monoid and addition with 0 form a monoid when considering familiar numeric sets such as N, Z, Q, R). Is the parallel that composition of functions forms a monoid with idA and idB playing a role analog to 1 and 0 (when using corresponding arithmetic operators)?

Monday, June 19, 2017 at 12:24:00 AM GMT+5:30