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