@@ -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)
@@ -26,8 +24,7 @@ import Data.Vinyl hiding (Dict)
2624import qualified Data.Vinyl.TypeLevel as V
2725import GHC.Exts
2826import GHC.Generics (Generic)
29−import GHC.TypeLits
30− (SomeNat(..), SomeSymbol(..), someNatVal, someSymbolVal)
27+import GHC.TypeLits (SomeNat(..), SomeSymbol(..), someSymbolVal, someNatVal)
3128import Prelude as P
3229import Test.SmallCheck.Series
3330
@@ -59,25 +56,25 @@ instance SingKind TextConstraint where
5956 STRegex s -> withKnownSymbol s (DTRegex $ T.pack $ symbolVal s)
6057 STEnum s -> let
6158 d :: Sing (s :: [Symbol]) -> [Text]
62− d SNil = []
59+ d SNil = []
6360 d (SCons ss@SSym fs) = T.pack (symbolVal ss) : d fs
6461 in DTEnum $ d s
6562 toSing = \case
6663 DTEq n -> case someNatVal n of
6764 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STEq (SNat :: Sing n))
68− Nothing -> error "Negative singleton nat"
65+ Nothing -> error "Negative singleton nat"
6966 DTLt n -> case someNatVal n of
7067 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLt (SNat :: Sing n))
71− Nothing -> error "Negative singleton nat"
68+ Nothing -> error "Negative singleton nat"
7269 DTLe n -> case someNatVal n of
7370 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STLe (SNat :: Sing n))
74− Nothing -> error "Negative singleton nat"
71+ Nothing -> error "Negative singleton nat"
7572 DTGt n -> case someNatVal n of
7673 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGt (SNat :: Sing n))
77− Nothing -> error "Negative singleton nat"
74+ Nothing -> error "Negative singleton nat"
7875 DTGe n -> case someNatVal n of
7976 Just (SomeNat (_ :: Proxy n)) -> SomeSing (STGe (SNat :: Sing n))
80− Nothing -> error "Negative singleton nat"
77+ Nothing -> error "Negative singleton nat"
8178 DTRegex s -> case someSymbolVal (T.unpack s) of
8279 SomeSymbol (_ :: Proxy n) -> SomeSing (STRegex (SSym :: Sing n))
8380 DTEnum ss -> case toSing ss of
@@ -91,7 +88,7 @@ data DemotedTextConstraint
9188 | DTGe Integer
9289 | DTRegex Text
9390 | DTEnum [Text]
94− deriving (Generic, Eq, Show)
91+ deriving (Generic)
9592
9693data instance Sing (tc :: TextConstraint) where
9794 STEq :: Sing n -> Sing ('TEq n)
@@ -132,7 +129,7 @@ data DemotedNumberConstraint
132129 | DNGt Integer
133130 | DNGe Integer
134131 | DNEq Integer
135− deriving (Generic, Eq, Show)
132+ deriving (Generic)
136133
137134data instance Sing (nc :: NumberConstraint) where
138135 SNEq :: Sing n -> Sing ('NEq n)
@@ -164,79 +161,43 @@ instance SingKind NumberConstraint where
164161 toSing = \case
165162 DNEq n -> case someNatVal n of
166163 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNEq (SNat :: Sing n))
167− Nothing -> error "Negative singleton nat"
164+ Nothing -> error "Negative singleton nat"
168165 DNGt n -> case someNatVal n of
169166 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGt (SNat :: Sing n))
170− Nothing -> error "Negative singleton nat"
167+ Nothing -> error "Negative singleton nat"
171168 DNGe n -> case someNatVal n of
172169 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNGe (SNat :: Sing n))
173− Nothing -> error "Negative singleton nat"
170+ Nothing -> error "Negative singleton nat"
174171 DNLt n -> case someNatVal n of
175172 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLt (SNat :: Sing n))
176− Nothing -> error "Negative singleton nat"
173+ Nothing -> error "Negative singleton nat"
177174 DNLe n -> case someNatVal n of
178175 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SNLe (SNat :: Sing n))
179− Nothing -> error "Negative singleton nat"
176+ Nothing -> error "Negative singleton nat"
180177
181178data ArrayConstraint
182179 = AEq Nat
183− | AGt Nat
184− | AGe Nat
185− | ALt Nat
186− | ALe Nat
187180 deriving (Generic)
188181
189182data DemotedArrayConstraint
190183 = DAEq Integer
191− | DAGt Integer
192− | DAGe Integer
193− | DALt Integer
194− | DALe Integer
195− deriving (Generic, Eq, Show)
184+ deriving (Generic)
196185
197186data instance Sing (ac :: ArrayConstraint) where
198187 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)
203188
204189instance 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
209190
210191instance 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
215192
216193instance SingKind ArrayConstraint where
217194 type Demote ArrayConstraint = DemotedArrayConstraint
218195 fromSing = \case
219196 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)
224197 toSing = \case
225198 DAEq n -> case someNatVal n of
226199 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"
200+ Nothing -> error "Negative singleton nat"
240201
241202data Schema
242203 = SchemaText [TextConstraint]
@@ -258,7 +219,7 @@ data DemotedSchema
258219 | DSchemaNull
259220 | DSchemaOptional DemotedSchema
260221 | DSchemaUnion [DemotedSchema]
261− deriving (Generic, Eq, Show)
222+ deriving (Generic)
262223
263224data instance Sing (schema :: Schema) where
264225 SSchemaText :: Sing tcs -> Sing ('SchemaText tcs)
@@ -373,31 +334,6 @@ data JsonRepr :: Schema -> Type where
373334 ReprOptional :: Maybe (JsonRepr s) -> JsonRepr ('SchemaOptional s)
374335 ReprUnion :: Union JsonRepr (h ': tl) -> JsonRepr ('SchemaUnion (h ': tl))
375336
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
387−
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−
401337-- | Move to the union package
402338instance Show (JsonRepr ('SchemaText cs)) where
403339 show (ReprText t) = "ReprText " P.++ show t
@@ -405,31 +341,39 @@ instance Show (JsonRepr ('SchemaText cs)) where
405341instance Show (JsonRepr ('SchemaNumber cs)) where
406342 show (ReprNumber n) = "ReprNumber " P.++ show n
407343
408−instance Show (JsonRepr 'SchemaBoolean) where
409− show (ReprBoolean n) = "ReprBoolean " P.++ show n
410−
411344instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
412345
413346instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
414347 show (ReprArray v) = "ReprArray " P.++ show v
415348
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
423349instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424350 show (ReprObject fs) = "ReprObject " P.++ show fs
425−#endif
426351
427352instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428353 show (ReprOptional s) = "ReprOptional " P.++ show s
429354
430−instance Show (Union JsonRepr (h ': tl))
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
355+instance (Monad m, Serial m Text)
356+ => Serial m (JsonRepr ('SchemaText cs)) where
357+ series = cons1 ReprText
358+
359+instance (Monad m, Serial m Scientific)
360+ => Serial m (JsonRepr ('SchemaNumber cs)) where
361+ series = cons1 ReprNumber
362+
363+instance Monad m => Serial m (JsonRepr 'SchemaNull) where
364+ series = cons0 ReprNull
365+
366+instance (Serial m (V.Vector (JsonRepr s)))
367+ => Serial m (JsonRepr ('SchemaArray cs s)) where
368+ series = cons1 ReprArray
369+
370+instance (Serial m (JsonRepr s))
371+ => Serial m (JsonRepr ('SchemaOptional s)) where
372+ series = cons1 ReprOptional
373+
374+instance (Monad m, Serial m (Rec FieldRepr fs))
375+ => Serial m (JsonRepr ('SchemaObject fs)) where
376+ series = cons1 ReprObject
433377
434378instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435379 ReprObject a == ReprObject b = a == b
@@ -440,9 +384,6 @@ instance Eq (JsonRepr ('SchemaText cs)) where
440384instance Eq (JsonRepr ('SchemaNumber cs)) where
441385 ReprNumber a == ReprNumber b = a == b
442386
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
445−
446387instance Eq (JsonRepr 'SchemaNull) where
447388 ReprNull == ReprNull = True
448389
@@ -452,35 +393,6 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
452393instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453394 ReprOptional a == ReprOptional b = a == b
454395
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−
484396instance IsList (JsonRepr ('SchemaArray cs s)) where
485397 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
486398 fromList = ReprArray . GHC.Exts.fromList
@@ -542,22 +454,22 @@ instance SingI schema => J.FromJSON (JsonRepr schema) where
542454 fieldRepr <- case s of
543455 SSchemaText so -> case H.lookup fieldName h of
544456 Just v -> withSingI so $ FieldRepr <$> parseJSON v
545− Nothing -> fail $ "No text field: " P.++ show fieldName
457+ Nothing -> fail "schematext"
546458 SSchemaNumber so -> case H.lookup fieldName h of
547459 Just v -> withSingI so $ FieldRepr <$> parseJSON v
548− Nothing -> fail $ "No number field: " P.++ show fieldName
460+ Nothing -> fail "schemanumber"
549461 SSchemaBoolean -> case H.lookup fieldName h of
550462 Just v -> FieldRepr <$> parseJSON v
551− Nothing -> fail $ "No boolean field: " P.++ show fieldName
463+ Nothing -> fail "schemaboolean"
552464 SSchemaNull -> case H.lookup fieldName h of
553465 Just v -> FieldRepr <$> parseJSON v
554− Nothing -> fail $ "No null field: " P.++ show fieldName
466+ Nothing -> fail "schemanull"
555467 SSchemaArray sa sb -> case H.lookup fieldName h of
556468 Just v -> withSingI sa $ withSingI sb $ FieldRepr <$> parseJSON v
557− Nothing -> fail $ "No array field: " P.++ show fieldName
469+ Nothing -> fail "schemaarray"
558470 SSchemaObject so -> case H.lookup fieldName h of
559471 Just v -> withSingI so $ FieldRepr <$> parseJSON v
560− Nothing -> fail $ "No object field" P.++ show fieldName
472+ Nothing -> fail "schemaobject"
561473 SSchemaOptional so -> case H.lookup fieldName h of
562474 Just v -> withSingI so $ FieldRepr <$> parseJSON v
563475 Nothing -> withSingI so $ pure $ FieldRepr $ ReprOptional Nothing
@@ -572,7 +484,7 @@ instance J.ToJSON (JsonRepr a) where
572484 toJSON (ReprText t) = J.String t
573485 toJSON (ReprNumber n) = J.Number n
574486 toJSON (ReprOptional s) = case s of
575− Just v -> toJSON v
487+ Just v -> toJSON v
576488 Nothing -> J.Null
577489 toJSON (ReprArray v) = J.Array $ toJSON <$> v
578490 toJSON (ReprObject r) = J.Object . H.fromList . fold $ r