@@ -1,7 +1,6 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE OverloadedLists #-}
4−{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE OverloadedLists #-}
3+{-# LANGUAGE OverloadedStrings #-}
54{-# OPTIONS_GHC -fprint-explicit-kinds #-}
65
76module Data.Schematic.Schema where
@@ -13,7 +12,6 @@ import Data.Aeson.Types as J
1312import Data.HashMap.Strict as H
1413import Data.Kind
1514import Data.Maybe
16−import Data.Schematic.Generator
1715import Data.Schematic.Instances ()
1816import Data.Scientific
1917import Data.Singletons.Prelude.List hiding (All, Union)
@@ -24,10 +22,8 @@ import Data.Union
2422import Data.Vector as V
2523import Data.Vinyl hiding (Dict)
2624import qualified Data.Vinyl.TypeLevel as V
27−import GHC.Exts
2825import GHC.Generics (Generic)
29−import GHC.TypeLits
30− (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
26+import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
3127import Prelude as P
3228import Test.SmallCheck.Series
3329
@@ -51,33 +47,33 @@ data TextConstraint
5147instance SingKind TextConstraint where
5248 type Demote TextConstraint = DemotedTextConstraint
5349 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)
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)
5955 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
6056 STEnum s -> let
6157 d :: Sing (s :: [Symbol]) -> [Text]
62− d SNil = []
58+ d SNil = []
6359 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
6460 in DTEnum $ d s
6561 toSing = \case
6662 DTEq n -> case someNatVal n of
6763 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
64+ Nothing -> error "Negative singleton nat"
6965 DTLt n -> case someNatVal n of
7066 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
67+ Nothing -> error "Negative singleton nat"
7268 DTLe n -> case someNatVal n of
7369 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
70+ Nothing -> error "Negative singleton nat"
7571 DTGt n -> case someNatVal n of
7672 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
73+ Nothing -> error "Negative singleton nat"
7874 DTGe n -> case someNatVal n of
7975 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80− Nothing -> error "Negative singleton nat"
76+ Nothing -> error "Negative singleton nat"
8177 DTRegex s -> case someSymbolVal (T.unpack s) of
8278 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
8379 DTEnum ss -> case toSing ss of
@@ -91,7 +87,7 @@ data DemotedTextConstraint
9187 | DTGe Integer
9288 | DTRegex Text
9389 | DTEnum [Text]
94− deriving (Generic, Eq, Show)
90+ deriving (Generic)
9591
9692data instance Sing (tc :: TextConstraint) where
9793 STEq :: Sing n -> Sing ('TEq n)
@@ -132,7 +128,7 @@ data DemotedNumberConstraint
132128 | DNGt Integer
133129 | DNGe Integer
134130 | DNEq Integer
135− deriving (Generic, Eq, Show)
131+ deriving (Generic)
136132
137133data instance Sing (nc :: NumberConstraint) where
138134 SNEq :: Sing n -> Sing ('NEq n)
@@ -156,87 +152,51 @@ instance Eq (Sing ('NGe n)) where _ == _ = True
156152instance SingKind NumberConstraint where
157153 type Demote NumberConstraint = DemotedNumberConstraint
158154 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)
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)
164160 toSing = \case
165161 DNEq n -> case someNatVal n of
166162 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
163+ Nothing -> error "Negative singleton nat"
168164 DNGt n -> case someNatVal n of
169165 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
166+ Nothing -> error "Negative singleton nat"
171167 DNGe n -> case someNatVal n of
172168 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
169+ Nothing -> error "Negative singleton nat"
174170 DNLt n -> case someNatVal n of
175171 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
172+ Nothing -> error "Negative singleton nat"
177173 DNLe n -> case someNatVal n of
178174 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179− Nothing -> error "Negative singleton nat"
175+ Nothing -> error "Negative singleton nat"
180176
181177data ArrayConstraint
182178 = AEq Nat
183− | AGt Nat
184− | AGe Nat
185− | ALt Nat
186− | ALe Nat
187179 deriving (Generic)
188180
189181data DemotedArrayConstraint
190182 = DAEq Integer
191− | DAGt Integer
192− | DAGe Integer
193− | DALt Integer
194− | DALe Integer
195− deriving (Generic, Eq, Show)
183+ deriving (Generic)
196184
197185data instance Sing (ac :: ArrayConstraint) where
198186 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)
203187
204188instance 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
209189
210190instance 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
215191
216192instance SingKind ArrayConstraint where
217193 type Demote ArrayConstraint = DemotedArrayConstraint
218194 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)
195+ SAEq n -> withKnownNat n (DAEq $ natVal n)
224196 toSing = \case
225197 DAEq n -> case someNatVal n of
226198 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"
199+ Nothing -> error "Negative singleton nat"
240200
241201data Schema
242202 = SchemaText [TextConstraint]
@@ -258,7 +218,7 @@ data DemotedSchema
258218 | DSchemaNull
259219 | DSchemaOptional DemotedSchema
260220 | DSchemaUnion [DemotedSchema]
261− deriving (Generic, Eq, Show)
221+ deriving (Generic)
262222
263223data instance Sing (schema :: Schema) where
264224 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -371,65 +331,61 @@ data JsonRepr :: Schema -> Type where
371331 ReprArray :: V.Vector (JsonRepr s) -> JsonRepr ('SchemaArray cs s)
372332 ReprObject :: Rec FieldRepr fs -> JsonRepr ('SchemaObject fs)
373333 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
334+ ReprUnion
335+ -- :: USubsets (h ': tl)
336+ :: Union JsonRepr (h ': tl)
337+ -> JsonRepr ('SchemaUnion (h ': tl))
387338
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
339+-- | Move to the union package
340+type family UnionAll (f :: u -> *) (rs :: [u]) (c :: * -> Constraint) :: Constraint where
341+ UnionAll f '[] c = ()
342+ UnionAll f (r ': rs) c = (c (f r), UnionAll f rs c)
396343
397−instance (Monad m, Serial m (Rec FieldRepr fs))
398− => Serial m (JsonRepr ('SchemaObject fs)) where
399− series = cons1 ReprObject
344+-- instance (UnionAll f as Show) => Show (Union f as) where
345+-- show (This fa) = "This " P.++ show fa
346+-- show (That u) = "That " P.++ show u
400347
401−-- | Move to the union package
402348instance Show (JsonRepr ('SchemaText cs)) where
403349 show (ReprText t) = "ReprText " P.++ show t
404350
405351instance Show (JsonRepr ('SchemaNumber cs)) where
406352 show (ReprNumber n) = "ReprNumber " P.++ show n
407353
408−instance Show (JsonRepr 'SchemaBoolean) where
409− show (ReprBoolean n) = "ReprBoolean " P.++ show n
410−
411354instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
412355
413356instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
414357 show (ReprArray v) = "ReprArray " P.++ show v
415358
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
423359instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424360 show (ReprObject fs) = "ReprObject " P.++ show fs
425−#endif
426361
427362instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428363 show (ReprOptional s) = "ReprOptional " P.++ show s
429364
430−instance Show (Union JsonRepr (h ': tl))
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
365+deriving instance UnionAll f as Show => Show (Union f as)
366+
367+instance (Monad m, Serial m Text)
368+ => Serial m (JsonRepr ('SchemaText cs)) where
369+ series = cons1 ReprText
370+
371+instance (Monad m, Serial m Scientific)
372+ => Serial m (JsonRepr ('SchemaNumber cs)) where
373+ series = cons1 ReprNumber
374+
375+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
376+ series = cons0 ReprNull
377+
378+instance (Serial m (V.Vector (JsonRepr s)))
379+ => Serial m (JsonRepr ('SchemaArray cs s)) where
380+ series = cons1 ReprArray
381+
382+instance (Serial m (JsonRepr s))
383+ => Serial m (JsonRepr ('SchemaOptional s)) where
384+ series = cons1 ReprOptional
385+
386+instance (Monad m, Serial m (Rec FieldRepr fs))
387+ => Serial m (JsonRepr ('SchemaObject fs)) where
388+ series = cons1 ReprObject
433389
434390instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435391 ReprObject a == ReprObject b = a == b
@@ -440,9 +396,6 @@ instance Eq (JsonRepr ('SchemaText cs)) where
440396instance Eq (JsonRepr ('SchemaNumber cs)) where
441397 ReprNumber a == ReprNumber b = a == b
442398
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
445−
446399instance Eq (JsonRepr 'SchemaNull) where
447400 ReprNull == ReprNull = True
448401
@@ -452,51 +405,6 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
452405instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453406 ReprOptional a == ReprOptional b = a == b
454407
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−
500408fromOptional
501409 :: SingI s
502410 => Sing ('SchemaOptional s)
@@ -542,25 +450,25 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
542450 fieldRepr <- case s of
543451 SSchemaText so -> case H.lookup fieldName h of
544452 Just v -> withSingI so $ FieldRepr <$> parseJSON v
545− Nothing -> fail $ "No text field: " P.++ show fieldName
453+ Nothing -> fail "schematext"
546454 SSchemaNumber so -> case H.lookup fieldName h of
547455 Just v -> withSingI so $ FieldRepr <$> parseJSON v
548− Nothing -> fail $ "No number field: " P.++ show fieldName
456+ Nothing -> fail "schemanumber"
549457 SSchemaBoolean -> case H.lookup fieldName h of
550458 Just v -> FieldRepr <$> parseJSON v
551− Nothing -> fail $ "No boolean field: " P.++ show fieldName
459+ Nothing -> fail "schemaboolean"
552460 SSchemaNull -> case H.lookup fieldName h of
553461 Just v -> FieldRepr <$> parseJSON v
554− Nothing -> fail $ "No null field: " P.++ show fieldName
462+ Nothing -> fail "schemanull"
555463 SSchemaArray sa sb -> case H.lookup fieldName h of
556464 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
557− Nothing -> fail $ "No array field: " P.++ show fieldName
465+ Nothing -> fail "schemaarray"
558466 SSchemaObject so -> case H.lookup fieldName h of
559467 Just v -> withSingI so $ FieldRepr <$> parseJSON v
560− Nothing -> fail $ "No object field" P.++ show fieldName
468+ Nothing -> fail "schemaobject"
561469 SSchemaOptional so -> case H.lookup fieldName h of
562470 Just v -> withSingI so $ FieldRepr <$> parseJSON v
563− Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
471+ Nothing -> fail "schemaoptional"
564472 SSchemaUnion ss -> withSingI ss $ FieldRepr <$> parseUnion ss value
565473 (fieldRepr :&) <$> demoteFields tl h
566474 ReprObject <$> withObject "Object" (demoteFields fs) value
@@ -572,7 +480,7 @@ instance J.ToJSON (JsonRepr a) where
572480 toJSON (ReprText t) = J.String t
573481 toJSON (ReprNumber n) = J.Number n
574482 toJSON (ReprOptional s) = case s of
575− Just v -> toJSON v
483+ Just v -> toJSON v
576484 Nothing -> J.Null
577485 toJSON (ReprArray v) = J.Array $ toJSON <$> v
578486 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r