on master help sign in
Add constructors for HKD
commitcf8bdc7a0caef228ad227353f8add1ee45788682 authorShane O'Brien <shane.obrien@circuithub.com> dateWed 5 May 2021, 01:58 (6 years ago) parent7a526b6
3 files changed+108−9
src/Rel8.hs+16−4 src/Rel8/Aggregate/Legacy.hs+16−2 src/Rel8/Table/HKD.hs+76−3
src/Rel8.hs+16−4
@@ -82,9 +82,6 @@ module Rel8
8282 , bindTheseTable
8383 , bitraverseTheseTable
8484
85− -- ** @ADT@
86− , ADT
87−
8885 -- ** @ListTable@
8986 , ListTable
9087 , many
@@ -93,6 +90,22 @@ module Rel8
9390 , NonEmptyTable
9491 , some
9592
93+ -- ** @ADT@
94+ , ADT
95+ , ConstructADT, constructADT
96+ , DeconstructADT, deconstructADT
97+ , InsertADT, insertADT
98+ , NameADT, nameADT
99+ , AggregateADT, aggregateADT
100+
101+ -- ** @HKD@
102+ , HKD
103+ , ConstructHKD, constructHKD
104+ , DeconstructHKD, deconstructHKD
105+ , InsertHKD, insertHKD
106+ , NameHKD, nameHKD
107+ , AggregateHKD, aggregateHKD
108+
96109 -- ** Table schemas
97110 , TableSchema(..)
98111 , Name
@@ -246,7 +259,6 @@ module Rel8
246259 , ToExprs(..)
247260 , FromExprs
248261 , Result
249− , HKD
250262 , HKDT(..)
251263 ) where
252264
src/Rel8/Aggregate/Legacy.hs+16−2
@@ -16,14 +16,15 @@ module Rel8.Aggregate.Legacy
1616 , listAgg
1717 , nonEmptyAgg
1818
19− , AggregateADT
20− , aggregateADT
19+ , AggregateADT, aggregateADT
20+ , AggregateHKD, aggregateHKD
2121 )
2222where
2323
2424-- base
2525import Data.Functor.Identity ( Identity( Identity ) )
2626import Data.Kind ( Type )
27+import GHC.Generics ( Rep )
2728import Prelude
2829
2930-- opaleye
@@ -34,6 +35,7 @@ import Rel8.Aggregate ( Aggregate, Aggregates, Col(..) )
3435import Rel8.Expr ( Col(..), Expr )
3536import Rel8.Expr.Aggregate ( groupByExpr, listAggExpr, nonEmptyAggExpr )
3637import Rel8.Generic.Construction ( GGAggregate', ggaggregate' )
38+import Rel8.Generic.Table ( GAlgebra )
3739import Rel8.Kind.Algebra ( Algebra( Sum ) )
3840import Rel8.Query ( Query )
3941import Rel8.Query.Opaleye ( mapOpaleye )
@@ -44,6 +46,7 @@ import Rel8.Schema.Kind ( Rel8able )
4446import Rel8.Table ( toColumns, fromColumns )
4547import Rel8.Table.ADT ( ConstructableADT, ADT( ADT ), ADTRep )
4648import Rel8.Table.Eq ( EqTable, eqTable )
49+import Rel8.Table.HKD ( ConstructableHKD, HKD( HKD ), HKDRep )
4750import Rel8.Table.List ( ListTable )
4851import Rel8.Table.NonEmpty ( NonEmptyTable )
4952import Rel8.Table.Opaleye ( aggregator )
@@ -113,3 +116,14 @@ aggregateADT :: forall t. ConstructableADT t
113116aggregateADT f =
114117 ggaggregate' @'Sum @(ADTRep t) @(ADT t Expr) @(ADT t Aggregate) ADT (\(ADT a) -> a)
115118 (f @(ADT t Aggregate))
119+
120+
121+type AggregateHKD :: Type -> Type
122+type AggregateHKD a = forall r. GGAggregate' (GAlgebra (Rep a)) (HKDRep a) r
123+
124+
125+aggregateHKD :: forall a. ConstructableHKD a
126+ => AggregateHKD a -> HKD a Expr -> HKD a Aggregate
127+aggregateHKD f =
128+ ggaggregate' @(GAlgebra (Rep a)) @(HKDRep a) @(HKD a Expr) @(HKD a Aggregate) HKD (\(HKD a) -> a)
129+ (f @(HKD a Aggregate))
src/Rel8/Table/HKD.hs+76−3
@@ -1,7 +1,9 @@
1+{-# language AllowAmbiguousTypes #-}
12{-# language DataKinds #-}
23{-# language FlexibleContexts #-}
34{-# language FlexibleInstances #-}
45{-# language MultiParamTypeClasses #-}
6+{-# language RankNTypes #-}
57{-# language ScopedTypeVariables #-}
68{-# language StandaloneKindSignatures #-}
79{-# language TypeApplications #-}
@@ -10,9 +12,15 @@
1012{-# language UndecidableSuperClasses #-}
1113
1214module Rel8.Table.HKD
13− ( HKD( HKD ), HKDT(..)
14− , fromHKD, toHKD
15− , GHKD
15+ ( HKD( HKD )
16+ , HKDT(..), fromHKD, toHKD, GHKD
17+ , ConstructableHKD
18+ , ConstructHKD, constructHKD
19+ , DeconstructHKD, deconstructHKD
20+ , InsertHKD, insertHKD
21+ , NameHKD, nameHKD
22+ , AggregateHKD, aggregateHKD
23+ , HKDRep
1624 )
1725where
1826
@@ -24,11 +32,20 @@ import GHC.Generics ( Generic, Rep, from, to )
2432import Prelude
2533
2634-- rel8
35+import Rel8.Aggregate ( Aggregate )
2736import Rel8.Column ( Column )
2837import Rel8.Expr ( Expr )
2938import Rel8.FCF ( Eval, Exp )
3039import Rel8.Kind.Algebra ( KnownAlgebra )
3140import qualified Rel8.Kind.Algebra as K
41+import Rel8.Generic.Construction
42+ ( GGConstructable
43+ , GGConstruct, ggconstruct
44+ , GGDeconstruct, ggdeconstruct
45+ , GGInsert, gginsert
46+ , GGName, ggname
47+ , GGAggregate, ggaggregate
48+ )
3249import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
3350import Rel8.Generic.Record ( GRecord, GRecordable, grecord, gunrecord )
3451import Rel8.Generic.Rel8able
@@ -46,6 +63,8 @@ import qualified Rel8.Generic.Table.ADT as G
4663import Rel8.Schema.Context ( Col )
4764import qualified Rel8.Schema.Kind as K
4865import Rel8.Schema.HTable ( HTable )
66+import Rel8.Schema.Insert ( Insert )
67+import Rel8.Schema.Name ( Name )
4968import Rel8.Schema.Reify ( Col( Reify ), Reify, hreify, hunreify )
5069import Rel8.Schema.Result ( Result )
5170import Rel8.Table
@@ -221,3 +240,57 @@ instance
221240 , GMap TUnreify (GMap (TColumn (Reify Result)) (Rep a)) ~ GMap (TColumn Result) (Rep a)
222241 )
223242 => GHKD a
243+
244+
245+type ConstructableHKD :: Type -> Constraint
246+class GGConstructable (GAlgebra (Rep a)) (HKDRep a) => ConstructableHKD a
247+instance GGConstructable (GAlgebra (Rep a)) (HKDRep a) => ConstructableHKD a
248+
249+
250+type ConstructHKD :: Type -> Type
251+type ConstructHKD a = GGConstruct (GAlgebra (Rep a)) (HKDRep a) (HKD a Expr)
252+
253+
254+constructHKD :: forall a. ConstructableHKD a => ConstructHKD a
255+constructHKD = ggconstruct @(GAlgebra (Rep a)) @(HKDRep a) @(HKD a Expr) HKD
256+
257+
258+type DeconstructHKD :: Type -> Type -> Type
259+type DeconstructHKD a r = GGDeconstruct (GAlgebra (Rep a)) (HKDRep a) (HKD a Expr) r
260+
261+
262+deconstructHKD :: forall a r. (ConstructableHKD a, Table Expr r)
263+ => DeconstructHKD a r
264+deconstructHKD = ggdeconstruct @(GAlgebra (Rep a)) @(HKDRep a) @(HKD a Expr) @r (\(HKD a) -> a)
265+
266+
267+type InsertHKD :: Type -> Type
268+type InsertHKD a = GGInsert (GAlgebra (Rep a)) (HKDRep a) (HKD a Insert)
269+
270+
271+insertHKD :: forall a. ConstructableHKD a => InsertHKD a
272+insertHKD = gginsert @(GAlgebra (Rep a)) @(HKDRep a) @(HKD a Insert) HKD
273+
274+
275+type NameHKD :: Type -> Type
276+type NameHKD a = GGName (GAlgebra (Rep a)) (HKDRep a) (HKD a Name)
277+
278+
279+nameHKD :: forall a. ConstructableHKD a => NameHKD a
280+nameHKD = ggname @(GAlgebra (Rep a)) @(HKDRep a) @(HKD a Name) HKD
281+
282+
283+type AggregateHKD :: Type -> Type
284+type AggregateHKD a = forall r. GGAggregate (GAlgebra (Rep a)) (HKDRep a) r
285+
286+
287+aggregateHKD :: forall a. ConstructableHKD a
288+ => AggregateHKD a -> HKD a Expr -> Aggregate (HKD a Expr)
289+aggregateHKD f =
290+ ggaggregate @(GAlgebra (Rep a)) @(HKDRep a) @(HKD a Expr) HKD (\(HKD a) -> a)
291+ (f @(Aggregate (HKD a Expr)))
292+
293+
294+data HKDRep :: Type -> K.Context -> Exp (Type -> Type)
295+type instance Eval (HKDRep a context) =
296+ GRecord (GMap (TColumn context) (Rep a))