Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 6e43a67

Browse files
matthewleonpaf31
authored andcommitted
Data.Generic.Rep.Bounded (#6)
* Data.Generic.Rep.Bounded Generic implementations of Prelude.Bounded class's top and bottom. * GenericBounded - don't support product types * GenericBounded - only support NoArguments
1 parent 8749548 commit 6e43a67

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Data/Generic/Rep/Bounded.purs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Data.Generic.Rep.Bounded
2+
( class GenericBottom
3+
, genericBottom'
4+
, genericBottom
5+
, class GenericTop
6+
, genericTop'
7+
, genericTop
8+
) where
9+
10+
import Data.Generic.Rep
11+
12+
class GenericBottom a where
13+
genericBottom' :: a
14+
15+
instance genericBottomNoArguments :: GenericBottom NoArguments where
16+
genericBottom' = NoArguments
17+
18+
instance genericBottomSum :: GenericBottom a => GenericBottom (Sum a b) where
19+
genericBottom' = Inl genericBottom'
20+
21+
instance genericBottomConstructor :: GenericBottom a => GenericBottom (Constructor name a) where
22+
genericBottom' = Constructor genericBottom'
23+
24+
class GenericTop a where
25+
genericTop' :: a
26+
27+
instance genericTopNoArguments :: GenericTop NoArguments where
28+
genericTop' = NoArguments
29+
30+
instance genericTopSum :: GenericTop b => GenericTop (Sum a b) where
31+
genericTop' = Inr genericTop'
32+
33+
instance genericTopConstructor :: GenericTop a => GenericTop (Constructor name a) where
34+
genericTop' = Constructor genericTop'
35+
36+
-- | A `Generic` implementation of the `bottom` member from the `Bounded` type class.
37+
genericBottom :: forall a rep. (Generic a rep, GenericBottom rep) => a
38+
genericBottom = to genericBottom'
39+
40+
-- | A `Generic` implementation of the `top` member from the `Bounded` type class.
41+
genericTop :: forall a rep. (Generic a rep, GenericTop rep) => a
42+
genericTop = to genericTop'

test/Main.purs

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Data.Generic.Rep as G
77
import Data.Generic.Rep.Eq as GEq
88
import Data.Generic.Rep.Ord as GOrd
99
import Data.Generic.Rep.Show as GShow
10+
import Data.Generic.Rep.Bounded as GBounded
1011

1112
data List a = Nil | Cons { head :: a, tail :: List a }
1213

@@ -24,6 +25,18 @@ instance ordList :: Ord a => Ord (List a) where
2425
instance showList :: Show a => Show (List a) where
2526
show x = GShow.genericShow x
2627

28+
data SimpleBounded = A | B | C | D
29+
derive instance genericSimpleBounded :: G.Generic SimpleBounded _
30+
instance eqSimpleBounded :: Eq SimpleBounded where
31+
eq x y = GEq.genericEq x y
32+
instance ordSimpleBounded :: Ord SimpleBounded where
33+
compare x y = GOrd.genericCompare x y
34+
instance showSimpleBounded :: Show SimpleBounded where
35+
show x = GShow.genericShow x
36+
instance boundedSimpleBounded :: Bounded SimpleBounded where
37+
bottom = GBounded.genericBottom
38+
top = GBounded.genericTop
39+
2740
main :: Eff (console :: CONSOLE) Unit
2841
main = do
2942
logShow (cons 1 (cons 2 Nil))
@@ -33,3 +46,6 @@ main = do
3346

3447
logShow (cons 1 (cons 2 Nil) `compare` cons 1 (cons 2 Nil))
3548
logShow (cons 1 (cons 2 Nil) `compare` cons 1 Nil)
49+
50+
logShow (bottom :: SimpleBounded)
51+
logShow (top :: SimpleBounded)

0 commit comments

Comments
 (0)