The Magic Dot
So, reading about functors the other day, I noticed something interesting. Specifically, I noticed that in the case of functions,
is equivalent to the composition operation. I think
is possibly the coolest function in all of Haskell, so it annoys me when it requires six characters just to use it as an infix operator.
So I present what is, character-for-character, the coolest Haskell trick I have seen so far:
import Prelude hiding ((.)) import Control.Monad.Instances a . b = a `fmap` b infixr 9 .
And now, all sorts of fun little tricks are possible, like replacing
with
, and replacing
with
And of course, the same function composition magic still works like always. Maybe it's just me, but something this simple, elegant, and fun to use just makes me happy inside.
But finally, I have to ask: is there any way of specifically declaring that a module *replaces* the standard Prelude (or even better, individual elements of it)? It would be really nice to just type
and not have to also bother with adding













September 2nd, 2009 - 09:50
Actually, not quite. The dot operator acts over two functions, and the second function’s argument.
It’s definition is
(.) :: (b->c) -> (a->b) -> a -> c
a . b x = a (b x)
Notice, how ‘.’ takes two /functions/ and the second function’s argument as arguments. It can’t take a function and a value. So something like a.b.x is not equal to the above. It is invalid, unless x is a function in point-free form.
fmap is often abbreviated as infix operator.
It doesn’t take two functions. It takes one function and one functor (i.e. a list). It then lifts the function into the functor class and applies it over the functor. Here is the signature:
() :: Functor f => (a -> b) -> f a -> f b
As you can see is a little more general than ‘.’ and acts only on instances of the functor typeclass. I would show you the fill definition, but I think you should head over to hoogle or hayoo, and look up the operator, and the Control.Applicative module for a better understanding of this. If anything, is more closely related to the $ operator than the . operator.
Hope this is helpful, on your haskell journey.
September 2nd, 2009 - 09:50
sorry, for the second signature should be:
() :: Functor f => (a -> b) -> f a -> f b
instead of () :: Functor f => (a -> b) -> f a -> f b
September 2nd, 2009 - 09:52
html hates my code …
September 2nd, 2009 - 09:53
because I suck at html, here is the link to the operator that gets eaten:
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Applicative.html#v:%3C$%3E
September 2nd, 2009 - 12:28
I think I understand what you are saying. I worded the post badly. What I meant to convey was that “in the case of functions, fmap is equivalent to the composition operation”. I suppose I should fix the wording in the original post.
My point was that since it is perfectly valid to have
in the Control.Monad.Instances module, regular function composition can be viewed as simply being
as it applies to functions. And once that conceptual jump was made, I thought it would be interesting to see what happens if
is redefined to always mean
.
September 3rd, 2009 - 16:45
Ah. Ok. It sounded like you were wrongly convinced that the two were identical (which they are not. `fmap` merely has a ‘function’ instance, as you said earlier), and I was just trying clear up the misconception I perceived, wrongly. Also, a nice thing about `fmap` in applicative is that it has instances for monads where
If you haven’t already, check out the Control.Applicative library. It is truly a thing of beauty.