如何在SBV中摆脱-0.0的解决方案

时间:2015-03-26 16:01:29

标签: haskell smt

以下代码(使用SBV):

{-# LANGUAGE ScopedTypeVariables #-}
import Data.SBV
main :: IO ()
main = do
    res <- allSat zeros
    putStrLn $ show res

zeros :: Predicate
zeros = do
    z1 <- sDouble "Z1"
    constrain $ z1 .== 0.0
    return true

生成两个解决方案:

Solution #1:
  Z1 = 0.0 :: SDouble
Solution #2:
  Z1 = -0.0 :: SDouble
Found 2 different solutions.

如何消除无趣的-0.0解决方案?我无法使用z1 ./= -0.0,因为z10.0时也是如此。

2 个答案:

答案 0 :(得分:3)

[在Hackage(http://hackage.haskell.org/package/sbv)上发布SBV 4.4之后更新,提供了适当的支持。]

假设你有SBV&gt; = 4.4,你现在可以:

Prelude Data.SBV> allSat $ \z -> z .== (0::SDouble)
Solution #1:
  s0 = 0.0 :: Double
Solution #2:
  s0 = -0.0 :: Double
Found 2 different solutions.
Prelude Data.SBV> allSat $ \z -> z .== (0::SDouble) &&& bnot (isNegativeZeroFP z)
Solution #1:
  s0 = 0.0 :: Double
This is the only solution.

答案 1 :(得分:1)

根据评论,以下代码只生成一个解决方案:

zeros :: Predicate
zeros = do
    z1 <- sDouble "Z1"
    constrain $ z1 .== 0.0 &&& ((1 / z1) .> 0)
    return true

输出:

Solution #1:
  Z1 = 0.0 :: SDouble
This is the only solution.