@@ -25,6 +25,7 @@ where
27import Data.Bifunctor ( Bifunctor, bimap )
27import Data.Bifunctor ( Bifunctor, bimap )
28+import Data.Functor.Identity ( Identity( Identity ) )
28import Data.Kind ( Type )
29import Data.Kind ( Type )
29import Prelude hiding ( undefined )
30import Prelude hiding ( undefined )
@@ -32,8 +33,8 @@ import Prelude hiding ( undefined )
32import Control.Comonad ( extract )
33import Control.Comonad ( extract )
35−import Rel8.Aggregate ( Aggregate( A ) )
36+import Rel8.Aggregate ( Aggregate )
36−import Rel8.Expr ( Expr( E ) )
37+import Rel8.Expr ( Expr )
37import Rel8.Expr.Aggregate ( groupByExpr )
38import Rel8.Expr.Aggregate ( groupByExpr )
38import Rel8.Expr.Serialize ( litExpr )
39import Rel8.Expr.Serialize ( litExpr )
39import Rel8.Kind.Context ( Reifiable )
40import Rel8.Kind.Context ( Reifiable )
@@ -43,9 +44,7 @@ import Rel8.Schema.HTable.Either ( HEitherTable(..) )
43import Rel8.Schema.HTable.Identity ( HIdentity(..) )
44import Rel8.Schema.HTable.Identity ( HIdentity(..) )
44import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
45import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
45import qualified Rel8.Schema.Kind as K
46import qualified Rel8.Schema.Kind as K
46−import Rel8.Schema.Name ( Name( N ) )
47+import Rel8.Schema.Name ( Name )
47−import Rel8.Schema.Result ( Result( R ) )
48−import Rel8.Schema.Spec ( Spec( Spec ) )
49import Rel8.Table
48import Rel8.Table
50 ( Table, Columns, Context, fromColumns, toColumns
49 ( Table, Columns, Context, fromColumns, toColumns
51 , FromExprs, fromResult, toResult
50 , FromExprs, fromResult, toResult
@@ -72,7 +71,7 @@ import Data.Functor.Bind ( Bind, (>>-) )
72-- adapted to work with Rel8.
71-- adapted to work with Rel8.
73type EitherTable :: K.Context -> Type -> Type -> Type
72type EitherTable :: K.Context -> Type -> Type -> Type
74data EitherTable context a b = EitherTable
73data EitherTable context a b = EitherTable
75− { tag :: context ('Spec EitherTag)
74+ { tag :: context EitherTag
76 , left :: Nullify context a
75 , left :: Nullify context a
77 , right :: Nullify context b
76 , right :: Nullify context b
@@ -84,8 +83,8 @@ instance Nullifiable context => Bifunctor (EitherTable context) where
86instance (context ~ Expr, Table Expr a) => Apply (EitherTable context a) where
85instance (context ~ Expr, Table Expr a) => Apply (EitherTable context a) where
87− EitherTable (E tag) l1 f <.> EitherTable (E tag') l2 a =
86+ EitherTable tag l1 f <.> EitherTable tag' l2 a =
88− EitherTable (E (tag <> tag')) (bool l1 l2 (isLeft tag)) (f <.> a)
87+ EitherTable (tag <> tag') (bool l1 l2 (isLeft tag)) (f <.> a)
91instance (context ~ Expr, Table Expr a) => Applicative (EitherTable context a) where
90instance (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
96instance (context ~ Expr, Table Expr a) => Bind (EitherTable context a) where
95instance (context ~ Expr, Table Expr a) => Bind (EitherTable context a) where
97− EitherTable (E tag) l1 a >>- f = case f (extract a) of
96+ EitherTable tag l1 a >>- f = case f (extract a) of
98− EitherTable (E tag') l2 b ->
97+ EitherTable tag' l2 b ->
99− EitherTable (E (tag <> tag')) (bool l1 l2 (isRight tag)) b
98+ EitherTable (tag <> tag') (bool l1 l2 (isRight tag)) b
102instance (context ~ Expr, Table Expr a) => Monad (EitherTable context a) where
101instance (context ~ Expr, Table Expr a) => Monad (EitherTable context a) where
@@ -122,7 +121,7 @@ instance
122 EitherTable to (Transpose to a) (Transpose to b)
121 EitherTable to (Transpose to a) (Transpose to b)
124 toColumns EitherTable {tag, left, right} = HEitherTable
123 toColumns EitherTable {tag, left, right} = HEitherTable
125− { htag = hlabel $ HType tag
124+ { htag = hlabel $ HIdentity tag
126 , hleft = hlabel $ guard tag (== IsLeft) isLeft $ toColumns left
125 , hleft = hlabel $ guard tag (== IsLeft) isLeft $ toColumns left
127 , hright = hlabel $ guard tag (== IsRight) isRight $ toColumns right
126 , hright = hlabel $ guard tag (== IsRight) isRight $ toColumns right
@@ -135,18 +134,18 @@ instance
136 toResult = \case
135 toResult = \case
137 Left table -> HEitherTable
136 Left table -> HEitherTable
138− { htag = hlabel (HType (R IsLeft))
137+ { htag = hlabel (HIdentity (Identity IsLeft))
139 , hleft = hlabel (toResult @_ @(Nullify context a) (Just table))
138 , hleft = hlabel (toResult @_ @(Nullify context a) (Just table))
140 , hright = hlabel (toResult @_ @(Nullify context b) Nothing)
139 , hright = hlabel (toResult @_ @(Nullify context b) Nothing)
142 Right table -> HEitherTable
141 Right table -> HEitherTable
143− { htag = hlabel (HType (R IsRight))
142+ { htag = hlabel (HIdentity (Identity IsRight))
144 , hleft = hlabel (toResult @_ @(Nullify context a) Nothing)
143 , hleft = hlabel (toResult @_ @(Nullify context a) Nothing)
145 , hright = hlabel (toResult @_ @(Nullify context b) (Just table))
144 , hright = hlabel (toResult @_ @(Nullify context b) (Just table))
148 fromResult HEitherTable {htag, hleft, hright} = case hunlabel htag of
147 fromResult HEitherTable {htag, hleft, hright} = case hunlabel htag of
149− HType (R tag) -> case tag of
148+ HIdentity (Identity tag) -> case tag of
150 IsLeft -> maybe err Left $ fromResult @_ @(Nullify context a) (hunlabel hleft)
149 IsLeft -> maybe err Left $ fromResult @_ @(Nullify context a) (hunlabel hleft)
151 IsRight -> maybe err Right $ fromResult @_ @(Nullify context b) (hunlabel hright)
150 IsRight -> maybe err Right $ fromResult @_ @(Nullify context b) (hunlabel hright)
@@ -157,7 +156,7 @@ instance (EqTable a, EqTable b, context ~ Expr) =>
157 EqTable (EitherTable context a b)
156 EqTable (EitherTable context a b)
159 eqTable = HEitherTable
158 eqTable = HEitherTable
160− { htag = hlabel (HType Dict)
159+ { htag = hlabel (HIdentity Dict)
161 , hleft = hlabel (eqTable @(Nullify context a))
160 , hleft = hlabel (eqTable @(Nullify context a))
162 , hright = hlabel (eqTable @(Nullify context b))
161 , hright = hlabel (eqTable @(Nullify context b))
@@ -167,7 +166,7 @@ instance (OrdTable a, OrdTable b, context ~ Expr) =>
167 OrdTable (EitherTable context a b)
166 OrdTable (EitherTable context a b)
169 ordTable = HEitherTable
168 ordTable = HEitherTable
170− { htag = hlabel (HType Dict)
169+ { htag = hlabel (HIdentity Dict)
171 , hleft = hlabel (ordTable @(Nullify context a))
170 , hleft = hlabel (ordTable @(Nullify context a))
172 , hright = hlabel (ordTable @(Nullify context b))
171 , hright = hlabel (ordTable @(Nullify context b))
@@ -179,30 +178,30 @@ instance (ToExprs exprs1 a, ToExprs exprs2 b, x ~ EitherTable Expr exprs1 exprs2
180-- | Test if an 'EitherTable' is a 'leftTable'.
179-- | Test if an 'EitherTable' is a 'leftTable'.
181isLeftTable :: EitherTable Expr a b -> Expr Bool
180isLeftTable :: EitherTable Expr a b -> Expr Bool
182−isLeftTable EitherTable {tag = E tag} = isLeft tag
181+isLeftTable EitherTable {tag} = isLeft tag
185-- | Test if an 'EitherTable' is a 'rightTable'.
184-- | Test if an 'EitherTable' is a 'rightTable'.
186isRightTable :: EitherTable Expr a b -> Expr Bool
185isRightTable :: EitherTable Expr a b -> Expr Bool
187−isRightTable EitherTable {tag = E tag} = isRight tag
186+isRightTable EitherTable {tag} = isRight tag
190-- | Pattern match/eliminate an 'EitherTable', by providing mappings from a
189-- | Pattern match/eliminate an 'EitherTable', by providing mappings from a
191-- 'leftTable' and 'rightTable'.
190-- 'leftTable' and 'rightTable'.
192eitherTable :: Table Expr c
191eitherTable :: Table Expr c
193 => (a -> c) -> (b -> c) -> EitherTable Expr a b -> c
192 => (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} =
195 bool (f (extract left)) (g (extract right)) (isRight tag)
194 bool (f (extract left)) (g (extract right)) (isRight tag)
198-- | Construct a left 'EitherTable'. Like 'Left'.
197-- | Construct a left 'EitherTable'. Like 'Left'.
199leftTable :: Table Expr b => a -> EitherTable Expr a b
198leftTable :: 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
203-- | Construct a right 'EitherTable'. Like 'Right'.
202-- | Construct a right 'EitherTable'. Like 'Right'.
204rightTable :: Table Expr a => b -> EitherTable Expr a b
203rightTable :: Table Expr a => b -> EitherTable Expr a b
205−rightTable = EitherTable (E (litExpr IsRight)) undefined . pure
204+rightTable = EitherTable (litExpr IsRight) undefined . pure
208-- | Lift a pair of aggregating functions to operate on an 'EitherTable'.
207-- | Lift a pair of aggregating functions to operate on an 'EitherTable'.
@@ -212,8 +211,8 @@ aggregateEitherTable :: ()
212 -> (exprs' -> aggregates')
211 -> (exprs' -> aggregates')
213 -> EitherTable Expr exprs exprs'
212 -> EitherTable Expr exprs exprs'
214 -> EitherTable Aggregate aggregates aggregates'
213 -> EitherTable Aggregate aggregates aggregates'
215−aggregateEitherTable f g (EitherTable (E tag) a b) = EitherTable
214+aggregateEitherTable f g (EitherTable tag a b) = EitherTable
216− { tag = A (groupByExpr tag)
215+ { tag = groupByExpr tag
217 , left = aggregateNullify f a
216 , left = aggregateNullify f a
218 , right = aggregateNullify g b
217 , right = aggregateNullify g b
@@ -231,4 +230,4 @@ nameEitherTable
232 -- ^ Names of the columns in the @b@ table.
231 -- ^ Names of the columns in the @b@ table.
233 -> EitherTable Name a b
232 -> 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)