[Haskell-cafe] Re: Applicative but not Monad

David Menendez dave at zednenem.com
Sat Oct 31 11:38:18 EDT 2009


On Sat, Oct 31, 2009 at 6:22 AM, Heinrich Apfelmus
<apfelmus at quantentunnel.de> wrote:
> Dan Weston wrote:
>> Can you elaborate on why Const is not a monad?
>>
>> return x = Const x
>> fmap f (Const x) = Const (f x)
>> join (Const (Const x)) = Const x
>>
>
> This is not  Const , this is the  Identity  monad.
>
> The real  Const  looks like this:
>
>   newtype Const b a = Const b
>
>   instance Monoid b => Applicative (Const b) where
>        pure x = Const mempty
>        (Const b) <*> (Const b') = Const (b `mappend` b')
>
> The only possible monad instance would be
>
>   return x = Const mempty
>   fmap f (Const b) = Const b
>   join (Const b)   = Const b
>
> but that's not just  ()  turned into a monad.

This is inconsistent with the Applicative instance given above.

Const a <*> Const b = Const (a `mappend` b)

Const a `ap` Const b = Const a

In other words, Const has at least two Applicative instances, one of
which is not also a monad.

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list