This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
/
Alternate.purs
58 lines (36 loc) · 2.04 KB
/
Alternate.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Data.Monoid.Alternate where
import Prelude
import Control.Alternative (class Alt, class Plus, class Alternative, empty, (<|>))
import Control.Comonad (class Comonad, class Extend)
import Data.Functor.Invariant (class Invariant)
import Data.Monoid (class Monoid)
import Data.Newtype (class Newtype)
-- | Monoid and semigroup instances corresponding to `Plus` and `Alt` instances
-- | for `f`
-- |
-- | ``` purescript
-- | Alternate fx <> Alternate fy == Alternate (fx <|> fy)
-- | mempty :: Alternate _ == Alternate empty
-- | ```
newtype Alternate f a = Alternate (f a)
derive instance newtypeAlternate :: Newtype (Alternate f a) _
derive newtype instance eqAlternate :: Eq (f a) => Eq (Alternate f a)
derive newtype instance ordAlternate :: Ord (f a) => Ord (Alternate f a)
derive newtype instance boundedAlternate :: Bounded (f a) => Bounded (Alternate f a)
derive newtype instance functorAlternate :: Functor f => Functor (Alternate f)
derive newtype instance invariantAlternate :: Invariant f => Invariant (Alternate f)
derive newtype instance applyAlternate :: Apply f => Apply (Alternate f)
derive newtype instance applicativeAlternate :: Applicative f => Applicative (Alternate f)
derive newtype instance altAlternate :: Alt f => Alt (Alternate f)
derive newtype instance plusAlternate :: Plus f => Plus (Alternate f)
derive newtype instance alternativeAlternate :: Alternative f => Alternative (Alternate f)
derive newtype instance bindAlternate :: Bind f => Bind (Alternate f)
derive newtype instance monadAlternate :: Monad f => Monad (Alternate f)
derive newtype instance extendAlternate :: Extend f => Extend (Alternate f)
derive newtype instance comonadAlternate :: Comonad f => Comonad (Alternate f)
instance showAlternate :: Show (f a) => Show (Alternate f a) where
show (Alternate a) = "(Alternate " <> show a <> ")"
instance semigroupAlternate :: Alt f => Semigroup (Alternate f a) where
append (Alternate a) (Alternate b) = Alternate (a <|> b)
instance monoidAlternate :: Plus f => Monoid (Alternate f a) where
mempty = Alternate empty