on db148ff help sign in
rel8/src/Rel8/Table/These.hs
320 lines 8.5 kB history raw
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
19 ( TheseTable(..)
20 , theseTable, thisTable, thatTable, thoseTable
21 , isThisTable, isThatTable, isThoseTable
22 , hasHereTable, hasThereTable
23 , justHereTable, justThereTable
24 , aggregateTheseTable, insertTheseTable, nameTheseTable
25 )
26where
28-- base
29import Control.Applicative ( liftA2 )
30import Data.Bifunctor ( Bifunctor, bimap )
31import Data.Functor.Identity ( runIdentity )
32import Data.Kind ( Type )
33import Prelude hiding ( undefined )
35-- rel8
36import Rel8.Aggregate ( Aggregate )
37import Rel8.Expr ( Expr )
38import Rel8.Expr.Bool ( (&&.), not_ )
39import Rel8.Expr.Null ( isNonNull )
40import Rel8.Schema.Context.Label
41 ( Labelable
42 , HLabelable, hlabeler, hunlabeler
43 )
44import Rel8.Schema.Context.Nullify
45 ( Nullifiable, ConstrainTag
46 , HNullifiable, HConstrainTag
47 , hencodeTag, hdecodeTag
48 , hnullifier, hunnullifier
49 )
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 )
57import Rel8.Table
58 ( Table, Columns, Context, fromColumns, toColumns
59 , reify, unreify
60 )
61import Rel8.Table.Eq ( EqTable, eqTable )
62import Rel8.Table.Maybe
63 ( MaybeTable(..)
64 , maybeTable, justTable, nothingTable
65 , isJustTable
66 , aggregateMaybeTable, insertMaybeTable, nameMaybeTable
67 )
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 )
75-- semigroupoids
76import Data.Functor.Apply ( Apply, (<.>), liftF2 )
77import Data.Functor.Bind ( Bind, (>>-) )
79-- these
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
88 }
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
100 }
103instance (Table Expr a, Semigroup a) => Applicative (TheseTable a)
104 where
105 pure = thatTable
106 (<*>) = (<.>)
109instance (Table Expr a, Semigroup a) => Bind (TheseTable a) where
110 TheseTable here1 ma >>- f = case ma >>- f' of
111 mtb -> TheseTable
112 { here = maybeTable here1 ((here1 <>) . fst) mtb
113 , there = snd <$> mtb
114 }
115 where
116 f' a = case f a of
117 TheseTable here2 mb -> (here2,) <$> mb
120instance (Table Expr a, Semigroup a) => Monad (TheseTable a) where
121 (>>=) = (>>-)
124instance (Table Expr a, Table Expr b, Semigroup a, Semigroup b) =>
125 Semigroup (TheseTable a b)
126 where
127 a <> b = TheseTable
128 { here = here a <> here b
129 , there = there a <> there b
130 }
133instance
134 ( Table context a, Table context b
135 , Labelable context, Nullifiable context, ConstrainTag context MaybeTag
136 ) => Table context (TheseTable a b)
137 where
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
147instance
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
152 ) =>
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)
169 where
170 fromResult =
171 bimap (fromResult @exprs1) (fromResult @exprs2) .
172 fromColumns
173 toResult =
174 toColumns .
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
207justHereTable = here
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} =
229 maybeTable
230 (maybeTable undefined f here)
231 (\b -> maybeTable (g b) (`h` b) here)
232 there
235aggregateTheseTable :: ()
236 => (a -> Aggregate c)
237 -> (b -> Aggregate d)
238 -> TheseTable a b
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
246insertTheseTable =
247 TheseTable <$> insertMaybeTable . justHere <*> insertMaybeTable . justThere
250nameTheseTable :: ()
251 => Name (Maybe MaybeTag)
252 -> Name (Maybe MaybeTag)
253 -> a
254 -> b
255 -> TheseTable a b
256nameTheseTable here there a b =
257 TheseTable
258 { here = nameMaybeTable here a
259 , there = nameMaybeTable there b
260 }
263toColumns2 ::
264 ( HTable t
265 , HTable u
266 , HConstrainTag context MaybeTag
267 , HLabelable context
268 , HNullifiable context
269 )
270 => (a -> t context)
271 -> (b -> u context)
272 -> TheseTable a b
273 -> HTheseTable t u context
274toColumns2 f g TheseTable {here, there} = HTheseTable
275 { hhereTag = HIdentity $ hencodeTag (toHereTag (tag here))
276 , hhere =
277 hlabel hlabeler $ hnullify (hnullifier (tag here) isNonNull) $ f (just here)
278 , hthereTag = HIdentity $ hencodeTag (toThereTag (tag there))
279 , hthere =
280 hlabel hlabeler $ hnullify (hnullifier (tag there) isNonNull) $ g (just there)
281 }
284fromColumns2 ::
285 ( HTable t
286 , HTable u
287 , HConstrainTag context MaybeTag
288 , HLabelable context
289 , HNullifiable context
290 )
291 => (t context -> a)
292 -> (u context -> b)
293 -> HTheseTable t u context
294 -> TheseTable a b
295fromColumns2 f g HTheseTable {hhereTag, hhere, hthereTag, hthere} = TheseTable
296 { here =
297 let
298 tag = hdecodeTag $ unHIdentity hhereTag
299 in
300 MaybeTable
301 { tag
302 , just = f $
303 runIdentity $
304 hunnullify (\a -> pure . hunnullifier a) $
305 hunlabel hunlabeler
306 hhere
307 }
308 , there =
309 let
310 tag = hdecodeTag $ unHIdentity hthereTag
311 in
312 MaybeTable
313 { tag
314 , just = g $
315 runIdentity $
316 hunnullify (\a -> pure . hunnullifier a) $
317 hunlabel hunlabeler
318 hthere
319 }
320 }