1{-# language FlexibleContexts #-}
2{-# language FlexibleInstances #-}
3{-# language MultiParamTypeClasses #-}
4{-# language NamedFieldPuns #-}
5{-# language ScopedTypeVariables #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeApplications #-}
8{-# language TypeFamilies #-}
9{-# language UndecidableInstances #-}
11module Rel8.Table.NonEmpty
13 , nonEmptyTable, nameNonEmptyTable
18import Data.Functor.Identity ( Identity( Identity ) )
19import Data.Kind ( Type )
20import Data.List.NonEmpty ( NonEmpty )
21import Data.Type.Equality ( (:~:)( Refl ) )
25import Rel8.Expr ( Expr, Col( E, unE ) )
26import Rel8.Expr.Array ( sappend1, snonEmptyOf )
27import Rel8.Schema.Dict ( Dict( Dict ) )
28import Rel8.Schema.HTable.NonEmpty ( HNonEmptyTable )
29import Rel8.Schema.HTable.Vectorize ( happend, hvectorize )
30import Rel8.Schema.Name ( Col( N ), Name( Name ) )
31import Rel8.Schema.Null ( Nullity( Null, NotNull ) )
32import Rel8.Schema.Reify ( hreify, hunreify )
33import Rel8.Schema.Spec ( SSpec(..) )
34import Rel8.Schema.Spec.ConstrainDBType ( dbTypeDict, dbTypeNullity )
36 ( Table, Context, Columns, fromColumns, toColumns
39import Rel8.Table.Alternative ( AltTable, (<|>:) )
40import Rel8.Table.Eq ( EqTable, eqTable )
41import Rel8.Table.Ord ( OrdTable, ordTable )
42import Rel8.Table.Recontextualize ( Recontextualize )
43import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
44import Rel8.Table.Unreify ( Unreifies )
47-- | A @NonEmptyTable@ value contains one or more instances of @a@. You
48-- construct @NonEmptyTable@s with 'Rel8.some' or 'nonEmptyAgg'.
49type NonEmptyTable :: Type -> Type
50newtype NonEmptyTable a =
51 NonEmptyTable (HNonEmptyTable (Columns a) (Col (Context a)))
54instance (Table context a, Unreifies context a) =>
55 Table context (NonEmptyTable a)
57 type Columns (NonEmptyTable a) = HNonEmptyTable (Columns a)
58 type Context (NonEmptyTable a) = Context a
60 fromColumns = NonEmptyTable
61 toColumns (NonEmptyTable a) = a
63 reify Refl (NonEmptyTable a) = NonEmptyTable (hreify a)
64 unreify Refl (NonEmptyTable a) = NonEmptyTable (hunreify a)
68 ( Unreifies from a, Unreifies to b
69 , Recontextualize from to a b
71 => Recontextualize from to (NonEmptyTable a) (NonEmptyTable b)
74instance EqTable a => EqTable (NonEmptyTable a) where
77 (\SSpec {} (Identity dict) -> case dbTypeDict dict of
78 Dict -> case dbTypeNullity dict of
81 (Identity (eqTable @a))
84instance OrdTable a => OrdTable (NonEmptyTable a) where
87 (\SSpec {} (Identity dict) -> case dbTypeDict dict of
88 Dict -> case dbTypeNullity dict of
91 (Identity (ordTable @a))
94type instance FromExprs (NonEmptyTable a) = NonEmpty (FromExprs a)
97instance ToExprs exprs a => ToExprs (NonEmptyTable exprs) (NonEmpty a)
99 fromResult = fmap (fromResult @exprs) . fromColumns
100 toResult = toColumns . fmap (toResult @exprs)
103instance AltTable NonEmptyTable where
107instance Table Expr a => Semigroup (NonEmptyTable a) where
108 NonEmptyTable as <> NonEmptyTable bs = NonEmptyTable $
109 happend (\_ _ (E a) (E b) -> E (sappend1 a b)) as bs
112nonEmptyTable :: Table Expr a => NonEmpty a -> NonEmptyTable a
115 hvectorize (\SSpec {info} -> E . snonEmptyOf info . fmap unE) .
119nameNonEmptyTable :: Table Name a => a -> NonEmptyTable a
122 hvectorize (\_ (Identity (N (Name a))) -> N (Name 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