@@ -25,6 +25,7 @@ where
2525
2626-- base
2727import Data.Bifunctor ( Bifunctor, bimap )
28+import Data.Functor.Identity ( Identity( Identity ) )
2829import Data.Kind ( Type )
2930import Prelude hiding ( undefined )
3031
@@ -32,8 +33,8 @@ import Prelude hiding ( undefined )
3233import Control.Comonad ( extract )
3334
3435-- rel8
35−import Rel8.Aggregate ( Aggregate( A ) )
36−import Rel8.Expr ( Expr( E ) )
36+import Rel8.Aggregate ( Aggregate )
37+import Rel8.Expr ( Expr )
3738import Rel8.Expr.Aggregate ( groupByExpr )
3839import Rel8.Expr.Serialize ( litExpr )
3940import Rel8.Kind.Context ( Reifiable )
@@ -43,9 +44,7 @@ import Rel8.Schema.HTable.Either ( HEitherTable(..) )
4344import Rel8.Schema.HTable.Identity ( HIdentity(..) )
4445import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
4546import qualified Rel8.Schema.Kind as K
46−import Rel8.Schema.Name ( Name( N ) )
47−import Rel8.Schema.Result ( Result( R ) )
48−import Rel8.Schema.Spec ( Spec( Spec ) )
47+import Rel8.Schema.Name ( Name )
4948import Rel8.Table
5049 ( Table, Columns, Context, fromColumns, toColumns
5150 , FromExprs, fromResult, toResult
@@ -72,7 +71,7 @@ import Data.Functor.Bind ( Bind, (>>-) )
7271-- adapted to work with Rel8.
7372type EitherTable :: K.Context -> Type -> Type -> Type
7473data EitherTable context a b = EitherTable
75− { tag :: context ('Spec EitherTag)
74+ { tag :: context EitherTag
7675 , left :: Nullify context a
7776 , right :: Nullify context b
7877 }
@@ -84,8 +83,8 @@ instance Nullifiable context => Bifunctor (EitherTable context) where
8483
8584
8685instance (context ~ Expr, Table Expr a) => Apply (EitherTable context a) where
87− EitherTable (E tag) l1 f <.> EitherTable (E tag') l2 a =
88− EitherTable (E (tag <> tag')) (bool l1 l2 (isLeft tag)) (f <.> a)
86+ EitherTable tag l1 f <.> EitherTable tag' l2 a =
87+ EitherTable (tag <> tag') (bool l1 l2 (isLeft tag)) (f <.> a)
8988
9089
9190instance (context ~ Expr, Table Expr a) => Applicative (EitherTable context a) where
@@ -94,9 +93,9 @@ instance (context ~ Expr, Table Expr a) => Applicative (EitherTable context a) w
9493
9594
9695instance (context ~ Expr, Table Expr a) => Bind (EitherTable context a) where
97− EitherTable (E tag) l1 a >>- f = case f (extract a) of
98− EitherTable (E tag') l2 b ->
99− EitherTable (E (tag <> tag')) (bool l1 l2 (isRight tag)) b
96+ EitherTable tag l1 a >>- f = case f (extract a) of
97+ EitherTable tag' l2 b ->
98+ EitherTable (tag <> tag') (bool l1 l2 (isRight tag)) b
10099
101100
102101instance (context ~ Expr, Table Expr a) => Monad (EitherTable context a) where
@@ -122,7 +121,7 @@ instance
122121 EitherTable to (Transpose to a) (Transpose to b)
123122
124123 toColumns EitherTable {tag, left, right} = HEitherTable
125− { htag = hlabel $ HType tag
124+ { htag = hlabel $ HIdentity tag
126125 , hleft = hlabel $ guard tag (== IsLeft) isLeft $ toColumns left
127126 , hright = hlabel $ guard tag (== IsRight) isRight $ toColumns right
128127 }
@@ -135,18 +134,18 @@ instance
135134
136135 toResult = \case
137136 Left table -> HEitherTable
138− { htag = hlabel (HType (R IsLeft))
137+ { htag = hlabel (HIdentity (Identity IsLeft))
139138 , hleft = hlabel (toResult @_ @(Nullify context a) (Just table))
140139 , hright = hlabel (toResult @_ @(Nullify context b) Nothing)
141140 }
142141 Right table -> HEitherTable
143− { htag = hlabel (HType (R IsRight))
142+ { htag = hlabel (HIdentity (Identity IsRight))
144143 , hleft = hlabel (toResult @_ @(Nullify context a) Nothing)
145144 , hright = hlabel (toResult @_ @(Nullify context b) (Just table))
146145 }
147146
148147 fromResult HEitherTable {htag, hleft, hright} = case hunlabel htag of
149− HType (R tag) -> case tag of
148+ HIdentity (Identity tag) -> case tag of
150149 IsLeft -> maybe err Left $ fromResult @_ @(Nullify context a) (hunlabel hleft)
151150 IsRight -> maybe err Right $ fromResult @_ @(Nullify context b) (hunlabel hright)
152151 where
@@ -157,7 +156,7 @@ instance (EqTable a, EqTable b, context ~ Expr) =>
157156 EqTable (EitherTable context a b)
158157 where
159158 eqTable = HEitherTable
160− { htag = hlabel (HType Dict)
159+ { htag = hlabel (HIdentity Dict)
161160 , hleft = hlabel (eqTable @(Nullify context a))
162161 , hright = hlabel (eqTable @(Nullify context b))
163162 }
@@ -167,7 +166,7 @@ instance (OrdTable a, OrdTable b, context ~ Expr) =>
167166 OrdTable (EitherTable context a b)
168167 where
169168 ordTable = HEitherTable
170− { htag = hlabel (HType Dict)
169+ { htag = hlabel (HIdentity Dict)
171170 , hleft = hlabel (ordTable @(Nullify context a))
172171 , hright = hlabel (ordTable @(Nullify context b))
173172 }
@@ -179,30 +178,30 @@ instance (ToExprs exprs1 a, ToExprs exprs2 b, x ~ EitherTable Expr exprs1 exprs2
179178
180179-- | Test if an 'EitherTable' is a 'leftTable'.
181180isLeftTable :: EitherTable Expr a b -> Expr Bool
182−isLeftTable EitherTable {tag = E tag} = isLeft tag
181+isLeftTable EitherTable {tag} = isLeft tag
183182
184183
185184-- | Test if an 'EitherTable' is a 'rightTable'.
186185isRightTable :: EitherTable Expr a b -> Expr Bool
187−isRightTable EitherTable {tag = E tag} = isRight tag
186+isRightTable EitherTable {tag} = isRight tag
188187
189188
190189-- | Pattern match/eliminate an 'EitherTable', by providing mappings from a
191190-- 'leftTable' and 'rightTable'.
192191eitherTable :: Table Expr c
193192 => (a -> c) -> (b -> c) -> EitherTable Expr a b -> c
194−eitherTable f g EitherTable {tag = E tag, left, right} =
193+eitherTable f g EitherTable {tag, left, right} =
195194 bool (f (extract left)) (g (extract right)) (isRight tag)
196195
197196
198197-- | Construct a left 'EitherTable'. Like 'Left'.
199198leftTable :: Table Expr b => a -> EitherTable Expr a b
200−leftTable a = EitherTable (E (litExpr IsLeft)) (pure a) undefined
199+leftTable a = EitherTable (litExpr IsLeft) (pure a) undefined
201200
202201
203202-- | Construct a right 'EitherTable'. Like 'Right'.
204203rightTable :: Table Expr a => b -> EitherTable Expr a b
205−rightTable = EitherTable (E (litExpr IsRight)) undefined . pure
204+rightTable = EitherTable (litExpr IsRight) undefined . pure
206205
207206
208207-- | Lift a pair of aggregating functions to operate on an 'EitherTable'.
@@ -212,8 +211,8 @@ aggregateEitherTable :: ()
212211 -> (exprs' -> aggregates')
213212 -> EitherTable Expr exprs exprs'
214213 -> EitherTable Aggregate aggregates aggregates'
215−aggregateEitherTable f g (EitherTable (E tag) a b) = EitherTable
216− { tag = A (groupByExpr tag)
214+aggregateEitherTable f g (EitherTable tag a b) = EitherTable
215+ { tag = groupByExpr tag
217216 , left = aggregateNullify f a
218217 , right = aggregateNullify g b
219218 }
@@ -231,4 +230,4 @@ nameEitherTable
231230 -> b
232231 -- ^ Names of the columns in the @b@ table.
233232 -> EitherTable Name a b
234−nameEitherTable tag left right = EitherTable (N tag) (pure left) (pure right)
233+nameEitherTable tag left right = EitherTable tag (pure left) (pure right)