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 #-}
18 ( Table (Columns, Context, Unreify, toColumns, fromColumns, reify, unreify)
20 , TTable, TColumns, TContext, TUnreify
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 )
35import Rel8.FCF ( Eval, Exp )
36import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
37import Rel8.Generic.Table
38 ( GTable, GColumns, GContext, fromGColumns, toGColumns
40import Rel8.Generic.Record ( Record(..) )
41import Rel8.Schema.Context ( Col(..) )
42import Rel8.Schema.Context.Label ( Labelable, labeler, unlabeler )
43import Rel8.Schema.HTable ( HTable )
44import Rel8.Schema.HTable.Either ( HEitherTable(..) )
45import Rel8.Schema.HTable.Identity ( HIdentity(..) )
46import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
47import Rel8.Schema.HTable.List ( HListTable )
48import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
49import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
50import Rel8.Schema.HTable.Nullify ( hnulls, hnullify, hunnullify )
51import Rel8.Schema.HTable.These ( HTheseTable(..) )
52import Rel8.Schema.HTable.Type ( HType( HType ) )
53import Rel8.Schema.HTable.Vectorize ( hvectorize, hunvectorize )
54import qualified Rel8.Schema.Kind as K
55import Rel8.Schema.Null ( Nullify, Nullity( Null, NotNull ), Sql )
56import Rel8.Schema.Reify
57 ( Reify, Col( Reify ), hreify, hunreify
61import Rel8.Schema.Result ( Result )
62import Rel8.Schema.Spec ( Spec( Spec ), SSpec(..), KnownSpec )
63import Rel8.Type ( DBType )
64import Rel8.Type.Tag ( EitherTag( IsLeft, IsRight ), MaybeTag( IsJust ) )
67import Data.These ( These( This, That, These ) )
68import Data.These.Combinators ( justHere, justThere )
71-- | @Table@s are one of the foundational elements of Rel8, and describe data
72-- types that have a finite number of columns. Each of these columns contains
73-- data under a shared context, and contexts describe how to interpret the
74-- metadata about a column to a particular Haskell type. In Rel8, we have
75-- contexts for expressions (the 'Rel8.Expr' context), aggregations (the
76-- 'Rel8.Aggregate' context), insert values (the 'Rel8.Insert' contex), among
79-- In typical usage of Rel8 you don't need to derive instances of 'Table'
80-- yourself, as anything that's an instance of 'Rel8.Rel8able' is always a
82type Table :: K.Context -> Type -> Constraint
83class (HTable (Columns a), context ~ Context a) => Table context a | a -> context where
84 -- | The 'HTable' functor that describes the schema of this table.
85 type Columns a :: K.HTable
87 -- | The common context that all columns use as an interpretation.
88 type Context a :: K.Context
90 type Unreify a :: Type
92 toColumns :: a -> Columns a (Col context)
93 fromColumns :: Columns a (Col context) -> a
95 reify :: context :~: Reify ctx -> Unreify a -> a
96 unreify :: context :~: Reify ctx -> a -> Unreify a
98 type Columns a = GColumns TColumns (Rep (Record a))
99 type Context a = GContext TContext (Rep (Record a))
100 type Unreify a = DefaultUnreify a
104 , GTable (TTable context) TColumns (Col context) (Rep (Record a))
105 , Columns a ~ GColumns TColumns (Rep (Record a))
107 => a -> Columns a (Col context)
109 toGColumns @(TTable context) @TColumns toColumns .
113 default fromColumns ::
115 , GTable (TTable context) TColumns (Col context) (Rep (Record a))
116 , Columns a ~ GColumns TColumns (Rep (Record a))
118 => Columns a (Col context) -> a
122 fromGColumns @(TTable context) @TColumns fromColumns
126 , Generic (Record (Unreify a))
127 , GMappable (TTable context) (Rep (Record a))
128 , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
130 => context :~: Reify ctx -> Unreify a -> a
134 gunmap @(TTable context) (Proxy @TUnreify) (reify Refl) .
140 , Generic (Record (Unreify a))
141 , GMappable (TTable context) (Rep (Record a))
142 , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
144 => context :~: Reify ctx -> a -> Unreify a
148 gmap @(TTable context) (Proxy @TUnreify) (unreify Refl) .
153data TTable :: K.Context -> Type -> Exp Constraint
154type instance Eval (TTable context a) = Table context a
157data TColumns :: Type -> Exp K.HTable
158type instance Eval (TColumns a) = Columns a
161data TContext :: Type -> Exp K.Context
162type instance Eval (TContext a) = Context a
165data TUnreify :: Type -> Exp Type
166type instance Eval (TUnreify a) = Unreify a
169type DefaultUnreify :: Type -> Type
170type family DefaultUnreify a where
171 DefaultUnreify (t a b c d e f g) =
172 t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e) (Unreify f) (Unreify g)
173 DefaultUnreify (t a b c d e f) =
174 t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e) (Unreify f)
175 DefaultUnreify (t a b c d e) =
176 t (Unreify a) (Unreify b) (Unreify c) (Unreify d) (Unreify e)
177 DefaultUnreify (t a b c d) =
178 t (Unreify a) (Unreify b) (Unreify c) (Unreify d)
179 DefaultUnreify (t a b c) = t (Unreify a) (Unreify b) (Unreify c)
180 DefaultUnreify (t a b) = t (Unreify a) (Unreify b)
181 DefaultUnreify (t a) = t (Unreify a)
184-- | Any 'HTable' is also a 'Table'.
185instance HTable t => Table context (t (Col context)) where
186 type Columns (t (Col context)) = t
187 type Context (t (Col context)) = context
188 type Unreify (t (Col context)) = t (Col (UnwrapReify context))
194 unreify Refl = hunreify
197-- | Any context is trivially a table.
198instance KnownSpec spec => Table context (Col context spec) where
199 type Columns (Col context spec) = HIdentity spec
200 type Context (Col context spec) = context
201 type Unreify (Col context spec) = Col (UnwrapReify context) spec
203 toColumns = HIdentity
204 fromColumns = unHIdentity
207 unreify Refl (Reify a) = a
210instance Sql DBType a => Table Result (Identity a) where
211 type Columns (Identity a) = HType a
212 type Context (Identity a) = Result
214 toColumns (Identity a) = HType (Result a)
215 fromColumns (HType (Result a)) = Identity a
221instance (Table Result a, Table Result b) => Table Result (Either a b) where
222 type Columns (Either a b) = HEitherTable (Columns a) (Columns b)
223 type Context (Either a b) = Result
226 Left table -> HEitherTable
227 { htag = HIdentity (Result IsLeft)
228 , hleft = hlabel labeler (hnullify nullifier (toColumns table))
229 , hright = hlabel labeler (hnulls null)
231 Right table -> HEitherTable
232 { htag = HIdentity (Result IsRight)
233 , hleft = hlabel labeler (hnulls null)
234 , hright = hlabel labeler (hnullify nullifier (toColumns table))
237 fromColumns HEitherTable {htag, hleft, hright} = case htag of
238 HIdentity (Result tag) -> case tag of
239 IsLeft -> maybe err (Left . fromColumns) $ hunnullify unnullifier (hunlabel unlabeler hleft)
240 IsRight -> maybe err (Right . fromColumns) $ hunnullify unnullifier (hunlabel unlabeler hright)
242 err = error "Either.fromColumns: mismatch between tag and data"
248instance Table Result a => Table Result [a] where
249 type Columns [a] = HListTable (Columns a)
250 type Context [a] = Result
252 toColumns = hvectorize vectorizer . fmap toColumns
253 fromColumns = fmap fromColumns . hunvectorize unvectorizer
259instance Table Result a => Table Result (Maybe a) where
260 type Columns (Maybe a) = HMaybeTable (Columns a)
261 type Context (Maybe a) = Result
264 Nothing -> HMaybeTable
265 { htag = HIdentity (Result Nothing)
266 , hjust = hlabel labeler (hnulls null)
268 Just table -> HMaybeTable
269 { htag = HIdentity (Result (Just IsJust))
270 , hjust = hlabel labeler (hnullify nullifier (toColumns table))
273 fromColumns HMaybeTable {htag, hjust} = case htag of
274 HIdentity (Result tag) -> tag $>
275 case hunnullify unnullifier (hunlabel unlabeler hjust) of
276 Nothing -> error "Maybe.fromColumns: mismatch between tag and data"
277 Just just -> fromColumns just
283instance Table Result a => Table Result (NonEmpty a) where
284 type Columns (NonEmpty a) = HNonEmptyTable (Columns a)
285 type Context (NonEmpty a) = Result
287 toColumns = hvectorize vectorizer . fmap toColumns
288 fromColumns = fmap fromColumns . hunvectorize unvectorizer
294instance (Table Result a, Table Result b) => Table Result (These a b) where
295 type Columns (These a b) = HTheseTable (Columns a) (Columns b)
296 type Context (These a b) = Result
298 toColumns tables = HTheseTable
299 { hhereTag = relabel hhereTag
300 , hhere = hlabel labeler (hunlabel unlabeler (toColumns hhere))
301 , hthereTag = relabel hthereTag
302 , hthere = hlabel labeler (hunlabel unlabeler (toColumns hthere))
308 } = toColumns (justHere tables)
312 } = toColumns (justThere tables)
314 fromColumns HTheseTable {hhereTag, hhere, hthereTag, hthere} =
315 case (fromColumns mhere, fromColumns mthere) of
316 (Just a, Nothing) -> This (fromColumns a)
317 (Nothing, Just b) -> That (fromColumns b)
318 (Just a, Just b) -> These (fromColumns a) (fromColumns b)
319 _ -> error "These.fromColumns: mismatch between tags and data"
322 { htag = relabel hhereTag
323 , hjust = hlabel labeler (hunlabel unlabeler hhere)
326 { htag = relabel hthereTag
327 , hjust = hlabel labeler (hunlabel unlabeler hthere)
334instance (Table context a, Table context b, Labelable context)
335 => Table context (a, b)
339 ( Table context a, Table context b, Table context c
342 => Table context (a, b, c)
346 ( Table context a, Table context b, Table context c, Table context d
349 => Table context (a, b, c, d)
353 ( Table context a, Table context b, Table context c, Table context d
357 => Table context (a, b, c, d, e)
361 ( Table context a, Table context b, Table context c, Table context d
362 , Table context e, Table context f
365 => Table context (a, b, c, d, e, f)
369 ( Table context a, Table context b, Table context c, Table context d
370 , Table context e, Table context f, Table context g
373 => Table context (a, b, c, d, e, f, g)
376type Congruent :: Type -> Type -> Constraint
377class Columns a ~ Columns b => Congruent a b
378instance Columns a ~ Columns b => Congruent a b
381null :: Col Result ('Spec labels necessity (Maybe a))
386 => SSpec ('Spec labels necessity a)
387 -> Col Result ('Spec labels necessity a)
388 -> Col Result ('Spec labels necessity (Nullify a))
389nullifier SSpec {nullity} (Result a) = Result $ case nullity of
395 => SSpec ('Spec labels necessity a)
396 -> Col Result ('Spec labels necessity (Nullify a))
397 -> Maybe (Col Result ('Spec labels necessity a))
398unnullifier SSpec {nullity} (Result a) =
400 Null -> pure $ Result a
401 NotNull -> Result <$> a
404vectorizer :: Functor f
405 => SSpec ('Spec labels necessity a)
406 -> f (Col Result ('Spec labels necessity a))
407 -> Col Result ('Spec labels necessity (f a))
408vectorizer _ = Result . fmap (\(Result a) -> a)
411unvectorizer :: Functor f
412 => SSpec ('Spec labels necessity a)
413 -> Col Result ('Spec labels necessity (f a))
414 -> f (Col Result ('Spec labels necessity a))
415unvectorizer _ (Result results) = Result <$> results
419 => HIdentity ('Spec labels necessity a) (Col Result)
420 -> HIdentity ('Spec relabels necessity a) (Col Result)
421relabel (HIdentity (Result a)) = HIdentity (Result a)
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