The Magic Dot
August 24, 2009 at 11:45 am | In haskell | 6 CommentsTags: fmap, haskell, prelude
So, reading about functors the other day, I noticed something interesting. Specifically, I noticed that in the case of functions, fmap is equivalent to the composition operation. I think fmap 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 "map succ $ [0..9]" with "succ . [0..9]", and replacing "getLine >>= return . (+1) . read" with "(+1) . read . getLine"
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 import Prelude.MagicDot and not have to also bother with adding import Prelude hiding ((.))
6 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.
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.
Comment by nick — September 2, 2009 #
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
Comment by nick — September 2, 2009 #
html hates my code …
Comment by nick — September 2, 2009 #
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
Comment by nick — September 2, 2009 #
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
instance Functor ((->) r) where fmap = (.)in the Control.Monad.Instances module, regular function composition can be viewed as simply beingfmapas 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 meanfmap.Comment by Will Donnelly — September 2, 2009 #
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
f `fmap` xs == xs >>= return.f
If you haven’t already, check out the Control.Applicative library. It is truly a thing of beauty.
Comment by Nick Zivkovic — September 3, 2009 #