Semantic Editor Combinators - one of my favorite blog posts
I wanted to squeeze this blog post into my Best Haskell Papers of 2009 article, but it unfortunately was written in late 2008. For your enjoyment, here is the entry below.
Semantic Editor Combinators - Conal Elliott [link]
Strictly speaking this isn’t a paper. On the other hand, the functional pearl therein is a must for any Haskeller’s box of tools. The essence of the presented technique allows one to write easy to understand transforms of functions, or function like things. For example:
f :: Int -> (Int,Char) -> String -- Want to convert into type g . . . g :: Int -> (Int,Int) -> String -- with transformer z. z :: Int -> Char -- Implementation using semantic editor combinators: g = (result . argument . second) z f
The g implementation can be read backwards as “apply the z transformation to the second element of the tuple of the argument of the result of f”. Recall that functions only have one argument and one result and that the result of a curried function is another function.
It takes a bit of getting used to, but it is definitely worth the investment in time learning. It frequently makes code more elegant and concise.
Note, if you ever get stumbled by an (fmap.fmap.fmap) in code, this is where to go.