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.Kind.Algebra ( KnownAlgebra )
37import Rel8.Generic.Map ( GMap, GMappable, gmap, gunmap )
38import Rel8.Generic.Table
39 ( GGTable, GGColumns, GGContext, ggfromColumns, ggtoColumns
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
63import Rel8.Schema.Result
64 ( Col( Result ), Result
66 , null, nullifier, unnullifier
67 , vectorizer, unvectorizer
69import Rel8.Schema.Spec ( KnownSpec )
70import Rel8.Type ( DBType )
71import Rel8.Type.Tag ( EitherTag( IsLeft, IsRight ), MaybeTag( IsJust ) )
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
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
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
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)))
116 => a -> Columns a (Col context)
119 @(GAlgebra (Rep (Record a)))
128 default fromColumns ::
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)))
135 => Columns a (Col context) -> a
140 @(GAlgebra (Rep (Record a)))
149 , Generic (Record (Unreify a))
150 , GMappable (TTable context) (Rep (Record a))
151 , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
153 => context :~: Reify ctx -> Unreify a -> a
157 gunmap @(TTable context) (Proxy @TUnreify) (reify Refl) .
163 , Generic (Record (Unreify a))
164 , GMappable (TTable context) (Rep (Record a))
165 , Rep (Record (Unreify a)) ~ GMap TUnreify (Rep (Record a))
167 => context :~: Reify ctx -> a -> Unreify a
171 gmap @(TTable context) (Proxy @TUnreify) (unreify Refl) .
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))
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
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
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
250 Left table -> HEitherTable
251 { htag = HIdentity (Result IsLeft)
252 , hleft = hlabel labeler (hnullify nullifier (toColumns table))
253 , hright = hlabel labeler (hnulls (const null))
255 Right table -> HEitherTable
256 { htag = HIdentity (Result IsRight)
257 , hleft = hlabel labeler (hnulls (const null))
258 , hright = hlabel labeler (hnullify nullifier (toColumns table))
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)
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
282 Nothing -> HMaybeTable
283 { htag = HIdentity (Result Nothing)
284 , hjust = hlabel labeler (hnulls (const null))
286 Just table -> HMaybeTable
287 { htag = HIdentity (Result (Just IsJust))
288 , hjust = hlabel labeler (hnullify nullifier (toColumns table))
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))
320 } = toColumns (justHere tables)
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"
334 { htag = relabel hhereTag
335 , hjust = hlabel labeler (hunlabel unlabeler hhere)
338 { htag = relabel hthereTag
339 , hjust = hlabel labeler (hunlabel unlabeler hthere)
343instance (Table context a, Table context b, Labelable context)
344 => Table context (a, b)
348 ( Table context a, Table context b, Table context c
351 => Table context (a, b, c)
355 ( Table context a, Table context b, Table context c, Table context d
358 => Table context (a, b, c, d)
362 ( Table context a, Table context b, Table context c, Table context d
366 => Table context (a, b, c, d, e)
370 ( Table context a, Table context b, Table context c, Table context d
371 , Table context e, Table context f
374 => Table context (a, b, c, d, e, f)
378 ( Table context a, Table context b, Table context c, Table context d
379 , Table context e, Table context f, Table context g
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
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