@@ -37,12 +37,11 @@ import Rel8.Expr ( Expr )
3737import Rel8.Expr.Bool ( (&&.), not_ )
3838import Rel8.Expr.Null ( isNonNull )
3939import Rel8.Schema.Context.Nullify
40− ( Nullifiable, ConstrainTag
41− , HNullifiable, HConstrainTag
42− , hencodeTag, hdecodeTag
43− , hnullifier, hunnullifier
40+ ( Nullifiable
41+ , encodeTag, decodeTag
42+ , nullifier, unnullifier
4443 )
45−import Rel8.Schema.HTable ( HTable )
44+import Rel8.Schema.Dict ( Dict( Dict ) )
4645import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
4746import Rel8.Schema.HTable.Identity ( HIdentity(..) )
4847import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
@@ -132,33 +131,82 @@ instance (Table Expr a, Table Expr b, Semigroup a, Semigroup b) =>
132131
133132instance
134133 ( Table context a, Table context b
135− , Nullifiable context, ConstrainTag context MaybeTag
136− ) => Table context (TheseTable a b)
134+ , Nullifiable context
135+ )
136+ => Table context (TheseTable a b)
137137 where
138138 type Columns (TheseTable a b) = HTheseTable (Columns a) (Columns b)
139139 type Context (TheseTable a b) = Context a
140140
141− toColumns = toColumns2 toColumns toColumns
142− fromColumns = fromColumns2 fromColumns fromColumns
141+ toColumns TheseTable {here, there} = HTheseTable
142+ { hhereTag = hlabel $ HType $ encodeTag (toHereTag (tag here))
143+ , hhere =
144+ hlabel $ hnullify (nullifier (tag here) isNonNull) $ toColumns $
145+ just here
146+ , hthereTag = hlabel $ HType $ encodeTag (toThereTag (tag there))
147+ , hthere =
148+ hlabel $ hnullify (nullifier (tag there) isNonNull) $ toColumns $
149+ just there
150+ }
151+
152+ fromColumns HTheseTable {hhereTag, hhere, hthereTag, hthere} = TheseTable
153+ { here =
154+ let
155+ tag = decodeTag $ unHIdentity $ hunlabel hhereTag
156+ in
157+ MaybeTable
158+ { tag
159+ , just =
160+ fromColumns $
161+ runIdentity $
162+ hunnullify (\a -> pure . unnullifier a) $
163+ hunlabel
164+ hhere
165+ }
166+ , there =
167+ let
168+ tag = decodeTag $ unHIdentity $ hunlabel hthereTag
169+ in
170+ MaybeTable
171+ { tag
172+ , just =
173+ fromColumns $
174+ runIdentity $
175+ hunnullify (\a -> pure . unnullifier a) $
176+ hunlabel
177+ hthere
178+ }
179+ }
180+
181+
143182 reify = liftA2 bimap reify reify
144183 unreify = liftA2 bimap unreify unreify
145184
146185
147186instance
148− ( Nullifiable from, ConstrainTag from MaybeTag
149− , Nullifiable to, ConstrainTag to MaybeTag
187+ ( Nullifiable from, Nullifiable to
150188 , Recontextualize from to a1 b1
151189 , Recontextualize from to a2 b2
152− ) =>
153− Recontextualize from to (TheseTable a1 a2) (TheseTable b1 b2)
190+ )
191+ => Recontextualize from to (TheseTable a1 a2) (TheseTable b1 b2)
154192
155193
156194instance (EqTable a, EqTable b) => EqTable (TheseTable a b) where
157− eqTable = toColumns2 id id (thoseTable (eqTable @a) (eqTable @b))
195+ eqTable = HTheseTable
196+ { hhereTag = hlabel (HType Dict)
197+ , hhere = hlabel (hnullify (\_ Dict -> Dict) (eqTable @a))
198+ , hthereTag = hlabel (HType Dict)
199+ , hthere = hlabel (hnullify (\_ Dict -> Dict) (eqTable @b))
200+ }
158201
159202
160203instance (OrdTable a, OrdTable b) => OrdTable (TheseTable a b) where
161− ordTable = toColumns2 id id (thoseTable (ordTable @a) (ordTable @b))
204+ ordTable = HTheseTable
205+ { hhereTag = hlabel (HType Dict)
206+ , hhere = hlabel (hnullify (\_ Dict -> Dict) (ordTable @a))
207+ , hthereTag = hlabel (HType Dict)
208+ , hthere = hlabel (hnullify (\_ Dict -> Dict) (ordTable @b))
209+ }
162210
163211
164212type instance FromExprs (TheseTable a b) = These (FromExprs a) (FromExprs b)
@@ -275,61 +323,3 @@ nameTheseTable here there a b =
275323 { here = nameMaybeTable here a
276324 , there = nameMaybeTable there b
277325 }
278−
279−
280−toColumns2 ::
281− ( HTable t
282− , HTable u
283− , HConstrainTag context MaybeTag
284− , HNullifiable context
285− )
286− => (a -> t context)
287− -> (b -> u context)
288− -> TheseTable a b
289− -> HTheseTable t u context
290−toColumns2 f g TheseTable {here, there} = HTheseTable
291− { hhereTag = hlabel $ HType $ hencodeTag (toHereTag (tag here))
292− , hhere =
293− hlabel $ hnullify (hnullifier (tag here) isNonNull) $ f (just here)
294− , hthereTag = hlabel $ HType $ hencodeTag (toThereTag (tag there))
295− , hthere =
296− hlabel $ hnullify (hnullifier (tag there) isNonNull) $ g (just there)
297− }
298−
299−
300−fromColumns2 ::
301− ( HTable t
302− , HTable u
303− , HConstrainTag context MaybeTag
304− , HNullifiable context
305− )
306− => (t context -> a)
307− -> (u context -> b)
308− -> HTheseTable t u context
309− -> TheseTable a b
310−fromColumns2 f g HTheseTable {hhereTag, hhere, hthereTag, hthere} = TheseTable
311− { here =
312− let
313− tag = hdecodeTag $ unHIdentity $ hunlabel hhereTag
314− in
315− MaybeTable
316− { tag
317− , just = f $
318− runIdentity $
319− hunnullify (\a -> pure . hunnullifier a) $
320− hunlabel
321− hhere
322− }
323− , there =
324− let
325− tag = hdecodeTag $ unHIdentity $ hunlabel hthereTag
326− in
327− MaybeTable
328− { tag
329− , just = g $
330− runIdentity $
331− hunnullify (\a -> pure . hunnullifier a) $
332− hunlabel
333− hthere
334− }
335− }