on master help sign in
Unfold Rel8.Schema.HKD into Rel8.Generic.HKD and Rel8.Column.Lift
commit6e42c1ea6338c0d9c6876b945eac15d8d96a53eb authorShane O'Brien <shane.obrien@circuithub.com> dateFri 30 Apr 2021, 18:13 (6 years ago) parent95a2ebf
6 files changed+373−320
rel8.cabal+3−1 src/Rel8.hs+2−1 src/Rel8/Column/Lift.hs+129 src/Rel8/Generic/HKD.hs+197 src/Rel8/Schema/HKD.hs−318 src/Rel8/Table/HKD.hs+42
rel8.cabal+3−1
@@ -52,6 +52,7 @@ library
5252 Rel8.Column
5353 Rel8.Column.Either
5454 Rel8.Column.Field
55+ Rel8.Column.Lift
5556 Rel8.Column.List
5657 Rel8.Column.Maybe
5758 Rel8.Column.NonEmpty
@@ -76,6 +77,7 @@ library
7677 Rel8.Kind.Labels
7778 Rel8.Kind.Necessity
7879
80+ Rel8.Generic.HKD
7981 Rel8.Generic.Map
8082 Rel8.Generic.Record
8183 Rel8.Generic.Reify
@@ -109,7 +111,6 @@ library
109111 Rel8.Schema.Context.Label
110112 Rel8.Schema.Context.Nullify
111113 Rel8.Schema.Dict
112− Rel8.Schema.HKD
113114 Rel8.Schema.HTable
114115 Rel8.Schema.HTable.Either
115116 Rel8.Schema.HTable.Identity
@@ -147,6 +148,7 @@ library
147148 Rel8.Table.Bool
148149 Rel8.Table.Either
149150 Rel8.Table.Eq
151+ Rel8.Table.HKD
150152 Rel8.Table.Insert
151153 Rel8.Table.List
152154 Rel8.Table.Maybe
src/Rel8.hs+2−1
@@ -253,6 +253,7 @@ import Rel8.Aggregate
253253import Rel8.Column
254254import Rel8.Column.Either
255255import Rel8.Column.Field
256+import Rel8.Column.Lift
256257import Rel8.Column.List
257258import Rel8.Column.Maybe
258259import Rel8.Column.NonEmpty
@@ -287,7 +288,6 @@ import Rel8.Query.Set
287288import Rel8.Query.These
288289import Rel8.Query.Values
289290import Rel8.Schema.Context.Label
290−import Rel8.Schema.HKD
291291import Rel8.Schema.HTable
292292import Rel8.Schema.Name
293293import Rel8.Schema.Null hiding ( nullable )
@@ -305,6 +305,7 @@ import Rel8.Table.Alternative
305305import Rel8.Table.Bool
306306import Rel8.Table.Either
307307import Rel8.Table.Eq
308+import Rel8.Table.HKD
308309import Rel8.Table.Insert
309310import Rel8.Table.List
310311import Rel8.Table.Maybe
src/Rel8/Column/Lift.hsnew file+129
@@ -0,0 +1,129 @@
1+{-# language DataKinds #-}
2+{-# language FlexibleContexts #-}
3+{-# language LambdaCase #-}
4+{-# language MultiParamTypeClasses #-}
5+{-# language ScopedTypeVariables #-}
6+{-# language StandaloneKindSignatures #-}
7+{-# language TypeApplications #-}
8+{-# language TypeFamilies #-}
9+{-# language UndecidableInstances #-}
10+
11+module Rel8.Column.Lift
12+ ( Lift, ALift(..)
13+ )
14+where
15+
16+-- base
17+import Data.Functor.Compose ( Compose(..) )
18+import Data.Functor.Identity ( Identity(..), runIdentity )
19+import Data.Kind ( Type )
20+import GHC.Generics ( Rep )
21+import Prelude
22+
23+-- higgledy
24+import Data.Generic.HKD ( Construct, HKD(..), construct, deconstruct )
25+
26+-- rel8
27+import Rel8.Aggregate ( Col(..), Aggregate )
28+import Rel8.Expr ( Expr )
29+import Rel8.Generic.HKD ( GTable, GColumns, gfromColumns, gtoColumns )
30+import Rel8.Kind.Context ( Reifiable(..), SContext(..) )
31+import Rel8.Schema.HTable.Type ( HType( HType ) )
32+import Rel8.Schema.Insert ( Insert, Col(..) )
33+import qualified Rel8.Schema.Kind as K
34+import Rel8.Schema.Name ( Name(..) )
35+import Rel8.Schema.Reify ( Reify, hreify, hunreify )
36+import Rel8.Schema.Result ( Result )
37+import Rel8.Table
38+ ( Table, Columns, Context, fromColumns, toColumns
39+ , Unreify, reify, unreify
40+ )
41+import Rel8.Table.Recontextualize ( Recontextualize )
42+
43+
44+type Lift :: K.Context -> Type -> Type
45+type family Lift context a where
46+ Lift (Reify context) a = ALift context a
47+ Lift Aggregate a = HKD a (Compose Aggregate Expr)
48+ Lift Expr a = HKD a Expr
49+ Lift Insert a = HKD a Expr
50+ Lift Name a = HKD a Name
51+ Lift Result a = a
52+
53+
54+type ALift :: K.Context -> Type -> Type
55+newtype ALift context a = ALift
56+ { unALift :: Lift context a
57+ }
58+
59+
60+instance
61+ ( Reifiable context
62+ , GTable (Rep a)
63+ , Construct Identity a
64+ )
65+ => Table (Reify context) (ALift context a)
66+ where
67+ type Context (ALift context a) = Reify context
68+ type Columns (ALift context a) = GColumns (Rep a)
69+ type Unreify (ALift context a) = Lift context a
70+
71+ fromColumns = sfromColumnsLift contextSing
72+ toColumns = stoColumnsLift contextSing
73+ reify _ = ALift
74+ unreify _ (ALift a) = a
75+
76+
77+instance
78+ ( Reifiable context
79+ , Reifiable context'
80+ , GTable (Rep a)
81+ , Construct Identity a
82+ )
83+ => Recontextualize
84+ (Reify context)
85+ (Reify context')
86+ (ALift context a)
87+ (ALift context' a)
88+
89+
90+sfromColumnsLift :: forall a context. (GTable (Rep a), Construct Identity a)
91+ => SContext context
92+ -> GColumns (Rep a) (Col (Reify context))
93+ -> ALift context a
94+sfromColumnsLift = \case
95+ SAggregate ->
96+ ALift .
97+ HKD .
98+ gfromColumns (\(HType (Aggregation a)) -> Compose a) .
99+ hunreify
100+ SExpr -> ALift . fromColumns . hunreify
101+ SInsert ->
102+ ALift .
103+ HKD .
104+ gfromColumns (\(HType (RequiredInsert a)) -> a) .
105+ hunreify
106+ SName -> ALift . fromColumns . hunreify
107+ SResult -> ALift . runIdentity . construct . fromColumns . hunreify
108+ SReify context -> ALift . sfromColumnsLift context . hunreify
109+
110+
111+stoColumnsLift :: forall a context. (GTable (Rep a), Construct Identity a)
112+ => SContext context
113+ -> ALift context a
114+ -> GColumns (Rep a) (Col (Reify context))
115+stoColumnsLift = \case
116+ SAggregate ->
117+ hreify .
118+ gtoColumns (\(Compose a) -> HType (Aggregation a)) .
119+ runHKD .
120+ unALift
121+ SExpr -> hreify . toColumns . unALift
122+ SInsert ->
123+ hreify .
124+ gtoColumns (HType . RequiredInsert) .
125+ runHKD .
126+ unALift
127+ SName -> hreify . toColumns . unALift
128+ SResult -> hreify . toColumns . deconstruct @Identity . unALift
129+ SReify context -> hreify . stoColumnsLift context . unALift
src/Rel8/Generic/HKD.hsnew file+197
@@ -0,0 +1,197 @@
1+{-# language DataKinds #-}
2+{-# language FlexibleContexts #-}
3+{-# language FlexibleInstances #-}
4+{-# language FunctionalDependencies #-}
5+{-# language QuantifiedConstraints #-}
6+{-# language RankNTypes #-}
7+{-# language ScopedTypeVariables #-}
8+{-# language StandaloneKindSignatures #-}
9+{-# language TypeApplications #-}
10+{-# language TypeFamilies #-}
11+{-# language TypeOperators #-}
12+{-# language UndecidableInstances #-}
13+{-# language UndecidableSuperClasses #-}
14+
15+{-# options -Wno-orphans #-}
16+
17+module Rel8.Generic.HKD
18+ ( GTable, GColumns, gfromColumns, gtoColumns
19+ )
20+where
21+
22+-- base
23+import Data.Kind ( Constraint, Type )
24+import GHC.Generics
25+ ( (:*:)( (:*:) ), K1( K1 ), M1( M1 ), C, D, S, Meta( MetaSel )
26+ , Rep
27+ )
28+import GHC.TypeLits ( KnownSymbol )
29+import Prelude
30+
31+-- higgledy
32+import Data.Generic.HKD ( HKD(..), GHKD_ )
33+
34+-- rel8
35+import Rel8.Aggregate ( Col(..) )
36+import Rel8.Schema.Context.Label
37+ ( Labelable
38+ , HLabelable, hlabeler, hunlabeler
39+ )
40+import Rel8.Schema.Dict ( Dict( Dict ) )
41+import Rel8.Schema.HTable ( HTable )
42+import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
43+import Rel8.Schema.HTable.Product ( HProduct( HProduct ) )
44+import Rel8.Schema.HTable.Type ( HType )
45+import qualified Rel8.Schema.Kind as K
46+import Rel8.Schema.Null ( Sql )
47+import Rel8.Schema.Reify ( NotReify, notReify )
48+import Rel8.Table
49+ ( Table, Columns, Context, fromColumns, toColumns
50+ , reify, unreify
51+ )
52+import Rel8.Table.Recontextualize ( Recontextualize )
53+import Rel8.Type ( DBType )
54+
55+
56+type Column1Helper :: K.Context -> (Type -> Type) -> Type -> Constraint
57+class
58+ ( Table context (f a)
59+ , Context1 f ~ context
60+ , Context (f a) ~ context
61+ , Columns (f a) ~ HType a
62+ )
63+ => Column1Helper context f a | f -> context
64+instance
65+ ( Table context (f a)
66+ , Context1 f ~ context
67+ , Context (f a) ~ context
68+ , Columns (f a) ~ HType a
69+ )
70+ => Column1Helper context f a
71+
72+
73+type Column1 :: K.Context -> (Type -> Type) -> Constraint
74+class
75+ ( forall a. Sql DBType a => Column1Helper context f a
76+ , Context1 f ~ context
77+ )
78+ => Column1 context f | f -> context
79+instance
80+ ( forall a. Sql DBType a => Column1Helper context f a
81+ , Context1 f ~ context
82+ )
83+ => Column1 context f
84+
85+
86+type Context1 :: (Type -> Type) -> K.Context
87+type Context1 f = Context (f Bool)
88+
89+
90+toColumn1 :: forall a f context. (Column1 context f, Sql DBType a)
91+ => f a -> HType a (Col context)
92+toColumn1 = case Dict @(Column1Helper context f) @a of
93+ Dict -> toColumns
94+
95+
96+fromColumn1 :: forall a f context. (Column1 context f, Sql DBType a)
97+ => HType a (Col context) -> f a
98+fromColumn1 = case Dict @(Column1Helper context f) @a of
99+ Dict -> fromColumns
100+
101+
102+type Recontextualize1
103+ :: K.Context
104+ -> K.Context
105+ -> (Type -> Type)
106+ -> (Type -> Type)
107+ -> Constraint
108+class Recontextualize context context' (f Bool) (f' Bool) =>
109+ Recontextualize1 context context' f f'
110+instance Recontextualize context context' (f Bool) (f' Bool) =>
111+ Recontextualize1 context context' f f'
112+
113+
114+class HTable (GColumns rep) => GTable rep where
115+ gtoColumns :: HLabelable context
116+ => (forall a. Sql DBType a => f a -> HType a context)
117+ -> GHKD_ f rep x
118+ -> GColumns rep context
119+ gfromColumns :: HLabelable context
120+ => (forall a. Sql DBType a => HType a context -> f a)
121+ -> GColumns rep context
122+ -> GHKD_ f rep x
123+
124+
125+instance GTable rep => GTable (M1 D c rep) where
126+ gtoColumns f (M1 a) = gtoColumns f a
127+ {-# INLINABLE gtoColumns #-}
128+
129+ gfromColumns f = M1 . gfromColumns f
130+ {-# INLINABLE gfromColumns #-}
131+
132+
133+instance GTable rep => GTable (M1 C c rep) where
134+ gtoColumns f (M1 a) = gtoColumns f a
135+ {-# INLINABLE gtoColumns #-}
136+
137+ gfromColumns f = M1 . gfromColumns f
138+ {-# INLINABLE gfromColumns #-}
139+
140+
141+instance (KnownSymbol name, Sql DBType a) =>
142+ GTable (M1 S ('MetaSel ('Just name) _su _ss _ds) (K1 i a))
143+ where
144+ gtoColumns f (M1 (K1 a)) = hlabel hlabeler (f a)
145+ {-# INLINABLE gtoColumns #-}
146+
147+ gfromColumns f a = M1 (K1 (f (hunlabel hunlabeler a)))
148+ {-# INLINABLE gfromColumns #-}
149+
150+
151+instance (GTable f, GTable g) => GTable (f :*: g) where
152+ gtoColumns f (x :*: y) = HProduct (gtoColumns f x) (gtoColumns f y)
153+ {-# INLINABLE gtoColumns #-}
154+
155+ gfromColumns f (HProduct x y) = gfromColumns f x :*: gfromColumns f y
156+ {-# INLINABLE gfromColumns #-}
157+
158+
159+type GColumns :: (Type -> Type) -> K.HTable
160+type family GColumns rep where
161+ GColumns (M1 D _ f) = GColumns f
162+ GColumns (M1 C _ f) = GColumns f
163+ GColumns (M1 S ('MetaSel ('Just name) _ _ _) (K1 _ a)) =
164+ HLabel name (HType a)
165+ GColumns (f :*: g) = HProduct (GColumns f) (GColumns g)
166+
167+
168+instance
169+ ( GTable (Rep a)
170+ , Column1 context f
171+ , Labelable context
172+ , NotReify context
173+ )
174+ => Table context (HKD a f)
175+ where
176+ type Columns (HKD a f) = GColumns (Rep a)
177+ type Context (HKD a f) = Context1 f
178+
179+ toColumns = gtoColumns toColumn1 . runHKD
180+ fromColumns = HKD . gfromColumns fromColumn1
181+ reify = notReify
182+ unreify = notReify
183+
184+
185+instance
186+ ( a ~ a'
187+ , GTable (Rep a)
188+ , NotReify context, NotReify context'
189+ , Recontextualize1 context context' f f'
190+ , Column1 context f, Labelable context
191+ , Column1 context' f', Labelable context'
192+ )
193+ => Recontextualize
194+ context
195+ context'
196+ (HKD a f)
197+ (HKD a' f')
src/Rel8/Schema/HKD.hsdeleted−318
@@ -1,318 +0,0 @@
1−{-# language AllowAmbiguousTypes #-}
2−{-# language BlockArguments #-}
3−{-# language ConstraintKinds #-}
4−{-# language DataKinds #-}
5−{-# language FlexibleContexts #-}
6−{-# language FlexibleInstances #-}
7−{-# language FunctionalDependencies #-}
8−{-# language GADTs #-}
9−{-# language LambdaCase #-}
10−{-# language QuantifiedConstraints #-}
11−{-# language PolyKinds #-}
12−{-# language RankNTypes #-}
13−{-# language ScopedTypeVariables #-}
14−{-# language StandaloneKindSignatures #-}
15−{-# language TypeApplications #-}
16−{-# language TypeFamilyDependencies #-}
17−{-# language TypeOperators #-}
18−{-# language UndecidableInstances #-}
19−{-# language UndecidableSuperClasses #-}
20−
21−{-# options -Wno-orphans #-}
22−
23−module Rel8.Schema.HKD
24− ( Lift
25− , HKDT(..)
26− ) where
27−
28−-- base
29−import Data.Functor.Compose ( Compose(..) )
30−import Data.Functor.Identity ( Identity(..), runIdentity )
31−import Data.Kind ( Constraint, Type )
32−import GHC.Generics
33− ( (:*:)( (:*:) ), K1( K1 ), M1( M1 ), C, D, S, Meta( MetaSel )
34− , Rep
35− )
36−import GHC.TypeLits ( KnownSymbol )
37−import Prelude
38−
39−-- higgledy
40−import Data.Generic.HKD ( Construct, HKD(..), GHKD_, construct, deconstruct )
41−
42−-- rel8
43−import Rel8.Aggregate ( Col(..), Aggregate )
44−import Rel8.Expr ( Expr )
45−import Rel8.Kind.Context ( Reifiable(..), SContext(..) )
46−import Rel8.Schema.Context.Label
47− ( Labelable
48− , HLabelable, hlabeler, hunlabeler
49− )
50−import Rel8.Schema.Dict ( Dict( Dict ) )
51−import Rel8.Schema.HTable ( HTable )
52−import Rel8.Schema.HTable.Label ( HLabel, hlabel, hunlabel )
53−import Rel8.Schema.HTable.Product ( HProduct( HProduct ) )
54−import Rel8.Schema.HTable.Type ( HType( HType ) )
55−import Rel8.Schema.Insert ( Insert, Col(..) )
56−import qualified Rel8.Schema.Kind as K
57−import Rel8.Schema.Name ( Name(..) )
58−import Rel8.Schema.Null ( Sql )
59−import Rel8.Schema.Reify ( Reify, hreify, hunreify, NotReify, notReify )
60−import Rel8.Schema.Result ( Result )
61−import Rel8.Table
62− ( Table, Columns, Context, fromColumns, toColumns
63− , Unreify, reify, unreify
64− )
65−import Rel8.Table.Recontextualize ( Recontextualize )
66−import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
67−import Rel8.Type ( DBType )
68−
69−
70−type Column1Helper :: K.Context -> (Type -> Type) -> Type -> Constraint
71−class
72− ( Table context (f a)
73− , Context1 f ~ context
74− , Context (f a) ~ context
75− , Columns (f a) ~ HType a
76− )
77− => Column1Helper context f a | f -> context
78−instance
79− ( Table context (f a)
80− , Context1 f ~ context
81− , Context (f a) ~ context
82− , Columns (f a) ~ HType a
83− )
84− => Column1Helper context f a
85−
86−
87−type Column1 :: K.Context -> (Type -> Type) -> Constraint
88−class
89− ( forall a. Sql DBType a => Column1Helper context f a
90− , Context1 f ~ context
91− ) =>
92− Column1 context f | f -> context
93−instance
94− ( forall a. Sql DBType a => Column1Helper context f a
95− , Context1 f ~ context
96− ) =>
97− Column1 context f
98−
99−
100−type Context1 :: (Type -> Type) -> K.Context
101−type Context1 f = Context (f Bool)
102−
103−
104−toColumn1 :: forall a f context. (Column1 context f, Sql DBType a)
105− => f a -> HType a (Col context)
106−toColumn1 = case Dict @(Column1Helper context f) @a of
107− Dict -> toColumns
108−
109−
110−fromColumn1 :: forall a f context. (Column1 context f, Sql DBType a)
111− => HType a (Col context) -> f a
112−fromColumn1 = case Dict @(Column1Helper context f) @a of
113− Dict -> fromColumns
114−
115−
116−type Recontextualize1
117− :: K.Context
118− -> K.Context
119− -> (Type -> Type)
120− -> (Type -> Type)
121− -> Constraint
122−class Recontextualize context context' (f Bool) (f' Bool) =>
123− Recontextualize1 context context' f f'
124−instance Recontextualize context context' (f Bool) (f' Bool) =>
125− Recontextualize1 context context' f f'
126−
127−
128−class HTable (GColumns rep) => GTable rep where
129− toGColumns :: HLabelable context
130− => (forall a. Sql DBType a => f a -> HType a context)
131− -> GHKD_ f rep x
132− -> GColumns rep context
133− fromGColumns :: HLabelable context
134− => (forall a. Sql DBType a => HType a context -> f a)
135− -> GColumns rep context
136− -> GHKD_ f rep x
137−
138−
139−instance GTable rep => GTable (M1 D c rep) where
140− toGColumns f (M1 a) = toGColumns f a
141− {-# INLINABLE toGColumns #-}
142−
143− fromGColumns f = M1 . fromGColumns f
144− {-# INLINABLE fromGColumns #-}
145−
146−
147−instance GTable rep => GTable (M1 C c rep) where
148− toGColumns f (M1 a) = toGColumns f a
149− {-# INLINABLE toGColumns #-}
150−
151− fromGColumns f = M1 . fromGColumns f
152− {-# INLINABLE fromGColumns #-}
153−
154−
155−instance (KnownSymbol name, Sql DBType a) =>
156− GTable (M1 S ('MetaSel ('Just name) _su _ss _ds) (K1 i a))
157− where
158− toGColumns f (M1 (K1 a)) = hlabel hlabeler (f a)
159− {-# INLINABLE toGColumns #-}
160−
161− fromGColumns f a = M1 (K1 (f (hunlabel hunlabeler a)))
162− {-# INLINABLE fromGColumns #-}
163−
164−
165−instance (GTable f, GTable g) => GTable (f :*: g) where
166− toGColumns f (x :*: y) = HProduct (toGColumns f x) (toGColumns f y)
167− {-# INLINABLE toGColumns #-}
168−
169− fromGColumns f (HProduct x y) = fromGColumns f x :*: fromGColumns f y
170− {-# INLINABLE fromGColumns #-}
171−
172−
173−type GRep a = GColumns (Rep a)
174−
175−
176−type GColumns :: (Type -> Type) -> K.HTable
177−type family GColumns rep where
178− GColumns (M1 D _ f) = GColumns f
179− GColumns (M1 C _ f) = GColumns f
180− GColumns (M1 S ('MetaSel ('Just name) _ _ _) (K1 _ a)) =
181− HLabel name (HType a)
182− GColumns (f :*: g) = HProduct (GColumns f) (GColumns g)
183−
184−
185−instance
186− ( GTable (Rep a)
187− , Column1 context f
188− , Labelable context
189− , NotReify context
190− )
191− => Table context (HKD a f)
192− where
193− type Columns (HKD a f) = GRep a
194− type Context (HKD a f) = Context1 f
195−
196− toColumns = toGColumns toColumn1 . runHKD
197− fromColumns = HKD . fromGColumns fromColumn1
198− reify = notReify
199− unreify = notReify
200−
201−
202−instance
203− ( a ~ a'
204− , GTable (Rep a)
205− , NotReify context, NotReify context'
206− , Recontextualize1 context context' f f'
207− , Column1 context f, Labelable context
208− , Column1 context' f', Labelable context'
209− ) =>
210− Recontextualize
211− context
212− context'
213− (HKD a f)
214− (HKD a' f')
215−
216−
217−type Lift :: K.Context -> Type -> Type
218−type family Lift context a where
219− Lift (Reify context) a = ALift context a
220− Lift Aggregate a = HKD a (Compose Aggregate Expr)
221− Lift Expr a = HKD a Expr
222− Lift Insert a = HKD a Expr
223− Lift Name a = HKD a Name
224− Lift Result a = a
225−
226−
227−type ALift :: K.Context -> Type -> Type
228−newtype ALift context a = ALift
229− { unALift :: Lift context a
230− }
231−
232−
233−instance
234− ( Reifiable context
235− , GTable (Rep a)
236− , Construct Identity a
237− )
238− => Table (Reify context) (ALift context a)
239− where
240− type Context (ALift context a) = Reify context
241− type Columns (ALift context a) = GRep a
242− type Unreify (ALift context a) = Lift context a
243−
244− fromColumns = sfromColumnsLift contextSing
245− toColumns = stoColumnsLift contextSing
246− reify _ = ALift
247− unreify _ (ALift a) = a
248−
249−
250−instance
251− ( Reifiable context
252− , Reifiable context'
253− , GTable (Rep a)
254− , Construct Identity a
255− )
256− => Recontextualize
257− (Reify context)
258− (Reify context')
259− (ALift context a)
260− (ALift context' a)
261−
262−
263−sfromColumnsLift :: forall a context. (GTable (Rep a), Construct Identity a)
264− => SContext context
265− -> GRep a (Col (Reify context))
266− -> ALift context a
267−sfromColumnsLift = \case
268− SAggregate ->
269− ALift .
270− HKD .
271− fromGColumns (\(HType (Aggregation a)) -> Compose a) .
272− hunreify
273− SExpr -> ALift . fromColumns . hunreify
274− SInsert ->
275− ALift .
276− HKD .
277− fromGColumns (\(HType (RequiredInsert a)) -> a) .
278− hunreify
279− SName -> ALift . fromColumns . hunreify
280− SResult -> ALift . runIdentity . construct . fromColumns . hunreify
281− SReify context -> ALift . sfromColumnsLift context . hunreify
282−
283−
284−stoColumnsLift :: forall a context. (GTable (Rep a), Construct Identity a)
285− => SContext context
286− -> ALift context a
287− -> GRep a (Col (Reify context))
288−stoColumnsLift = \case
289− SAggregate ->
290− hreify .
291− toGColumns (\(Compose a) -> HType (Aggregation a)) .
292− runHKD .
293− unALift
294− SExpr -> hreify . toColumns . unALift
295− SInsert ->
296− hreify .
297− toGColumns (HType . RequiredInsert) .
298− runHKD .
299− unALift
300− SName -> hreify . toColumns . unALift
301− SResult -> hreify . toColumns . deconstruct @Identity . unALift
302− SReify context -> hreify . stoColumnsLift context . unALift
303−
304−
305−type HKDT :: Type -> Type
306−newtype HKDT a = HKDT
307− { unHKDT :: a
308− }
309−
310−
311−instance (GTable (Rep a), Construct Identity a, x ~ HKD a Expr) =>
312− ToExprs x (HKDT a)
313− where
314− toResult = toColumns . deconstruct @Identity . unHKDT
315− fromResult = HKDT . runIdentity . construct . fromColumns
316−
317−
318−type instance FromExprs (HKD a Expr) = a
src/Rel8/Table/HKD.hsnew file+42
@@ -0,0 +1,42 @@
1+{-# language FlexibleInstances #-}
2+{-# language MultiParamTypeClasses #-}
3+{-# language StandaloneKindSignatures #-}
4+{-# language TypeApplications #-}
5+{-# language TypeFamilies #-}
6+{-# language UndecidableInstances #-}
7+
8+module Rel8.Table.HKD
9+ ( HKDT(..)
10+ )
11+where
12+
13+-- base
14+import Data.Functor.Identity ( Identity(..), runIdentity )
15+import Data.Kind ( Type )
16+import GHC.Generics ( Rep )
17+import Prelude
18+
19+-- higgledy
20+import Data.Generic.HKD ( Construct, HKD(..), construct, deconstruct )
21+
22+-- rel8
23+import Rel8.Expr ( Expr )
24+import Rel8.Generic.HKD ( GTable )
25+import Rel8.Table ( fromColumns, toColumns )
26+import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
27+
28+
29+type HKDT :: Type -> Type
30+newtype HKDT a = HKDT
31+ { unHKDT :: a
32+ }
33+
34+
35+instance (GTable (Rep a), Construct Identity a, x ~ HKD a Expr) =>
36+ ToExprs x (HKDT a)
37+ where
38+ toResult = toColumns . deconstruct @Identity . unHKDT
39+ fromResult = HKDT . runIdentity . construct . fromColumns
40+
41+
42+type instance FromExprs (HKD a Expr) = a