@@ -4,7 +4,7 @@ import Prelude
44
55import Control.Monad.Eff (Eff )
66import 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 )
88import Data.Generic.Rep as G
99import Data.Generic.Rep.Bounded as GBounded
1010import Data.Generic.Rep.Enum as GEnum
@@ -68,6 +68,45 @@ instance boundedEnumOption :: BoundedEnum a => BoundedEnum (Option a) where
6868 toEnum = GEnum .genericToEnum
6969 fromEnum = GEnum .genericFromEnum
7070
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+
71110main :: Eff (console :: CONSOLE , assert :: ASSERT ) Unit
72111main = do
73112 logShow (cons 1 (cons 2 Nil ))
@@ -99,6 +138,12 @@ main = do
99138 log " Checking composite top"
100139 assert $ top == Some D
101140
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+
102147 log " Checking simple pred bottom"
103148 assert $ pred (bottom :: SimpleBounded ) == Nothing
104149
@@ -123,16 +168,35 @@ main = do
123168 log " Checking composite (succ =<< pred top)"
124169 assert $ (succ =<< pred top) == Just (Some D )
125170
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+
126183 log " Checking simple cardinality"
127184 assert $ (cardinality :: Cardinality SimpleBounded ) == Cardinality 4
128185
129186 log " Checking composite cardinality"
130187 assert $ (cardinality :: Cardinality (Option SimpleBounded )) == Cardinality 5
131188
189+ log " Checking product cardinality"
190+ assert $ (cardinality :: Cardinality (Pair Bit SimpleBounded )) == Cardinality 8
191+
132192 log " Checking simple toEnum/fromEnum roundtrip"
133193 assert $ toEnum (fromEnum A ) == Just A
134194 assert $ toEnum (fromEnum B ) == Just B
135195
136196 log " Checking composite toEnum/fromEnum roundtrip"
137197 assert $ toEnum (fromEnum (None :: Option SimpleBounded )) == Just (None :: Option SimpleBounded )
138198 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