@@ -1,7 +1,6 @@
1{-# LANGUAGE AllowAmbiguousTypes #-}
1{-# LANGUAGE AllowAmbiguousTypes #-}
3−{-# LANGUAGE OverloadedLists #-}
2+{-# LANGUAGE OverloadedLists #-}
4−{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE OverloadedStrings #-}
5{-# OPTIONS_GHC -fprint-explicit-kinds #-}
4{-# OPTIONS_GHC -fprint-explicit-kinds #-}
7module Data.Schematic.Schema where
6module Data.Schematic.Schema where
@@ -13,8 +12,8 @@ import Data.Aeson.Types as J
13import Data.HashMap.Strict as H
12import Data.HashMap.Strict as H
14import Data.Kind
13import Data.Kind
15import Data.Maybe
14import Data.Maybe
16−import Data.Schematic.Generator
17import Data.Schematic.Instances ()
15import Data.Schematic.Instances ()
16+import Data.Schematic.Generator
18import Data.Scientific
17import Data.Scientific
19import Data.Singletons.Prelude.List hiding (All, Union)
18import Data.Singletons.Prelude.List hiding (All, Union)
20import Data.Singletons.TH
19import Data.Singletons.TH
@@ -180,63 +179,27 @@ instance SingKind NumberConstraint where
181data ArrayConstraint
180data ArrayConstraint
182 = AEq Nat
181 = AEq Nat
187 deriving (Generic)
182 deriving (Generic)
189data DemotedArrayConstraint
184data DemotedArrayConstraint
190 = DAEq Integer
185 = DAEq Integer
195 deriving (Generic, Eq, Show)
186 deriving (Generic, Eq, Show)
197data instance Sing (ac :: ArrayConstraint) where
188data instance Sing (ac :: ArrayConstraint) where
198 SAEq :: Sing n -> Sing ('AEq n)
189 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)
204instance KnownNat n => SingI ('AEq n) where sing = SAEq sing
191instance 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
210instance Eq (Sing ('AEq n)) where _ == _ = True
193instance 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
216instance SingKind ArrayConstraint where
195instance SingKind ArrayConstraint where
217 type Demote ArrayConstraint = DemotedArrayConstraint
196 type Demote ArrayConstraint = DemotedArrayConstraint
218 fromSing = \case
197 fromSing = \case
219 SAEq n -> withKnownNat n (DAEq . fromIntegral $ natVal n)
198 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
199 toSing = \case
225 DAEq n -> case someNatVal n of
200 DAEq n -> case someNatVal n of
226 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
201 Just (SomeNat (_ :: Proxy n)) -> SomeSing (SAEq (SNat :: Sing n))
227 Nothing -> error "Negative singleton nat"
202 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"
241data Schema
204data Schema
242 = SchemaText [TextConstraint]
205 = SchemaText [TextConstraint]
@@ -405,32 +368,17 @@ instance Show (JsonRepr ('SchemaText cs)) where
405instance Show (JsonRepr ('SchemaNumber cs)) where
368instance Show (JsonRepr ('SchemaNumber cs)) where
406 show (ReprNumber n) = "ReprNumber " P.++ show n
369 show (ReprNumber n) = "ReprNumber " P.++ show n
408−instance Show (JsonRepr 'SchemaBoolean) where
409− show (ReprBoolean n) = "ReprBoolean " P.++ show n
411instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
371instance Show (JsonRepr 'SchemaNull) where show _ = "ReprNull"
413instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
373instance Show (JsonRepr s) => Show (JsonRepr ('SchemaArray acs s)) where
414 show (ReprArray v) = "ReprArray " P.++ show v
374 show (ReprArray v) = "ReprArray " P.++ show v
416−#if MIN_VERSION_base(4,12,0)
418− ( V.RecAll FieldRepr fs Show, RMap fs, ReifyConstraint Show FieldRepr fs
420− => Show (JsonRepr ('SchemaObject fs)) where
421− show (ReprObject fs) = "ReprObject " P.++ show fs
423instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
376instance V.RecAll FieldRepr fs Show => Show (JsonRepr ('SchemaObject fs)) where
424 show (ReprObject fs) = "ReprObject " P.++ show fs
377 show (ReprObject fs) = "ReprObject " P.++ show fs
427instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
379instance Show (JsonRepr s) => Show (JsonRepr ('SchemaOptional s)) where
428 show (ReprOptional s) = "ReprOptional " P.++ show s
380 show (ReprOptional s) = "ReprOptional " P.++ show s
430−instance Show (Union JsonRepr (h ': tl))
431− => Show (JsonRepr ('SchemaUnion (h ': tl))) where
432− show (ReprUnion s) = "ReprUnion " P.++ show s
434instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
382instance Eq (Rec FieldRepr fs) => Eq (JsonRepr ('SchemaObject fs)) where
435 ReprObject a == ReprObject b = a == b
383 ReprObject a == ReprObject b = a == b
@@ -440,9 +388,6 @@ instance Eq (JsonRepr ('SchemaText cs)) where
440instance Eq (JsonRepr ('SchemaNumber cs)) where
388instance Eq (JsonRepr ('SchemaNumber cs)) where
441 ReprNumber a == ReprNumber b = a == b
389 ReprNumber a == ReprNumber b = a == b
443−instance Eq (JsonRepr 'SchemaBoolean) where
444− ReprBoolean a == ReprBoolean b = a == b
446instance Eq (JsonRepr 'SchemaNull) where
391instance Eq (JsonRepr 'SchemaNull) where
447 ReprNull == ReprNull = True
392 ReprNull == ReprNull = True
@@ -452,10 +397,6 @@ instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaArray as s)) where
452instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
397instance Eq (JsonRepr s) => Eq (JsonRepr ('SchemaOptional s)) where
453 ReprOptional a == ReprOptional b = a == b
398 ReprOptional a == ReprOptional b = a == b
455−instance Eq (Union JsonRepr (h ': tl))
456− => Eq (JsonRepr ('SchemaUnion (h ': tl))) where
457− ReprUnion a == ReprUnion b = a == b
459instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
400instance Ord (Rec FieldRepr fs) => Ord (JsonRepr ('SchemaObject fs)) where
460 ReprObject a `compare` ReprObject b = a `compare` b
401 ReprObject a `compare` ReprObject b = a `compare` b
@@ -465,9 +406,6 @@ instance Ord (JsonRepr ('SchemaText cs)) where
465instance Ord (JsonRepr ('SchemaNumber cs)) where
406instance Ord (JsonRepr ('SchemaNumber cs)) where
466 ReprNumber a `compare` ReprNumber b = a `compare` b
407 ReprNumber a `compare` ReprNumber b = a `compare` b
468−instance Ord (JsonRepr 'SchemaBoolean) where
469− ReprBoolean a `compare` ReprBoolean b = a `compare` b
471instance Ord (JsonRepr 'SchemaNull) where
409instance Ord (JsonRepr 'SchemaNull) where
472 compare _ _ = EQ
410 compare _ _ = EQ
@@ -477,10 +415,6 @@ instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaArray as s)) where
477instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
415instance Ord (JsonRepr s) => Ord (JsonRepr ('SchemaOptional s)) where
478 ReprOptional a `compare` ReprOptional b = a `compare` b
416 ReprOptional a `compare` ReprOptional b = a `compare` b
480−instance Ord (Union JsonRepr (h ': tl))
481− => Ord (JsonRepr ('SchemaUnion (h ': tl))) where
482− ReprUnion a `compare` ReprUnion b = a `compare` b
484instance IsList (JsonRepr ('SchemaArray cs s)) where
418instance IsList (JsonRepr ('SchemaArray cs s)) where
485 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
419 type Item (JsonRepr ('SchemaArray cs s)) = JsonRepr s
486 fromList = ReprArray . GHC.Exts.fromList
420 fromList = ReprArray . GHC.Exts.fromList