1
- -- | This module defines the `Const` type constructor.
2
-
3
1
module Data.Const where
4
2
5
- import Prelude
6
-
7
- import Data.Bifoldable (Bifoldable )
8
- import Data.Foldable (Foldable )
9
- import Data.Functor.Contravariant (Contravariant )
10
- import Data.Functor.Invariant (Invariant , imapF )
11
- import Data.Monoid (Monoid , mempty )
12
- import Data.Traversable (Traversable )
3
+ import Control.Applicative (class Applicative , pure )
4
+ import Control.Apply (class Apply )
5
+ import Control.Bind (class Bind )
6
+ import Control.Semigroupoid (class Semigroupoid )
7
+
8
+ import Data.BooleanAlgebra (class BooleanAlgebra , not , (||), (&&))
9
+ import Data.Bounded (class Bounded , bottom , top )
10
+ import Data.BoundedOrd (class BoundedOrd )
11
+ import Data.DivisionRing (class DivisionRing )
12
+ import Data.Eq (class Eq , (==))
13
+ import Data.Foldable (class Foldable )
14
+ import Data.Functor (class Functor )
15
+ import Data.Functor.Contravariant (class Contravariant )
16
+ import Data.Functor.Invariant (class Invariant , imapF )
17
+ import Data.ModuloSemiring (class ModuloSemiring , mod , (/))
18
+ import Data.Monoid (class Monoid , mempty )
19
+ import Data.Num (class Num )
20
+ import Data.Ord (class Ord , compare )
21
+ import Data.Ring (class Ring , (-))
22
+ import Data.Semigroup (class Semigroup , (<>))
23
+ import Data.Semiring (class Semiring , one , zero , (+), (*))
24
+ import Data.Show (class Show , show )
25
+ import Data.Traversable (class Traversable )
13
26
14
27
-- | The `Const` type constructor, which wraps its first type argument
15
28
-- | and ignores its second. That is, `Const a b` is isomorphic to `a`
@@ -24,46 +37,70 @@ newtype Const a b = Const a
24
37
getConst :: forall a b . Const a b -> a
25
38
getConst (Const x) = x
26
39
27
- instance eqConst :: ( Eq a ) => Eq (Const a b ) where
40
+ instance eqConst :: Eq a => Eq (Const a b ) where
28
41
eq (Const x) (Const y) = x == y
29
42
30
- instance ordConst :: ( Ord a ) => Ord (Const a b ) where
43
+ instance ordConst :: Ord a => Ord (Const a b ) where
31
44
compare (Const x) (Const y) = compare x y
32
45
33
- instance boundedConst :: ( Bounded a ) => Bounded (Const a b ) where
46
+ instance boundedConst :: Bounded a => Bounded (Const a b ) where
34
47
top = Const top
35
48
bottom = Const bottom
36
49
37
- instance showConst :: (Show a ) => Show (Const a b ) where
38
- show (Const x) = " Const (" ++ show x ++ " )"
50
+ instance boundedOrdConst :: BoundedOrd a => BoundedOrd (Const a b )
51
+
52
+ instance showConst :: Show a => Show (Const a b ) where
53
+ show (Const x) = " (Const " <> show x <> " )"
39
54
40
55
instance semigroupoidConst :: Semigroupoid Const where
41
56
compose _ (Const x) = Const x
42
57
43
- instance semigroupConst :: ( Semigroup a ) => Semigroup (Const a b ) where
58
+ instance semigroupConst :: Semigroup a => Semigroup (Const a b ) where
44
59
append (Const x) (Const y) = Const (x <> y)
45
60
46
- instance monoidConst :: ( Monoid a ) => Monoid (Const a b ) where
61
+ instance monoidConst :: Monoid a => Monoid (Const a b ) where
47
62
mempty = Const mempty
48
63
64
+ instance semiringConst :: Semiring a => Semiring (Const a b ) where
65
+ add (Const x) (Const y) = Const (x + y)
66
+ zero = Const zero
67
+ mul (Const x) (Const y) = Const (x * y)
68
+ one = Const one
69
+
70
+ instance ringConst :: Ring a => Ring (Const a b ) where
71
+ sub (Const x) (Const y) = Const (x - y)
72
+
73
+ instance moduloSemiringConst :: ModuloSemiring a => ModuloSemiring (Const a b ) where
74
+ div (Const x) (Const y) = Const (x / y)
75
+ mod (Const x) (Const y) = Const (x `mod` y)
76
+
77
+ instance divisionRingConst :: DivisionRing a => DivisionRing (Const a b )
78
+
79
+ instance numConst :: Num a => Num (Const a b )
80
+
81
+ instance booleanAlgebraConst :: BooleanAlgebra a => BooleanAlgebra (Const a b ) where
82
+ conj (Const x) (Const y) = Const (x && y)
83
+ disj (Const x) (Const y) = Const (x || y)
84
+ not (Const x) = Const (not x)
85
+
49
86
instance functorConst :: Functor (Const a ) where
50
87
map _ (Const x) = Const x
51
88
52
89
instance invariantConst :: Invariant (Const a ) where
53
90
imap = imapF
54
91
55
- instance applyConst :: (Semigroup a ) => Apply (Const a ) where
92
+ instance contravariantConst :: Contravariant (Const a ) where
93
+ cmap _ (Const x) = Const x
94
+
95
+ instance applyConst :: Semigroup a => Apply (Const a ) where
56
96
apply (Const x) (Const y) = Const (x <> y)
57
97
58
- instance bindConst :: ( Semigroup a ) => Bind (Const a ) where
98
+ instance bindConst :: Semigroup a => Bind (Const a ) where
59
99
bind (Const x) _ = Const x
60
100
61
- instance applicativeConst :: ( Monoid a ) => Applicative (Const a ) where
101
+ instance applicativeConst :: Monoid a => Applicative (Const a ) where
62
102
pure _ = Const mempty
63
103
64
- instance contravariantConst :: Contravariant (Const a ) where
65
- cmap _ (Const x) = Const x
66
-
67
104
instance foldableConst :: Foldable (Const a ) where
68
105
foldr _ z _ = z
69
106
foldl _ z _ = z
0 commit comments