1{-# language AllowAmbiguousTypes #-}
2{-# language ConstraintKinds #-}
3{-# language DataKinds #-}
4{-# language DefaultSignatures #-}
5{-# language FlexibleContexts #-}
6{-# language FlexibleInstances #-}
7{-# language LambdaCase #-}
8{-# language MultiParamTypeClasses #-}
9{-# language QuantifiedConstraints #-}
10{-# language ScopedTypeVariables #-}
11{-# language StandaloneKindSignatures #-}
12{-# language TypeApplications #-}
13{-# language TypeFamilyDependencies #-}
14{-# language TypeOperators #-}
15{-# language UndecidableInstances #-}
17module Rel8.Generic.Rel8able
21 , GColumns, gfromColumns, gtoColumns
22 , GFromExprs, gfromResult, gtoResult
23 , TSerialize, serialize, deserialize
28import Data.Kind ( Constraint, Type )
29import Data.Type.Equality ( type (==) )
30import GHC.Generics ( Generic, Rep, from, to )
34import Rel8.Aggregate ( Aggregate )
35import Rel8.Expr ( Expr )
36import Rel8.FCF ( Exp, Eval )
37import Rel8.Generic.Record ( Record(..) )
38import Rel8.Generic.Table ( GAlgebra )
39import qualified Rel8.Generic.Table.Record as G
40import qualified Rel8.Kind.Algebra as K ( Algebra(..) )
41import Rel8.Kind.Context ( SContext(..) )
42import Rel8.Schema.Field ( Field )
43import Rel8.Schema.HTable ( HTable )
44import qualified Rel8.Schema.Kind as K
45import Rel8.Schema.Name ( Name )
46import Rel8.Schema.Result ( Result )
48 ( Table, Columns, Context, fromColumns, toColumns
49 , FromExprs, fromResult, toResult
53import Rel8.Table.Transpose ( Transposes )
56-- | The kind of 'Rel8able' types
58type KRel8able = K.Rel8able
61type Serialize :: Bool -> Type -> Type -> Constraint
62class transposition ~ (a == Transpose Result expr) =>
63 Serialize transposition expr a
65 serialize :: a -> Columns expr Result
66 deserialize :: Columns expr Result -> a
70 ( (a == Transpose Result expr) ~ 'True
71 , Transposes Expr Result expr a
73 => Serialize 'True expr a
76 deserialize = fromColumns
80 ( (a == Transpose Result expr) ~ 'False
81 , Table (Context expr) expr
84 => Serialize 'False expr a
86 serialize = toResult @_ @expr
87 deserialize = fromResult @_ @expr
90data TSerialize :: Type -> Type -> Exp Constraint
91type instance Eval (TSerialize expr a) =
92 Serialize (a == Transpose Result expr) expr a
95-- | This type class allows you to define custom 'Table's using higher-kinded
96-- data types. Higher-kinded data types are data types of the pattern:
100-- MyType { field1 :: Column f T1 OR HK1 f
101-- , field2 :: Column f T2 OR HK2 f
103-- , fieldN :: Column f Tn OR HKn f
107-- where @Tn@ is any Haskell type, and @HKn@ is any higher-kinded type.
109-- That is, higher-kinded data are records where all fields in the record are
110-- all either of the type @Column f T@ (for any @T@), or are themselves
111-- higher-kinded data:
117-- Nested { nested1 :: MyType f
118-- , nested2 :: MyType f
122-- The @Rel8able@ type class is used to give us a special mapping operation
123-- that lets us change the type parameter @f@.
125-- [Supplying @Rel8able@ instances]
127-- This type class should be derived generically for all table types in your
128-- project. To do this, enable the @DeriveAnyType@ and @DeriveGeneric@ language
132-- \{\-\# LANGUAGE DeriveAnyClass, DeriveGeneric #-\}
134-- data MyType f = MyType { fieldA :: Column f T }
135-- deriving ( GHC.Generics.Generic, Rel8able )
137type Rel8able :: K.Rel8able -> Constraint
138class HTable (GColumns t) => Rel8able t where
139 type GColumns t :: K.HTable
140 type GFromExprs t :: Type
142 gfromColumns :: SContext context -> GColumns t context -> t context
143 gtoColumns :: SContext context -> t context -> GColumns t context
145 gfromResult :: GColumns t Result -> GFromExprs t
146 gtoResult :: GFromExprs t -> GColumns t Result
148 type GColumns t = G.GColumns TColumns (GRep t Expr)
149 type GFromExprs t = t Result
151 default gfromColumns :: forall context.
152 ( SRel8able t Aggregate
154 , forall table. SRel8able t (Field table)
158 => SContext context -> GColumns t context -> t context
160 SAggregate -> sfromColumns
161 SExpr -> sfromColumns
162 SField -> sfromColumns
163 SName -> sfromColumns
164 SResult -> sfromResult
166 default gtoColumns :: forall context.
167 ( SRel8able t Aggregate
169 , forall table. SRel8able t (Field table)
173 => SContext context -> t context -> GColumns t context
175 SAggregate -> stoColumns
181 default gfromResult :: (SSerialize t, GFromExprs t ~ t Result)
182 => GColumns t Result -> GFromExprs t
183 gfromResult = sfromResult
185 default gtoResult :: (SSerialize t, GFromExprs t ~ t Result)
186 => GFromExprs t -> GColumns t Result
187 gtoResult = stoResult
190type Algebra :: K.Rel8able -> K.Algebra
191type Algebra t = GAlgebra (GRep t Expr)
194type GRep :: K.Rel8able -> K.Context -> Type -> Type
195type GRep t context = Rep (Record (t context))
198type SRel8able :: K.Rel8able -> K.Context -> Constraint
200 ( Generic (Record (t context))
201 , G.GTable (TTable context) TColumns (GRep t context)
202 , G.GColumns TColumns (GRep t context) ~ GColumns t
204 => SRel8able t context
206 ( Generic (Record (t context))
207 , G.GTable (TTable context) TColumns (GRep t context)
208 , G.GColumns TColumns (GRep t context) ~ GColumns t
210 => SRel8able t context
213type SSerialize :: K.Rel8able -> Constraint
215 ( Generic (Record (t Result))
216 , G.GSerialize TSerialize TColumns (GRep t Expr) (GRep t Result)
217 , G.GColumns TColumns (GRep t Expr) ~ GColumns t
221sfromColumns :: forall t context. SRel8able t context
222 => GColumns t context -> t context
226 G.gfromColumns @(TTable context) @TColumns fromColumns
229stoColumns :: forall t context. SRel8able t context
230 => t context -> GColumns t context
232 G.gtoColumns @(TTable context) @TColumns toColumns .
237sfromResult :: forall t. SSerialize t
238 => GColumns t Result -> t Result
247 (\(_ :: proxy x) -> deserialize @_ @x)
250stoResult :: forall t. SSerialize t
251 => t Result -> GColumns t Result
258 (\(_ :: proxy x) -> serialize @_ @x) .
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list