on db148ff help sign in
rel8/src/Rel8/Table.hs
387 lines 12 kB history raw
1{-# language AllowAmbiguousTypes #-}
2{-# language DataKinds #-}
3{-# language DefaultSignatures #-}
4{-# language DisambiguateRecordFields #-}
5{-# language FlexibleContexts #-}
6{-# language FlexibleInstances #-}
7{-# language FunctionalDependencies #-}
8{-# language LambdaCase #-}
9{-# language NamedFieldPuns #-}
10{-# language ScopedTypeVariables #-}
11{-# language StandaloneKindSignatures #-}
12{-# language TypeApplications #-}
13{-# language TypeFamilies #-}
14{-# language TypeOperators #-}
15{-# language UndecidableInstances #-}
17module Rel8.Table
18 ( Table (Columns, Context, Unreify, toColumns, fromColumns, reify, unreify)
19 , Congruent
20 , TTable, TColumns, TContext, TUnreify
21 )
22where
24-- base
25import Data.Functor ( ($>) )
26import Data.Functor.Identity ( Identity( Identity ) )
27import Data.Kind ( Constraint, Type )
28import Data.List.NonEmpty ( NonEmpty )
29import Data.Proxy ( Proxy( Proxy ) )
30import Data.Type.Equality ( (:~:)( Refl ) )
31import GHC.Generics ( Generic, Rep, from, to )
32import Prelude hiding ( null )
34-- rel8
35import Rel8.FCF ( Eval, Exp )
36import Rel8.Kind.Algebra ( KnownAlgebra )
37import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
38import Rel8.Generic.Table
39 ( GGTable, GGColumns, GGContext, ggfromColumns, ggtoColumns
40 , GAlgebra
41 )
42import Rel8.Generic.Record ( Record(..) )
43import Rel8.Generic.Reify ( ARep )
44import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
45import Rel8.Schema.HTable ( HTable )
46import Rel8.Schema.HTable.Either ( HEitherTable(..) )
47import Rel8.Schema.HTable.Identity ( HIdentity(..) )
48import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
49import Rel8.Schema.HTable.List ( HListTable )
50import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
51import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
52import Rel8.Schema.HTable.Nullify ( hnulls, hnullify, hunnullify )
53import Rel8.Schema.HTable.These ( HTheseTable(..) )
54import Rel8.Schema.HTable.Type ( HType( HType ) )
55import Rel8.Schema.HTable.Vectorize ( hvectorize, hunvectorize )
56import qualified Rel8.Schema.Kind as K
57import Rel8.Schema.Null ( Sql )
58import Rel8.Schema.Reify
59 ( Reify, Col( Reify ), hreify, hunreify
60 , UnwrapReify
61 , notReify
62 )
63import Rel8.Schema.Result
64 ( Col( Result ), Result
65 , relabel
66 , null, nullifier, unnullifier
67 , vectorizer, unvectorizer
68 )
69import Rel8.Schema.Spec ( KnownSpec )
70import Rel8.Type ( DBType )
71import Rel8.Type.Tag ( EitherTag( IsLeft, IsRight ), MaybeTag( IsJust ) )
73-- these
74import Data.These ( These( This, That, These ) )
75import Data.These.Combinators ( justHere, justThere )
78-- | @Table@s are one of the foundational elements of Rel8, and describe data
79-- types that have a finite number of columns. Each of these columns contains
80-- data under a shared context, and contexts describe how to interpret the
81-- metadata about a column to a particular Haskell type. In Rel8, we have
82-- contexts for expressions (the 'Rel8.Expr' context), aggregations (the
83-- 'Rel8.Aggregate' context), insert values (the 'Rel8.Insert' contex), among
84-- others.
85--
86-- In typical usage of Rel8 you don't need to derive instances of 'Table'
87-- yourself, as anything that's an instance of 'Rel8.Rel8able' is always a
88-- 'Table'.
89type Table :: K.Context -> Type -> Constraint
90class (HTable (Columns a), context ~ Context a) => Table context a | a -> context where
91 -- | The 'HTable' functor that describes the schema of this table.
92 type Columns a :: K.HTable
94 -- | The common context that all columns use as an interpretation.
95 type Context a :: K.Context
97 type Unreify a :: Type
99 toColumns :: a -> Columns a (Col context)
100 fromColumns :: Columns a (Col context) -> a
102 reify :: context :~: Reify ctx -> Unreify a -> a
103 unreify :: context :~: Reify ctx -> a -> Unreify a
105 type Columns a = Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
106 type Context a = Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
107 type Unreify a = DefaultUnreify a
109 default toColumns ::
110 ( Generic (Record a)
111 , KnownAlgebra (GAlgebra (Rep (Record a)))
112 , Eval (GGTable (GAlgebra (Rep (Record a))) (TTable context) TColumns (Col context) (Rep (Record a)))
113 , Columns a ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
114 , Context a ~ Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
115 )
116 => a -> Columns a (Col context)
117 toColumns =
118 ggtoColumns
119 @(GAlgebra (Rep (Record a)))
120 @(TTable context)
121 @TColumns
122 id
123 id
124 toColumns .
125 from .
126 Record
128 default fromColumns ::
129 ( Generic (Record a)
130 , KnownAlgebra (GAlgebra (Rep (Record a)))
131 , Eval (GGTable (GAlgebra (Rep (Record a))) (TTable context) TColumns (Col context) (Rep (Record a)))
132 , Columns a ~ Eval (GGColumns (GAlgebra (Rep (Record a))) TColumns (Rep (Record a)))
133 , Context a ~ Eval (GGContext (GAlgebra (Rep (Record a))) TContext (Rep (Record a)))
134 )
135 => Columns a (Col context) -> a
136 fromColumns =
137 unrecord .
138 to .
139 ggfromColumns
140 @(GAlgebra (Rep (Record a)))
141 @(TTable context)
142 @TColumns
143 id
144 id
145 fromColumns
147 default reify ::
148 ( Generic (Record a)
149 , Generic (Record (Unreify a))
150 , GMappable (TTable context) (Rep (Record a))
151 , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
152 )
153 => context :~: Reify ctx -> Unreify a -> a
154 reify Refl =
155 unrecord .
156 to .
157 gunmap @(TTable context) (Proxy @TUnreify) (reify Refl) .
158 from .
159 Record
161 default unreify ::
162 ( Generic (Record a)
163 , Generic (Record (Unreify a))
164 , GMappable (TTable context) (Rep (Record a))
165 , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
166 )
167 => context :~: Reify ctx -> a -> Unreify a
168 unreify Refl =
169 unrecord .
170 to .
171 gmap @(TTable context) (Proxy @TUnreify) (unreify Refl) .
172 from .
173 Record
176data TTable :: K.Context -> Type -> Exp Constraint
177type instance Eval (TTable context a) = Table context a
180data TColumns :: Type -> Exp K.HTable
181type instance Eval (TColumns a) = Columns a
184data TContext :: Type -> Exp K.Context
185type instance Eval (TContext a) = Context a
188data TUnreify :: Type -> Exp Type
189type instance Eval (TUnreify a) = Unreify a
192type DefaultUnreify :: Type -> Type
193type family DefaultUnreify a where
194 DefaultUnreify (t a b c d e f g) =
195 t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e) (Unreify f) (Unreify g)
196 DefaultUnreify (t a b c d e f) =
197 t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e) (Unreify f)
198 DefaultUnreify (t a b c d e) =
199 t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e)
200 DefaultUnreify (t a b c d) =
201 t (Unreify a) (Unreify b) (Unreify c) (Unreify d)
202 DefaultUnreify (t a b c) = t (Unreify a) (Unreify b) (Unreify c)
203 DefaultUnreify (t a b) = t (Unreify a) (Unreify b)
204 DefaultUnreify (t a) = t (Unreify a)
205 DefaultUnreify a = ARep (GMap TUnreify (Rep a))
208-- | Any 'HTable' is also a 'Table'.
209instance HTable t => Table context (t (Col context)) where
210 type Columns (t (Col context)) = t
211 type Context (t (Col context)) = context
212 type Unreify (t (Col context)) = t (Col (UnwrapReify context))
214 toColumns = id
215 fromColumns = id
217 reify Refl = hreify
218 unreify Refl = hunreify
221-- | Any context is trivially a table.
222instance KnownSpec spec => Table context (Col context spec) where
223 type Columns (Col context spec) = HIdentity spec
224 type Context (Col context spec) = context
225 type Unreify (Col context spec) = Col (UnwrapReify context) spec
227 toColumns = HIdentity
228 fromColumns = unHIdentity
230 reify Refl = Reify
231 unreify Refl (Reify a) = a
234instance Sql DBType a => Table Result (Identity a) where
235 type Columns (Identity a) = HType a
236 type Context (Identity a) = Result
238 toColumns (Identity a) = HType (Result a)
239 fromColumns (HType (Result a)) = Identity a
241 reify = notReify
242 unreify = notReify
245instance (Table Result a, Table Result b) => Table Result (Either a b) where
246 type Columns (Either a b) = HEitherTable (Columns a) (Columns b)
247 type Context (Either a b) = Result
249 toColumns = \case
250 Left table -> HEitherTable
251 { htag = HIdentity (Result IsLeft)
252 , hleft = hlabel labeler (hnullify nullifier (toColumns table))
253 , hright = hlabel labeler (hnulls (const null))
254 }
255 Right table -> HEitherTable
256 { htag = HIdentity (Result IsRight)
257 , hleft = hlabel labeler (hnulls (const null))
258 , hright = hlabel labeler (hnullify nullifier (toColumns table))
259 }
261 fromColumns HEitherTable {htag, hleft, hright} = case htag of
262 HIdentity (Result tag) -> case tag of
263 IsLeft -> maybe err (Left . fromColumns) $ hunnullify unnullifier (hunlabel unlabeler hleft)
264 IsRight -> maybe err (Right . fromColumns) $ hunnullify unnullifier (hunlabel unlabeler hright)
265 where
266 err = error "Either.fromColumns: mismatch between tag and data"
269instance Table Result a => Table Result [a] where
270 type Columns [a] = HListTable (Columns a)
271 type Context [a] = Result
273 toColumns = hvectorize vectorizer . fmap toColumns
274 fromColumns = fmap fromColumns . hunvectorize unvectorizer
277instance Table Result a => Table Result (Maybe a) where
278 type Columns (Maybe a) = HMaybeTable (Columns a)
279 type Context (Maybe a) = Result
281 toColumns = \case
282 Nothing -> HMaybeTable
283 { htag = HIdentity (Result Nothing)
284 , hjust = hlabel labeler (hnulls (const null))
285 }
286 Just table -> HMaybeTable
287 { htag = HIdentity (Result (Just IsJust))
288 , hjust = hlabel labeler (hnullify nullifier (toColumns table))
289 }
291 fromColumns HMaybeTable {htag, hjust} = case htag of
292 HIdentity (Result tag) -> tag $>
293 case hunnullify unnullifier (hunlabel unlabeler hjust) of
294 Nothing -> error "Maybe.fromColumns: mismatch between tag and data"
295 Just just -> fromColumns just
298instance Table Result a => Table Result (NonEmpty a) where
299 type Columns (NonEmpty a) = HNonEmptyTable (Columns a)
300 type Context (NonEmpty a) = Result
302 toColumns = hvectorize vectorizer . fmap toColumns
303 fromColumns = fmap fromColumns . hunvectorize unvectorizer
306instance (Table Result a, Table Result b) => Table Result (These a b) where
307 type Columns (These a b) = HTheseTable (Columns a) (Columns b)
308 type Context (These a b) = Result
310 toColumns tables = HTheseTable
311 { hhereTag = relabel hhereTag
312 , hhere = hlabel labeler (hunlabel unlabeler (toColumns hhere))
313 , hthereTag = relabel hthereTag
314 , hthere = hlabel labeler (hunlabel unlabeler (toColumns hthere))
315 }
316 where
317 HMaybeTable
318 { htag = hhereTag
319 , hjust = hhere
320 } = toColumns (justHere tables)
321 HMaybeTable
322 { htag = hthereTag
323 , hjust = hthere
324 } = toColumns (justThere tables)
326 fromColumns HTheseTable {hhereTag, hhere, hthereTag, hthere} =
327 case (fromColumns mhere, fromColumns mthere) of
328 (Just a, Nothing) -> This (fromColumns a)
329 (Nothing, Just b) -> That (fromColumns b)
330 (Just a, Just b) -> These (fromColumns a) (fromColumns b)
331 _ -> error "These.fromColumns: mismatch between tags and data"
332 where
333 mhere = HMaybeTable
334 { htag = relabel hhereTag
335 , hjust = hlabel labeler (hunlabel unlabeler hhere)
336 }
337 mthere = HMaybeTable
338 { htag = relabel hthereTag
339 , hjust = hlabel labeler (hunlabel unlabeler hthere)
340 }
343instance (Table context a, Table context b, Labelable context)
344 => Table context (a, b)
347instance
348 ( Table context a, Table context b, Table context c
349 , Labelable context
350 )
351 => Table context (a, b, c)
354instance
355 ( Table context a, Table context b, Table context c, Table context d
356 , Labelable context
357 )
358 => Table context (a, b, c, d)
361instance
362 ( Table context a, Table context b, Table context c, Table context d
363 , Table context e
364 , Labelable context
365 )
366 => Table context (a, b, c, d, e)
369instance
370 ( Table context a, Table context b, Table context c, Table context d
371 , Table context e, Table context f
372 , Labelable context
373 )
374 => Table context (a, b, c, d, e, f)
377instance
378 ( Table context a, Table context b, Table context c, Table context d
379 , Table context e, Table context f, Table context g
380 , Labelable context
381 )
382 => Table context (a, b, c, d, e, f, g)
385type Congruent :: Type -> Type -> Constraint
386class Columns a ~ Columns b => Congruent a b
387instance Columns a ~ Columns b => Congruent a b