@@ -1,6 +1,7 @@
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
@@ -12,6 +13,7 @@ import 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
1719import Data.Singletons.Prelude.List hiding (All, Union)
@@ -22,8 +24,10 @@ import Data.Union
2224import Data.Vector as V
2325import Data.Vinyl hiding (Dict)
2426import qualified Data.Vinyl.TypeLevel as V
27+import GHC.Exts
2528import GHC.Generics (Generic)
26−import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
29+import GHC.TypeLits
30+ (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
2731import Prelude as P
2832import Test.SmallCheck.Series
2933
@@ -47,33 +51,33 @@ data TextConstraint
4751instance SingKind TextConstraint where
4852 type Demote TextConstraint = DemotedTextConstraint
4953 fromSing = \case
50− STEq n -> withKnownNat n (DTEq $ natVal n)
51− STLt n -> withKnownNat n (DTLt $ natVal n)
52− STLe n -> withKnownNat n (DTLe $ natVal n)
53− STGt n -> withKnownNat n (DTGt $ natVal n)
54− STGe n -> withKnownNat n (DTGe $ natVal n)
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)
5559 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
5660 STEnum s -> let
5761 d :: Sing (s :: [Symbol]) -> [Text]
58− d SNil = []
62+ d SNil = []
5963 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
6064 in DTEnum $ d s
6165 toSing = \case
6266 DTEq n -> case someNatVal n of
6367 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
64− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
6569 DTLt n -> case someNatVal n of
6670 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
67− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
6872 DTLe n -> case someNatVal n of
6973 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
70− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
7175 DTGt n -> case someNatVal n of
7276 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
73− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
7478 DTGe n -> case someNatVal n of
7579 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
76− Nothing -> error "Negative singleton nat"
80+ Nothing -> error "Negative singleton nat"
7781 DTRegex s -> case someSymbolVal (T.unpack s) of
7882 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
7983 DTEnum ss -> case toSing ss of
@@ -87,7 +91,7 @@ data DemotedTextConstraint
8791 | DTGe Integer
8892 | DTRegex Text
8993 | DTEnum [Text]
90− deriving (Generic)
94+ deriving (Generic, Eq, Show)
9195
9296data instance Sing (tc :: TextConstraint) where
9397 STEq :: Sing n -> Sing ('TEq n)
@@ -128,7 +132,7 @@ data DemotedNumberConstraint
128132 | DNGt Integer
129133 | DNGe Integer
130134 | DNEq Integer
131− deriving (Generic)
135+ deriving (Generic, Eq, Show)
132136
133137data instance Sing (nc :: NumberConstraint) where
134138 SNEq :: Sing n -> Sing ('NEq n)
@@ -152,51 +156,87 @@ instance Eq (Sing ('NGe n)) where _ == _ = True
152156instance SingKind NumberConstraint where
153157 type Demote NumberConstraint = DemotedNumberConstraint
154158 fromSing = \case
155− SNEq n -> withKnownNat n (DNEq $ natVal n)
156− SNGt n -> withKnownNat n (DNGt $ natVal n)
157− SNGe n -> withKnownNat n (DNGe $ natVal n)
158− SNLt n -> withKnownNat n (DNLt $ natVal n)
159− SNLe n -> withKnownNat n (DNLe $ natVal n)
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)
160164 toSing = \case
161165 DNEq n -> case someNatVal n of
162166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
163− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
164168 DNGt n -> case someNatVal n of
165169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
166− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
167171 DNGe n -> case someNatVal n of
168172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
169− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
170174 DNLt n -> case someNatVal n of
171175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
172− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
173177 DNLe n -> case someNatVal n of
174178 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
175− Nothing -> error "Negative singleton nat"
179+ Nothing -> error "Negative singleton nat"
176180
177181data ArrayConstraint
178182 = AEq Nat
183+ | AGt Nat
184+ | AGe Nat
185+ | ALt Nat
186+ | ALe Nat
179187 deriving (Generic)
180188
181189data DemotedArrayConstraint
182190 = DAEq Integer
183− deriving (Generic)
191+ | DAGt Integer
192+ | DAGe Integer
193+ | DALt Integer
194+ | DALe Integer
195+ deriving (Generic, Eq, Show)
184196
185197data instance Sing (ac :: ArrayConstraint) where
186198 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)
187203
188204instance 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
189209
190210instance 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
191215
192216instance SingKind ArrayConstraint where
193217 type Demote ArrayConstraint = DemotedArrayConstraint
194218 fromSing = \case
195− SAEq n -> withKnownNat n (DAEq $ natVal n)
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)
196224 toSing = \case
197225 DAEq n -> case someNatVal n of
198226 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
199− Nothing -> error "Negative singleton nat"
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"
200240
201241data Schema
202242 = SchemaText [TextConstraint]
@@ -218,7 +258,7 @@ data DemotedSchema
218258 | DSchemaNull
219259 | DSchemaOptional DemotedSchema
220260 | DSchemaUnion [DemotedSchema]
221− deriving (Generic)
261+ deriving (Generic, Eq, Show)
222262
223263data instance Sing (schema :: Schema) where
224264 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -319,6 +359,10 @@ instance
319359 => Serial m (FieldRepr '(name, schema)) where
320360 series = FieldRepr <$> series
321361
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+
322366data JsonRepr :: Schema -> Type where
323367 ReprText :: Text -> JsonRepr ('SchemaText cs)
324368 ReprNumber :: Scientific -> JsonRepr ('SchemaNumber cs)
@@ -327,58 +371,65 @@ data JsonRepr :: Schema -> Type where
327371 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
328372 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
329373 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
330− ReprUnion :: Union JsonRepr s -> JsonRepr ('SchemaUnion ss)
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)
331379
332−-- | Move to the different package
333−type family UnionAll (f :: u -> *) (rs :: [u]) (c :: * -> Constraint) :: Constraint where
334− UnionAll f '[] c = ()
335− UnionAll f (r ': rs) c = (c (f r), UnionAll f rs c)
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)
336384
337−-- instance (UnionAll f as Show) => Show (Union f as) where
338−-- show (This fa) = "This " P.++ show fa
339−-- show (That u) = "That " P.++ show u
385+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
386+ series = cons0 ReprNull
340387
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
341402instance Show (JsonRepr ('SchemaText cs)) where
342403 show (ReprText t) = "ReprText " P.++ show t
343404
344405instance Show (JsonRepr ('SchemaNumber cs)) where
345406 show (ReprNumber n) = "ReprNumber " P.++ show n
346407
408+instance Show (JsonRepr 'SchemaBoolean) where
409+ show (ReprBoolean n) = "ReprBoolean " P.++ show n
410+
347411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
348412
349413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
350414 show (ReprArray v) = "ReprArray " P.++ show v
351415
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
352423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
353424 show (ReprObject fs) = "ReprObject " P.++ show fs
425+#endif
354426
355427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
356428 show (ReprOptional s) = "ReprOptional " P.++ show s
357429
358−deriving instance UnionAll f as Show => Show (Union f as)
359−
360−instance (Monad m, Serial m Text)
361− => Serial m (JsonRepr ('SchemaText cs)) where
362− series = cons1 ReprText
363−
364−instance (Monad m, Serial m Scientific)
365− => Serial m (JsonRepr ('SchemaNumber cs)) where
366− series = cons1 ReprNumber
367−
368−instance Monad m => Serial m (JsonRepr 'SchemaNull) where
369− series = cons0 ReprNull
370−
371−instance (Serial m (V.Vector (JsonRepr s)))
372− => Serial m (JsonRepr ('SchemaArray cs s)) where
373− series = cons1 ReprArray
374−
375−instance (Serial m (JsonRepr s))
376− => Serial m (JsonRepr ('SchemaOptional s)) where
377− series = cons1 ReprOptional
378−
379−instance (Monad m, Serial m (Rec FieldRepr fs))
380− => Serial m (JsonRepr ('SchemaObject fs)) where
381− 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
382433
383434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
384435 ReprObject a == ReprObject b = a == b
@@ -389,6 +440,9 @@ instance Eq (JsonRepr ('SchemaText cs)) where
389440instance Eq (JsonRepr ('SchemaNumber cs)) where
390441 ReprNumber a == ReprNumber b = a == b
391442
443+instance Eq (JsonRepr 'SchemaBoolean) where
444+ ReprBoolean a == ReprBoolean b = a == b
445+
392446instance Eq (JsonRepr 'SchemaNull) where
393447 ReprNull == ReprNull = True
394448
@@ -398,6 +452,51 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
398452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
399453 ReprOptional a == ReprOptional b = a == b
400454
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+
401500fromOptional
402501 :: SingI s
403502 => Sing ('SchemaOptional s)
@@ -443,25 +542,25 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
443542 fieldRepr <- case s of
444543 SSchemaText so -> case H.lookup fieldName h of
445544 Just v -> withSingI so $ FieldRepr <$> parseJSON v
446− Nothing -> fail "schematext"
545+ Nothing -> fail $ "No text field: " P.++ show fieldName
447546 SSchemaNumber so -> case H.lookup fieldName h of
448547 Just v -> withSingI so $ FieldRepr <$> parseJSON v
449− Nothing -> fail "schemanumber"
548+ Nothing -> fail $ "No number field: " P.++ show fieldName
450549 SSchemaBoolean -> case H.lookup fieldName h of
451550 Just v -> FieldRepr <$> parseJSON v
452− Nothing -> fail "schemaboolean"
551+ Nothing -> fail $ "No boolean field: " P.++ show fieldName
453552 SSchemaNull -> case H.lookup fieldName h of
454553 Just v -> FieldRepr <$> parseJSON v
455− Nothing -> fail "schemanull"
554+ Nothing -> fail $ "No null field: " P.++ show fieldName
456555 SSchemaArray sa sb -> case H.lookup fieldName h of
457556 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
458− Nothing -> fail "schemaarray"
557+ Nothing -> fail $ "No array field: " P.++ show fieldName
459558 SSchemaObject so -> case H.lookup fieldName h of
460559 Just v -> withSingI so $ FieldRepr <$> parseJSON v
461− Nothing -> fail "schemaobject"
560+ Nothing -> fail $ "No object field" P.++ show fieldName
462561 SSchemaOptional so -> case H.lookup fieldName h of
463562 Just v -> withSingI so $ FieldRepr <$> parseJSON v
464− Nothing -> fail "schemaoptional"
563+ Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
465564 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
466565 (fieldRepr :&) <$> demoteFields tl h
467566 ReprObject <$> withObject "Object" (demoteFields fs) value
@@ -473,7 +572,7 @@ instance J.ToJSON (JsonRepr a) where
473572 toJSON (ReprText t) = J.String t
474573 toJSON (ReprNumber n) = J.Number n
475574 toJSON (ReprOptional s) = case s of
476− Just v -> toJSON v
575+ Just v -> toJSON v
477576 Nothing -> J.Null
478577 toJSON (ReprArray v) = J.Array $ toJSON <$> v
479578 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r