1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
3{-# language LambdaCase #-}
4{-# language MultiParamTypeClasses #-}
5{-# language ScopedTypeVariables #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeApplications #-}
8{-# language TypeFamilyDependencies #-}
9{-# language UndecidableInstances #-}
11module Rel8.Column.Either
12 ( HEither, AHEither(..)
17import Control.Applicative ( liftA2 )
18import Control.Category ( id )
19import Data.Bifunctor ( Bifunctor, bimap )
20import Data.Kind ( Type )
21import Data.Type.Equality ( (:~:)( Refl ), apply )
22import Prelude hiding ( id )
25import Rel8.Aggregate ( Aggregate )
26import Rel8.Expr ( Expr )
27import Rel8.Kind.Context ( SContext(..), Reifiable( contextSing ) )
28import Rel8.Schema.Context ( Col )
29import Rel8.Schema.HTable.Either ( HEitherTable )
30import qualified Rel8.Schema.Kind as K
31import Rel8.Schema.Name ( Name(..) )
32import Rel8.Schema.Reify ( Reify, hreify, hunreify )
33import Rel8.Schema.Result ( Result, absurd )
35 ( Table, Columns, Context, fromColumns, toColumns
36 , Unreify, reify, unreify, coherence, congruence
38import Rel8.Table.Either ( EitherTable )
39import Rel8.Table.Recontextualize ( Recontextualize )
42-- | Nest an 'Either' value within a 'Rel8able'. @HEither f a b@ will produce a
43-- 'EitherTable' @a b@ in the 'Expr' context, and a 'Either' @a b@ in the
45type HEither :: K.Context -> Type -> Type -> Type
46type family HEither context = either | either -> context where
47 HEither (Reify context) = AHEither context
48 HEither Aggregate = EitherTable Aggregate
49 HEither Expr = EitherTable Expr
50 HEither Name = EitherTable Name
51 HEither Result = Either
54type AHEither :: K.Context -> Type -> Type -> Type
55newtype AHEither context a b = AHEither (HEither context a b)
58instance Reifiable context => Bifunctor (AHEither context) where
59 bimap = sbimapEither contextSing
62instance Reifiable context => Functor (AHEither context a) where
66instance (Reifiable context, Table (Reify context) a, Table (Reify context) b)
67 => Table (Reify context) (AHEither context a b)
69 type Context (AHEither context a b) = Reify context
70 type Columns (AHEither context a b) = HEitherTable (Columns a) (Columns b)
71 type Unreify (AHEither context a b) = HEither context (Unreify a) (Unreify b)
73 fromColumns = sfromColumnsEither contextSing
74 toColumns = stoColumnsEither contextSing
75 reify proof = liftA2 bimap reify reify proof . AHEither
76 unreify proof = (\(AHEither a) -> a) . liftA2 bimap unreify unreify proof
78 coherence = case contextSing @context of
79 SAggregate -> coherence @(Reify context) @a
80 SExpr -> coherence @(Reify context) @a
81 SName -> coherence @(Reify context) @a
82 SResult -> \Refl -> absurd
83 SReify _ -> \Refl _ -> Refl
85 congruence proof@Refl abstract = case contextSing @context of
88 congruence @(Reify context) @a proof abstract `apply`
89 congruence @(Reify context) @b proof abstract
92 congruence @(Reify context) @a proof abstract `apply`
93 congruence @(Reify context) @b proof abstract
96 congruence @(Reify context) @a proof abstract `apply`
97 congruence @(Reify context) @b proof abstract
98 SResult -> absurd abstract
101 congruence @(Reify context) @a proof abstract `apply`
102 congruence @(Reify context) @b proof abstract
106 ( Reifiable context, Reifiable context'
107 , Recontextualize (Reify context) (Reify context') a a'
108 , Recontextualize (Reify context) (Reify context') b b'
113 (AHEither context a b)
114 (AHEither context' a' b')
121 -> AHEither context a b
122 -> AHEither context c d
124 SAggregate -> \f g (AHEither a) -> AHEither (bimap f g a)
125 SExpr -> \f g (AHEither a) -> AHEither (bimap f g a)
126 SResult -> \f g (AHEither a) -> AHEither (bimap f g a)
127 SName -> \f g (AHEither a) -> AHEither (bimap f g a)
128 SReify context -> \f g (AHEither a) -> AHEither (sbimapEither context f g a)
131sfromColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
133 -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
134 -> AHEither context a b
135sfromColumnsEither = \case
138 bimap (fromColumns . hreify) (fromColumns . hreify) .
143 bimap (fromColumns . hreify) (fromColumns . hreify) .
148 bimap (fromColumns . hreify) (fromColumns . hreify) .
153 bimap (fromColumns . hreify) (fromColumns . hreify) .
158 sbimapEither context (fromColumns . hreify) (fromColumns . hreify) .
159 sfromColumnsEither context .
163stoColumnsEither :: (Table (Reify context) a, Table (Reify context) b)
165 -> AHEither context a b
166 -> HEitherTable (Columns a) (Columns b) (Col (Reify context))
167stoColumnsEither = \case
171 bimap (hunreify . toColumns) (hunreify . toColumns) .
176 bimap (hunreify . toColumns) (hunreify . toColumns) .
181 bimap (hunreify . toColumns) (hunreify . toColumns) .
186 bimap (hunreify . toColumns) (hunreify . toColumns) .
190 stoColumnsEither context .
191 sbimapEither context (hunreify . toColumns) (hunreify . toColumns) .
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