@@ -11,12 +11,13 @@
1111{-# language TypeFamilies #-}
1212{-# language UndecidableInstances #-}
1313
14−{-# options_ghc -fno-warn-orphans #-}
15−
1614module Rel8.Generic.Rel8able
1715 ( KRel8able, Rel8able
18− , GColumns, GContext, gfromColumns, gtoColumns, greify, gunreify
1916 , Algebra
17+ , GRep
18+ , GColumns, GContext, gfromColumns, gtoColumns
19+ , GColumnsADT, gfromColumnsADT, gtoColumnsADT
20+ , greify, gunreify
2021 )
2122where
2223
@@ -28,11 +29,7 @@ import GHC.Generics ( Generic, Rep, from, to )
2829import Prelude
2930
3031-- rel8
31−import Rel8.Kind.Context
32− ( SContext( SReify )
33− , Reifiable, contextSing
34− , sLabelable, sReifiable
35− )
32+import Rel8.Kind.Context ( Reifiable )
3633import Rel8.FCF ( Eval, Exp )
3734import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
3835import Rel8.Generic.Record ( Record(..) )
@@ -40,6 +37,7 @@ import Rel8.Generic.Table
4037 ( GGTable, GGColumns, GGContext, ggfromColumns, ggtoColumns
4138 , GAlgebra
4239 )
40+import qualified Rel8.Generic.Table.ADT as G
4341import Rel8.Kind.Algebra
4442 ( SAlgebra( SProduct, SSum )
4543 , KnownAlgebra, algebraSing
@@ -47,48 +45,17 @@ import Rel8.Kind.Algebra
4745import qualified Rel8.Kind.Algebra as K ( Algebra(..) )
4846import Rel8.Schema.Context ( Col )
4947import Rel8.Schema.Context.Label ( Labelable )
50−import Rel8.Schema.Dict ( Dict( Dict ) )
5148import Rel8.Schema.HTable ( HTable )
5249import qualified Rel8.Schema.Kind as K
5350import Rel8.Schema.Name ( Name )
54−import Rel8.Schema.Reify
55− ( Col( Reify ), Reify, hreify, hunreify
56− , UnwrapReify
57− )
51+import Rel8.Schema.Reify ( Col( Reify ), Reify, UnwrapReify )
5852import Rel8.Schema.Result ( Result )
5953import Rel8.Table
60− ( Table, Columns, Context, fromColumns, toColumns
61− , Unreify, reify, unreify
54+ ( fromColumns, toColumns, reify, unreify
6255 , TTable, TColumns, TContext, TUnreify
6356 )
6457
6558
66−instance
67− ( Rel8able t
68− , GContext t context ~ context
69− , Labelable context
70− , Reifiable context
71− )
72− => Table context (t context)
73− where
74− type Columns (t context) = GColumns t
75− type Context (t context) = context
76− type Unreify (t context) = t (UnwrapReify context)
77−
78− fromColumns = gunreify . gfromColumns . hreify
79− toColumns = hunreify . gtoColumns . greify
80−
81− reify Refl = case contextSing @context of
82− SReify context -> case sLabelable context of
83− Dict -> case sReifiable context of
84− Dict -> greify
85−
86− unreify Refl = case contextSing @context of
87− SReify context -> case sLabelable context of
88− Dict -> case sReifiable context of
89− Dict -> gunreify
90−
91−
9259type KRel8able :: Type
9360type KRel8able = K.Rel8able
9461
@@ -136,9 +103,19 @@ type KRel8able = K.Rel8able
136103-- deriving ( GHC.Generics.Generic, Rel8able )
137104-- @
138105type Rel8able :: K.Rel8able -> Constraint
139−class HTable (GColumns t) => Rel8able t where
106+class
107+ ( HTable (GColumns t)
108+ , HTable (GColumnsADT t)
109+ , GContext t Result ~ Result
110+ )
111+ => Rel8able t
112+ where
140113 type Algebra t :: K.Algebra
114+
115+ type GRep t (context :: K.Context) :: Type -> Type
116+
141117 type GColumns t :: K.HTable
118+
142119 type GContext t (context :: K.Context) :: K.Context
143120
144121 gfromColumns :: (Labelable context, Reifiable context, GContext t context ~ context)
@@ -147,21 +124,31 @@ class HTable (GColumns t) => Rel8able t where
147124 gtoColumns :: (Labelable context, Reifiable context, GContext t context ~ context)
148125 => t (Reify context) -> GColumns t (Col (Reify context))
149126
127+ gfromColumnsADT :: GColumnsADT t (Col (Reify Result)) -> t (Reify Result)
128+
129+ gtoColumnsADT :: t (Reify Result) -> GColumnsADT t (Col (Reify Result))
130+
150131 greify :: (Labelable context, Reifiable context)
151132 => t context -> t (Reify context)
152133
153134 gunreify :: (Labelable context, Reifiable context)
154135 => t (Reify context) -> t context
155136
156− type Algebra t = GAlgebra (Rep (Record (t (Reify Name))))
157− type GColumns t = Eval (GGColumns (Algebra t) TColumns (Rep (Record (t (Reify Name)))))
158− type GContext t context = Eval (GGContext (Algebra t) TUnreifyContext (Rep (Record (t (Reify context)))))
137+ type Algebra t = GAlgebra (GRep t (Reify Name))
138+
139+ type GRep t context = Rep (Record (t context))
140+
141+ type GColumns t = Eval (GGColumns (Algebra t) TColumns (GRep t (Reify Name)))
142+
143+ type GContext t context =
144+ Eval (GGContext (Algebra t) TUnreifyContext (GRep t (Reify context)))
159145
160146 default gfromColumns :: forall context.
161147 ( Generic (Record (t (Reify context)))
148+ , GRep t (Reify context) ~ Rep (Record (t (Reify context)))
162149 , KnownAlgebra (Algebra t)
163− , Eval (GGTable (Algebra t) (TTable (Reify context)) TColumns (Col (Reify context)) (Rep (Record (t (Reify context)))))
164− , Eval (GGColumns (Algebra t) TColumns (Rep (Record (t (Reify context))))) ~ GColumns t
150+ , Eval (GGTable (Algebra t) (TTable (Reify context)) TColumns (Col (Reify context)) (GRep t (Reify context)))
151+ , Eval (GGColumns (Algebra t) TColumns (GRep t (Reify context))) ~ GColumns t
165152 , Constraints t context
166153 )
167154 => GColumns t (Col (Reify context)) -> t (Reify context)
@@ -179,9 +166,10 @@ class HTable (GColumns t) => Rel8able t where
179166
180167 default gtoColumns :: forall context.
181168 ( Generic (Record (t (Reify context)))
169+ , GRep t (Reify context) ~ Rep (Record (t (Reify context)))
182170 , KnownAlgebra (Algebra t)
183− , Eval (GGTable (Algebra t) (TTable (Reify context)) TColumns (Col (Reify context)) (Rep (Record (t (Reify context)))))
184− , Eval (GGColumns (Algebra t) TColumns (Rep (Record (t (Reify context))))) ~ GColumns t
171+ , Eval (GGTable (Algebra t) (TTable (Reify context)) TColumns (Col (Reify context)) (GRep t (Reify context)))
172+ , Eval (GGColumns (Algebra t) TColumns (GRep t (Reify context))) ~ GColumns t
185173 , Constraints t context
186174 )
187175 => t (Reify context) -> GColumns t (Col (Reify context))
@@ -197,11 +185,47 @@ class HTable (GColumns t) => Rel8able t where
197185 from .
198186 Record
199187
188+ default gfromColumnsADT ::
189+ ( Generic (Record (t (Reify Result)))
190+ , GRep t (Reify Result) ~ Rep (Record (t (Reify Result)))
191+ , G.GTableADT (TTable (Reify Result)) TColumns (Col (Reify Result)) (GRep t (Reify Result))
192+ , G.GColumnsADT TColumns (GRep t (Reify Result)) ~ GColumnsADT t
193+ )
194+ => GColumnsADT t (Col (Reify Result)) -> t (Reify Result)
195+ gfromColumnsADT =
196+ unrecord .
197+ to .
198+ G.gfromColumnsADT
199+ @(TTable (Reify Result))
200+ @TColumns
201+ (\(Reify a) -> a)
202+ Reify
203+ fromColumns
204+
205+ default gtoColumnsADT ::
206+ ( Generic (Record (t (Reify Result)))
207+ , GRep t (Reify Result) ~ Rep (Record (t (Reify Result)))
208+ , G.GTableADT (TTable (Reify Result)) TColumns (Col (Reify Result)) (GRep t (Reify Result))
209+ , G.GColumnsADT TColumns (GRep t (Reify Result)) ~ GColumnsADT t
210+ )
211+ => t (Reify Result) -> GColumnsADT t (Col (Reify Result))
212+ gtoColumnsADT =
213+ G.gtoColumnsADT
214+ @(TTable (Reify Result))
215+ @TColumns
216+ (\(Reify a) -> a)
217+ Reify
218+ toColumns .
219+ from .
220+ Record
221+
200222 default greify :: forall context.
201223 ( Generic (Record (t context))
224+ , GRep t context ~ Rep (Record (t context))
202225 , Generic (Record (t (Reify context)))
203− , GMappable (TTable (Reify context)) (Rep (Record (t (Reify context))))
204− , Rep (Record (t context)) ~ GMap TUnreify (Rep (Record (t (Reify context))))
226+ , GRep t (Reify context) ~ Rep (Record (t (Reify context)))
227+ , GMappable (TTable (Reify context)) (GRep t (Reify context))
228+ , GRep t context ~ GMap TUnreify (GRep t (Reify context))
205229 )
206230 => t context -> t (Reify context)
207231 greify =
@@ -213,9 +237,11 @@ class HTable (GColumns t) => Rel8able t where
213237
214238 default gunreify :: forall context.
215239 ( Generic (Record (t context))
240+ , GRep t context ~ Rep (Record (t context))
216241 , Generic (Record (t (Reify context)))
217− , GMappable (TTable (Reify context)) (Rep (Record (t (Reify context))))
218− , Rep (Record (t context)) ~ GMap TUnreify (Rep (Record (t (Reify context))))
242+ , GRep t (Reify context) ~ Rep (Record (t (Reify context)))
243+ , GMappable (TTable (Reify context)) (GRep t (Reify context))
244+ , GRep t context ~ GMap TUnreify (GRep t (Reify context))
219245 )
220246 => t (Reify context) -> t context
221247 gunreify =
@@ -226,6 +252,9 @@ class HTable (GColumns t) => Rel8able t where
226252 Record
227253
228254
255+type GColumnsADT t = G.GColumnsADT TColumns (GRep t (Reify Name))
256+
257+
229258data TUnreifyContext :: Type -> Exp K.Context
230259type instance Eval (TUnreifyContext a) = UnwrapReify (Eval (TContext a))
231260
@@ -237,9 +266,8 @@ type Constraints t context = Constraints' (Algebra t) t context
237266type Constraints' :: K.Algebra -> K.Rel8able -> K.Context -> Constraint
238267type family Constraints' algebra t context where
239268 Constraints' 'K.Product t context =
240− ( Eval (GGContext 'K.Product TContext (Rep (Record (t (Reify context))))) ~
241− Reify context
242− )
269+ Eval (GGContext 'K.Product TContext (GRep t (Reify context))) ~
270+ Reify context
243271 Constraints' 'K.Sum _ context = context ~ Result
244272
245273
@@ -248,7 +276,7 @@ fromContext :: forall t context spec.
248276 , Constraints t context
249277 )
250278 => Col (Reify context) spec
251− -> Col (Eval (GGContext (Algebra t) TContext (Rep (Record (t (Reify context)))))) spec
279+ -> Col (Eval (GGContext (Algebra t) TContext (GRep t (Reify context)))) spec
252280fromContext = case algebraSing @(Algebra t) of
253281 SProduct -> id
254282 SSum -> \(Reify a) -> a
@@ -258,7 +286,7 @@ toContext :: forall t context spec.
258286 ( KnownAlgebra (Algebra t)
259287 , Constraints t context
260288 )
261− => Col (Eval (GGContext (Algebra t) TContext (Rep (Record (t (Reify context)))))) spec
289+ => Col (Eval (GGContext (Algebra t) TContext (GRep t (Reify context)))) spec
262290 -> Col (Reify context) spec
263291toContext = case algebraSing @(Algebra t) of
264292 SProduct -> id