Adequate instantiation of foldMap

120 Views Asked by At
data MyData a b = MyData a b b

Why is the first instantiation good and the second not ?

instance Foldable (MyData a) where
    foldMap f (MyData x y z) = f y <> f z
instance Foldable (MyData a) where 
       foldMap f (MyData x y z) = f z

f maps both y and z into a monoid, so f z and f y <> f z are instances of that monoid. So, why is the second not ok ?

1

There are 1 best solutions below

8
radrow On

Your instance is alright as it does not violate any of the Foldable laws, as long as you keep it consequent. It is just odd that MyData holds two components of type b, one of which is not considered in folds.