on master help sign in
Add Floating instance for Expr
commit08dea9fa90de230aed02eeba266716154c44b029 authorShane O'Brien <shane.obrien@circuithub.com> dateThu 20 May 2021, 09:36 (5 years ago) parente48c73e
5 files changed+38−9
src/Rel8.hs+1 src/Rel8/Expr.hs+23−1 src/Rel8/Expr/Function.hs+1−1 src/Rel8/Expr/Num.hs+6−6 src/Rel8/Type/Num.hs+7−1
src/Rel8.hs+1
@@ -28,6 +28,7 @@ module Rel8
2828 , DBNum
2929 , DBIntegral
3030 , DBFractional
31+ , DBFloating
3132
3233 -- * Tables and higher-kinded tables
3334 , Rel8able, KRel8able
src/Rel8/Expr.hs+23−1
@@ -29,6 +29,7 @@ import Prelude hiding ( null )
2929import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
3030
3131-- rel8
32+import Rel8.Expr.Function ( function, nullaryFunction )
3233import Rel8.Expr.Null ( liftOpNull, nullify )
3334import Rel8.Expr.Opaleye
3435 ( castExpr
@@ -50,7 +51,7 @@ import Rel8.Table
5051import Rel8.Table.Recontextualize ( Recontextualize )
5152import Rel8.Type ( DBType )
5253import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
53−import Rel8.Type.Num ( DBFractional, DBNum )
54+import Rel8.Type.Num ( DBFloating, DBFractional, DBNum )
5455import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
5556
5657
@@ -104,6 +105,27 @@ instance Sql DBFractional a => Fractional (Expr a) where
104105 castExpr . Expr . Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
105106
106107
108+instance Sql DBFloating a => Floating (Expr a) where
109+ pi = nullaryFunction "PI"
110+ exp = function "exp"
111+ log = function "ln"
112+ sqrt = function "sqrt"
113+ (**) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:^))
114+ logBase = function "log"
115+ sin = function "sin"
116+ cos = function "cos"
117+ tan = function "tan"
118+ asin = function "asin"
119+ acos = function "acos"
120+ atan = function "atan"
121+ sinh = function "sinh"
122+ cosh = function "cosh"
123+ tanh = function "tanh"
124+ asinh = function "asinh"
125+ acosh = function "acosh"
126+ atanh = function "atanh"
127+
128+
107129instance Interpretation Expr where
108130 data Col Expr _spec where
109131 DB :: {unDB :: Expr a} -> Col Expr ('Spec labels necessity a)
src/Rel8/Expr/Function.hs+1−1
@@ -20,7 +20,7 @@ import Prelude
2020import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
2121
2222-- rel8
23−import Rel8.Expr ( Expr( Expr ) )
23+import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
2424import Rel8.Expr.Opaleye
2525 ( castExpr
2626 , fromPrimExpr, toPrimExpr, zipPrimExprsWith
src/Rel8/Expr/Num.hs+6−6
@@ -39,17 +39,17 @@ realToFrac (Expr a) = castExpr (Expr a)
3939-- Corresponds to the @ceiling()@ function.
4040ceiling :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
4141 => Expr a -> Expr b
42−ceiling = function "CEILING"
42+ceiling = function "ceiling"
4343
4444
4545-- | Perform integral division. Corresponds to the @div()@ function.
4646div :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
47−div = function "DIV"
47+div = function "div"
4848
4949
5050-- | Corresponds to the @mod()@ function.
5151mod :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
52−mod = function "MOD"
52+mod = function "mod"
5353
5454
5555-- | Round a 'DFractional' to a 'DBIntegral' by rounding to the nearest smaller
@@ -58,7 +58,7 @@ mod = function "MOD"
5858-- Corresponds to the @floor()@ function.
5959floor :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
6060 => Expr a -> Expr b
61−floor = function "FLOOR"
61+floor = function "floor"
6262
6363
6464-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
@@ -67,11 +67,11 @@ floor = function "FLOOR"
6767-- Corresponds to the @round()@ function.
6868round :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
6969 => Expr a -> Expr b
70−round = function "ROUND"
70+round = function "round"
7171
7272
7373-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
7474-- integer towards zero.
7575truncate :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
7676 => Expr a -> Expr b
77−truncate = function "TRUNC"
77+truncate = function "trunc"
src/Rel8/Type/Num.hs+7−1
@@ -7,7 +7,7 @@
77{-# language UndecidableInstances #-}
88
99module Rel8.Type.Num
10− ( DBNum, DBIntegral, DBFractional
10+ ( DBNum, DBIntegral, DBFractional, DBFloating
1111 )
1212where
1313
@@ -50,3 +50,9 @@ class DBNum a => DBFractional a
5050instance DBFractional Float
5151instance DBFractional Double
5252instance DBFractional Scientific
53+
54+
55+-- | The class of database types that support the @/@ operator.
56+class DBFractional a => DBFloating a
57+instance DBFloating Float
58+instance DBFloating Double