1{-# language DataKinds #-}
2{-# language DeriveFunctor #-}
3{-# language DerivingStrategies #-}
4{-# language FlexibleContexts #-}
5{-# language FlexibleInstances #-}
6{-# language MultiParamTypeClasses #-}
7{-# language NamedFieldPuns #-}
8{-# language RecordWildCards #-}
9{-# language ScopedTypeVariables #-}
10{-# language StandaloneKindSignatures #-}
11{-# language TupleSections #-}
12{-# language TypeApplications #-}
13{-# language TypeFamilies #-}
14{-# language UndecidableInstances #-}
16{-# options_ghc -fno-warn-orphans #-}
18module Rel8.Table.These
20 , theseTable, thisTable, thatTable, thoseTable
21 , isThisTable, isThatTable, isThoseTable
22 , hasHereTable, hasThereTable
23 , justHereTable, justThereTable
24 , aggregateTheseTable, insertTheseTable, nameTheseTable
29import Control.Applicative ( liftA2 )
30import Data.Bifunctor ( Bifunctor, bimap )
31import Data.Functor.Identity ( runIdentity )
32import Data.Kind ( Type )
33import Prelude hiding ( undefined )
36import Rel8.Aggregate ( Aggregate )
37import Rel8.Expr ( Expr )
38import Rel8.Expr.Bool ( (&&.), not_ )
39import Rel8.Expr.Null ( isNonNull )
40import Rel8.Schema.Context.Label
42 , HLabelable, hlabeler, hunlabeler
44import Rel8.Schema.Context.Nullify
45 ( Nullifiable, ConstrainTag
46 , HNullifiable, HConstrainTag
47 , hencodeTag, hdecodeTag
48 , hnullifier, hunnullifier
50import Rel8.Schema.HTable ( HTable )
51import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
52import Rel8.Schema.HTable.Identity ( HIdentity(..) )
53import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
54import Rel8.Schema.HTable.These ( HTheseTable(..) )
55import Rel8.Schema.Insert ( Insert )
56import Rel8.Schema.Name ( Name )
58 ( Table, Columns, Context, fromColumns, toColumns
61import Rel8.Table.Eq ( EqTable, eqTable )
62import Rel8.Table.Maybe
64 , maybeTable, justTable, nothingTable
66 , aggregateMaybeTable, insertMaybeTable, nameMaybeTable
68import Rel8.Table.Ord ( OrdTable, ordTable )
69import Rel8.Table.Recontextualize ( Recontextualize )
70import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
71import Rel8.Table.Tag ( Tag(..) )
72import Rel8.Table.Undefined ( undefined )
73import Rel8.Type.Tag ( MaybeTag )
76import Data.Functor.Apply ( Apply, (<.>), liftF2 )
77import Data.Functor.Bind ( Bind, (>>-) )
80import Data.These ( These )
81import Data.These.Combinators ( justHere, justThere )
84type TheseTable :: Type -> Type -> Type
85data TheseTable a b = TheseTable
86 { here :: MaybeTable a
87 , there :: MaybeTable b
89 deriving stock Functor
92instance Bifunctor TheseTable where
93 bimap f g (TheseTable a b) = TheseTable (fmap f a) (fmap g b)
96instance (Table Expr a, Semigroup a) => Apply (TheseTable a) where
97 fs <.> as = TheseTable
98 { here = here fs <> here as
99 , there = there fs <.> there as
103instance (Table Expr a, Semigroup a) => Applicative (TheseTable a)
109instance (Table Expr a, Semigroup a) => Bind (TheseTable a) where
110 TheseTable here1 ma >>- f = case ma >>- f' of
112 { here = maybeTable here1 ((here1 <>) . fst) mtb
113 , there = snd <$> mtb
117 TheseTable here2 mb -> (here2,) <$> mb
120instance (Table Expr a, Semigroup a) => Monad (TheseTable a) where
124instance (Table Expr a, Table Expr b, Semigroup a, Semigroup b) =>
125 Semigroup (TheseTable a b)
128 { here = here a <> here b
129 , there = there a <> there b
134 ( Table context a, Table context b
135 , Labelable context, Nullifiable context, ConstrainTag context MaybeTag
136 ) => Table context (TheseTable a b)
138 type Columns (TheseTable a b) = HTheseTable (Columns a) (Columns b)
139 type Context (TheseTable a b) = Context a
141 toColumns = toColumns2 toColumns toColumns
142 fromColumns = fromColumns2 fromColumns fromColumns
143 reify = liftA2 bimap reify reify
144 unreify = liftA2 bimap unreify unreify
148 ( Labelable from, Nullifiable from, ConstrainTag from MaybeTag
149 , Labelable to, Nullifiable to, ConstrainTag to MaybeTag
150 , Recontextualize from to a1 b1
151 , Recontextualize from to a2 b2
153 Recontextualize from to (TheseTable a1 a2) (TheseTable b1 b2)
156instance (EqTable a, EqTable b) => EqTable (TheseTable a b) where
157 eqTable = toColumns2 id id (thoseTable (eqTable @a) (eqTable @b))
160instance (OrdTable a, OrdTable b) => OrdTable (TheseTable a b) where
161 ordTable = toColumns2 id id (thoseTable (ordTable @a) (ordTable @b))
164type instance FromExprs (TheseTable a b) = These (FromExprs a) (FromExprs b)
167instance (ToExprs exprs1 a, ToExprs exprs2 b, x ~ TheseTable exprs1 exprs2) =>
168 ToExprs x (These a b)
171 bimap (fromResult @exprs1) (fromResult @exprs2) .
175 bimap (toResult @exprs1) (toResult @exprs2)
178toHereTag :: Tag "isJust" a -> Tag "hasHere" a
179toHereTag Tag {..} = Tag {..}
182toThereTag :: Tag "isJust" a -> Tag "hasThere" a
183toThereTag Tag {..} = Tag {..}
186isThisTable :: TheseTable a b -> Expr Bool
187isThisTable a = hasHereTable a &&. not_ (hasThereTable a)
190isThatTable :: TheseTable a b -> Expr Bool
191isThatTable a = not_ (hasHereTable a) &&. hasThereTable a
194isThoseTable :: TheseTable a b -> Expr Bool
195isThoseTable a = hasHereTable a &&. hasThereTable a
198hasHereTable :: TheseTable a b -> Expr Bool
199hasHereTable TheseTable {here} = isJustTable here
202hasThereTable :: TheseTable a b -> Expr Bool
203hasThereTable TheseTable {there} = isJustTable there
206justHereTable :: TheseTable a b -> MaybeTable a
210justThereTable :: TheseTable a b -> MaybeTable b
211justThereTable = there
214thisTable :: Table Expr b => a -> TheseTable a b
215thisTable a = TheseTable (justTable a) nothingTable
218thatTable :: Table Expr a => b -> TheseTable a b
219thatTable b = TheseTable nothingTable (justTable b)
222thoseTable :: a -> b -> TheseTable a b
223thoseTable a b = TheseTable (justTable a) (justTable b)
226theseTable :: Table Expr c
227 => (a -> c) -> (b -> c) -> (a -> b -> c) -> TheseTable a b -> c
228theseTable f g h TheseTable {here, there} =
230 (maybeTable undefined f here)
231 (\b -> maybeTable (g b) (`h` b) here)
235aggregateTheseTable :: ()
236 => (a -> Aggregate c)
237 -> (b -> Aggregate d)
239 -> Aggregate (TheseTable c d)
240aggregateTheseTable f g TheseTable {here, there} =
241 liftF2 TheseTable (aggregateMaybeTable f here) (aggregateMaybeTable g there)
244insertTheseTable :: (Table Insert a, Table Insert b)
245 => These a b -> TheseTable a b
247 TheseTable <$> insertMaybeTable . justHere <*> insertMaybeTable . justThere
251 => Name (Maybe MaybeTag)
252 -> Name (Maybe MaybeTag)
256nameTheseTable here there a b =
258 { here = nameMaybeTable here a
259 , there = nameMaybeTable there b
266 , HConstrainTag context MaybeTag
268 , HNullifiable context
273 -> HTheseTable t u context
274toColumns2 f g TheseTable {here, there} = HTheseTable
275 { hhereTag = HIdentity $ hencodeTag (toHereTag (tag here))
277 hlabel hlabeler $ hnullify (hnullifier (tag here) isNonNull) $ f (just here)
278 , hthereTag = HIdentity $ hencodeTag (toThereTag (tag there))
280 hlabel hlabeler $ hnullify (hnullifier (tag there) isNonNull) $ g (just there)
287 , HConstrainTag context MaybeTag
289 , HNullifiable context
293 -> HTheseTable t u context
295fromColumns2 f g HTheseTable {hhereTag, hhere, hthereTag, hthere} = TheseTable
298 tag = hdecodeTag $ unHIdentity hhereTag
304 hunnullify (\a -> pure . hunnullifier a) $
310 tag = hdecodeTag $ unHIdentity hthereTag
316 hunnullify (\a -> pure . hunnullifier 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