@@ -3,12 +3,14 @@
3{-# language DataKinds #-}
3{-# language DataKinds #-}
4{-# language DefaultSignatures #-}
4{-# language DefaultSignatures #-}
5{-# language FlexibleContexts #-}
5{-# language FlexibleContexts #-}
6+{-# language FlexibleInstances #-}
6{-# language LambdaCase #-}
7{-# language LambdaCase #-}
8+{-# language MultiParamTypeClasses #-}
7{-# language ScopedTypeVariables #-}
9{-# language ScopedTypeVariables #-}
8{-# language StandaloneKindSignatures #-}
10{-# language StandaloneKindSignatures #-}
9{-# language TypeApplications #-}
11{-# language TypeApplications #-}
10−{-# language TypeFamilies #-}
11{-# language TypeFamilyDependencies #-}
12{-# language TypeFamilyDependencies #-}
13+{-# language TypeOperators #-}
12{-# language UndecidableInstances #-}
14{-# language UndecidableInstances #-}
14module Rel8.Generic.Rel8able
16module Rel8.Generic.Rel8able
@@ -17,31 +19,36 @@ module Rel8.Generic.Rel8able
18 , GColumns, gfromColumns, gtoColumns
20 , GColumns, gfromColumns, gtoColumns
19 , GFromExprs, gfromResult, gtoResult
21 , GFromExprs, gfromResult, gtoResult
22+ , TSerialize, serialize, deserialize
24import Data.Kind ( Constraint, Type )
27import Data.Kind ( Constraint, Type )
28+import Data.Type.Equality ( type (==) )
25import GHC.Generics ( Generic, Rep, from, to )
29import GHC.Generics ( Generic, Rep, from, to )
26import Prelude
30import Prelude
29import Rel8.Aggregate ( Aggregate )
33import Rel8.Aggregate ( Aggregate )
30import Rel8.Expr ( Expr )
34import Rel8.Expr ( Expr )
31−import Rel8.Generic.Map ( GMap )
35+import Rel8.FCF ( Exp, Eval )
32import Rel8.Generic.Record ( Record(..) )
36import Rel8.Generic.Record ( Record(..) )
33import Rel8.Generic.Table ( GAlgebra )
37import Rel8.Generic.Table ( GAlgebra )
34import qualified Rel8.Generic.Table.Record as G
38import qualified Rel8.Generic.Table.Record as G
35import qualified Rel8.Kind.Algebra as K ( Algebra(..) )
39import qualified Rel8.Kind.Algebra as K ( Algebra(..) )
36−import Rel8.Schema.Context.Virtual ( Abstract(..) )
40+import Rel8.Kind.Context ( SContext(..) )
37import Rel8.Schema.HTable ( HTable )
41import Rel8.Schema.HTable ( HTable )
38import qualified Rel8.Schema.Kind as K
42import qualified Rel8.Schema.Kind as K
39import Rel8.Schema.Name ( Name )
43import Rel8.Schema.Name ( Name )
40import Rel8.Schema.Result ( Result )
44import Rel8.Schema.Result ( Result )
41import Rel8.Table
45import Rel8.Table
46+ ( Table, Columns, Context, fromColumns, toColumns
42− ( fromColumns, toColumns, fromResult, toResult
47+ , FromExprs, fromResult, toResult
43− , TTable, TColumns, TFromExprs
49+ , TTable, TColumns
51+import Rel8.Table.Transpose ( Transposes )
47-- | The kind of 'Rel8able' types
54-- | The kind of 'Rel8able' types
@@ -49,6 +56,40 @@ type KRel8able :: Type
49type KRel8able = K.Rel8able
56type KRel8able = K.Rel8able
59+type Serialize :: Bool -> Type -> Type -> Constraint
60+class transposition ~ (a == Transpose Result expr) =>
61+ Serialize transposition expr a
63+ serialize :: a -> Columns expr Result
64+ deserialize :: Columns expr Result -> a
68+ ( (a == Transpose Result expr) ~ 'True
69+ , Transposes Expr Result expr a
71+ => Serialize 'True expr a
73+ serialize = toColumns
74+ deserialize = fromColumns
78+ ( (a == Transpose Result expr) ~ 'False
79+ , Table (Context expr) expr
82+ => Serialize 'False expr a
84+ serialize = toResult @_ @expr
85+ deserialize = fromResult @_ @expr
88+data TSerialize :: Type -> Type -> Exp Constraint
89+type instance Eval (TSerialize expr a) =
90+ Serialize (a == Transpose Result expr) expr a
52-- | This type class allows you to define custom 'Table's using higher-kinded
93-- | This type class allows you to define custom 'Table's using higher-kinded
53-- data types. Higher-kinded data types are data types of the pattern:
94-- data types. Higher-kinded data types are data types of the pattern:
@@ -96,8 +137,8 @@ class HTable (GColumns t) => Rel8able t where
96 type GColumns t :: K.HTable
137 type GColumns t :: K.HTable
97 type GFromExprs t :: Type
138 type GFromExprs t :: Type
99− gfromColumns :: Abstract context -> GColumns t context -> t context
140+ gfromColumns :: SContext context -> GColumns t context -> t context
100− gtoColumns :: Abstract context -> t context -> GColumns t context
141+ gtoColumns :: SContext context -> t context -> GColumns t context
102 gfromResult :: GColumns t Result -> GFromExprs t
143 gfromResult :: GColumns t Result -> GFromExprs t
103 gtoResult :: GFromExprs t -> GColumns t Result
144 gtoResult :: GFromExprs t -> GColumns t Result
@@ -106,62 +147,38 @@ class HTable (GColumns t) => Rel8able t where
106 type GFromExprs t = t Result
147 type GFromExprs t = t Result
108 default gfromColumns :: forall context.
149 default gfromColumns :: forall context.
109− ( VRel8able t Aggregate
150+ ( SRel8able t Aggregate
110− , VRel8able t Expr
151+ , SRel8able t Expr
111− , VRel8able t Name
152+ , SRel8able t Name
113− => Abstract context -> GColumns t context -> t context
155+ => SContext context -> GColumns t context -> t context
114 gfromColumns = \case
156 gfromColumns = \case
115− VAggregate -> vfromColumns
157+ SAggregate -> sfromColumns
116− VExpr -> vfromColumns
158+ SExpr -> sfromColumns
117− VName -> vfromColumns
159+ SName -> sfromColumns
160+ SResult -> sfromResult
119 default gtoColumns :: forall context.
162 default gtoColumns :: forall context.
120− ( VRel8able t Aggregate
163+ ( SRel8able t Aggregate
121− , VRel8able t Expr
164+ , SRel8able t Expr
122− , VRel8able t Name
165+ , SRel8able t Name
124− => Abstract context -> t context -> GColumns t context
168+ => SContext context -> t context -> GColumns t context
125 gtoColumns = \case
169 gtoColumns = \case
126− VAggregate -> vtoColumns
170+ SAggregate -> stoColumns
127− VExpr -> vtoColumns
171+ SExpr -> stoColumns
128− VName -> vtoColumns
172+ SName -> stoColumns
129−
173+ SResult -> stoResult
130− default gfromResult ::
175+ default gfromResult :: (SSerialize t, GFromExprs t ~ t Result)
131− ( Generic (Record (t Result))
132− , G.GTable (TTable Expr) TColumns TFromExprs (GRep t Expr)
133− , G.GColumns TColumns (GRep t Expr) ~ GColumns t
134− , Rep (Record (t Result)) ~ GMap TFromExprs (GRep t Expr)
135− , GFromExprs t ~ t Result
137 => GColumns t Result -> GFromExprs t
176 => GColumns t Result -> GFromExprs t
138− gfromResult =
177+ gfromResult = sfromResult
146− (\(_ :: proxy x) -> fromResult @Expr @x)
148− default gtoResult ::
149− ( Generic (Record (t Result))
150− , G.GTable (TTable Expr) TColumns TFromExprs (GRep t Expr)
151− , G.GColumns TColumns (GRep t Expr) ~ GColumns t
152− , Rep (Record (t Result)) ~ GMap TFromExprs (GRep t Expr)
153− , GFromExprs t ~ t Result
179+ default gtoResult :: (SSerialize t, GFromExprs t ~ t Result)
155 => GFromExprs t -> GColumns t Result
180 => GFromExprs t -> GColumns t Result
156− gtoResult =
181+ gtoResult = stoResult
162− (\(_ :: proxy x) -> toResult @Expr @x) .
167type Algebra :: K.Rel8able -> K.Algebra
184type Algebra :: K.Rel8able -> K.Algebra
@@ -172,25 +189,59 @@ type GRep :: K.Rel8able -> K.Context -> Type -> Type
172type GRep t context = Rep (Record (t context))
189type GRep t context = Rep (Record (t context))
175−type VRel8able :: K.Rel8able -> K.Context -> Constraint
192+type SRel8able :: K.Rel8able -> K.Context -> Constraint
176−type VRel8able t context =
193+type SRel8able t context =
177 ( Generic (Record (t context))
194 ( Generic (Record (t context))
178− , G.GTable (TTable context) TColumns TFromExprs (GRep t context)
195+ , G.GTable (TTable context) TColumns (GRep t context)
179 , G.GColumns TColumns (GRep t context) ~ GColumns t
196 , G.GColumns TColumns (GRep t context) ~ GColumns t
200+type SSerialize :: K.Rel8able -> Constraint
202+ ( Generic (Record (t Result))
203+ , G.GSerialize TSerialize TColumns (GRep t Expr) (GRep t Result)
204+ , G.GColumns TColumns (GRep t Expr) ~ GColumns t
183−vfromColumns :: forall t context. VRel8able t context
208+sfromColumns :: forall t context. SRel8able t context
184 => GColumns t context -> t context
209 => GColumns t context -> t context
185−vfromColumns =
210+sfromColumns =
186 unrecord .
211 unrecord .
188− G.gfromColumns @(TTable context) @TColumns @TFromExprs fromColumns
213+ G.gfromColumns @(TTable context) @TColumns fromColumns
191−vtoColumns :: forall t context. VRel8able t context
216+stoColumns :: forall t context. SRel8able t context
192 => t context -> GColumns t context
217 => t context -> GColumns t context
193−vtoColumns =
218+stoColumns =
194− G.gtoColumns @(TTable context) @TColumns @TFromExprs toColumns .
219+ G.gtoColumns @(TTable context) @TColumns toColumns .
224+sfromResult :: forall t. SSerialize t
225+ => GColumns t Result -> t Result
234+ (\(_ :: proxy x) -> deserialize @_ @x)
237+stoResult :: forall t. SSerialize t
238+ => t Result -> GColumns t Result
245+ (\(_ :: proxy x) -> serialize @_ @x) .