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

Commit

Permalink
Data.Generic.Rep.Bounded (#6)
Browse files Browse the repository at this point in the history
* Data.Generic.Rep.Bounded

Generic implementations of Prelude.Bounded class's top and bottom.

* GenericBounded - don't support product types

* GenericBounded - only support NoArguments
  • Loading branch information
matthewleon authored and paf31 committed Jan 11, 2017
1 parent 8749548 commit 6e43a67
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Data/Generic/Rep/Bounded.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Data.Generic.Rep.Bounded
( class GenericBottom
, genericBottom'
, genericBottom
, class GenericTop
, genericTop'
, genericTop
) where

import Data.Generic.Rep

class GenericBottom a where
genericBottom' :: a

instance genericBottomNoArguments :: GenericBottom NoArguments where
genericBottom' = NoArguments

instance genericBottomSum :: GenericBottom a => GenericBottom (Sum a b) where
genericBottom' = Inl genericBottom'

instance genericBottomConstructor :: GenericBottom a => GenericBottom (Constructor name a) where
genericBottom' = Constructor genericBottom'

class GenericTop a where
genericTop' :: a

instance genericTopNoArguments :: GenericTop NoArguments where
genericTop' = NoArguments

instance genericTopSum :: GenericTop b => GenericTop (Sum a b) where
genericTop' = Inr genericTop'

instance genericTopConstructor :: GenericTop a => GenericTop (Constructor name a) where
genericTop' = Constructor genericTop'

-- | A `Generic` implementation of the `bottom` member from the `Bounded` type class.
genericBottom :: forall a rep. (Generic a rep, GenericBottom rep) => a
genericBottom = to genericBottom'

-- | A `Generic` implementation of the `top` member from the `Bounded` type class.
genericTop :: forall a rep. (Generic a rep, GenericTop rep) => a
genericTop = to genericTop'
16 changes: 16 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Data.Generic.Rep as G
import Data.Generic.Rep.Eq as GEq
import Data.Generic.Rep.Ord as GOrd
import Data.Generic.Rep.Show as GShow
import Data.Generic.Rep.Bounded as GBounded

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

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

data SimpleBounded = A | B | C | D
derive instance genericSimpleBounded :: G.Generic SimpleBounded _
instance eqSimpleBounded :: Eq SimpleBounded where
eq x y = GEq.genericEq x y
instance ordSimpleBounded :: Ord SimpleBounded where
compare x y = GOrd.genericCompare x y
instance showSimpleBounded :: Show SimpleBounded where
show x = GShow.genericShow x
instance boundedSimpleBounded :: Bounded SimpleBounded where
bottom = GBounded.genericBottom
top = GBounded.genericTop

main :: Eff (console :: CONSOLE) Unit
main = do
logShow (cons 1 (cons 2 Nil))
Expand All @@ -33,3 +46,6 @@ main = do

logShow (cons 1 (cons 2 Nil) `compare` cons 1 (cons 2 Nil))
logShow (cons 1 (cons 2 Nil) `compare` cons 1 Nil)

logShow (bottom :: SimpleBounded)
logShow (top :: SimpleBounded)

0 comments on commit 6e43a67

Please sign in to comment.