on c6ffdab help sign in
rel8/src/Rel8/Expr.hs
149 lines 4.1 kB history raw
1{-# language DataKinds #-}
2{-# language DerivingStrategies #-}
3{-# language FlexibleInstances #-}
4{-# language GADTs #-}
5{-# language MultiParamTypeClasses #-}
6{-# language PolyKinds #-}
7{-# language RoleAnnotations #-}
8{-# language ScopedTypeVariables #-}
9{-# language StandaloneDeriving #-}
10{-# language StandaloneKindSignatures #-}
11{-# language TypeApplications #-}
12{-# language TypeFamilies #-}
13{-# language UndecidableInstances #-}
14{-# language UndecidableSuperClasses #-}
16module Rel8.Expr
17 ( Expr(..)
18 , Col( E, unE )
19 )
20where
22-- base
23import Data.Functor.Identity ( Identity )
24import Data.Kind ( Type )
25import Data.String ( IsString, fromString )
26import Prelude hiding ( null )
28-- opaleye
29import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
31-- rel8
32import Rel8.Expr.Function ( function, nullaryFunction )
33import Rel8.Expr.Null ( liftOpNull, nullify )
34import Rel8.Expr.Opaleye
35 ( castExpr
36 , fromPrimExpr
37 , mapPrimExpr
38 , zipPrimExprsWith
39 )
40import Rel8.Expr.Serialize ( litExpr )
41import Rel8.Schema.Context ( Interpretation, Col )
42import Rel8.Schema.HTable.Identity ( HIdentity( HType ), HType )
43import Rel8.Schema.Null ( Nullity( Null, NotNull ), Sql, nullable )
44import Rel8.Schema.Reify ( notReify )
45import Rel8.Schema.Result ( Result )
46import Rel8.Schema.Spec ( Spec( Spec ) )
47import Rel8.Table
48 ( Table, Columns, Context, fromColumns, toColumns, reify, unreify
49 )
50import Rel8.Table.Recontextualize ( Recontextualize )
51import Rel8.Type ( DBType )
52import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
53import Rel8.Type.Num ( DBFloating, DBFractional, DBNum )
54import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
57-- | Typed SQL expressions.
58type role Expr representational
59type Expr :: k -> Type
60data Expr a where
61 Expr :: k ~ Type => !Opaleye.PrimExpr -> Expr (a :: k)
64deriving stock instance Show (Expr a)
67instance Sql DBSemigroup a => Semigroup (Expr a) where
68 (<>) = case nullable @a of
69 Null -> liftOpNull (<>.)
70 NotNull -> (<>.)
71 {-# INLINABLE (<>) #-}
74instance Sql DBMonoid a => Monoid (Expr a) where
75 mempty = case nullable @a of
76 Null -> nullify memptyExpr
77 NotNull -> memptyExpr
78 {-# INLINABLE mempty #-}
81instance (Sql IsString a, Sql DBType a) => IsString (Expr a) where
82 fromString = litExpr . case nullable @a of
83 Null -> Just . fromString
84 NotNull -> fromString
87instance Sql DBNum a => Num (Expr a) where
88 (+) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:+))
89 (*) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:*))
90 (-) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:-))
92 abs = mapPrimExpr (Opaleye.UnExpr Opaleye.OpAbs)
93 negate = mapPrimExpr (Opaleye.UnExpr Opaleye.OpNegate)
95 signum = castExpr . mapPrimExpr (Opaleye.UnExpr (Opaleye.UnOpOther "SIGN"))
97 fromInteger = castExpr . fromPrimExpr . Opaleye.ConstExpr . Opaleye.IntegerLit
100instance Sql DBFractional a => Fractional (Expr a) where
101 (/) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:/))
103 fromRational =
104 castExpr . Expr . Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
107instance Sql DBFloating a => Floating (Expr a) where
108 pi = nullaryFunction "PI"
109 exp = function "exp"
110 log = function "ln"
111 sqrt = function "sqrt"
112 (**) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:^))
113 logBase = function "log"
114 sin = function "sin"
115 cos = function "cos"
116 tan = function "tan"
117 asin = function "asin"
118 acos = function "acos"
119 atan = function "atan"
120 sinh = function "sinh"
121 cosh = function "cosh"
122 tanh = function "tanh"
123 asinh = function "asinh"
124 acosh = function "acosh"
125 atanh = function "atanh"
128instance Interpretation Expr where
129 data Col Expr _spec where
130 E :: {unE :: !(Expr a)} -> Col Expr ('Spec a)
133instance Sql DBType a => Table Expr (Expr a) where
134 type Columns (Expr a) = HType a
135 type Context (Expr a) = Expr
137 toColumns a = HType (E a)
138 fromColumns (HType (E a)) = a
139 reify = notReify
140 unreify = notReify
143instance Sql DBType a => Recontextualize Expr Expr (Expr a) (Expr a)
146instance Sql DBType a => Recontextualize Expr Result (Expr a) (Identity a)
149instance Sql DBType a => Recontextualize Result Expr (Identity a) (Expr a)