-
Notifications
You must be signed in to change notification settings - Fork 39
/
Table.hs
223 lines (178 loc) · 6.09 KB
/
Table.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{-# language AllowAmbiguousTypes #-}
{-# language DataKinds #-}
{-# language DefaultSignatures #-}
{-# language FlexibleContexts #-}
{-# language FlexibleInstances #-}
{-# language FunctionalDependencies #-}
{-# language ScopedTypeVariables #-}
{-# language StandaloneKindSignatures #-}
{-# language TypeApplications #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}
{-# language UndecidableInstances #-}
module Rel8.Table
( Table
( Columns, Context, fromColumns, toColumns
, FromExprs, fromResult, toResult
, Transpose
)
, Congruent
, TTable, TColumns, TContext, TFromExprs, TTranspose
, TSerialize
)
where
-- base
import Data.Functor.Identity ( Identity( Identity ) )
import Data.Kind ( Constraint, Type )
import GHC.Generics ( Generic, Rep, from, to )
import Prelude hiding ( null )
-- rel8
import Rel8.FCF ( Eval, Exp )
import Rel8.Generic.Map ( Map )
import Rel8.Generic.Table.Record
( GTable, GColumns, GContext, gfromColumns, gtoColumns
, GSerialize, gfromResult, gtoResult
)
import Rel8.Generic.Record ( Record(..) )
import Rel8.Schema.HTable ( HTable )
import Rel8.Schema.HTable.Identity ( HIdentity( HIdentity ) )
import qualified Rel8.Schema.Kind as K
import Rel8.Schema.Null ( Sql )
import Rel8.Schema.Result ( Result )
import Rel8.Type ( DBType )
-- | @Table@s are one of the foundational elements of Rel8, and describe data
-- types that have a finite number of columns. Each of these columns contains
-- data under a shared context, and contexts describe how to interpret the
-- metadata about a column to a particular Haskell type. In Rel8, we have
-- contexts for expressions (the 'Rel8.Expr' context), aggregations (the
-- 'Rel8.Aggregate' context), insert values (the 'Rel8.Insert' contex), among
-- others.
--
-- In typical usage of Rel8 you don't need to derive instances of 'Table'
-- yourself, as anything that's an instance of 'Rel8.Rel8able' is always a
-- 'Table'.
type Table :: K.Context -> Type -> Constraint
class
( HTable (Columns a)
, context ~ Context a
, a ~ Transpose context a
)
=> Table context a | a -> context
where
-- | The 'HTable' functor that describes the schema of this table.
type Columns a :: K.HTable
-- | The common context that all columns use as an interpretation.
type Context a :: K.Context
-- | The @FromExprs@ type family maps a type in the @Expr@ context to the
-- corresponding Haskell type.
type FromExprs a :: Type
type Transpose (context' :: K.Context) a :: Type
toColumns :: a -> Columns a context
fromColumns :: Columns a context -> a
fromResult :: Columns a Result -> FromExprs a
toResult :: FromExprs a -> Columns a Result
type Columns a = GColumns TColumns (Rep (Record a))
type Context a = GContext TContext (Rep (Record a))
type FromExprs a = Map TFromExprs a
type Transpose context a = Map (TTranspose context) a
default toColumns ::
( Generic (Record a)
, GTable (TTable context) TColumns (Rep (Record a))
, Columns a ~ GColumns TColumns (Rep (Record a))
)
=> a -> Columns a context
toColumns =
gtoColumns @(TTable context) @TColumns toColumns .
from .
Record
default fromColumns ::
( Generic (Record a)
, GTable (TTable context) TColumns (Rep (Record a))
, Columns a ~ GColumns TColumns (Rep (Record a))
)
=> Columns a context -> a
fromColumns =
unrecord .
to .
gfromColumns @(TTable context) @TColumns fromColumns
default toResult ::
( Generic (Record (FromExprs a))
, GSerialize TSerialize TColumns (Rep (Record a)) (Rep (Record (FromExprs a)))
, Columns a ~ GColumns TColumns (Rep (Record a))
)
=> FromExprs a -> Columns a Result
toResult =
gtoResult
@TSerialize
@TColumns
@(Rep (Record a))
@(Rep (Record (FromExprs a)))
(\(_ :: proxy x) -> toResult @(Context x) @x) .
from .
Record
default fromResult ::
( Generic (Record (FromExprs a))
, GSerialize TSerialize TColumns (Rep (Record a)) (Rep (Record (FromExprs a)))
, Columns a ~ GColumns TColumns (Rep (Record a))
)
=> Columns a Result -> FromExprs a
fromResult =
unrecord .
to .
gfromResult
@TSerialize
@TColumns
@(Rep (Record a))
@(Rep (Record (FromExprs a)))
(\(_ :: proxy x) -> fromResult @(Context x) @x)
instance Sql DBType a => Table Result (Identity a) where
type Columns (Identity a) = HIdentity a
type Context (Identity a) = Result
type FromExprs (Identity a) = a
type Transpose to (Identity a) = to a
toColumns = HIdentity
fromColumns (HIdentity a) = a
toResult a = HIdentity (Identity a)
fromResult (HIdentity (Identity a)) = a
data TTable :: K.Context -> Type -> Exp Constraint
type instance Eval (TTable context a) = Table context a
data TColumns :: Type -> Exp K.HTable
type instance Eval (TColumns a) = Columns a
data TContext :: Type -> Exp K.Context
type instance Eval (TContext a) = Context a
data TFromExprs :: Type -> Exp Type
type instance Eval (TFromExprs a) = FromExprs a
data TTranspose :: K.Context -> Type -> Exp Type
type instance Eval (TTranspose context a) = Transpose context a
data TSerialize :: Type -> Type -> Exp Constraint
type instance Eval (TSerialize expr a) =
( Table (Context expr) expr
, a ~ FromExprs expr
)
instance (Table context a, Table context b) => Table context (a, b)
instance
( Table context a, Table context b, Table context c
)
=> Table context (a, b, c)
instance
( Table context a, Table context b, Table context c, Table context d
)
=> Table context (a, b, c, d)
instance
( Table context a, Table context b, Table context c, Table context d
, Table context e
)
=> Table context (a, b, c, d, e)
instance
( Table context a, Table context b, Table context c, Table context d
, Table context e, Table context f
)
=> Table context (a, b, c, d, e, f)
instance
( Table context a, Table context b, Table context c, Table context d
, Table context e, Table context f, Table context g
)
=> Table context (a, b, c, d, e, f, g)
type Congruent :: Type -> Type -> Constraint
class Columns a ~ Columns b => Congruent a b
instance Columns a ~ Columns b => Congruent a b