@@ -4,7 +4,7 @@ import Prelude
4
4
5
5
import Control.Monad.Eff (Eff )
6
6
import Control.Monad.Eff.Console (CONSOLE , log , logShow )
7
- import Data.Enum (class BoundedEnum , class Enum , Cardinality (..), cardinality , fromEnum , pred , succ , toEnum )
7
+ import Data.Enum (class BoundedEnum , class Enum , Cardinality (..), cardinality , fromEnum , pred , succ , toEnum , enumFromTo )
8
8
import Data.Generic.Rep as G
9
9
import Data.Generic.Rep.Bounded as GBounded
10
10
import Data.Generic.Rep.Enum as GEnum
@@ -68,6 +68,45 @@ instance boundedEnumOption :: BoundedEnum a => BoundedEnum (Option a) where
68
68
toEnum = GEnum .genericToEnum
69
69
fromEnum = GEnum .genericFromEnum
70
70
71
+ data Bit = Zero | One
72
+ derive instance genericBit :: G.Generic Bit _
73
+ instance eqBit :: Eq Bit where
74
+ eq x y = GEq .genericEq x y
75
+ instance ordBit :: Ord Bit where
76
+ compare x y = GOrd .genericCompare x y
77
+ instance showBit :: Show Bit where
78
+ show x = GShow .genericShow x
79
+ instance boundedBit :: Bounded Bit where
80
+ bottom = GBounded .genericBottom
81
+ top = GBounded .genericTop
82
+ instance enumBit :: Enum Bit where
83
+ pred = GEnum .genericPred
84
+ succ = GEnum .genericSucc
85
+ instance boundedEnumBit :: BoundedEnum Bit where
86
+ cardinality = GEnum .genericCardinality
87
+ toEnum = GEnum .genericToEnum
88
+ fromEnum = GEnum .genericFromEnum
89
+
90
+ data Pair a b = Pair a b
91
+ derive instance genericPair :: G.Generic (Pair a b ) _
92
+ instance eqPair :: (Eq a , Eq b ) => Eq (Pair a b ) where
93
+ eq = GEq .genericEq
94
+ instance ordPair :: (Ord a , Ord b ) => Ord (Pair a b ) where
95
+ compare = GOrd .genericCompare
96
+ instance showPair :: (Show a , Show b ) => Show (Pair a b ) where
97
+ show = GShow .genericShow
98
+ instance boundedPair :: (Bounded a , Bounded b ) => Bounded (Pair a b ) where
99
+ bottom = GBounded .genericBottom
100
+ top = GBounded .genericTop
101
+ instance enumPair :: (Bounded a , Enum a , Bounded b , Enum b ) => Enum (Pair a b ) where
102
+ pred = GEnum .genericPred
103
+ succ = GEnum .genericSucc
104
+ instance boundedEnumPair :: (BoundedEnum a , BoundedEnum b ) => BoundedEnum (Pair a b ) where
105
+ cardinality = GEnum .genericCardinality
106
+ toEnum = GEnum .genericToEnum
107
+ fromEnum = GEnum .genericFromEnum
108
+
109
+
71
110
main :: Eff (console :: CONSOLE , assert :: ASSERT ) Unit
72
111
main = do
73
112
logShow (cons 1 (cons 2 Nil ))
@@ -99,6 +138,12 @@ main = do
99
138
log " Checking composite top"
100
139
assert $ top == Some D
101
140
141
+ log " Checking product bottom"
142
+ assert $ bottom == Pair Zero A :: Pair Bit SimpleBounded
143
+
144
+ log " Checking product top"
145
+ assert $ top == Pair One D :: Pair Bit SimpleBounded
146
+
102
147
log " Checking simple pred bottom"
103
148
assert $ pred (bottom :: SimpleBounded ) == Nothing
104
149
@@ -123,16 +168,35 @@ main = do
123
168
log " Checking composite (succ =<< pred top)"
124
169
assert $ (succ =<< pred top) == Just (Some D )
125
170
171
+ log " Checking product pred bottom"
172
+ assert $ pred (bottom :: Pair Bit SimpleBounded ) == Nothing
173
+
174
+ log " Checking product (pred =<< succ bottom)"
175
+ assert $ (pred =<< succ (bottom :: Pair Bit SimpleBounded )) == Just (Pair Zero A )
176
+
177
+ log " Checking product succ top"
178
+ assert $ succ (top :: Pair Bit SimpleBounded ) == Nothing
179
+
180
+ log " Checking product (succ =<< pred top)"
181
+ assert $ (succ =<< pred top) == Just (Pair One D )
182
+
126
183
log " Checking simple cardinality"
127
184
assert $ (cardinality :: Cardinality SimpleBounded ) == Cardinality 4
128
185
129
186
log " Checking composite cardinality"
130
187
assert $ (cardinality :: Cardinality (Option SimpleBounded )) == Cardinality 5
131
188
189
+ log " Checking product cardinality"
190
+ assert $ (cardinality :: Cardinality (Pair Bit SimpleBounded )) == Cardinality 8
191
+
132
192
log " Checking simple toEnum/fromEnum roundtrip"
133
193
assert $ toEnum (fromEnum A ) == Just A
134
194
assert $ toEnum (fromEnum B ) == Just B
135
195
136
196
log " Checking composite toEnum/fromEnum roundtrip"
137
197
assert $ toEnum (fromEnum (None :: Option SimpleBounded )) == Just (None :: Option SimpleBounded )
138
198
assert $ toEnum (fromEnum (Some A )) == Just (Some A )
199
+
200
+ log " Checking product toEnum/fromEnum roundtrip"
201
+ assert $ let allPairs = enumFromTo bottom top :: Array (Pair Bit SimpleBounded )
202
+ in toEnum <<< fromEnum <$> allPairs == Just <$> allPairs
0 commit comments