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 )
81-- | @TheseTable a b@ is a Rel8 table that contains either the table @a@, the
82-- table @b@, or both tables @a@ and @b@. You can construct @TheseTable@s using
83-- 'thisTable', 'thatTable' and 'thoseTable'. @TheseTable@s can be
84-- eliminated/pattern matched using 'theseTable'.
86-- @TheseTable@ is operationally the same as Haskell's 'These' type, but
87-- adapted to work with Rel8.
88type TheseTable :: Type -> Type -> Type
89data TheseTable a b = TheseTable
90 { here :: MaybeTable a
91 , there :: MaybeTable b
93 deriving stock Functor
96instance Bifunctor TheseTable where
97 bimap f g (TheseTable a b) = TheseTable (fmap f a) (fmap g b)
100instance (Table Expr a, Semigroup a) => Apply (TheseTable a) where
101 fs <.> as = TheseTable
102 { here = here fs <> here as
103 , there = there fs <.> there as
107instance (Table Expr a, Semigroup a) => Applicative (TheseTable a)
113instance (Table Expr a, Semigroup a) => Bind (TheseTable a) where
114 TheseTable here1 ma >>- f = case ma >>- f' of
116 { here = maybeTable here1 ((here1 <>) . fst) mtb
117 , there = snd <$> mtb
121 TheseTable here2 mb -> (here2,) <$> mb
124instance (Table Expr a, Semigroup a) => Monad (TheseTable a) where
128instance (Table Expr a, Table Expr b, Semigroup a, Semigroup b) =>
129 Semigroup (TheseTable a b)
132 { here = here a <> here b
133 , there = there a <> there b
138 ( Table context a, Table context b
139 , Labelable context, Nullifiable context, ConstrainTag context MaybeTag
140 ) => Table context (TheseTable a b)
142 type Columns (TheseTable a b) = HTheseTable (Columns a) (Columns b)
143 type Context (TheseTable a b) = Context a
145 toColumns = toColumns2 toColumns toColumns
146 fromColumns = fromColumns2 fromColumns fromColumns
147 reify = liftA2 bimap reify reify
148 unreify = liftA2 bimap unreify unreify
152 ( Labelable from, Nullifiable from, ConstrainTag from MaybeTag
153 , Labelable to, Nullifiable to, ConstrainTag to MaybeTag
154 , Recontextualize from to a1 b1
155 , Recontextualize from to a2 b2
157 Recontextualize from to (TheseTable a1 a2) (TheseTable b1 b2)
160instance (EqTable a, EqTable b) => EqTable (TheseTable a b) where
161 eqTable = toColumns2 id id (thoseTable (eqTable @a) (eqTable @b))
164instance (OrdTable a, OrdTable b) => OrdTable (TheseTable a b) where
165 ordTable = toColumns2 id id (thoseTable (ordTable @a) (ordTable @b))
168type instance FromExprs (TheseTable a b) = These (FromExprs a) (FromExprs b)
171instance (ToExprs exprs1 a, ToExprs exprs2 b, x ~ TheseTable exprs1 exprs2) =>
172 ToExprs x (These a b)
175 bimap (fromResult @exprs1) (fromResult @exprs2) .
179 bimap (toResult @exprs1) (toResult @exprs2)
182toHereTag :: Tag "isJust" a -> Tag "hasHere" a
183toHereTag Tag {..} = Tag {..}
186toThereTag :: Tag "isJust" a -> Tag "hasThere" a
187toThereTag Tag {..} = Tag {..}
190-- | Test if a 'TheseTable' was constructed with 'thisTable'.
191isThisTable :: TheseTable a b -> Expr Bool
192isThisTable a = hasHereTable a &&. not_ (hasThereTable a)
195-- | Test if a 'TheseTable' was constructed with 'thatTable'.
196isThatTable :: TheseTable a b -> Expr Bool
197isThatTable a = not_ (hasHereTable a) &&. hasThereTable a
200-- | Test if a 'TheseTable' was constructed with 'thoseTable'.
201isThoseTable :: TheseTable a b -> Expr Bool
202isThoseTable a = hasHereTable a &&. hasThereTable a
205-- | Test if the @a@ table of @TheseTable a b@ is present.
206hasHereTable :: TheseTable a b -> Expr Bool
207hasHereTable TheseTable {here} = isJustTable here
210-- | Test if the @b@ table of @TheseTable a b@ is present.
211hasThereTable :: TheseTable a b -> Expr Bool
212hasThereTable TheseTable {there} = isJustTable there
215-- | Attempt to project out the @a@ table of a @TheseTable a b@.
216justHereTable :: TheseTable a b -> MaybeTable a
220-- | Attempt to project out the @b@ table of a @TheseTable a b@.
221justThereTable :: TheseTable a b -> MaybeTable b
222justThereTable = there
225-- | Construct a @TheseTable@. Corresponds to 'This'.
226thisTable :: Table Expr b => a -> TheseTable a b
227thisTable a = TheseTable (justTable a) nothingTable
230-- | Construct a @TheseTable@. Corresponds to 'That'.
231thatTable :: Table Expr a => b -> TheseTable a b
232thatTable b = TheseTable nothingTable (justTable b)
235-- | Construct a @TheseTable@. Corresponds to 'These'.
236thoseTable :: a -> b -> TheseTable a b
237thoseTable a b = TheseTable (justTable a) (justTable b)
240-- | Pattern match on a 'TheseTable'.
241theseTable :: Table Expr c
242 => (a -> c) -> (b -> c) -> (a -> b -> c) -> TheseTable a b -> c
243theseTable f g h TheseTable {here, there} =
245 (maybeTable undefined f here)
246 (\b -> maybeTable (g b) (`h` b) here)
250-- | Construct a 'TheseTable' in the 'Name' context. This can be useful if you
251-- have a 'TheseTable' that you are storing in a table and need to construct a
254 => Name (Maybe MaybeTag)
255 -- ^ The name of the column to track the presence of 'thisTable'.
256 -> Name (Maybe MaybeTag)
257 -- ^ The name of the column to track the presence of 'thatTable'.
259 -- ^ Names of the columns in the @a@ table.
261 -- ^ Names of the columns in the @b@ table.
263nameTheseTable here there a b =
265 { here = nameMaybeTable here a
266 , there = nameMaybeTable there b
273 , HConstrainTag context MaybeTag
275 , HNullifiable context
280 -> HTheseTable t u context
281toColumns2 f g TheseTable {here, there} = HTheseTable
282 { hhereTag = HIdentity $ hencodeTag (toHereTag (tag here))
284 hlabel hlabeler $ hnullify (hnullifier (tag here) isNonNull) $ f (just here)
285 , hthereTag = HIdentity $ hencodeTag (toThereTag (tag there))
287 hlabel hlabeler $ hnullify (hnullifier (tag there) isNonNull) $ g (just there)
294 , HConstrainTag context MaybeTag
296 , HNullifiable context
300 -> HTheseTable t u context
302fromColumns2 f g HTheseTable {hhereTag, hhere, hthereTag, hthere} = TheseTable
305 tag = hdecodeTag $ unHIdentity hhereTag
311 hunnullify (\a -> pure . hunnullifier a) $
317 tag = hdecodeTag $ unHIdentity hthereTag
323 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