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
29import Control.Applicative ( liftA2 )
30import Data.Bifunctor ( Bifunctor, bimap )
31import Data.Functor.Identity ( runIdentity )
32import Data.Kind ( Type )
33import Prelude hiding ( undefined )
36import Rel8.Expr ( Expr )
37import Rel8.Expr.Bool ( (&&.), not_ )
38import Rel8.Expr.Null ( isNonNull )
39import Rel8.Schema.Context.Label
41 , HLabelable, hlabeler, hunlabeler
43import Rel8.Schema.Context.Nullify
44 ( Nullifiable, ConstrainTag
45 , HNullifiable, HConstrainTag
46 , hencodeTag, hdecodeTag
47 , hnullifier, hunnullifier
49import Rel8.Schema.HTable ( HTable )
50import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
51import Rel8.Schema.HTable.Identity ( HIdentity(..) )
52import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
53import Rel8.Schema.HTable.These ( HTheseTable(..) )
54import Rel8.Schema.Name ( Name )
56 ( Table, Columns, Context, fromColumns, toColumns
59import Rel8.Table.Eq ( EqTable, eqTable )
60import Rel8.Table.Maybe
62 , maybeTable, justTable, nothingTable
66import Rel8.Table.Ord ( OrdTable, ordTable )
67import Rel8.Table.Recontextualize ( Recontextualize )
68import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
69import Rel8.Table.Tag ( Tag(..) )
70import Rel8.Table.Undefined ( undefined )
71import Rel8.Type.Tag ( MaybeTag )
74import Data.Functor.Apply ( Apply, (<.>) )
75import Data.Functor.Bind ( Bind, (>>-) )
78import Data.These ( These )
81type TheseTable :: Type -> Type -> Type
82data TheseTable a b = TheseTable
83 { here :: MaybeTable a
84 , there :: MaybeTable b
86 deriving stock Functor
89instance Bifunctor TheseTable where
90 bimap f g (TheseTable a b) = TheseTable (fmap f a) (fmap g b)
93instance (Table Expr a, Semigroup a) => Apply (TheseTable a) where
94 fs <.> as = TheseTable
95 { here = here fs <> here as
96 , there = there fs <.> there as
100instance (Table Expr a, Semigroup a) => Applicative (TheseTable a)
106instance (Table Expr a, Semigroup a) => Bind (TheseTable a) where
107 TheseTable here1 ma >>- f = case ma >>- f' of
109 { here = maybeTable here1 ((here1 <>) . fst) mtb
110 , there = snd <$> mtb
114 TheseTable here2 mb -> (here2,) <$> mb
117instance (Table Expr a, Semigroup a) => Monad (TheseTable a) where
121instance (Table Expr a, Table Expr b, Semigroup a, Semigroup b) =>
122 Semigroup (TheseTable a b)
125 { here = here a <> here b
126 , there = there a <> there b
131 ( Table context a, Table context b
132 , Labelable context, Nullifiable context, ConstrainTag context MaybeTag
133 ) => Table context (TheseTable a b)
135 type Columns (TheseTable a b) = HTheseTable (Columns a) (Columns b)
136 type Context (TheseTable a b) = Context a
138 toColumns = toColumns2 toColumns toColumns
139 fromColumns = fromColumns2 fromColumns fromColumns
140 reify = liftA2 bimap reify reify
141 unreify = liftA2 bimap unreify unreify
145 ( Labelable from, Nullifiable from, ConstrainTag from MaybeTag
146 , Labelable to, Nullifiable to, ConstrainTag to MaybeTag
147 , Recontextualize from to a1 b1
148 , Recontextualize from to a2 b2
150 Recontextualize from to (TheseTable a1 a2) (TheseTable b1 b2)
153instance (EqTable a, EqTable b) => EqTable (TheseTable a b) where
154 eqTable = toColumns2 id id (thoseTable (eqTable @a) (eqTable @b))
157instance (OrdTable a, OrdTable b) => OrdTable (TheseTable a b) where
158 ordTable = toColumns2 id id (thoseTable (ordTable @a) (ordTable @b))
161type instance FromExprs (TheseTable a b) = These (FromExprs a) (FromExprs b)
164instance (ToExprs exprs1 a, ToExprs exprs2 b, x ~ TheseTable exprs1 exprs2) =>
165 ToExprs x (These a b)
168 bimap (fromResult @exprs1) (fromResult @exprs2) .
172 bimap (toResult @exprs1) (toResult @exprs2)
175toHereTag :: Tag "isJust" a -> Tag "hasHere" a
176toHereTag Tag {..} = Tag {..}
179toThereTag :: Tag "isJust" a -> Tag "hasThere" a
180toThereTag Tag {..} = Tag {..}
183isThisTable :: TheseTable a b -> Expr Bool
184isThisTable a = hasHereTable a &&. not_ (hasThereTable a)
187isThatTable :: TheseTable a b -> Expr Bool
188isThatTable a = not_ (hasHereTable a) &&. hasThereTable a
191isThoseTable :: TheseTable a b -> Expr Bool
192isThoseTable a = hasHereTable a &&. hasThereTable a
195hasHereTable :: TheseTable a b -> Expr Bool
196hasHereTable TheseTable {here} = isJustTable here
199hasThereTable :: TheseTable a b -> Expr Bool
200hasThereTable TheseTable {there} = isJustTable there
203justHereTable :: TheseTable a b -> MaybeTable a
207justThereTable :: TheseTable a b -> MaybeTable b
208justThereTable = there
211thisTable :: Table Expr b => a -> TheseTable a b
212thisTable a = TheseTable (justTable a) nothingTable
215thatTable :: Table Expr a => b -> TheseTable a b
216thatTable b = TheseTable nothingTable (justTable b)
219thoseTable :: a -> b -> TheseTable a b
220thoseTable a b = TheseTable (justTable a) (justTable b)
223theseTable :: Table Expr c
224 => (a -> c) -> (b -> c) -> (a -> b -> c) -> TheseTable a b -> c
225theseTable f g h TheseTable {here, there} =
227 (maybeTable undefined f here)
228 (\b -> maybeTable (g b) (`h` b) here)
233 => Name (Maybe MaybeTag)
234 -> Name (Maybe MaybeTag)
238nameTheseTable here there a b =
240 { here = nameMaybeTable here a
241 , there = nameMaybeTable there b
248 , HConstrainTag context MaybeTag
250 , HNullifiable context
255 -> HTheseTable t u context
256toColumns2 f g TheseTable {here, there} = HTheseTable
257 { hhereTag = HIdentity $ hencodeTag (toHereTag (tag here))
259 hlabel hlabeler $ hnullify (hnullifier (tag here) isNonNull) $ f (just here)
260 , hthereTag = HIdentity $ hencodeTag (toThereTag (tag there))
262 hlabel hlabeler $ hnullify (hnullifier (tag there) isNonNull) $ g (just there)
269 , HConstrainTag context MaybeTag
271 , HNullifiable context
275 -> HTheseTable t u context
277fromColumns2 f g HTheseTable {hhereTag, hhere, hthereTag, hthere} = TheseTable
280 tag = hdecodeTag $ unHIdentity hhereTag
286 hunnullify (\a -> pure . hunnullifier a) $
292 tag = hdecodeTag $ unHIdentity hthereTag
298 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