[Haskell-beginners] Trying to prove Applicative is superclass of Functor, etc

Daniel Bergey bergey at alum.mit.edu
Sat Apr 30 21:18:44 UTC 2016


On 2016-04-30 at 15:14, Silent Leaf <silent.leaf0 at gmail.com> wrote:
> I think I got what exactly was the change of ghc 7.10. basically it only adds a
> constraint Applicative on the Monad class, and same with
> Functor/Applicative.

7.10 adds the constraint on the Monad class.  Prior to 7.10, both Monads
and Applicatives already needed to be Functors.

> Otherwise it's forcing the programmer to do even more possibly-useless
> work (we don't always need applicative and functors for every use of a
> new monad, do we?)

The practical advantage comes when I want to write some code that is
generic over Monads.  If I can't assume that every Monad is Applicative,
my choices are:

1) Write (Applicative m, Monad m) =>, and not work for those Monads
2) Write `ap` everywhere I mean <*>, which for some instances is less
efficient
3) Write two versions, one like (1) and one like (2)

None of these are very appealing, and orphan instances are a pain, so
there's already strong social pressure that any Monad instance on
Hackage should have the corresponding Applicative instance defined.

> in short i think if they wanted mathematical correctness they should
> have added automatic instanciation of superclasses, with possibility
> to override the default definitions, like they do so with some
> superfluous methods.

Several ways of automatically defining superclasses were discussed as
part of the AMP changes.  Maybe we'll get one in some future GHC.  I
don't know the details, but some of the discussion:

https://ghc.haskell.org/trac/ghc/wiki/IntrinsicSuperclasses
https://ghc.haskell.org/trac/ghc/wiki/DefaultSuperclassInstances
https://ghc.haskell.org/trac/ghc/wiki/InstanceTemplates

> Not that write functors or applicative functor instances is actually heavywork, of
> course, thankfully.

You know that if the instances are all in the same module, you can use
the Monad functions, right?  So the extra work is just pasting in:

 instance Functor m where
    fmap = liftM
 
instance Applicative m where
    pure  = return
    (<*>) = ap


More information about the Beginners mailing list