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

Daniel Fischer daniel.is.fischer at web.de
Fri Jun 11 06:39:57 EDT 2010


On Friday 11 June 2010 11:50:55, Luke Palmer wrote:
> 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.

Good spot.

I think

(Named s f) <*> (Named t v) = Named (s ++ " $ " ++ t) (f v)

fixes it.

> 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