Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 3853bac

Browse files
committed
Merge pull request #11 from purescript/bool
Fix #7
2 parents ee161ac + 30ef2df commit 3853bac

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

docs/Data/Generic.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ data GenericSpine
77
= SProd String (Array (Unit -> GenericSpine))
88
| SRecord (Array { recLabel :: String, recValue :: Unit -> GenericSpine })
99
| SNumber Number
10+
| SBoolean Boolean
1011
| SInt Int
1112
| SString String
1213
| SArray (Array (Unit -> GenericSpine))
@@ -27,6 +28,7 @@ data GenericSignature
2728
= SigProd (Array { sigConstructor :: String, sigValues :: Array (Unit -> GenericSignature) })
2829
| SigRecord (Array { recLabel :: String, recValue :: Unit -> GenericSignature })
2930
| SigNumber
31+
| SigBoolean
3032
| SigInt
3133
| SigString
3234
| SigArray (Unit -> GenericSignature)

src/Data/Generic.purs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ import Data.String (joinWith)
2222
data GenericSpine = SProd String (Array (Unit -> GenericSpine))
2323
| SRecord (Array {recLabel :: String, recValue :: Unit -> GenericSpine})
2424
| SNumber Number
25+
| SBoolean Boolean
2526
| SInt Int
2627
| SString String
2728
| SArray (Array (Unit -> GenericSpine))
2829

2930
-- | A GenericSignature is a universal representation of the structure of an arbitrary data structure (that does not contain function arrows).
3031
data GenericSignature = SigProd (Array {sigConstructor :: String, sigValues :: Array (Unit -> GenericSignature)})
3132
| SigRecord (Array {recLabel :: String, recValue :: Unit -> GenericSignature})
32-
| SigNumber | SigInt | SigString | SigArray (Unit -> GenericSignature)
33+
| SigNumber
34+
| SigBoolean
35+
| SigInt
36+
| SigString
37+
| SigArray (Unit -> GenericSignature)
3338

3439
-- | A proxy is a simple placeholder data type to allow us to pass type-level data at runtime.
3540
data Proxy a = Proxy
@@ -63,12 +68,9 @@ instance genericString :: Generic String where
6368
fromSpine _ = Nothing
6469

6570
instance genericBool :: Generic Boolean where
66-
toSpine true = SProd "true" []
67-
toSpine false = SProd "false" []
68-
toSignature _ = SigProd [{sigConstructor: "true",sigValues: []},
69-
{sigConstructor: "false",sigValues: []}]
70-
fromSpine (SProd "true" []) = Just true
71-
fromSpine (SProd "false" []) = Just false
71+
toSpine b = SBoolean b
72+
toSignature _ = SigBoolean
73+
fromSpine (SBoolean b) = Just b
7274
fromSpine _ = Nothing
7375

7476
instance genericArray :: (Generic a) => Generic (Array a) where
@@ -127,11 +129,11 @@ genericShowPrec d (SProd s arr) =
127129
showParen true x = "(" <> x <> ")"
128130

129131
genericShowPrec d (SRecord xs) = "{" <> joinWith ", " (map (\x -> x.recLabel <> ": " <> genericShowPrec 0 (x.recValue unit)) xs) <> "}"
130-
131-
genericShowPrec d (SInt x) = show x
132-
genericShowPrec d (SNumber x) = show x
133-
genericShowPrec d (SString x) = show x
134-
genericShowPrec d (SArray xs) = "[" <> joinWith ", " (map (\x -> genericShowPrec 0 (x unit)) xs) <> "]"
132+
genericShowPrec d (SBoolean x) = show x
133+
genericShowPrec d (SInt x) = show x
134+
genericShowPrec d (SNumber x) = show x
135+
genericShowPrec d (SString x) = show x
136+
genericShowPrec d (SArray xs) = "[" <> joinWith ", " (map (\x -> genericShowPrec 0 (x unit)) xs) <> "]"
135137

136138
-- | This function can be used as the default instance for Show for any instance of Generic
137139
gShow :: forall a. (Generic a) => a -> String
@@ -145,10 +147,11 @@ instance eqGeneric :: Eq GenericSpine where
145147
&& zipAll (\x y -> x unit == y unit) arr1 arr2
146148
eq (SRecord xs) (SRecord ys) = length xs == length ys && zipAll go xs ys
147149
where go x y = x.recLabel == y.recLabel && x.recValue unit == y.recValue unit
148-
eq (SInt x) (SInt y) = x == y
149-
eq (SNumber x) (SNumber y) = x == y
150-
eq (SString x) (SString y) = x == y
151-
eq (SArray xs) (SArray ys) = length xs == length ys && zipAll (\x y -> x unit == y unit) xs ys
150+
eq (SInt x) (SInt y) = x == y
151+
eq (SNumber x) (SNumber y) = x == y
152+
eq (SBoolean x) (SBoolean y) = x == y
153+
eq (SString x) (SString y) = x == y
154+
eq (SArray xs) (SArray ys) = length xs == length ys && zipAll (\x y -> x unit == y unit) xs ys
152155
eq _ _ = false
153156

154157
-- | This function can be used as the default instance for Eq for any instance of Generic
@@ -180,6 +183,9 @@ instance ordGeneric :: Ord GenericSpine where
180183
compare (SInt x) (SInt y) = compare x y
181184
compare (SInt _) _ = LT
182185
compare _ (SInt _) = GT
186+
compare (SBoolean x) (SBoolean y) = compare x y
187+
compare (SBoolean _) _ = LT
188+
compare _ (SBoolean _) = GT
183189
compare (SNumber x) (SNumber y) = compare x y
184190
compare (SNumber _) _ = LT
185191
compare _ (SNumber _) = GT

0 commit comments

Comments
 (0)