[Haskell-cafe] Re: How to "Show" an Operation?

Luke Palmer lrpalmer at gmail.com
Fri Jun 11 05:50:55 EDT 2010


On Thu, Jun 10, 2010 at 2:10 PM, Maciej Piechotka <uzytkownik2 at gmail.com> wrote:
> data Named a = Named String a
>
> instance Functor Named where
>    f `fmap` (Named s v) = Named s (f v)
>
> instance Applicative Named where
>    pure x = Named "" x
>    (Named s f) <*> (Named t v) = Named (s ++ "(" ++ t ++ ")") (f v)

This is not technically a legal applicative instance, because it is
not associative.  This can be seen when you try to clean up the usage
as we have been discussing:

g <.> f = liftA2 (.) g f

f = Named "f" (+1)
g = Named "g" (*2)
h = Named "h" (^3)

ghci> f <*> (g <*> (h <*> namedPure 42))
f(g(h(42)))
ghci> (f <.> g <.> h) <*> namedPure 42
f(g)(h)(42)

The Applicative laws are supposed to guarantee that this refactor is
legal.  Of course, the latter answer is nonsense.

Luke


More information about the Haskell-Cafe mailing list