Friday, August 10, 2012

monadic folds

I have been rewriting my type inference framework over late evening sessions this week. I would not do it just for the refactoring, I want to be able to do more with it. Although I will not tell you why, and I can tell you how.

This is F# code, and my first version was monadic, modular, but flat. In some sense I was using the monadic abstraction as a comfort zone but without pushing it. Now I have "internalized" the concept and "objectified" it to the point where I am comfortable to program mostly without the "do" notation, composing higher order monadic constructions. And this brings me to my favorite pattern of the moment: the monadic fold.

Here is how it works:
  • the fold pattern is "joining" an operator accross a slice of data. In effect it is a universal "block" but which is "projected" against existing data, see each fold as an new inner hierarchy in your code.
  • the monadic environment supports the "environmental" needs of the operators
Building your specialized library of environments and operators is no easy. Yet the good news is that it easier with time.  The good news is that monads are like "single dimensional objects", and that is to say that if you have packed things in objects and you know how to be selective, then packing single concepts into monads is not too hard.

No comments: