1
+ module Data.Monoid.Alternate where
2
+
3
+ import Prelude
4
+
5
+ import Control.Alternative (class Alt , class Plus , class Alternative , empty , (<|>))
6
+ import Control.Comonad (class Comonad , class Extend )
7
+
8
+ -- | Monoid and semigroup instances corresponding to `Plus` and `Alt` instances
9
+ -- | for `f`
10
+ -- |
11
+ -- | ``` purescript
12
+ -- | Alternate fx <> Alternate fy == Alternate (fx <|> fy)
13
+ -- | mempty :: Alternate _ == Alternate empty
14
+ -- | ```
15
+ newtype Alternate f a = Alternate (f a )
16
+
17
+ derive newtype instance eqAlternate :: Eq (f a ) => Eq (Alternate f a )
18
+
19
+ derive newtype instance ordAlternate :: Ord (f a ) => Ord (Alternate f a )
20
+
21
+ derive newtype instance boundedAlternate :: Bounded (f a ) => Bounded (Alternate f a )
22
+
23
+ derive newtype instance functorAlternate :: Functor f => Functor (Alternate f )
24
+
25
+ derive newtype instance applyAlternate :: Apply f => Apply (Alternate f )
26
+
27
+ derive newtype instance applicativeAlternate :: Applicative f => Applicative (Alternate f )
28
+
29
+ derive newtype instance altAlternate :: Alt f => Alt (Alternate f )
30
+
31
+ derive newtype instance plusAlternate :: Plus f => Plus (Alternate f )
32
+
33
+ derive newtype instance alternativeAlternate :: Alternative f => Alternative (Alternate f )
34
+
35
+ derive newtype instance bindAlternate :: Bind f => Bind (Alternate f )
36
+
37
+ derive newtype instance monadAlternate :: Monad f => Monad (Alternate f )
38
+
39
+ derive newtype instance extendAlternate :: Extend f => Extend (Alternate f )
40
+
41
+ derive newtype instance comonadAlternate :: Comonad f => Comonad (Alternate f )
42
+
43
+ instance showAlternate :: Show (f a ) => Show (Alternate f a ) where
44
+ show (Alternate a) = " (Alternate " <> show a <> " )"
45
+
46
+ instance semigroupAlternate :: Alt f => Semigroup (Alternate f a ) where
47
+ append (Alternate a) (Alternate b) = Alternate (a <|> b)
48
+
49
+ instance monoidAlternate :: Plus f => Monoid (Alternate f a ) where
50
+ mempty = Alternate empty
0 commit comments