@@ -10,6 +10,12 @@ import Data.Monoid.Disj (Disj(..))
1010import Data.Monoid.Dual (Dual (..))
1111import Data.Monoid.Endo (Endo (..))
1212import Data.Newtype (unwrap )
13+ import Data.Foldable (class Foldable , foldr , foldl , foldMap )
14+ import Data.Bifunctor.Clown (Clown (..))
15+ import Data.Bifunctor.Joker (Joker (..))
16+ import Data.Bifunctor.Flip (Flip (..))
17+ import Data.Bifunctor.Product (Product (..))
18+ import Data.Bifunctor.Wrap (Wrap (..))
1319
1420-- | `Bifoldable` represents data structures with two type arguments which can be
1521-- | folded.
@@ -33,6 +39,31 @@ class Bifoldable p where
3339 bifoldl :: forall a b c . (c -> a -> c ) -> (c -> b -> c ) -> c -> p a b -> c
3440 bifoldMap :: forall m a b . Monoid m => (a -> m ) -> (b -> m ) -> p a b -> m
3541
42+ instance bifoldableClown :: Foldable f => Bifoldable (Clown f ) where
43+ bifoldr l _ u (Clown f) = foldr l u f
44+ bifoldl l _ u (Clown f) = foldl l u f
45+ bifoldMap l _ (Clown f) = foldMap l f
46+
47+ instance bifoldableJoker :: Foldable f => Bifoldable (Joker f ) where
48+ bifoldr _ r u (Joker f) = foldr r u f
49+ bifoldl _ r u (Joker f) = foldl r u f
50+ bifoldMap _ r (Joker f) = foldMap r f
51+
52+ instance bifoldableFlip :: Bifoldable p => Bifoldable (Flip p ) where
53+ bifoldr r l u (Flip p) = bifoldr l r u p
54+ bifoldl r l u (Flip p) = bifoldl l r u p
55+ bifoldMap r l (Flip p) = bifoldMap l r p
56+
57+ instance bifoldableProduct :: (Bifoldable f , Bifoldable g ) => Bifoldable (Product f g ) where
58+ bifoldr l r u m = bifoldrDefault l r u m
59+ bifoldl l r u m = bifoldlDefault l r u m
60+ bifoldMap l r (Product f g) = bifoldMap l r f <> bifoldMap l r g
61+
62+ instance bifoldableWrap :: Bifoldable p => Bifoldable (Wrap p ) where
63+ bifoldr l r u (Wrap p) = bifoldr l r u p
64+ bifoldl l r u (Wrap p) = bifoldl l r u p
65+ bifoldMap l r (Wrap p) = bifoldMap l r p
66+
3667-- | A default implementation of `bifoldr` using `bifoldMap`.
3768-- |
3869-- | Note: when defining a `Bifoldable` instance, this function is unsafe to
@@ -71,36 +102,39 @@ bifoldlDefault f g z p =
71102-- | use in combination with `bifoldrDefault`.
72103bifoldMapDefaultR
73104 :: forall p m a b
74- . (Bifoldable p , Monoid m )
105+ . Bifoldable p
106+ => Monoid m
75107 => (a -> m )
76108 -> (b -> m )
77109 -> p a b
78110 -> m
79- bifoldMapDefaultR f g p = bifoldr (append <<< f) (append <<< g) mempty p
111+ bifoldMapDefaultR f g = bifoldr (append <<< f) (append <<< g) mempty
80112
81113-- | A default implementation of `bifoldMap` using `bifoldl`.
82114-- |
83115-- | Note: when defining a `Bifoldable` instance, this function is unsafe to
84116-- | use in combination with `bifoldlDefault`.
85117bifoldMapDefaultL
86118 :: forall p m a b
87- . (Bifoldable p , Monoid m )
119+ . Bifoldable p
120+ => Monoid m
88121 => (a -> m )
89122 -> (b -> m )
90123 -> p a b
91124 -> m
92- bifoldMapDefaultL f g p = bifoldl (\m a -> m <> f a) (\m b -> m <> g b) mempty p
125+ bifoldMapDefaultL f g = bifoldl (\m a -> m <> f a) (\m b -> m <> g b) mempty
93126
94127
95128-- | Fold a data structure, accumulating values in a monoidal type.
96- bifold :: forall t m . ( Bifoldable t , Monoid m ) => t m m -> m
129+ bifold :: forall t m . Bifoldable t => Monoid m => t m m -> m
97130bifold = bifoldMap id id
98131
99132-- | Traverse a data structure, accumulating effects using an `Applicative` functor,
100133-- | ignoring the final result.
101134bitraverse_
102135 :: forall t f a b c d
103- . (Bifoldable t , Applicative f )
136+ . Bifoldable t
137+ => Applicative f
104138 => (a -> f c )
105139 -> (b -> f d )
106140 -> t a b
@@ -110,7 +144,8 @@ bitraverse_ f g = bifoldr (applySecond <<< f) (applySecond <<< g) (pure unit)
110144-- | A version of `bitraverse_` with the data structure as the first argument.
111145bifor_
112146 :: forall t f a b c d
113- . (Bifoldable t , Applicative f )
147+ . Bifoldable t
148+ => Applicative f
114149 => t a b
115150 -> (a -> f c )
116151 -> (b -> f d )
@@ -121,15 +156,17 @@ bifor_ t f g = bitraverse_ f g t
121156-- | ignoring the final result.
122157bisequence_
123158 :: forall t f a b
124- . (Bifoldable t , Applicative f )
159+ . Bifoldable t
160+ => Applicative f
125161 => t (f a ) (f b )
126162 -> f Unit
127163bisequence_ = bitraverse_ id id
128164
129165-- | Test whether a predicate holds at any position in a data structure.
130166biany
131167 :: forall t a b c
132- . (Bifoldable t , BooleanAlgebra c )
168+ . Bifoldable t
169+ => BooleanAlgebra c
133170 => (a -> c )
134171 -> (b -> c )
135172 -> t a b
@@ -139,7 +176,8 @@ biany p q = unwrap <<< bifoldMap (Disj <<< p) (Disj <<< q)
139176-- | Test whether a predicate holds at all positions in a data structure.
140177biall
141178 :: forall t a b c
142- . (Bifoldable t , BooleanAlgebra c )
179+ . Bifoldable t
180+ => BooleanAlgebra c
143181 => (a -> c )
144182 -> (b -> c )
145183 -> t a b
0 commit comments