{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.Misc.Floating where
import Data.SBV
assocPlus :: SFloat -> SFloat -> SFloat -> SBool
assocPlus :: SFloat -> SFloat -> SFloat -> SBool
assocPlus SFloat
x SFloat
y SFloat
z = SFloat
x SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+ (SFloat
y SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+ SFloat
z) SFloat -> SFloat -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== (SFloat
x SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+ SFloat
y) SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+ SFloat
z
assocPlusRegular :: IO ThmResult
assocPlusRegular :: IO ThmResult
assocPlusRegular = SymbolicT IO SBool -> IO ThmResult
forall a. Provable a => a -> IO ThmResult
prove (SymbolicT IO SBool -> IO ThmResult)
-> SymbolicT IO SBool -> IO ThmResult
forall a b. (a -> b) -> a -> b
$ do [SFloat
x, SFloat
y, SFloat
z] <- [String] -> Symbolic [SFloat]
sFloats [String
"x", String
"y", String
"z"]
let lhs :: SFloat
lhs = SFloat
xSFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+(SFloat
ySFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+SFloat
z)
rhs :: SFloat
rhs = (SFloat
xSFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+SFloat
y)SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+SFloat
z
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloat
lhs
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloat
rhs
SBool -> SymbolicT IO SBool
forall (m :: * -> *) a. Monad m => a -> m a
return (SBool -> SymbolicT IO SBool) -> SBool -> SymbolicT IO SBool
forall a b. (a -> b) -> a -> b
$ SFloat
lhs SFloat -> SFloat -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== SFloat
rhs
nonZeroAddition :: IO ThmResult
nonZeroAddition :: IO ThmResult
nonZeroAddition = SymbolicT IO SBool -> IO ThmResult
forall a. Provable a => a -> IO ThmResult
prove (SymbolicT IO SBool -> IO ThmResult)
-> SymbolicT IO SBool -> IO ThmResult
forall a b. (a -> b) -> a -> b
$ do [SFloat
a, SFloat
b] <- [String] -> Symbolic [SFloat]
sFloats [String
"a", String
"b"]
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloat
a
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloat
b
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat
a SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+ SFloat
b SFloat -> SFloat -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== SFloat
a
SBool -> SymbolicT IO SBool
forall (m :: * -> *) a. Monad m => a -> m a
return (SBool -> SymbolicT IO SBool) -> SBool -> SymbolicT IO SBool
forall a b. (a -> b) -> a -> b
$ SFloat
b SFloat -> SFloat -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== SFloat
0
multInverse :: IO ThmResult
multInverse :: IO ThmResult
multInverse = SymbolicT IO SBool -> IO ThmResult
forall a. Provable a => a -> IO ThmResult
prove (SymbolicT IO SBool -> IO ThmResult)
-> SymbolicT IO SBool -> IO ThmResult
forall a b. (a -> b) -> a -> b
$ do SFloat
a <- String -> Symbolic SFloat
sFloat String
"a"
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloat
a
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint (SFloat
1SFloat -> SFloat -> SFloat
forall a. Fractional a => a -> a -> a
/SFloat
a)
SBool -> SymbolicT IO SBool
forall (m :: * -> *) a. Monad m => a -> m a
return (SBool -> SymbolicT IO SBool) -> SBool -> SymbolicT IO SBool
forall a b. (a -> b) -> a -> b
$ SFloat
a SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
* (SFloat
1SFloat -> SFloat -> SFloat
forall a. Fractional a => a -> a -> a
/SFloat
a) SFloat -> SFloat -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== SFloat
1
roundingAdd :: IO SatResult
roundingAdd :: IO SatResult
roundingAdd = SymbolicT IO SBool -> IO SatResult
forall a. Provable a => a -> IO SatResult
sat (SymbolicT IO SBool -> IO SatResult)
-> SymbolicT IO SBool -> IO SatResult
forall a b. (a -> b) -> a -> b
$ do SRoundingMode
m :: SRoundingMode <- String -> Symbolic SRoundingMode
forall a. SymVal a => String -> Symbolic (SBV a)
free String
"rm"
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SRoundingMode
m SRoundingMode -> SRoundingMode -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= RoundingMode -> SRoundingMode
forall a. SymVal a => a -> SBV a
literal RoundingMode
RoundNearestTiesToEven
SFloat
x <- String -> Symbolic SFloat
sFloat String
"x"
SFloat
y <- String -> Symbolic SFloat
sFloat String
"y"
let lhs :: SFloat
lhs = SRoundingMode -> SFloat -> SFloat -> SFloat
forall a.
IEEEFloating a =>
SRoundingMode -> SBV a -> SBV a -> SBV a
fpAdd SRoundingMode
m SFloat
x SFloat
y
let rhs :: SFloat
rhs = SFloat
x SFloat -> SFloat -> SFloat
forall a. Num a => a -> a -> a
+ SFloat
y
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloat
lhs
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloat -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloat
rhs
SBool -> SymbolicT IO SBool
forall (m :: * -> *) a. Monad m => a -> m a
return (SBool -> SymbolicT IO SBool) -> SBool -> SymbolicT IO SBool
forall a b. (a -> b) -> a -> b
$ SFloat
lhs SFloat -> SFloat -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= SFloat
rhs
fp54Bounds :: IO OptimizeResult
fp54Bounds :: IO OptimizeResult
fp54Bounds = OptimizeStyle -> SymbolicT IO SBool -> IO OptimizeResult
forall a. Provable a => OptimizeStyle -> a -> IO OptimizeResult
optimize OptimizeStyle
Independent (SymbolicT IO SBool -> IO OptimizeResult)
-> SymbolicT IO SBool -> IO OptimizeResult
forall a b. (a -> b) -> a -> b
$ do SFloatingPoint 5 4
x :: SFloatingPoint 5 4 <- String -> Symbolic (SFloatingPoint 5 4)
forall (eb :: Nat) (sb :: Nat).
ValidFloat eb sb =>
String -> Symbolic (SFloatingPoint eb sb)
sFloatingPoint String
"x"
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloatingPoint 5 4 -> SBool
forall a. IEEEFloating a => SBV a -> SBool
fpIsPoint SFloatingPoint 5 4
x
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SFloatingPoint 5 4
x SFloatingPoint 5 4 -> SFloatingPoint 5 4 -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.> SFloatingPoint 5 4
0
String -> SFloatingPoint 5 4 -> SymbolicT IO ()
forall a. Metric a => String -> SBV a -> SymbolicT IO ()
maximize String
"max" SFloatingPoint 5 4
x
String -> SFloatingPoint 5 4 -> SymbolicT IO ()
forall a. Metric a => String -> SBV a -> SymbolicT IO ()
minimize String
"min" SFloatingPoint 5 4
x
SBool -> SymbolicT IO SBool
forall (f :: * -> *) a. Applicative f => a -> f a
pure SBool
sTrue