@@ -10,31 +10,28 @@
10module Rel8.Schema.Field
10module Rel8.Schema.Field
12 , HEither, HList, HMaybe, HNonEmpty, HThese
12 , HEither, HList, HMaybe, HNonEmpty, HThese
13− , Reify, hreify, hunreify
15 , AField(..)
13 , AField(..)
16 , AHEither(..), AHList(..), AHMaybe(..), AHNonEmpty(..), AHThese(..)
14 , AHEither(..), AHList(..), AHMaybe(..), AHNonEmpty(..), AHThese(..)
19+import Control.Applicative ( liftA2 )
22import Data.Bifunctor ( Bifunctor, bimap )
20import Data.Bifunctor ( Bifunctor, bimap )
23−import Data.Kind ( Constraint, Type )
21+import Data.Kind ( Type )
24import Data.List.NonEmpty ( NonEmpty )
22import Data.List.NonEmpty ( NonEmpty )
25import Prelude
23import Prelude
28import Rel8.Aggregate ( Aggregate, Col(..) )
26import Rel8.Aggregate ( Aggregate, Col(..) )
29import Rel8.Expr ( Expr, Col(..) )
27import Rel8.Expr ( Expr, Col(..) )
28+import Rel8.Kind.Context ( SContext(..), Reifiable( contextSing ) )
30import Rel8.Kind.Necessity
29import Rel8.Kind.Necessity
31 ( Necessity( Required, Optional )
30 ( Necessity( Required, Optional )
32 , SNecessity( SRequired, SOptional )
31 , SNecessity( SRequired, SOptional )
33 , KnownNecessity, necessitySing
32 , KnownNecessity, necessitySing
35−import Rel8.Schema.Context ( Interpretation, Col(..) )
34+import Rel8.Schema.Context ( Col(..) )
36−import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
37−import Rel8.Schema.HTable ( HTable, hfield, htabulate )
38import Rel8.Schema.HTable.Either ( HEitherTable )
35import Rel8.Schema.HTable.Either ( HEitherTable )
39import Rel8.Schema.HTable.List ( HListTable )
36import Rel8.Schema.HTable.List ( HListTable )
40import Rel8.Schema.HTable.Maybe ( HMaybeTable )
37import Rel8.Schema.HTable.Maybe ( HMaybeTable )
@@ -45,11 +42,12 @@ import Rel8.Schema.Insert ( Insert, Col(..) )
45import qualified Rel8.Schema.Kind as K
42import qualified Rel8.Schema.Kind as K
46import Rel8.Schema.Name ( Name(..), Col(..) )
43import Rel8.Schema.Name ( Name(..), Col(..) )
47import Rel8.Schema.Null ( Sql )
44import Rel8.Schema.Null ( Sql )
45+import Rel8.Schema.Reify ( Reify, Col(..), hreify, hunreify )
48import Rel8.Schema.Result ( Result )
46import Rel8.Schema.Result ( Result )
49import Rel8.Schema.Spec ( Spec( Spec ) )
47import Rel8.Schema.Spec ( Spec( Spec ) )
50import Rel8.Table
48import Rel8.Table
51− ( Table, Columns, Context, fromColumns, toColumns
49+ ( Table, Columns, Congruent, Context, fromColumns, toColumns
52− , Congruent
50+ , Unreify, reify, unreify
54import Rel8.Table.Either ( EitherTable )
52import Rel8.Table.Either ( EitherTable )
55import Rel8.Table.List ( ListTable( ListTable ) )
53import Rel8.Table.List ( ListTable( ListTable ) )
@@ -57,6 +55,7 @@ import Rel8.Table.Maybe ( MaybeTable )
57import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
55import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
58import Rel8.Table.Recontextualize ( Recontextualize )
56import Rel8.Table.Recontextualize ( Recontextualize )
59import Rel8.Table.These ( TheseTable )
57import Rel8.Table.These ( TheseTable )
58+import Rel8.Table.Unreify ( Unreifiable )
60import Rel8.Type ( DBType )
59import Rel8.Type ( DBType )
@@ -133,9 +132,12 @@ instance (Reifiable context, KnownNecessity necessity, Sql DBType a) =>
134 type Context (AField context necessity a) = Reify context
133 type Context (AField context necessity a) = Reify context
135 type Columns (AField context necessity a) = HIdentity ('Spec '[""] necessity a)
134 type Columns (AField context necessity a) = HIdentity ('Spec '[""] necessity a)
135+ type Unreify (AField context necessity a) = Field context necessity a
137 fromColumns (HIdentity (Reify a)) = sfromColumn contextSing a
137 fromColumns (HIdentity (Reify a)) = sfromColumn contextSing a
138 toColumns = HIdentity . Reify . stoColumn contextSing necessitySing
138 toColumns = HIdentity . Reify . stoColumn contextSing necessitySing
140+ unreify _ (AField a) = a
@@ -166,9 +168,12 @@ instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
167 type Context (AHEither context a b) = Reify context
169 type Context (AHEither context a b) = Reify context
168 type Columns (AHEither context a b) = HEitherTable (Columns a) (Columns b)
170 type Columns (AHEither context a b) = HEitherTable (Columns a) (Columns b)
171+ type Unreify (AHEither context a b) = HEither context (Unreify a) (Unreify b)
170 fromColumns = sfromColumnsEither contextSing
173 fromColumns = sfromColumnsEither contextSing
171 toColumns = stoColumnsEither contextSing
174 toColumns = stoColumnsEither contextSing
175+ reify proof = liftA2 bimap reify reify proof . AHEither
176+ unreify proof = (\(AHEither a) -> a) . liftA2 bimap unreify unreify proof
@@ -187,18 +192,30 @@ type AHList :: K.Context -> Type -> Type
187newtype AHList context a = AHList (HList context a)
192newtype AHList context a = AHList (HList context a)
190−instance (Reifiable context, Table (Reify context) a) =>
197+ , Table (Reify context) a
198+ , Unreifiable (Reify context) a
191− Table (Reify context) (AHList context a)
200+ => Table (Reify context) (AHList context a)
193 type Context (AHList context a) = Reify context
202 type Context (AHList context a) = Reify context
194 type Columns (AHList context a) = HListTable (Columns a)
203 type Columns (AHList context a) = HListTable (Columns a)
204+ type Unreify (AHList context a) = HList context (Unreify a)
196 fromColumns = sfromColumnsList contextSing
206 fromColumns = sfromColumnsList contextSing
197 toColumns = stoColumnsList contextSing
207 toColumns = stoColumnsList contextSing
209+ smapList contextSing (reify proof) hreify .
212+ (\(AHList a) -> a) .
213+ smapList contextSing (unreify proof) hunreify
201 ( Reifiable context, Reifiable context'
217 ( Reifiable context, Reifiable context'
218+ , Unreifiable (Reify context) a, Unreifiable (Reify context') a'
202 , Recontextualize (Reify context) (Reify context') a a'
219 , Recontextualize (Reify context) (Reify context') a a'
204 Recontextualize
221 Recontextualize
@@ -221,9 +238,12 @@ instance (Reifiable context, Table (Reify context) a) =>
222 type Context (AHMaybe context a) = Reify context
239 type Context (AHMaybe context a) = Reify context
223 type Columns (AHMaybe context a) = HMaybeTable (Columns a)
240 type Columns (AHMaybe context a) = HMaybeTable (Columns a)
241+ type Unreify (AHMaybe context a) = HMaybe context (Unreify a)
225 fromColumns = sfromColumnsMaybe contextSing
243 fromColumns = sfromColumnsMaybe contextSing
226 toColumns = stoColumnsMaybe contextSing
244 toColumns = stoColumnsMaybe contextSing
245+ reify proof = fmap fmap reify proof . AHMaybe
246+ unreify proof = (\(AHMaybe a) -> a) . fmap fmap unreify proof
@@ -241,18 +261,30 @@ type AHNonEmpty :: K.Context -> Type -> Type
241newtype AHNonEmpty context a = AHNonEmpty (HNonEmpty context a)
261newtype AHNonEmpty context a = AHNonEmpty (HNonEmpty context a)
244−instance (Reifiable context, Table (Reify context) a) =>
266+ , Table (Reify context) a
267+ , Unreifiable (Reify context) a
245− Table (Reify context) (AHNonEmpty context a)
269+ => Table (Reify context) (AHNonEmpty context a)
247 type Context (AHNonEmpty context a) = Reify context
271 type Context (AHNonEmpty context a) = Reify context
248 type Columns (AHNonEmpty context a) = HNonEmptyTable (Columns a)
272 type Columns (AHNonEmpty context a) = HNonEmptyTable (Columns a)
273+ type Unreify (AHNonEmpty context a) = HNonEmpty context (Unreify a)
250 fromColumns = sfromColumnsNonEmpty contextSing
275 fromColumns = sfromColumnsNonEmpty contextSing
251 toColumns = stoColumnsNonEmpty contextSing
276 toColumns = stoColumnsNonEmpty contextSing
278+ smapNonEmpty contextSing (reify proof) hreify .
281+ (\(AHNonEmpty a) -> a) .
282+ smapNonEmpty contextSing (unreify proof) hunreify
255 ( Reifiable context, Reifiable context'
286 ( Reifiable context, Reifiable context'
287+ , Unreifiable (Reify context) a, Unreifiable (Reify context') a'
256 , Recontextualize (Reify context) (Reify context') a a'
288 , Recontextualize (Reify context) (Reify context') a a'
258 Recontextualize
290 Recontextualize
@@ -279,9 +311,12 @@ instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
280 type Context (AHThese context a b) = Reify context
312 type Context (AHThese context a b) = Reify context
281 type Columns (AHThese context a b) = HTheseTable (Columns a) (Columns b)
313 type Columns (AHThese context a b) = HTheseTable (Columns a) (Columns b)
314+ type Unreify (AHThese context a b) = HThese context (Unreify a) (Unreify b)
283 fromColumns = sfromColumnsThese contextSing
316 fromColumns = sfromColumnsThese contextSing
284 toColumns = stoColumnsThese contextSing
317 toColumns = stoColumnsThese contextSing
318+ reify proof = liftA2 bimap reify reify proof . AHThese
319+ unreify proof = (\(AHThese a) -> a) . liftA2 bimap unreify unreify proof
@@ -296,58 +331,6 @@ instance
296 (AHThese context' a' b')
331 (AHThese context' a' b')
299−type SContext :: K.Context -> Type
300−data SContext context where
301− SAggregate :: SContext Aggregate
302− SExpr :: SContext Expr
303− SInsert :: SContext Insert
304− SName :: SContext Name
305− SResult :: SContext Result
306− SReify :: SContext context -> SContext (Reify context)
309−type Reifiable :: K.Context -> Constraint
310−class Interpretation context => Reifiable context where
311− contextSing :: SContext context
314−instance Reifiable Aggregate where
315− contextSing = SAggregate
318−instance Reifiable Expr where
322−instance Reifiable Result where
323− contextSing = SResult
326−instance Reifiable Insert where
327− contextSing = SInsert
330−instance Reifiable Name where
334−type Reify :: K.Context -> K.Context
338−instance Interpretation (Reify context) where
339− newtype Col (Reify context) spec = Reify (Col context spec)
342−instance Labelable context => Labelable (Reify context) where
343− labeler (Reify a) = Reify (labeler a)
344− unlabeler (Reify a) = Reify (unlabeler a)
347−instance Reifiable context => Reifiable (Reify context) where
348− contextSing = SReify contextSing
351sfromColumn :: ()
334sfromColumn :: ()
352 => SContext context
335 => SContext context
353 -> Col context ('Spec labels necessity a)
336 -> Col context ('Spec labels necessity a)
@@ -710,12 +693,3 @@ stoColumnsThese = \case
710 stoColumnsThese context .
693 stoColumnsThese context .
711 sbimapThese context (hunreify . toColumns) (hunreify . toColumns) .
694 sbimapThese context (hunreify . toColumns) (hunreify . toColumns) .
712 (\(AHThese a) -> a)
695 (\(AHThese a) -> a)
715−hreify :: HTable t => t (Col context) -> t (Col (Reify context))
716−hreify a = htabulate $ \field -> Reify (hfield a field)
719−hunreify :: HTable t => t (Col (Reify context)) -> t (Col context)
720−hunreify a = htabulate $ \field -> case hfield a field of