@@ -14,10 +14,9 @@
14{-# language UndecidableInstances #-}
14{-# language UndecidableInstances #-}
16module Rel8.Table
16module Rel8.Table
17− ( Table (Columns, Context)
18− , toColumns, fromColumns
17+ ( Table (Columns, Context, toColumns, fromColumns)
19 , Congruent
18 , Congruent
20− , GTable, GColumns, GContext, fromGColumns, toGColumns
19+ , TTable, TColumns, TContext
@@ -26,28 +25,25 @@ import Data.Functor ( ($>) )
26import Data.Functor.Identity ( Identity( Identity ) )
25import Data.Functor.Identity ( Identity( Identity ) )
27import Data.Kind ( Constraint, Type )
26import Data.Kind ( Constraint, Type )
28import Data.List.NonEmpty ( NonEmpty )
27import Data.List.NonEmpty ( NonEmpty )
29−import GHC.Generics
28+import GHC.Generics ( Generic, Rep, from, to )
30− ( Generic, Rep, from, to
31− , (:*:)( (:*:) ), K1( K1 ), M1( M1 )
35−import GHC.TypeLits ( KnownSymbol )
36import Prelude hiding ( null )
29import Prelude hiding ( null )
32+import Rel8.FCF ( Eval, Exp )
33+import Rel8.Generic.Table
34+ ( GTable, GColumns, GContext, fromGColumns, toGColumns
39import Rel8.Generic.Record ( Record(..) )
36import Rel8.Generic.Record ( Record(..) )
40import Rel8.Schema.Context ( Col(..) )
37import Rel8.Schema.Context ( Col(..) )
41import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
38import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
42import Rel8.Schema.HTable ( HTable )
39import Rel8.Schema.HTable ( HTable )
43import Rel8.Schema.HTable.Either ( HEitherTable(..) )
40import Rel8.Schema.HTable.Either ( HEitherTable(..) )
44import Rel8.Schema.HTable.Identity ( HIdentity(..) )
41import Rel8.Schema.HTable.Identity ( HIdentity(..) )
45−import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
42+import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
46import Rel8.Schema.HTable.List ( HListTable )
43import Rel8.Schema.HTable.List ( HListTable )
47import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
44import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
48import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
45import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
49import Rel8.Schema.HTable.Nullify ( hnulls, hnullify, hunnullify )
46import Rel8.Schema.HTable.Nullify ( hnulls, hnullify, hunnullify )
50−import Rel8.Schema.HTable.Product ( HProduct(..) )
51import Rel8.Schema.HTable.These ( HTheseTable(..) )
47import Rel8.Schema.HTable.These ( HTheseTable(..) )
52import Rel8.Schema.HTable.Type ( HType( HType ) )
48import Rel8.Schema.HTable.Type ( HType( HType ) )
53import Rel8.Schema.HTable.Vectorize ( hvectorize, hunvectorize )
49import Rel8.Schema.HTable.Vectorize ( hvectorize, hunvectorize )
@@ -85,79 +81,42 @@ class (HTable (Columns a), context ~ Context a) => Table context a | a -> contex
85 toColumns :: a -> Columns a (Col (Context a))
81 toColumns :: a -> Columns a (Col (Context a))
86 fromColumns :: Columns a (Col (Context a)) -> a
82 fromColumns :: Columns a (Col (Context a)) -> a
88− type Columns a = GColumns (Rep (Record a))
84+ type Columns a = GColumns TColumns (Rep (Record a))
89− type Context a = GContext (Rep (Record a))
85+ type Context a = GContext TContext (Rep (Record a))
91 default toColumns ::
87 default toColumns ::
92− ( Generic (Record a), GTable (GContext (Rep (Record a))) (Rep (Record a))
88+ ( Generic (Record a)
89+ , GTable (TTable context) TColumns (Col (Context a)) (Rep (Record a))
93− , Columns a ~ GColumns (Rep (Record a))
90+ , Columns a ~ GColumns TColumns (Rep (Record a))
94− , Context a ~ GContext (Rep (Record a))
96 => a -> Columns a (Col (Context a))
92 => a -> Columns a (Col (Context a))
97− toColumns = toGColumns . from . Record
93+ toColumns =
94+ toGColumns @(TTable context) @TColumns toColumns .
99 default fromColumns ::
98 default fromColumns ::
100− ( Generic (Record a), GTable (GContext (Rep (Record a))) (Rep (Record a))
99+ ( Generic (Record a)
100+ , GTable (TTable context) TColumns (Col (Context a)) (Rep (Record a))
101− , Columns a ~ GColumns (Rep (Record a))
101+ , Columns a ~ GColumns TColumns (Rep (Record a))
102− , Context a ~ GContext (Rep (Record a))
104 => Columns a (Col (Context a)) -> a
103 => Columns a (Col (Context a)) -> a
105− fromColumns = unrecord . to . fromGColumns
104+ fromColumns =
108−type GColumns :: (Type -> Type) -> K.HTable
107+ fromGColumns @(TTable context) @TColumns fromColumns
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)
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
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)
110+data TTable :: K.Context -> Type -> Exp Constraint
111+type instance Eval (TTable context a) = Table context a
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
114+data TColumns :: Type -> Exp K.HTable
115+type instance Eval (TColumns a) = Columns a
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
139−
118+data TContext :: Type -> Exp K.Context
140−instance (GTable context rep1, GTable context rep2) =>
119+type instance Eval (TContext a) = Context a
141− GTable context (rep1 :*: rep2)
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)
153− , GColumns (M1 S meta k1) ~ HLabel label (Columns a)
154− , meta ~ 'MetaSel ('Just label) _su _ss _ds
157− => GTable context (M1 S meta k1)
159− fromGColumns = M1 . K1 . fromColumns . hunlabel unlabeler
160− toGColumns (M1 (K1 a)) = hlabel labeler (toColumns a)
163-- | Any 'HTable' is also a 'Table'.
122-- | Any 'HTable' is also a 'Table'.