It Seemed Like a Good Idea at the Time Coding, Mostly

24Aug/096

The Magic Dot

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 ((.))

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Comments (6) Trackbacks (0)
  1. 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.

  2. 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

  3. html hates my code …

    () :: Functor f => (a -> b) -> f a -> f b
    
    instead of () :: Functor f => (a -> b) -> f a -> f b
    
  4. 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 being

    fmap

    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

    fmap

    .

  5. 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.


Leave a comment


No trackbacks yet.