@@ -10,31 +10,28 @@
1010module Rel8.Schema.Field
1111 ( Field
1212 , HEither, HList, HMaybe, HNonEmpty, HThese
13− , Reify, hreify, hunreify
14− , Reifiable(..)
1513 , AField(..)
1614 , AHEither(..), AHList(..), AHMaybe(..), AHNonEmpty(..), AHThese(..)
17− , SContext(..)
1815 )
1916where
2017
2118-- base
19+import Control.Applicative ( liftA2 )
2220import Data.Bifunctor ( Bifunctor, bimap )
23−import Data.Kind ( Constraint, Type )
21+import Data.Kind ( Type )
2422import Data.List.NonEmpty ( NonEmpty )
2523import Prelude
2624
2725-- rel8
2826import Rel8.Aggregate ( Aggregate, Col(..) )
2927import Rel8.Expr ( Expr, Col(..) )
28+import Rel8.Kind.Context ( SContext(..), Reifiable( contextSing ) )
3029import Rel8.Kind.Necessity
3130 ( Necessity( Required, Optional )
3231 , SNecessity( SRequired, SOptional )
3332 , KnownNecessity, necessitySing
3433 )
35−import Rel8.Schema.Context ( Interpretation, Col(..) )
36−import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
37−import Rel8.Schema.HTable ( HTable, hfield, htabulate )
34+import Rel8.Schema.Context ( Col(..) )
3835import Rel8.Schema.HTable.Either ( HEitherTable )
3936import Rel8.Schema.HTable.List ( HListTable )
4037import Rel8.Schema.HTable.Maybe ( HMaybeTable )
@@ -45,11 +42,12 @@ import Rel8.Schema.Insert ( Insert, Col(..) )
4542import qualified Rel8.Schema.Kind as K
4643import Rel8.Schema.Name ( Name(..), Col(..) )
4744import Rel8.Schema.Null ( Sql )
45+import Rel8.Schema.Reify ( Reify, Col(..), hreify, hunreify )
4846import Rel8.Schema.Result ( Result )
4947import Rel8.Schema.Spec ( Spec( Spec ) )
5048import Rel8.Table
51− ( Table, Columns, Context, fromColumns, toColumns
52− , Congruent
49+ ( Table, Columns, Congruent, Context, fromColumns, toColumns
50+ , Unreify, reify, unreify
5351 )
5452import Rel8.Table.Either ( EitherTable )
5553import Rel8.Table.List ( ListTable( ListTable ) )
@@ -57,6 +55,7 @@ import Rel8.Table.Maybe ( MaybeTable )
5755import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
5856import Rel8.Table.Recontextualize ( Recontextualize )
5957import Rel8.Table.These ( TheseTable )
58+import Rel8.Table.Unreify ( Unreifiable )
6059import Rel8.Type ( DBType )
6160
6261-- these
@@ -133,9 +132,12 @@ instance (Reifiable context, KnownNecessity necessity, Sql DBType a) =>
133132 where
134133 type Context (AField context necessity a) = Reify context
135134 type Columns (AField context necessity a) = HIdentity ('Spec '[""] necessity a)
135+ type Unreify (AField context necessity a) = Field context necessity a
136136
137137 fromColumns (HIdentity (Reify a)) = sfromColumn contextSing a
138138 toColumns = HIdentity . Reify . stoColumn contextSing necessitySing
139+ reify _ = AField
140+ unreify _ (AField a) = a
139141
140142
141143instance
@@ -166,9 +168,12 @@ instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
166168 where
167169 type Context (AHEither context a b) = Reify context
168170 type Columns (AHEither context a b) = HEitherTable (Columns a) (Columns b)
171+ type Unreify (AHEither context a b) = HEither context (Unreify a) (Unreify b)
169172
170173 fromColumns = sfromColumnsEither contextSing
171174 toColumns = stoColumnsEither contextSing
175+ reify proof = liftA2 bimap reify reify proof . AHEither
176+ unreify proof = (\(AHEither a) -> a) . liftA2 bimap unreify unreify proof
172177
173178
174179instance
@@ -187,18 +192,30 @@ type AHList :: K.Context -> Type -> Type
187192newtype AHList context a = AHList (HList context a)
188193
189194
190−instance (Reifiable context, Table (Reify context) a) =>
191− Table (Reify context) (AHList context a)
195+instance
196+ ( Reifiable context
197+ , Table (Reify context) a
198+ , Unreifiable (Reify context) a
199+ )
200+ => Table (Reify context) (AHList context a)
192201 where
193202 type Context (AHList context a) = Reify context
194203 type Columns (AHList context a) = HListTable (Columns a)
204+ type Unreify (AHList context a) = HList context (Unreify a)
195205
196206 fromColumns = sfromColumnsList contextSing
197207 toColumns = stoColumnsList contextSing
208+ reify proof =
209+ smapList contextSing (reify proof) hreify .
210+ AHList
211+ unreify proof =
212+ (\(AHList a) -> a) .
213+ smapList contextSing (unreify proof) hunreify
198214
199215
200216instance
201217 ( Reifiable context, Reifiable context'
218+ , Unreifiable (Reify context) a, Unreifiable (Reify context') a'
202219 , Recontextualize (Reify context) (Reify context') a a'
203220 ) =>
204221 Recontextualize
@@ -221,9 +238,12 @@ instance (Reifiable context, Table (Reify context) a) =>
221238 where
222239 type Context (AHMaybe context a) = Reify context
223240 type Columns (AHMaybe context a) = HMaybeTable (Columns a)
241+ type Unreify (AHMaybe context a) = HMaybe context (Unreify a)
224242
225243 fromColumns = sfromColumnsMaybe contextSing
226244 toColumns = stoColumnsMaybe contextSing
245+ reify proof = fmap fmap reify proof . AHMaybe
246+ unreify proof = (\(AHMaybe a) -> a) . fmap fmap unreify proof
227247
228248
229249instance
@@ -241,18 +261,30 @@ type AHNonEmpty :: K.Context -> Type -> Type
241261newtype AHNonEmpty context a = AHNonEmpty (HNonEmpty context a)
242262
243263
244−instance (Reifiable context, Table (Reify context) a) =>
245− Table (Reify context) (AHNonEmpty context a)
264+instance
265+ ( Reifiable context
266+ , Table (Reify context) a
267+ , Unreifiable (Reify context) a
268+ )
269+ => Table (Reify context) (AHNonEmpty context a)
246270 where
247271 type Context (AHNonEmpty context a) = Reify context
248272 type Columns (AHNonEmpty context a) = HNonEmptyTable (Columns a)
273+ type Unreify (AHNonEmpty context a) = HNonEmpty context (Unreify a)
249274
250275 fromColumns = sfromColumnsNonEmpty contextSing
251276 toColumns = stoColumnsNonEmpty contextSing
277+ reify proof =
278+ smapNonEmpty contextSing (reify proof) hreify .
279+ AHNonEmpty
280+ unreify proof =
281+ (\(AHNonEmpty a) -> a) .
282+ smapNonEmpty contextSing (unreify proof) hunreify
252283
253284
254285instance
255286 ( Reifiable context, Reifiable context'
287+ , Unreifiable (Reify context) a, Unreifiable (Reify context') a'
256288 , Recontextualize (Reify context) (Reify context') a a'
257289 ) =>
258290 Recontextualize
@@ -279,9 +311,12 @@ instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
279311 where
280312 type Context (AHThese context a b) = Reify context
281313 type Columns (AHThese context a b) = HTheseTable (Columns a) (Columns b)
314+ type Unreify (AHThese context a b) = HThese context (Unreify a) (Unreify b)
282315
283316 fromColumns = sfromColumnsThese contextSing
284317 toColumns = stoColumnsThese contextSing
318+ reify proof = liftA2 bimap reify reify proof . AHThese
319+ unreify proof = (\(AHThese a) -> a) . liftA2 bimap unreify unreify proof
285320
286321
287322instance
@@ -296,58 +331,6 @@ instance
296331 (AHThese context' a' b')
297332
298333
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)
307−
308−
309−type Reifiable :: K.Context -> Constraint
310−class Interpretation context => Reifiable context where
311− contextSing :: SContext context
312−
313−
314−instance Reifiable Aggregate where
315− contextSing = SAggregate
316−
317−
318−instance Reifiable Expr where
319− contextSing = SExpr
320−
321−
322−instance Reifiable Result where
323− contextSing = SResult
324−
325−
326−instance Reifiable Insert where
327− contextSing = SInsert
328−
329−
330−instance Reifiable Name where
331− contextSing = SName
332−
333−
334−type Reify :: K.Context -> K.Context
335−data Reify context a
336−
337−
338−instance Interpretation (Reify context) where
339− newtype Col (Reify context) spec = Reify (Col context spec)
340−
341−
342−instance Labelable context => Labelable (Reify context) where
343− labeler (Reify a) = Reify (labeler a)
344− unlabeler (Reify a) = Reify (unlabeler a)
345−
346−
347−instance Reifiable context => Reifiable (Reify context) where
348− contextSing = SReify contextSing
349−
350−
351334sfromColumn :: ()
352335 => SContext context
353336 -> Col context ('Spec labels necessity a)
@@ -710,12 +693,3 @@ stoColumnsThese = \case
710693 stoColumnsThese context .
711694 sbimapThese context (hunreify . toColumns) (hunreify . toColumns) .
712695 (\(AHThese a) -> a)
713−
714−
715−hreify :: HTable t => t (Col context) -> t (Col (Reify context))
716−hreify a = htabulate $ \field -> Reify (hfield a field)
717−
718−
719−hunreify :: HTable t => t (Col (Reify context)) -> t (Col context)
720−hunreify a = htabulate $ \field -> case hfield a field of
721− Reify x -> x