The code as it currently stands only has two nested fmaps / foldMaps / traverses.

The reason he can do that is because Scope is defined as an "f (Var b a)".  Since Scope is only a functor if f is also functor, that means you can fmap over f, regardless of what it is.  But in addition to that Var is also a functor.  So you can fmap over f, and then fmap over the Var inside the f, which ends up being two nested fmaps.  That same condition exists for foldable and traversable.

On Fri, Aug 17, 2018 at 6:46 AM, Anthony Lee <anthonynlee@gmail.com> wrote:
In Scope.hs there are some functions I feel difficult to understand,
Why fmap/foldmap/traverse is applied three times?

instance Functor f => Functor (Scope b f) where
fmap f (Scope a) = Scope (fmap (fmap (fmap f)) a)
{-# INLINE fmap #-}

-- | @'toList'@ is provides a list (with duplicates) of the free variables
instance Foldable f => Foldable (Scope b f) where
foldMap f (Scope a) = foldMap (foldMap (foldMap f)) a
{-# INLINE foldMap #-}

instance Traversable f => Traversable (Scope b f) where
traverse f (Scope a) = Scope <$> traverse (traverse (traverse f)) a
{-# INLINE traverse #-}

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners