@@ -15,7 +15,10 @@
15{-# language UndecidableInstances #-}
15{-# language UndecidableInstances #-}
17module Rel8.Table
17module Rel8.Table
18− ( Table (Columns, Context, Unreify, toColumns, fromColumns, reify, unreify)
19+ ( Columns, Context, toColumns, fromColumns
20+ , Unreify, reify, unreify, coherence, congruence
19 , Congruent
22 , Congruent
20 , TTable, TColumns, TContext, TUnreify
23 , TTable, TColumns, TContext, TUnreify
@@ -33,7 +36,11 @@ import Prelude hiding ( null )
35import Rel8.FCF ( Eval, Exp )
38import Rel8.FCF ( Eval, Exp )
36−import Rel8.Kind.Algebra ( KnownAlgebra )
39+import Rel8.Kind.Algebra
40+ ( SAlgebra( SSum, SProduct )
41+ , KnownAlgebra, algebraSing
43+import Rel8.Generic.Coherence ( GCoherent, gcoherence, gcongruence )
37import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
44import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
38import Rel8.Generic.Table
45import Rel8.Generic.Table
39 ( GGTable, GGColumns, GGContext, ggfromColumns, ggtoColumns
46 ( GGTable, GGColumns, GGContext, ggfromColumns, ggtoColumns
@@ -60,6 +67,7 @@ import Rel8.Schema.Reify
61import Rel8.Schema.Result
68import Rel8.Schema.Result
62 ( Col( R ), Result
69 ( Col( R ), Result
63 , null, nullifier, unnullifier
71 , null, nullifier, unnullifier
64 , vectorizer, unvectorizer
72 , vectorizer, unvectorizer
@@ -98,6 +106,9 @@ class (HTable (Columns a), context ~ Context a) => Table context a | a -> contex
99 reify :: context :~: Reify ctx -> Unreify a -> a
107 reify :: context :~: Reify ctx -> Unreify a -> a
100 unreify :: context :~: Reify ctx -> a -> Unreify a
108 unreify :: context :~: Reify ctx -> a -> Unreify a
109+ coherence :: context :~: Reify ctx -> NotResult ctx -> Context (Unreify a) :~: ctx
110+ congruence :: context :~: Reify ctx -> NotResult ctx -> Columns a :~: Columns (Unreify a)
102 type Columns a = Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
113 type Columns a = Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
103 type Context a = Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
114 type Context a = Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
@@ -169,6 +180,43 @@ class (HTable (Columns a), context ~ Context a) => Table context a | a -> contex
183+ default coherence ::
184+ ( KnownAlgebra (GAlgebra (Rep (Record a)))
185+ , GCoherent (TTable context) (Rep (Record a))
186+ , Context a ~ Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
187+ , Context (Unreify a) ~ Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record (Unreify a))))
188+ , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
190+ => context :~: Reify ctx -> NotResult ctx -> Context (Unreify a) :~: ctx
191+ coherence = case algebraSing @(GAlgebra (Rep (Record a))) of
193+ SProduct -> \proof abstract ->
199+ (\(_ :: _proxy x) -> coherence @context @x proof abstract)
201+ default congruence ::
202+ ( KnownAlgebra (GAlgebra (Rep (Record a)))
203+ , GCoherent (TTable context) (Rep (Record a))
204+ , Context a ~ Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
205+ , Columns a ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
206+ , Columns (Unreify a) ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record (Unreify a))))
207+ , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
209+ => context :~: Reify ctx -> NotResult ctx -> Columns a :~: Columns (Unreify a)
210+ congruence = case algebraSing @(GAlgebra (Rep (Record a))) of
212+ SProduct -> \proof abstract ->
218+ (\(_ :: _proxy x) -> congruence @context @x proof abstract)
173data TTable :: K.Context -> Type -> Exp Constraint
221data TTable :: K.Context -> Type -> Exp Constraint
174type instance Eval (TTable context a) = Table context a
222type instance Eval (TTable context a) = Table context a
@@ -213,6 +261,8 @@ instance HTable t => Table context (t (Col context)) where
214 reify Refl = hreify
262 reify Refl = hreify
215 unreify Refl = hunreify
263 unreify Refl = hunreify
264+ coherence Refl _ = Refl
265+ congruence Refl _ = Refl
218-- | Any context is trivially a table.
268-- | Any context is trivially a table.
@@ -226,6 +276,8 @@ instance KnownSpec spec => Table context (Col context spec) where
227 reify Refl = Reify
277 reify Refl = Reify
228 unreify Refl (Reify a) = a
278 unreify Refl (Reify a) = a
279+ coherence Refl _ = Refl
280+ congruence Refl _ = Refl
231instance Sql DBType a => Table Result (Identity a) where
283instance Sql DBType a => Table Result (Identity a) where
@@ -237,11 +289,15 @@ instance Sql DBType a => Table Result (Identity a) where
238 reify = notReify
290 reify = notReify
239 unreify = notReify
291 unreify = notReify
292+ coherence = notReify
293+ congruence = notReify
242−instance (Table Result a, Table Result b) => Table Result (Either a b) where
296+instance (context ~ Result, Table context a, Table context b) =>
297+ Table context (Either a b)
243 type Columns (Either a b) = HEitherTable (Columns a) (Columns b)
299 type Columns (Either a b) = HEitherTable (Columns a) (Columns b)
244− type Context (Either a b) = Result
300+ type Context (Either a b) = Context a
246 toColumns = \case
302 toColumns = \case
247 Left table -> HEitherTable
303 Left table -> HEitherTable
@@ -262,18 +318,24 @@ instance (Table Result a, Table Result b) => Table Result (Either a b) where
263 err = error "Either.fromColumns: mismatch between tag and data"
319 err = error "Either.fromColumns: mismatch between tag and data"
321+ coherence = notReify
322+ congruence = notReify
266−instance Table Result a => Table Result [a] where
325+instance (context ~ Result, Table context a) => Table context [a] where
267 type Columns [a] = HListTable (Columns a)
326 type Columns [a] = HListTable (Columns a)
268− type Context [a] = Result
327+ type Context [a] = Context a
270 toColumns = hvectorize vectorizer . fmap toColumns
329 toColumns = hvectorize vectorizer . fmap toColumns
271 fromColumns = fmap fromColumns . hunvectorize unvectorizer
330 fromColumns = fmap fromColumns . hunvectorize unvectorizer
332+ coherence = notReify
333+ congruence = notReify
274−instance Table Result a => Table Result (Maybe a) where
336+instance (context ~ Result, Table context a) => Table context (Maybe a) where
275 type Columns (Maybe a) = HMaybeTable (Columns a)
337 type Columns (Maybe a) = HMaybeTable (Columns a)
276− type Context (Maybe a) = Result
338+ type Context (Maybe a) = Context a
278 toColumns = \case
340 toColumns = \case
279 Nothing -> HMaybeTable
341 Nothing -> HMaybeTable
@@ -291,18 +353,27 @@ instance Table Result a => Table Result (Maybe a) where
291 Nothing -> error "Maybe.fromColumns: mismatch between tag and data"
353 Nothing -> error "Maybe.fromColumns: mismatch between tag and data"
292 Just just -> fromColumns just
354 Just just -> fromColumns just
356+ coherence = notReify
357+ congruence = notReify
295−instance Table Result a => Table Result (NonEmpty a) where
360+instance (context ~ Result, Table context a) => Table context (NonEmpty a)
296 type Columns (NonEmpty a) = HNonEmptyTable (Columns a)
362 type Columns (NonEmpty a) = HNonEmptyTable (Columns a)
297− type Context (NonEmpty a) = Result
363+ type Context (NonEmpty a) = Context a
299 toColumns = hvectorize vectorizer . fmap toColumns
365 toColumns = hvectorize vectorizer . fmap toColumns
300 fromColumns = fmap fromColumns . hunvectorize unvectorizer
366 fromColumns = fmap fromColumns . hunvectorize unvectorizer
368+ coherence = notReify
369+ congruence = notReify
303−instance (Table Result a, Table Result b) => Table Result (These a b) where
372+instance (context ~ Result, Table context a, Table context b) =>
373+ Table context (These a b)
304 type Columns (These a b) = HTheseTable (Columns a) (Columns b)
375 type Columns (These a b) = HTheseTable (Columns a) (Columns b)
305− type Context (These a b) = Result
376+ type Context (These a b) = Context a
307 toColumns tables = HTheseTable
378 toColumns tables = HTheseTable
308 { hhereTag = hrelabel hhereTag
379 { hhereTag = hrelabel hhereTag
@@ -336,9 +407,11 @@ instance (Table Result a, Table Result b) => Table Result (These a b) where
336 , hjust = hrelabel hthere
407 , hjust = hrelabel hthere
410+ coherence = notReify
411+ congruence = notReify
340−instance (Table context a, Table context b)
414+instance (Table context a, Table context b) => Table context (a, b)
341− => Table context (a, b)