@@ -14,10 +14,9 @@
1414{-# language UndecidableInstances #-}
1515
1616module Rel8.Table
17− ( Table (Columns, Context)
18− , toColumns, fromColumns
17+ ( Table (Columns, Context, toColumns, fromColumns)
1918 , Congruent
20− , GTable, GColumns, GContext, fromGColumns, toGColumns
19+ , TTable, TColumns, TContext
2120 )
2221where
2322
@@ -26,28 +25,25 @@ import Data.Functor ( ($>) )
2625import Data.Functor.Identity ( Identity( Identity ) )
2726import Data.Kind ( Constraint, Type )
2827import Data.List.NonEmpty ( NonEmpty )
29−import GHC.Generics
30− ( Generic, Rep, from, to
31− , (:*:)( (:*:) ), K1( K1 ), M1( M1 )
32− , C, D, S
33− , Meta( MetaSel )
34− )
35−import GHC.TypeLits ( KnownSymbol )
28+import GHC.Generics ( Generic, Rep, from, to )
3629import Prelude hiding ( null )
3730
3831-- rel8
32+import Rel8.FCF ( Eval, Exp )
33+import Rel8.Generic.Table
34+ ( GTable, GColumns, GContext, fromGColumns, toGColumns
35+ )
3936import Rel8.Generic.Record ( Record(..) )
4037import Rel8.Schema.Context ( Col(..) )
4138import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
4239import Rel8.Schema.HTable ( HTable )
4340import Rel8.Schema.HTable.Either ( HEitherTable(..) )
4441import Rel8.Schema.HTable.Identity ( HIdentity(..) )
45−import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
42+import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
4643import Rel8.Schema.HTable.List ( HListTable )
4744import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
4845import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
4946import Rel8.Schema.HTable.Nullify ( hnulls, hnullify, hunnullify )
50−import Rel8.Schema.HTable.Product ( HProduct(..) )
5147import Rel8.Schema.HTable.These ( HTheseTable(..) )
5248import Rel8.Schema.HTable.Type ( HType( HType ) )
5349import Rel8.Schema.HTable.Vectorize ( hvectorize, hunvectorize )
@@ -85,79 +81,42 @@ class (HTable (Columns a), context ~ Context a) => Table context a | a -> contex
8581 toColumns :: a -> Columns a (Col (Context a))
8682 fromColumns :: Columns a (Col (Context a)) -> a
8783
88− type Columns a = GColumns (Rep (Record a))
89− type Context a = GContext (Rep (Record a))
84+ type Columns a = GColumns TColumns (Rep (Record a))
85+ type Context a = GContext TContext (Rep (Record a))
9086
9187 default toColumns ::
92− ( Generic (Record a), GTable (GContext (Rep (Record a))) (Rep (Record a))
93− , Columns a ~ GColumns (Rep (Record a))
94− , Context a ~ GContext (Rep (Record a))
88+ ( Generic (Record a)
89+ , GTable (TTable context) TColumns (Col (Context a)) (Rep (Record a))
90+ , Columns a ~ GColumns TColumns (Rep (Record a))
9591 )
9692 => a -> Columns a (Col (Context a))
97− toColumns = toGColumns . from . Record
93+ toColumns =
94+ toGColumns @(TTable context) @TColumns toColumns .
95+ from .
96+ Record
9897
9998 default fromColumns ::
100− ( Generic (Record a), GTable (GContext (Rep (Record a))) (Rep (Record a))
101− , Columns a ~ GColumns (Rep (Record a))
102− , Context a ~ GContext (Rep (Record a))
99+ ( Generic (Record a)
100+ , GTable (TTable context) TColumns (Col (Context a)) (Rep (Record a))
101+ , Columns a ~ GColumns TColumns (Rep (Record a))
103102 )
104103 => Columns a (Col (Context a)) -> a
105− fromColumns = unrecord . to . fromGColumns
106−
107−
108−type GColumns :: (Type -> Type) -> K.HTable
109−type family GColumns rep where
110− GColumns (M1 D _ rep) = GColumns rep
111− GColumns (M1 C _ rep) = GColumns rep
112− GColumns (rep1 :*: rep2) = HProduct (GColumns rep1) (GColumns rep2)
113− GColumns (M1 S ('MetaSel ('Just label) _ _ _) (K1 _ a)) =
114− HLabel label (Columns a)
115−
116−
117−type GContext :: (Type -> Type) -> K.Context
118−type family GContext rep where
119− GContext (M1 _ _ rep) = GContext rep
120− GContext (rep1 :*: _rep2) = GContext rep1
121− GContext (K1 _ a) = Context a
122−
104+ fromColumns =
105+ unrecord .
106+ to .
107+ fromGColumns @(TTable context) @TColumns fromColumns
123108
124−type GTable :: K.Context -> (Type -> Type) -> Constraint
125−class context ~ GContext rep => GTable context rep | rep -> context where
126− fromGColumns :: GColumns rep (Col context) -> rep x
127− toGColumns :: rep x -> GColumns rep (Col context)
128109
110+data TTable :: K.Context -> Type -> Exp Constraint
111+type instance Eval (TTable context a) = Table context a
129112
130−instance GTable context rep => GTable context (M1 D c rep) where
131− fromGColumns = M1 . fromGColumns @context @rep
132− toGColumns (M1 a) = toGColumns @context @rep a
133113
114+data TColumns :: Type -> Exp K.HTable
115+type instance Eval (TColumns a) = Columns a
134116
135−instance GTable context rep => GTable context (M1 C c rep) where
136− fromGColumns = M1 . fromGColumns @context @rep
137− toGColumns (M1 a) = toGColumns @context @rep a
138117
139−
140−instance (GTable context rep1, GTable context rep2) =>
141− GTable context (rep1 :*: rep2)
142− where
143− fromGColumns (HProduct a b) =
144− fromGColumns @context @rep1 a :*: fromGColumns @context @rep2 b
145− toGColumns (a :*: b) =
146− HProduct (toGColumns @context @rep1 a) (toGColumns @context @rep2 b)
147−
148−
149−instance
150− ( Table context a
151− , Labelable context
152− , KnownSymbol label
153− , GColumns (M1 S meta k1) ~ HLabel label (Columns a)
154− , meta ~ 'MetaSel ('Just label) _su _ss _ds
155− , k1 ~ K1 i a
156− )
157− => GTable context (M1 S meta k1)
158− where
159− fromGColumns = M1 . K1 . fromColumns . hunlabel unlabeler
160− toGColumns (M1 (K1 a)) = hlabel labeler (toColumns a)
118+data TContext :: Type -> Exp K.Context
119+type instance Eval (TContext a) = Context a
161120
162121
163122-- | Any 'HTable' is also a 'Table'.