The type class Data.Foldable has the following definition:
class Foldable t where
{-# MINIMAL foldMap | foldr #-}
foldMap :: Monoid m => (a -> m) -> t a -> m
foldr :: (a -> b -> b) -> b -> t a -> b
....
<more definitions>
The minimal stanza says, that I it's possible to define an instance only with the foldr function.
In all the examples I can compile, the foldr function has type a -> a -> a
. However I'm unable
to define something where the foldr function has really type a -> b -> b
where the types a
and b
are different.
The following code shows an example, that does NOT compile:
import Data.Foldable
data Tree a = Tree a a | Leaf a
class Size a where
size :: a -> Int
instance Size a => Foldable (Tree a) where
foldr :: a -> Int -> Int
foldr x n = size x + n
Is it possible to define an instance of Foldable with foldr
, where the types a
and b
are really different?