on master help sign in
Add Floating instance for Expr
08dea9fShane O'Brien
src/Rel8.hs+1
@@ -28,6 +28,7 @@ module Rel8
28 , DBNum
28 , DBNum
29 , DBIntegral
29 , DBIntegral
30 , DBFractional
30 , DBFractional
31+ , DBFloating
31
32
32 -- * Tables and higher-kinded tables
33 -- * Tables and higher-kinded tables
33 , Rel8able, KRel8able
34 , Rel8able, KRel8able
src/Rel8/Expr.hs+23−1
@@ -29,6 +29,7 @@ import Prelude hiding ( null )
29import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
29import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
30
30
31-- rel8
31-- rel8
32+import Rel8.Expr.Function ( function, nullaryFunction )
32import Rel8.Expr.Null ( liftOpNull, nullify )
33import Rel8.Expr.Null ( liftOpNull, nullify )
33import Rel8.Expr.Opaleye
34import Rel8.Expr.Opaleye
34 ( castExpr
35 ( castExpr
@@ -50,7 +51,7 @@ import Rel8.Table
50import Rel8.Table.Recontextualize ( Recontextualize )
51import Rel8.Table.Recontextualize ( Recontextualize )
51import Rel8.Type ( DBType )
52import Rel8.Type ( DBType )
52import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
53import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
53−import Rel8.Type.Num ( DBFractional, DBNum )
54+import Rel8.Type.Num ( DBFloating, DBFractional, DBNum )
54import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
55import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
55
56
56
57
@@ -104,6 +105,27 @@ instance Sql DBFractional a => Fractional (Expr a) where
104 castExpr . Expr . Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
105 castExpr . Expr . Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
105
106
106
107
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+
107instance Interpretation Expr where
129instance Interpretation Expr where
108 data Col Expr _spec where
130 data Col Expr _spec where
109 DB :: {unDB :: Expr a} -> Col Expr ('Spec labels necessity a)
131 DB :: {unDB :: Expr a} -> Col Expr ('Spec labels necessity a)
src/Rel8/Expr/Function.hs+1−1
@@ -20,7 +20,7 @@ import Prelude
20import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
20import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
21
21
22-- rel8
22-- rel8
23−import Rel8.Expr ( Expr( Expr ) )
23+import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
24import Rel8.Expr.Opaleye
24import Rel8.Expr.Opaleye
25 ( castExpr
25 ( castExpr
26 , fromPrimExpr, toPrimExpr, zipPrimExprsWith
26 , fromPrimExpr, toPrimExpr, zipPrimExprsWith
src/Rel8/Expr/Num.hs+6−6
@@ -39,17 +39,17 @@ realToFrac (Expr a) = castExpr (Expr a)
39-- Corresponds to the @ceiling()@ function.
39-- Corresponds to the @ceiling()@ function.
40ceiling :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
40ceiling :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
41 => Expr a -> Expr b
41 => Expr a -> Expr b
42−ceiling = function "CEILING"
42+ceiling = function "ceiling"
43
43
44
44
45-- | Perform integral division. Corresponds to the @div()@ function.
45-- | Perform integral division. Corresponds to the @div()@ function.
46div :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
46div :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
47−div = function "DIV"
47+div = function "div"
48
48
49
49
50-- | Corresponds to the @mod()@ function.
50-- | Corresponds to the @mod()@ function.
51mod :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
51mod :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
52−mod = function "MOD"
52+mod = function "mod"
53
53
54
54
55-- | Round a 'DFractional' to a 'DBIntegral' by rounding to the nearest smaller
55-- | Round a 'DFractional' to a 'DBIntegral' by rounding to the nearest smaller
@@ -58,7 +58,7 @@ mod = function "MOD"
58-- Corresponds to the @floor()@ function.
58-- Corresponds to the @floor()@ function.
59floor :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
59floor :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
60 => Expr a -> Expr b
60 => Expr a -> Expr b
61−floor = function "FLOOR"
61+floor = function "floor"
62
62
63
63
64-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
64-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
@@ -67,11 +67,11 @@ floor = function "FLOOR"
67-- Corresponds to the @round()@ function.
67-- Corresponds to the @round()@ function.
68round :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
68round :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
69 => Expr a -> Expr b
69 => Expr a -> Expr b
70−round = function "ROUND"
70+round = function "round"
71
71
72
72
73-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
73-- | Round a 'DBFractional' to a 'DBIntegral' by rounding to the nearest
74-- integer towards zero.
74-- integer towards zero.
75truncate :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
75truncate :: (Sql DBFractional a, Sql DBIntegral b, Homonullable a b)
76 => Expr a -> Expr b
76 => Expr a -> Expr b
77−truncate = function "TRUNC"
77+truncate = function "trunc"
src/Rel8/Type/Num.hs+7−1
@@ -7,7 +7,7 @@
7{-# language UndecidableInstances #-}
7{-# language UndecidableInstances #-}
8
8
9module Rel8.Type.Num
9module Rel8.Type.Num
10− ( DBNum, DBIntegral, DBFractional
10+ ( DBNum, DBIntegral, DBFractional, DBFloating
11 )
11 )
12where
12where
13
13
@@ -50,3 +50,9 @@ class DBNum a => DBFractional a
50instance DBFractional Float
50instance DBFractional Float
51instance DBFractional Double
51instance DBFractional Double
52instance DBFractional Scientific
52instance 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