@@ -1,35 +1,37 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE OverloadedLists #-}
3−{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE OverloadedLists #-}
4+{-# LANGUAGE OverloadedStrings #-}
45{-# OPTIONS_GHC -fprint-explicit-kinds #-}
56
67module Data.Schematic.Schema where
78
8−import Control.Applicative ()
9+import Control.Applicative ((<|>))
910import Control.Monad
1011import Data.Aeson as J
1112import Data.Aeson.Types as J
1213import Data.HashMap.Strict as H
1314import Data.Kind
1415import Data.Maybe
16+import Data.Schematic.Generator
1517import Data.Schematic.Instances ()
1618import Data.Scientific
17−import Data.Singletons.Prelude.List hiding (All)
19+import Data.Singletons.Prelude.List hiding (All, Union)
1820import Data.Singletons.TH
1921import Data.Singletons.TypeLits
2022import Data.Text as T
23+import Data.Union
2124import Data.Vector as V
2225import Data.Vinyl hiding (Dict)
2326import qualified Data.Vinyl.TypeLevel as V
27+import GHC.Exts
2428import GHC.Generics (Generic)
29+import GHC.TypeLits
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
2531import Prelude as P
2632import Test.SmallCheck.Series
2733
2834
29−type family All (c :: k -> Constraint) (s :: [k]) :: Constraint where
30− All c '[] = ()
31− All c (a ': as) = (c a, All c as)
32−
3335type family CRepr (s :: Schema) :: Type where
3436 CRepr ('SchemaText cs) = TextConstraint
3537 CRepr ('SchemaNumber cs) = NumberConstraint
@@ -46,6 +48,40 @@ data TextConstraint
4648 | TEnum [Symbol]
4749 deriving (Generic)
4850
51+instance SingKind TextConstraint where
52+ type Demote TextConstraint = DemotedTextConstraint
53+ fromSing = \case
54+ STEq n -> withKnownNat n (DTEq . fromIntegral $ natVal n)
55+ STLt n -> withKnownNat n (DTLt . fromIntegral $ natVal n)
56+ STLe n -> withKnownNat n (DTLe . fromIntegral $ natVal n)
57+ STGt n -> withKnownNat n (DTGt . fromIntegral $ natVal n)
58+ STGe n -> withKnownNat n (DTGe . fromIntegral $ natVal n)
59+ STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
60+ STEnum s -> let
61+ d :: Sing (s :: [Symbol]) -> [Text]
62+ d SNil = []
63+ d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
64+ in DTEnum $ d s
65+ toSing = \case
66+ DTEq n -> case someNatVal n of
67+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68+ Nothing -> error "Negative singleton nat"
69+ DTLt n -> case someNatVal n of
70+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71+ Nothing -> error "Negative singleton nat"
72+ DTLe n -> case someNatVal n of
73+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74+ Nothing -> error "Negative singleton nat"
75+ DTGt n -> case someNatVal n of
76+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77+ Nothing -> error "Negative singleton nat"
78+ DTGe n -> case someNatVal n of
79+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80+ Nothing -> error "Negative singleton nat"
81+ DTRegex s -> case someSymbolVal (T.unpack s) of
82+ SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
83+ DTEnum ss -> case toSing ss of
84+ SomeSing l -> SomeSing (STEnum l)
4985
5086data DemotedTextConstraint
5187 = DTEq Integer
@@ -55,7 +91,7 @@ data DemotedTextConstraint
5591 | DTGe Integer
5692 | DTRegex Text
5793 | DTEnum [Text]
58− deriving (Generic)
94+ deriving (Generic, Eq, Show)
5995
6096data instance Sing (tc :: TextConstraint) where
6197 STEq :: Sing n -> Sing ('TEq n)
@@ -64,7 +100,7 @@ data instance Sing (tc :: TextConstraint) where
64100 STGt :: Sing n -> Sing ('TGt n)
65101 STGe :: Sing n -> Sing ('TGe n)
66102 STRegex :: Sing s -> Sing ('TRegex s)
67− STEnum :: All KnownSymbol ss => Sing ss -> Sing ('TEnum ss)
103+ STEnum :: Sing ss -> Sing ('TEnum ss)
68104
69105instance (KnownNat n) => SingI ('TEq n) where sing = STEq sing
70106instance (KnownNat n) => SingI ('TGt n) where sing = STGt sing
@@ -72,7 +108,7 @@ instance (KnownNat n) => SingI ('TGe n) where sing = STGe sing
72108instance (KnownNat n) => SingI ('TLt n) where sing = STLt sing
73109instance (KnownNat n) => SingI ('TLe n) where sing = STLe sing
74110instance (KnownSymbol s, SingI s) => SingI ('TRegex s) where sing = STRegex sing
75−instance (All KnownSymbol ss, SingI ss) => SingI ('TEnum ss) where sing = STEnum sing
111+instance (SingI ss) => SingI ('TEnum ss) where sing = STEnum sing
76112
77113instance Eq (Sing ('TEq n)) where _ == _ = True
78114instance Eq (Sing ('TLt n)) where _ == _ = True
@@ -96,7 +132,7 @@ data DemotedNumberConstraint
96132 | DNGt Integer
97133 | DNGe Integer
98134 | DNEq Integer
99− deriving (Generic)
135+ deriving (Generic, Eq, Show)
100136
101137data instance Sing (nc :: NumberConstraint) where
102138 SNEq :: Sing n -> Sing ('NEq n)
@@ -117,46 +153,122 @@ instance Eq (Sing ('NLe n)) where _ == _ = True
117153instance Eq (Sing ('NGt n)) where _ == _ = True
118154instance Eq (Sing ('NGe n)) where _ == _ = True
119155
156+instance SingKind NumberConstraint where
157+ type Demote NumberConstraint = DemotedNumberConstraint
158+ fromSing = \case
159+ SNEq n -> withKnownNat n (DNEq . fromIntegral $ natVal n)
160+ SNGt n -> withKnownNat n (DNGt . fromIntegral $ natVal n)
161+ SNGe n -> withKnownNat n (DNGe . fromIntegral $ natVal n)
162+ SNLt n -> withKnownNat n (DNLt . fromIntegral $ natVal n)
163+ SNLe n -> withKnownNat n (DNLe . fromIntegral $ natVal n)
164+ toSing = \case
165+ DNEq n -> case someNatVal n of
166+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167+ Nothing -> error "Negative singleton nat"
168+ DNGt n -> case someNatVal n of
169+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170+ Nothing -> error "Negative singleton nat"
171+ DNGe n -> case someNatVal n of
172+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173+ Nothing -> error "Negative singleton nat"
174+ DNLt n -> case someNatVal n of
175+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176+ Nothing -> error "Negative singleton nat"
177+ DNLe n -> case someNatVal n of
178+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179+ Nothing -> error "Negative singleton nat"
180+
120181data ArrayConstraint
121182 = AEq Nat
183+ | AGt Nat
184+ | AGe Nat
185+ | ALt Nat
186+ | ALe Nat
122187 deriving (Generic)
123188
124189data DemotedArrayConstraint
125190 = DAEq Integer
126− deriving (Generic)
191+ | DAGt Integer
192+ | DAGe Integer
193+ | DALt Integer
194+ | DALe Integer
195+ deriving (Generic, Eq, Show)
127196
128197data instance Sing (ac :: ArrayConstraint) where
129198 SAEq :: Sing n -> Sing ('AEq n)
199+ SAGt :: Sing n -> Sing ('AGt n)
200+ SAGe :: Sing n -> Sing ('AGe n)
201+ SALt :: Sing n -> Sing ('ALt n)
202+ SALe :: Sing n -> Sing ('ALe n)
130203
131204instance KnownNat n => SingI ('AEq n) where sing = SAEq sing
205+instance KnownNat n => SingI ('AGt n) where sing = SAGt sing
206+instance KnownNat n => SingI ('AGe n) where sing = SAGe sing
207+instance KnownNat n => SingI ('ALt n) where sing = SALt sing
208+instance KnownNat n => SingI ('ALe n) where sing = SALe sing
132209
133210instance Eq (Sing ('AEq n)) where _ == _ = True
211+instance Eq (Sing ('AGt n)) where _ == _ = True
212+instance Eq (Sing ('AGe n)) where _ == _ = True
213+instance Eq (Sing ('ALt n)) where _ == _ = True
214+instance Eq (Sing ('ALe n)) where _ == _ = True
215+
216+instance SingKind ArrayConstraint where
217+ type Demote ArrayConstraint = DemotedArrayConstraint
218+ fromSing = \case
219+ SAEq n -> withKnownNat n (DAEq . fromIntegral $ natVal n)
220+ SAGt n -> withKnownNat n (DAGt . fromIntegral $ natVal n)
221+ SAGe n -> withKnownNat n (DAGe . fromIntegral $ natVal n)
222+ SALt n -> withKnownNat n (DALt . fromIntegral $ natVal n)
223+ SALe n -> withKnownNat n (DALe . fromIntegral $ natVal n)
224+ toSing = \case
225+ DAEq n -> case someNatVal n of
226+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
227+ Nothing -> error "Negative singleton nat"
228+ DAGt n -> case someNatVal n of
229+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAGt (SNat :: Sing n))
230+ Nothing -> error "Negative singleton nat"
231+ DAGe n -> case someNatVal n of
232+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAGe (SNat :: Sing n))
233+ Nothing -> error "Negative singleton nat"
234+ DALt n -> case someNatVal n of
235+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SALt (SNat :: Sing n))
236+ Nothing -> error "Negative singleton nat"
237+ DALe n -> case someNatVal n of
238+ Just (SomeNat (_ :: Proxy n)) -> SomeSing (SALe (SNat :: Sing n))
239+ Nothing -> error "Negative singleton nat"
134240
135241data Schema
136242 = SchemaText [TextConstraint]
243+ | SchemaBoolean
137244 | SchemaNumber [NumberConstraint]
138245 | SchemaObject [(Symbol, Schema)]
139246 | SchemaArray [ArrayConstraint] Schema
140247 | SchemaNull
141248 | SchemaOptional Schema
249+ | SchemaUnion [Schema]
142250 deriving (Generic)
143251
144252data DemotedSchema
145253 = DSchemaText [DemotedTextConstraint]
146254 | DSchemaNumber [DemotedNumberConstraint]
255+ | DSchemaBoolean
147256 | DSchemaObject [(Text, DemotedSchema)]
148257 | DSchemaArray [DemotedArrayConstraint] DemotedSchema
149258 | DSchemaNull
150259 | DSchemaOptional DemotedSchema
151− deriving (Generic)
260+ | DSchemaUnion [DemotedSchema]
261+ deriving (Generic, Eq, Show)
152262
153263data instance Sing (schema :: Schema) where
154264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
155265 SSchemaNumber :: Sing ncs -> Sing ('SchemaNumber ncs)
266+ SSchemaBoolean :: Sing 'SchemaBoolean
156267 SSchemaArray :: Sing acs -> Sing schema -> Sing ('SchemaArray acs schema)
157268 SSchemaObject :: Sing fields -> Sing ('SchemaObject fields)
158269 SSchemaOptional :: Sing s -> Sing ('SchemaOptional s)
159270 SSchemaNull :: Sing 'SchemaNull
271+ SSchemaUnion :: Sing ss -> Sing ('SchemaUnion ss)
160272
161273instance SingI sl => SingI ('SchemaText sl) where
162274 sing = SSchemaText sing
@@ -164,65 +276,52 @@ instance SingI sl => SingI ('SchemaNumber sl) where
164276 sing = SSchemaNumber sing
165277instance SingI 'SchemaNull where
166278 sing = SSchemaNull
279+instance SingI 'SchemaBoolean where
280+ sing = SSchemaBoolean
167281instance (SingI ac, SingI s) => SingI ('SchemaArray ac s) where
168282 sing = SSchemaArray sing sing
169283instance SingI stl => SingI ('SchemaObject stl) where
170284 sing = SSchemaObject sing
171285instance SingI s => SingI ('SchemaOptional s) where
172286 sing = SSchemaOptional sing
287+instance SingI s => SingI ('SchemaUnion s) where
288+ sing = SSchemaUnion sing
173289
174290instance Eq (Sing ('SchemaText cs)) where _ == _ = True
175291instance Eq (Sing ('SchemaNumber cs)) where _ == _ = True
176292instance Eq (Sing 'SchemaNull) where _ == _ = True
293+instance Eq (Sing 'SchemaBoolean) where _ == _ = True
177294instance Eq (Sing ('SchemaArray as s)) where _ == _ = True
178295instance Eq (Sing ('SchemaObject cs)) where _ == _ = True
179296instance Eq (Sing ('SchemaOptional s)) where _ == _ = True
180−
181−-- | One-way 'SingKind` variation
182−-- class SingKind' k where
183−-- type DemoteRep' k :: *
184−-- fromSing' :: Sing (a :: k) -> DemoteRep' k
185−
186−-- instance SingKind' TextConstraint where
187−-- type DemoteRep' TextConstraint = DemotedTextConstraint
188−-- fromSing' = \case
189−-- STEq n -> withKnownNat n (DTEq $ natVal n)
190−-- STLt n -> withKnownNat n (DTLt $ natVal n)
191−-- STLe n -> withKnownNat n (DTLe $ natVal n)
192−-- STGt n -> withKnownNat n (DTGt $ natVal n)
193−-- STGe n -> withKnownNat n (DTGe $ natVal n)
194−-- STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
195−-- STEnum s -> let
196−-- d :: Sing (s :: [Symbol]) -> [Text]
197−-- d SNil = []
198−-- d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
199−-- in DTEnum $ d s
200−
201−-- instance SingKind' NumberConstraint where
202−-- type DemoteRep' NumberConstraint = DemotedNumberConstraint
203−-- fromSing' = \case
204−-- SNLe n -> withKnownNat n (DNLe $ natVal n)
205−-- SNLt n -> withKnownNat n (DNLt $ natVal n)
206−-- SNGe n -> withKnownNat n (DNGe $ natVal n)
207−-- SNGt n -> withKnownNat n (DNGt $ natVal n)
208−-- SNEq n -> withKnownNat n (DNEq $ natVal n)
209−
210−-- instance SingKind' ArrayConstraint where
211−-- type DemoteRep' ArrayConstraint = DemotedArrayConstraint
212−-- fromSing' = \case
213−-- SAEq n -> withKnownNat n (DAEq $ natVal n)
214−
215−-- instance SingKind' Schema where
216−-- type DemoteRep' Schema = DemotedSchema
217−-- fromSing' = \case
218−-- SSchemaText tcs -> DSchemaText $ fromSing' tcs
219− -- SchemaText [TextConstraint]
220− -- SchemaNumber [NumberConstraint]
221− -- SchemaObject [(Symbol, Schema)]
222− -- SchemaArray [ArrayConstraint] Schema
223− -- SchemaNull
224− -- SchemaOptional Schema
225−
297+instance Eq (Sing ('SchemaUnion s)) where _ == _ = True
298+
299+instance SingKind Schema where
300+ type Demote Schema = DemotedSchema
301+ fromSing = \case
302+ SSchemaText cs -> DSchemaText $ fromSing cs
303+ SSchemaNumber cs -> DSchemaNumber $ fromSing cs
304+ SSchemaBoolean -> DSchemaBoolean
305+ SSchemaArray cs s -> DSchemaArray (fromSing cs) (fromSing s)
306+ SSchemaOptional s -> DSchemaOptional $ fromSing s
307+ SSchemaNull -> DSchemaNull
308+ SSchemaObject cs -> DSchemaObject $ fromSing cs
309+ SSchemaUnion ss -> DSchemaUnion $ fromSing ss
310+ toSing = \case
311+ DSchemaText cs -> case toSing cs of
312+ SomeSing scs -> SomeSing $ SSchemaText scs
313+ DSchemaNumber cs -> case toSing cs of
314+ SomeSing scs -> SomeSing $ SSchemaNumber scs
315+ DSchemaBoolean -> SomeSing $ SSchemaBoolean
316+ DSchemaArray cs sch -> case (toSing cs, toSing sch) of
317+ (SomeSing scs, SomeSing ssch) -> SomeSing $ SSchemaArray scs ssch
318+ DSchemaOptional sch -> case toSing sch of
319+ SomeSing ssch -> SomeSing $ SSchemaOptional ssch
320+ DSchemaNull -> SomeSing SSchemaNull
321+ DSchemaObject cs -> case toSing cs of
322+ SomeSing scs -> SomeSing $ SSchemaObject scs
323+ DSchemaUnion ss -> case toSing ss of
324+ SomeSing sss -> SomeSing $ SSchemaUnion sss
226325
227326data FieldRepr :: (Symbol, Schema) -> Type where
228327 FieldRepr
@@ -260,53 +359,77 @@ instance
260359 => Serial m (FieldRepr '(name, schema)) where
261360 series = FieldRepr <$> series
262361
362+type family USubsets (u :: [k]) :: Constraint where
363+ USubsets '[] = ()
364+ USubsets (h ': tl) = (USubset tl (h ': tl) (V.RImage tl (h ': tl)), USubsets tl)
365+
263366data JsonRepr :: Schema -> Type where
264367 ReprText :: Text -> JsonRepr ('SchemaText cs)
265368 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
369+ ReprBoolean :: Bool -> JsonRepr 'SchemaBoolean
266370 ReprNull :: JsonRepr 'SchemaNull
267371 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
268372 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
269373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
374+ ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
375+
376+instance (Monad m, Serial m Text, SingI cs)
377+ => Serial m (JsonRepr ('SchemaText cs)) where
378+ series = decDepth $ fmap ReprText $ textSeries $ fromSing (sing :: Sing cs)
379+
380+instance (Monad m, Serial m Scientific, SingI cs)
381+ => Serial m (JsonRepr ('SchemaNumber cs)) where
382+ series = decDepth $ fmap ReprNumber
383+ $ numberSeries $ fromSing (sing :: Sing cs)
384+
385+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
386+ series = cons0 ReprNull
270387
388+instance (Serial m (JsonRepr s), Serial m (V.Vector (JsonRepr s)), SingI cs)
389+ => Serial m (JsonRepr ('SchemaArray cs s)) where
390+ series = decDepth $ fmap ReprArray
391+ $ arraySeries $ fromSing (sing :: Sing cs)
392+
393+instance (Serial m (JsonRepr s))
394+ => Serial m (JsonRepr ('SchemaOptional s)) where
395+ series = cons1 ReprOptional
396+
397+instance (Monad m, Serial m (Rec FieldRepr fs))
398+ => Serial m (JsonRepr ('SchemaObject fs)) where
399+ series = cons1 ReprObject
400+
401+-- | Move to the union package
271402instance Show (JsonRepr ('SchemaText cs)) where
272403 show (ReprText t) = "ReprText " P.++ show t
273404
274405instance Show (JsonRepr ('SchemaNumber cs)) where
275406 show (ReprNumber n) = "ReprNumber " P.++ show n
276407
408+instance Show (JsonRepr 'SchemaBoolean) where
409+ show (ReprBoolean n) = "ReprBoolean " P.++ show n
410+
277411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
278412
279413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
280414 show (ReprArray v) = "ReprArray " P.++ show v
281415
416+#if MIN_VERSION_base(4,12,0)
417+instance
418+ ( V.RecAll FieldRepr fs Show, RMap fs, ReifyConstraint Show FieldRepr fs
419+ , RecordToList fs )
420+ => Show (JsonRepr ('SchemaObject fs)) where
421+ show (ReprObject fs) = "ReprObject " P.++ show fs
422+#else
282423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
283424 show (ReprObject fs) = "ReprObject " P.++ show fs
425+#endif
284426
285427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
286428 show (ReprOptional s) = "ReprOptional " P.++ show s
287429
288−instance (Monad m, Serial m Text)
289− => Serial m (JsonRepr ('SchemaText cs)) where
290− series = cons1 ReprText
291−
292−instance (Monad m, Serial m Scientific)
293− => Serial m (JsonRepr ('SchemaNumber cs)) where
294− series = cons1 ReprNumber
295−
296−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
297− series = cons0 ReprNull
298−
299−instance (Serial m (V.Vector (JsonRepr s)))
300− => Serial m (JsonRepr ('SchemaArray cs s)) where
301− series = cons1 ReprArray
302−
303−instance (Serial m (JsonRepr s))
304− => Serial m (JsonRepr ('SchemaOptional s)) where
305− series = cons1 ReprOptional
306−
307−instance (Monad m, Serial m (Rec FieldRepr fs))
308− => Serial m (JsonRepr ('SchemaObject fs)) where
309− series = cons1 ReprObject
430+instance Show (Union JsonRepr (h ': tl))
431+ => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432+ show (ReprUnion s) = "ReprUnion " P.++ show s
310433
311434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
312435 ReprObject a == ReprObject b = a == b
@@ -317,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
317440instance Eq (JsonRepr ('SchemaNumber cs)) where
318441 ReprNumber a == ReprNumber b = a == b
319442
443+instance Eq (JsonRepr 'SchemaBoolean) where
444+ ReprBoolean a == ReprBoolean b = a == b
445+
320446instance Eq (JsonRepr 'SchemaNull) where
321447 ReprNull == ReprNull = True
322448
@@ -326,6 +452,51 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
326452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
327453 ReprOptional a == ReprOptional b = a == b
328454
455+instance Eq (Union JsonRepr (h ': tl))
456+ => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457+ ReprUnion a == ReprUnion b = a == b
458+
459+instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460+ ReprObject a `compare` ReprObject b = a `compare` b
461+
462+instance Ord (JsonRepr ('SchemaText cs)) where
463+ ReprText a `compare` ReprText b = a `compare` b
464+
465+instance Ord (JsonRepr ('SchemaNumber cs)) where
466+ ReprNumber a `compare` ReprNumber b = a `compare` b
467+
468+instance Ord (JsonRepr 'SchemaBoolean) where
469+ ReprBoolean a `compare` ReprBoolean b = a `compare` b
470+
471+instance Ord (JsonRepr 'SchemaNull) where
472+ compare _ _ = EQ
473+
474+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
475+ ReprArray a `compare` ReprArray b = a `compare` b
476+
477+instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478+ ReprOptional a `compare` ReprOptional b = a `compare` b
479+
480+instance Ord (Union JsonRepr (h ': tl))
481+ => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482+ ReprUnion a `compare` ReprUnion b = a `compare` b
483+
484+instance IsList (JsonRepr ('SchemaArray cs s)) where
485+ type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
486+ fromList = ReprArray . GHC.Exts.fromList
487+ toList (ReprArray v) = GHC.Exts.toList v
488+
489+instance Num (JsonRepr ('SchemaNumber cs)) where
490+ ReprNumber a + ReprNumber b = ReprNumber $ a + b
491+ ReprNumber a - ReprNumber b = ReprNumber $ a - b
492+ ReprNumber a * ReprNumber b = ReprNumber $ a * b
493+ abs (ReprNumber a) = ReprNumber $ abs a
494+ signum (ReprNumber a) = ReprNumber $ signum a
495+ fromInteger = ReprNumber . fromIntegral
496+
497+instance IsString (JsonRepr ('SchemaText cs)) where
498+ fromString = ReprText . fromString
499+
329500fromOptional
330501 :: SingI s
331502 => Sing ('SchemaOptional s)
@@ -333,10 +504,29 @@ fromOptional
333504 -> Parser (Maybe (JsonRepr s))
334505fromOptional _ = parseJSON
335506
507+parseUnion
508+ :: FromJSON (JsonRepr ('SchemaUnion ss))
509+ => sing (ss :: [Schema])
510+ -> Value
511+ -> Parser (JsonRepr ('SchemaUnion ss))
512+parseUnion _ val = parseJSON val
513+
514+instance FromJSON (Union JsonRepr '[]) where
515+ parseJSON = fail "empty union"
516+
517+instance (SingI a, FromJSON (Union JsonRepr as)) => FromJSON (Union JsonRepr (a ': as)) where
518+ parseJSON val = (This <$> parseJSON val)
519+ <|> (That <$> (parseJSON val :: Parser (Union JsonRepr as)))
520+
521+instance ToJSON (Union JsonRepr as) where
522+ toJSON (This fa) = toJSON fa
523+ toJSON (That u) = toJSON u
524+
336525instance SingI schema => J.FromJSON (JsonRepr schema) where
337526 parseJSON value = case sing :: Sing schema of
338527 SSchemaText _ -> withText "String" (pure . ReprText) value
339528 SSchemaNumber _ -> withScientific "Number" (pure . ReprNumber) value
529+ SSchemaBoolean -> ReprBoolean <$> parseJSON value
340530 SSchemaNull -> case value of
341531 J.Null -> pure ReprNull
342532 _ -> typeMismatch "Null" value
@@ -352,31 +542,37 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
352542 fieldRepr <- case s of
353543 SSchemaText so -> case H.lookup fieldName h of
354544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
355− Nothing -> fail "schematext"
545+ Nothing -> fail $ "No text field: " P.++ show fieldName
356546 SSchemaNumber so -> case H.lookup fieldName h of
357547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
358− Nothing -> fail "schemanumber"
548+ Nothing -> fail $ "No number field: " P.++ show fieldName
549+ SSchemaBoolean -> case H.lookup fieldName h of
550+ Just v -> FieldRepr <$> parseJSON v
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
359552 SSchemaNull -> case H.lookup fieldName h of
360553 Just v -> FieldRepr <$> parseJSON v
361− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
362555 SSchemaArray sa sb -> case H.lookup fieldName h of
363556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
364− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
365558 SSchemaObject so -> case H.lookup fieldName h of
366559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
367− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
368561 SSchemaOptional so -> case H.lookup fieldName h of
369562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
370− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
564+ SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
371565 (fieldRepr :&) <$> demoteFields tl h
372566 ReprObject <$> withObject "Object" (demoteFields fs) value
567+ SSchemaUnion ss -> parseUnion ss value
373568
374569instance J.ToJSON (JsonRepr a) where
375570 toJSON ReprNull = J.Null
571+ toJSON (ReprBoolean b) = J.Bool b
376572 toJSON (ReprText t) = J.String t
377573 toJSON (ReprNumber n) = J.Number n
378574 toJSON (ReprOptional s) = case s of
379− Just v -> toJSON v
575+ Just v -> toJSON v
380576 Nothing -> J.Null
381577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
382578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r
@@ -389,6 +585,7 @@ instance J.ToJSON (JsonRepr a) where
389585 fold = \case
390586 RNil -> []
391587 fr@(FieldRepr _) :& tl -> (extract fr) : fold tl
588+ toJSON (ReprUnion u) = toJSON u
392589
393590class FalseConstraint a
394591