{-# LANGUAGE DeriveFoldable #-} data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) deriving FoldableA more detailed description can be found in the Overview section of Data.Foldable#overview. For the class laws see the Laws section of Data.Foldable#laws.
{-# LANGUAGE DeriveFoldable #-} data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) deriving FoldableA more detailed description can be found in the Overview section of Data.Foldable#overview. For the class laws see the Laws section of Data.Foldable#laws.
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)a suitable instance would be
instance Foldable Tree where foldMap f Empty = mempty foldMap f (Leaf x) = f x foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f rThis is suitable even for abstract types, as the monoid is assumed to satisfy the monoid laws. Alternatively, one could define foldr:
instance Foldable Tree where foldr f z Empty = z foldr f z (Leaf x) = f x z foldr f z (Node l k r) = foldr f (f k (foldr f z r)) lFoldable instances are expected to satisfy the following laws:
foldr f z t = appEndo (foldMap (Endo . f) t ) z
foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
fold = foldMap id
length = getSum . foldMap (Sum . const 1)sum, product, maximum, and minimum should all be essentially equivalent to foldMap forms, such as
sum = getSum . foldMap Sumbut may be less defined. If the type is also a Functor instance, it should satisfy
foldMap f = fold . fmap fwhich implies that
foldMap f . fmap g = foldMap (f . g)
{-# LANGUAGE DeriveFoldable #-} data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) deriving FoldableA more detailed description can be found in the Overview section of Data.Foldable#overview. For the class laws see the Laws section of Data.Foldable#laws.
data MyType a = ... {- Some custom Foldable type -} -- Method 1: Implement Foldr, default FoldMap. type instance Eval (Foldr f y xs) = ... {- Explicit implementation -} type instance Eval (FoldMap f xs) = FoldMapDefault_ f xs {- Default -} -- Method 2: Implement FoldMap, default Foldr. type instance Eval (FoldMap f xs) = ... {- Explicit implementation -} type instance Eval (Foldr f y xs) = FoldrDefault_ f y xs {- Default -}
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)a suitable instance would be
instance Foldable Tree where foldMap f Empty = mempty foldMap f (Leaf x) = f x foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f rThis is suitable even for abstract types, as the monoid is assumed to satisfy the monoid laws. Alternatively, one could define foldr:
instance Foldable Tree where foldr f z Empty = z foldr f z (Leaf x) = f x z foldr f z (Node l k r) = foldr f (f k (foldr f z r)) lFoldable instances are expected to satisfy the following laws:
foldr f z t = appEndo (foldMap (Endo . f) t ) z
foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
fold = foldMap idsum, product, maximum, and minimum should all be essentially equivalent to foldMap forms, such as
sum = getSum . foldMap Sumbut may be less defined. If the type is also a Functor instance, it should satisfy
foldMap f = fold . fmap fwhich implies that
foldMap f . fmap g = foldMap (f . g)