[Haskell-cafe] Functors [Comments from OCaml Hacker Brian Hurt]
Jonathan Cast
jonathanccast at fastmail.fm
Sun Jan 18 00:41:11 EST 2009
On Sat, 2009-01-17 at 12:04 +0000, Andrew Coppin wrote:
> Eugene Kirpichov wrote:
> > No, a functor is a more wide notion than that, it has nothing to do
> > with collections.
> > An explanation more close to truth would be "A structure is a functor
> > if it provides a way to convert a structure over X to a structure over
> > Y, given a function X -> Y, while preserving the underlying
> > 'structure'", where preserving structure means being compatible with
> > composition and identity.
> >
>
> As far as I'm aware, constraints like "while preserving the underlying
> structure" are not expressible in Haskell.
>
> > instance (Monad m) => Functor m where
> > fmap f ma = do a <- ma; return (f a)
> >
>
> While that's quite interesting from a mathematical point of view, how is
> this "useful" for programming purposes?
Good Lord. fmap (as above) is *at least* useful enough to be in the
standard library! (Control.Monad.liftM). Contrary to what some people
seem to think, every single function in Haskell's standard library was
included because enough people found it actually *useful* enough to add.
Here's the first example I found, searching the source code for my macro
language interpreter:[1]
Macro-call interpolation has syntax §(expression), parsed by
do
char '§'
lexParens $ Interpolation <$> parseExpr
= do
char '§'
lexParens $ fmap Interpolation $ parseExpr
= do [2]
char '§'
fmap Interpolation $ lexParens $ parseExpr
Useful enough?
jcc
[1] The only simplifications applied were (a) ignoring an interpolation
syntax substantially more complicated, and (b) re-naming the relevant
constructor.
[2] Valid because lexParens is a natural transformation.
More information about the Haskell-Cafe
mailing list