Given that he already knew how to define a Monad instance for his type, the Applicative instance could simply be:
import Control.Monad (ap)
instance Applicative Tree where
pure = return
(<*>) = ap
Also, given a Traversable instance—which is essentially just "fmap with effects"—one can get Foldable and Functor for free via foldMapDefault and fmapDefault.
The really interesting cases are the ones where you can define an instance for Applicative but not Monad.
An example of this is the two possible applicative instance of list. One is based on the monad instance and does a cross product if presented with two lists. The other zips its arguments. It is the more interesting one imho:
import Control.Applicative
newtype ZList a = ZList { runZList :: [a] }
instance Functor ZList where
fmap f = ZList . fmap f . runZList
instance Applicative ZList where
pure a = ZList [a]
(<*>) f x = ZList $ zipper (runZList f) (runZList x)
where zipper (f:fs) (x:xs) = f x : zipper fs xs
zipper [] _ = []
zipper _ [] = []
The standard ZipList definition is `pure a = ZipList (repeat a)` (an infinite list).
The issue with defining a Monad instance for ZList is that mapping the function on the right of (>>=) over the ZList on the left gets you a ZList of `ZList b`, with no meaningful way to zip these ZLists together.
It is interesting that he knew this fact,and that it would be nice if this code could be automatically derived too, but I think that should not replace the learning process.
Indeed, the learning process is important. I think it's particularly instructive to look at how `ap` is implemented, and what is left out in the transition from Monad to Applicative:
mf `ap` ma = do
f <- mf
a <- ma
return (f a)
With a Monad, the action on the right of the bind operator is a function of the result of the action on the left. The second operand of the `ap` function, however, does not have any access to the result of the first operand. The two operands are evaluated independently, apart from their side-effects. That reflects the fundamental difference between Applicative and Monad: an Applicative instance just sequences effects and combines results, whereas a Monad instance allows effects to depend on previous results.
All Applicatives have those two variants, not just Applicatives which are also Monads. There is a Backwards newtype in Control.Applicative.Backwards which reverses the order of effects for any Applicative:
I don't know about "many", but certainly some - e.g. you could form a "reversed MonadWriter" applicative that would write the messages from ma before the messages from mf. For a lot of monads the effects are commutative though, in which case the "two possible" Applicatives are actually the same, e.g. Maybe obviously behaves the same whichever order you "run" mf and ma in.
The really interesting cases are the ones where you can define an instance for Applicative but not Monad.