on 4bc16d3 help sign in
rel8/src/Rel8/Table/Rel8able.hs
167 lines 4.1 kB history raw
1{-# language DataKinds #-}
2{-# language FlexibleInstances #-}
3{-# language FunctionalDependencies #-}
4{-# language ScopedTypeVariables #-}
5{-# language StandaloneKindSignatures #-}
6{-# language TypeApplications #-}
7{-# language TypeFamilies #-}
8{-# language UndecidableInstances #-}
10{-# options_ghc -fno-warn-orphans #-}
12module Rel8.Table.Rel8able
13 (
14 )
15where
17-- base
18import Data.Kind ( Constraint )
19import Data.Type.Equality ( (:~:)( Refl ) )
20import Prelude
22-- rel8
23import Rel8.Expr ( Expr )
24import qualified Rel8.Kind.Algebra as K
25import Rel8.Kind.Context
26 ( SContext( SReify )
27 , Reifiable, contextSing
28 , sLabelable, sReifiable
29 )
30import Rel8.Generic.Rel8able
31 ( Rel8able, Algebra
32 , GColumns, GContext, gfromColumns, gtoColumns
33 , gfromColumnsADT, gtoColumnsADT
34 , greify, gunreify
35 )
36import Rel8.Schema.Context ( Col )
37import Rel8.Schema.Context.Label ( Labelable )
38import Rel8.Schema.Dict ( Dict( Dict ) )
39import qualified Rel8.Schema.Kind as K
40import Rel8.Schema.HTable ( HConstrainTable, hdicts )
41import Rel8.Schema.Reify ( hreify, hunreify, UnwrapReify )
42import Rel8.Schema.Result ( Result )
43import Rel8.Table
44 ( Table, Columns, Context, Congruent, fromColumns, toColumns
45 , Unreify, reify, unreify
46 )
47import Rel8.Schema.Spec.ConstrainDBType ( ConstrainDBType )
48import Rel8.Table.ADT ( ADT )
49import Rel8.Table.Eq ( EqTable, eqTable )
50import Rel8.Table.Ord ( OrdTable, ordTable )
51import Rel8.Table.Recontextualize ( Recontextualize )
52import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
53import Rel8.Type.Eq ( DBEq )
54import Rel8.Type.Ord ( DBOrd )
57instance
58 ( Rel8able t
59 , GContext t context ~ context
60 , Labelable context
61 , Reifiable context
62 )
63 => Table context (t context)
64 where
65 type Columns (t context) = GColumns t
66 type Context (t context) = context
67 type Unreify (t context) = t (UnwrapReify context)
69 fromColumns = gunreify . gfromColumns . hreify
70 toColumns = hunreify . gtoColumns . greify
72 reify Refl = case contextSing @context of
73 SReify context -> case sLabelable context of
74 Dict -> case sReifiable context of
75 Dict -> greify
77 unreify Refl = case contextSing @context of
78 SReify context -> case sLabelable context of
79 Dict -> case sReifiable context of
80 Dict -> gunreify
83instance
84 ( Rel8able t
85 , Labelable from, Reifiable from, GContext t from ~ from
86 , Labelable to, Reifiable to, GContext t to ~ to
87 , Congruent (t from) (t to)
88 )
89 => Recontextualize from to (t from) (t to)
92instance
93 ( context ~ Expr
94 , Rel8able t
95 , GContext t context ~ context
96 , HConstrainTable (Columns (t context)) (ConstrainDBType DBEq)
97 )
98 => EqTable (t context)
99 where
100 eqTable = hdicts @(Columns (t context)) @(ConstrainDBType DBEq)
103instance
104 ( context ~ Expr
105 , Rel8able t
106 , GContext t context ~ context
107 , HConstrainTable (Columns (t context)) (ConstrainDBType DBEq)
108 , HConstrainTable (Columns (t context)) (ConstrainDBType DBOrd)
109 )
110 => OrdTable (t context)
111 where
112 ordTable = hdicts @(Columns (t context)) @(ConstrainDBType DBOrd)
115type instance FromExprs (t Expr) = FromExprs' t Result
118instance
119 ( x ~ t' Expr
120 , result ~ Result
121 , ToExprs' (Algebra t) t' t
122 )
123 => ToExprs x (t result)
124 where
125 fromResult = fromResult' @(Algebra t) @t'
126 toResult = toResult' @(Algebra t) @t'
129type FromExprs' :: K.Rel8able -> K.Rel8able
130type family FromExprs' t where
131 FromExprs' (ADT t) = t
132 FromExprs' t = t
135type ToExprs' :: K.Algebra -> K.Rel8able -> K.Rel8able -> Constraint
136class
137 ( algebra ~ Algebra t
138 , Rel8able t'
139 , GContext t' Expr ~ Expr
140 )
141 => ToExprs' algebra t' t | algebra t -> t'
142 where
143 fromResult' :: GColumns t' (Col Result) -> t Result
144 toResult' :: t Result -> GColumns t' (Col Result)
147instance
148 ( Algebra t ~ 'K.Product
149 , Rel8able t
150 , GContext t Expr ~ Expr
151 , t ~ t'
152 )
153 => ToExprs' 'K.Product t' t
154 where
155 fromResult' = fromColumns
156 toResult' = toColumns
159instance
160 ( Algebra t ~ 'K.Sum
161 , Rel8able t
162 , t' ~ ADT t
163 )
164 => ToExprs' 'K.Sum t' t
165 where
166 fromResult' = gunreify . gfromColumnsADT . hreify
167 toResult' = hunreify . gtoColumnsADT . greify