This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Dual.purs
58 lines (39 loc) · 1.43 KB
/
Dual.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.Dual where
import Prelude
import Control.Comonad (class Comonad)
import Control.Extend (class Extend)
import Data.Functor.Invariant (class Invariant)
import Data.Monoid (class Monoid, mempty)
import Data.Newtype (class Newtype, unwrap)
-- | The dual of a monoid.
-- |
-- | ``` purescript
-- | Dual x <> Dual y == Dual (y <> x)
-- | mempty :: Dual _ == Dual mempty
-- | ```
newtype Dual a = Dual a
derive instance newtypeDual :: Newtype (Dual a) _
derive newtype instance eqDual :: Eq a => Eq (Dual a)
derive newtype instance ordDual :: Ord a => Ord (Dual a)
derive newtype instance boundedDual :: Bounded a => Bounded (Dual a)
instance functorDual :: Functor Dual where
map f (Dual x) = Dual (f x)
instance invariantDual :: Invariant Dual where
imap f _ (Dual x) = Dual (f x)
instance applyDual :: Apply Dual where
apply (Dual f) (Dual x) = Dual (f x)
instance applicativeDual :: Applicative Dual where
pure = Dual
instance bindDual :: Bind Dual where
bind (Dual x) f = f x
instance monadDual :: Monad Dual
instance extendDual :: Extend Dual where
extend f x = Dual (f x)
instance comonadDual :: Comonad Dual where
extract = unwrap
instance showDual :: Show a => Show (Dual a) where
show (Dual a) = "(Dual " <> show a <> ")"
instance semigroupDual :: Semigroup a => Semigroup (Dual a) where
append (Dual x) (Dual y) = Dual (y <> x)
instance monoidDual :: Monoid a => Monoid (Dual a) where
mempty = Dual mempty