@@ -7,12 +7,11 @@ module Data.Semigroup.Foldable
77 , traverse1_
88 , for1_
99 , sequence1_
10- , foldMap1Default
11- , fold1Default
12- , fold1DefaultR
13- , fold1DefaultL
1410 , foldr1Default
1511 , foldl1Default
12+ , foldMap1DefaultR
13+ , foldMap1DefaultL
14+ , foldMap1Default
1615 , intercalate
1716 , intercalateMap
1817 , maximum
@@ -29,67 +28,74 @@ import Data.Monoid.Multiplicative (Multiplicative(..))
2928import Data.Newtype (ala , alaF )
3029import Data.Ord.Max (Max (..))
3130import Data.Ord.Min (Min (..))
31+ import Prim.TypeError (class Warn , Text )
3232
3333-- | `Foldable1` represents data structures with a minimum of one element that can be _folded_.
3434-- |
35- -- | - `fold1` folds a structure using a `Semigroup` instance
36- -- | - `foldMap1` folds a structure by accumulating values in a `Semigroup`
3735-- | - `foldr1` folds a structure from the right
3836-- | - `foldl1` folds a structure from the left
37+ -- | - `foldMap1` folds a structure by accumulating values in a `Semigroup`
3938-- |
4039-- | Default implementations are provided by the following functions:
4140-- |
42- -- | - `fold1Default`
43- -- | - `fold1DefaultR`
44- -- | - `fold1DefaultL`
45- -- | - `foldMap1Default`
4641-- | - `foldr1Default`
4742-- | - `foldl1Default`
43+ -- | - `foldMap1DefaultR`
44+ -- | - `foldMap1DefaultL`
4845-- |
4946-- | Note: some combinations of the default implementations are unsafe to
5047-- | use together - causing a non-terminating mutually recursive cycle.
5148-- | These combinations are documented per function.
5249class Foldable t <= Foldable1 t where
53- foldMap1 :: forall a m . Semigroup m => (a -> m ) -> t a -> m
54- fold1 :: forall m . Semigroup m => t m -> m
5550 foldr1 :: forall a . (a -> a -> a ) -> t a -> a
5651 foldl1 :: forall a . (a -> a -> a ) -> t a -> a
57-
58- -- | A default implementation of `fold1` using `foldMap1`.
59- fold1Default :: forall t m . Foldable1 t => Semigroup m => t m -> m
60- fold1Default = foldMap1 identity
61-
62- -- | A default implementation of `fold1` using `foldr1`.
63- fold1DefaultR :: forall t m . Foldable1 t => Semigroup m => t m -> m
64- fold1DefaultR = foldr1 append
65-
66- -- | A default implementation of `fold1` using `foldl1`.
67- fold1DefaultL :: forall t m . Foldable1 t => Semigroup m => t m -> m
68- fold1DefaultL = foldl1 append
69-
70- -- | A default implementation of `foldMap1` using `fold1`.
71- foldMap1Default :: forall t m a . Foldable1 t => Functor t => Semigroup m => (a -> m ) -> t a -> m
72- foldMap1Default f = (map f) >>> fold1
52+ foldMap1 :: forall a m . Semigroup m => (a -> m ) -> t a -> m
7353
7454-- | A default implementation of `foldr1` using `foldMap1`.
55+ -- |
56+ -- | Note: when defining a `Foldable1` instance, this function is unsafe to use
57+ -- | in combination with `foldMap1DefaultR`.
7558foldr1Default :: forall t a . Foldable1 t => (a -> a -> a ) -> t a -> a
7659foldr1Default = flip (runFoldRight1 <<< foldMap1 mkFoldRight1)
7760
7861-- | A default implementation of `foldl1` using `foldMap1`.
62+ -- |
63+ -- | Note: when defining a `Foldable1` instance, this function is unsafe to use
64+ -- | in combination with `foldMap1DefaultL`.
7965foldl1Default :: forall t a . Foldable1 t => (a -> a -> a ) -> t a -> a
8066foldl1Default = flip (runFoldRight1 <<< alaF Dual foldMap1 mkFoldRight1) <<< flip
8167
68+ -- | A default implementation of `foldMap1` using `foldr1`.
69+ -- |
70+ -- | Note: when defining a `Foldable1` instance, this function is unsafe to use
71+ -- | in combination with `foldr1Default`.
72+ foldMap1DefaultR :: forall t m a . Foldable1 t => Functor t => Semigroup m => (a -> m ) -> t a -> m
73+ foldMap1DefaultR f = map f >>> foldr1 (<>)
74+
75+ -- | A default implementation of `foldMap1` using `foldl1`.
76+ -- |
77+ -- | Note: when defining a `Foldable1` instance, this function is unsafe to use
78+ -- | in combination with `foldl1Default`.
79+ foldMap1DefaultL :: forall t m a . Foldable1 t => Functor t => Semigroup m => (a -> m ) -> t a -> m
80+ foldMap1DefaultL f = map f >>> foldl1 (<>)
81+
82+ -- | Deprecated previous name of `foldMap1DefaultL`.
83+ foldMap1Default :: forall t m a . Warn (Text " 'foldMap1Default' is deprecated, use 'foldMap1DefaultL' instead" ) => Foldable1 t => Functor t => Semigroup m => (a -> m ) -> t a -> m
84+ foldMap1Default = foldMap1DefaultL
85+
8286instance foldableDual :: Foldable1 Dual where
83- foldMap1 f (Dual x) = f x
84- fold1 = fold1Default
8587 foldr1 _ (Dual x) = x
8688 foldl1 _ (Dual x) = x
89+ foldMap1 f (Dual x) = f x
8790
8891instance foldableMultiplicative :: Foldable1 Multiplicative where
89- foldMap1 f (Multiplicative x) = f x
90- fold1 = fold1Default
9192 foldr1 _ (Multiplicative x) = x
9293 foldl1 _ (Multiplicative x) = x
94+ foldMap1 f (Multiplicative x) = f x
95+
96+ -- | Fold a data structure, accumulating values in some `Semigroup`.
97+ fold1 :: forall t m . Foldable1 t => Semigroup m => t m -> m
98+ fold1 = foldMap1 identity
9399
94100newtype Act :: forall k . (k -> Type ) -> k -> Type
95101newtype Act f a = Act (f a )
0 commit comments