1{-# language DataKinds #-}
2{-# language DeriveTraversable #-}
3{-# language DerivingStrategies #-}
4{-# language GeneralizedNewtypeDeriving #-}
5{-# language NamedFieldPuns #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeFamilies #-}
8{-# language UndecidableInstances #-}
10{-# options_ghc -fno-warn-redundant-constraints #-}
12module Rel8.Type.Array1D
29import Control.Applicative ( Alternative, (<|>) )
30import Control.Monad ( MonadPlus )
31import Data.Functor.Classes ( Eq1, Ord1, Read1, Show1 )
32import Data.Kind ( Type )
33import GHC.Exts ( IsList )
34import Prelude hiding ( null )
37import qualified Hasql.Decoders as Hasql
40import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
43import Rel8.Expr.Opaleye ( zipPrimExprsWith )
44import Rel8.Expr.Serialize ( litExpr )
45import Rel8.Schema.Null ( Unnullify, Nullity( Null, NotNull ), Sql, nullable )
46import Rel8.Type ( DBType, typeInformation )
47import Rel8.Type.Eq ( DBEq )
48import Rel8.Type.Information ( TypeInformation(..) )
49import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
50import Rel8.Type.Ord ( DBMax, DBMin, DBOrd )
51import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
54import Data.Functor.Alt ( Alt, (<!>) )
55import Data.Functor.Apply ( Apply )
56import Data.Functor.Bind ( Bind )
57import Data.Functor.Plus ( Plus )
60import Data.Align ( Align )
61import Data.Semialign ( Semialign )
62import Data.Zip ( Repeat, Unzip, Zip )
65-- | A one dimensional array.
66newtype Array1D a = Array1D [a]
67 deriving stock Traversable
69 ( Eq, Ord, Read, Show, Semigroup, Monoid, IsList
71 , Eq1, Ord1, Read1, Show1
72 , FromJSON1, ToJSON1, FromJSON, FromJSONKey, ToJSON, ToJSONKey
73 , Apply, Applicative, Alternative, Plus, Bind, Monad, MonadPlus
74 , Align, Semialign, Repeat, Unzip, Zip
78instance Alt Array1D where
82getArray1D :: Array1D a -> [a]
83getArray1D (Array1D a) = a
86type IsArray1D :: Type -> Bool
87type family IsArray1D a where
88 IsArray1D (Array1D _) = 'True
92array1DTypeInformation :: IsArray1D (Unnullify a) ~ 'False
94 -> TypeInformation (Unnullify a)
95 -> TypeInformation (Array1D a)
96array1DTypeInformation nullity info =
98 TypeInformation{ encode, decode, typeName } -> TypeInformation
99 { decode = case nullity of
100 Null -> Array1D <$> Hasql.listArray (Hasql.nullable decode)
101 NotNull -> Array1D <$> Hasql.listArray (Hasql.nonNullable decode)
102 , encode = case nullity of
103 Null -> Opaleye.ArrayExpr . fmap (maybe null encode) . getArray1D
104 NotNull -> Opaleye.ArrayExpr . fmap encode . getArray1D
105 , typeName = typeName <> "[]"
108 null = Opaleye.ConstExpr Opaleye.NullLit
111instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBType (Array1D a) where
112 typeInformation = array1DTypeInformation nullable typeInformation
115instance (Sql DBEq a, IsArray1D (Unnullify a) ~ 'False) => DBEq (Array1D a)
118instance (Sql DBOrd a, IsArray1D (Unnullify a) ~ 'False) => DBOrd (Array1D a)
121instance (Sql DBMax a, IsArray1D (Unnullify a) ~ 'False) => DBMax (Array1D a)
124instance (Sql DBMin a, IsArray1D (Unnullify a) ~ 'False) => DBMin (Array1D a)
127instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBSemigroup (Array1D a) where
128 (<>.) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:||))
131instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBMonoid (Array1D a) where
132 memptyExpr = litExpr mempty
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