hsmt-0.1.0.0: test/HSMT/LIASpec.hs
module HSMT.LIASpec (tests) where
import Control.Monad (forM_)
import qualified Data.Map.Strict as Map
import Data.Maybe (isJust)
import Data.Text (Text)
import Test.QuickCheck
import Test.QuickCheck.Monadic (monadicIO, run)
import qualified Test.QuickCheck.Monadic as QC
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck (testProperty)
import HSMT.Core.Eval
import HSMT.Core.Expr
import HSMT.Core.Op
import HSMT.Core.Sort
import HSMT.Core.Value
import HSMT.SMT.Engine
import qualified HSMT.Theory.Arith.Simplex as Sx
tests :: TestTree
tests =
testGroup
"LIA"
[ testCase "simplex finds a feasible point" $ do
sx <- Sx.newSimplex
u <- Sx.newVar sx
w <- Sx.newVar sx
s <- Sx.addRow sx [(1, u), (1, w)]
d <- Sx.addRow sx [(1, u), (-1, w)]
_ <- Sx.assertLower sx s 10 []
_ <- Sx.assertUpper sx d 2 []
_ <- Sx.assertLower sx d 2 []
r <- Sx.check sx
isJust r @?= False
vu <- Sx.value sx u
vw <- Sx.value sx w
assertBool "u + w >= 10" (vu + vw >= 10)
vu - vw @?= 2
, testCase "simplex detects infeasibility and backtracks" $ do
sx <- Sx.newSimplex
u <- Sx.newVar sx
w <- Sx.newVar sx
s <- Sx.addRow sx [(1, u), (1, w)]
_ <- Sx.assertUpper sx u 1 []
Sx.push sx
_ <- Sx.assertUpper sx w 1 []
_ <- Sx.assertLower sx s 3 []
r1 <- Sx.check sx
isJust r1 @?= True
Sx.pop sx 0
_ <- Sx.assertLower sx s 3 []
r2 <- Sx.check sx
isJust r2 @?= False
, testCase "integrality on unbounded problems" $ do
-- 3x + 3y = 2 has rational solutions only.
r1 <- solve [eq (add [mul 3 x, mul 3 y]) (int 2)]
r1 @?= "unsat"
r2 <- solve [le (add [int 1, mul 3 x, mul 2 y]) (int 0), lt (int 3) x]
r2 @?= "sat"
, testCase "division and remainder by literals" $
forM_ [(-7, 2), (7, -2), (-7, -2), (7, 2), (0, 5)] $ \(a, b) -> do
let q = App IntDiv [x, int b]
r = App IntMod [x, int b]
(q0, r0) = euclid a b
res <- solve [eq x (int a), eq q (int q0), eq r (int r0)]
res @?= "sat"
res' <- solve [eq x (int a), App Not [eq q (int q0)]]
res' @?= "unsat"
, testCase "division by zero is uninterpreted" $ do
let d t = App IntDiv [t, int 0]
r1 <- solve [eq (d x) (int 1), eq (d y) (int 2)]
r1 @?= "sat"
r2 <- solve [eq x y, eq (d x) (int 1), eq (d y) (int 2)]
r2 @?= "unsat"
, testCase "congruence meets arithmetic" $ do
r1 <- solve [le x y, le y x, App Not [eq (f x) (f y)]]
r1 @?= "unsat"
r2 <- solve [eq (add [x, int 1]) y, eq (f (add [y, int (-1)])) (int 3), eq (f x) (int 4)]
r2 @?= "unsat"
r3 <- solve [lt x y, eq (f x) (f y)]
r3 @?= "sat"
r4 <- solve [le (int 1) x, le x (int 2), distinct [f x, f (int 1), f (int 2)]]
r4 @?= "unsat"
, testProperty "agrees with brute force on bounded problems" prop_bruteForce
]
where
x = var "x"
y = var "y"
f t = App (Uninterpreted (Symbol "f" [IntSort] IntSort)) [t]
euclid a b = let r = a `mod` abs b in ((a - r) `div` b, r)
var :: Text -> Expr
var n = constant (Uninterpreted (Symbol n [] IntSort))
int :: Integer -> Expr
int = constant . IntConst
add :: [Expr] -> Expr
add = App Add
mul :: Integer -> Expr -> Expr
mul k e = App Mul [int k, e]
eq, le, lt :: Expr -> Expr -> Expr
eq a b = App Eq [a, b]
le a b = App Le [a, b]
lt a b = App Lt [a, b]
distinct :: [Expr] -> Expr
distinct = App Distinct
solve :: [Expr] -> IO String
solve as =
solveAssertions True as >>= \case
ESat _ -> pure "sat"
EUnsat _ -> pure "unsat"
EUnknown why -> pure ("unknown: " <> show why)
-- | Random linear constraints over three variables restricted to [-3, 3],
-- decided by enumeration.
prop_bruteForce :: Property
prop_bruteForce = forAll genProblem $ \cs -> monadicIO $ do
let names = ["x", "y", "z"]
bounds = concat [[le (int (-3)) (var n), le (var n) (int 3)] | n <- names]
problem = bounds <> cs
models =
[ Map.fromList [(Symbol n [] IntSort, constInterp (VInt v)) | (n, v) <- zip names vs]
| vs <- sequence (replicate 3 [-3 .. 3])
]
holds m = all (\c -> evalExpr emptyInterpretation {interpSymbols = m} c == Right (VBool True)) problem
expected = if any holds models then "sat" else "unsat"
r <- run (solve problem)
QC.monitor (cover 20 (expected == "sat") "sat" . cover 20 (expected == "unsat") "unsat")
QC.monitor (counterexample (show cs <> "\nhsmt: " <> r <> ", expected: " <> expected))
QC.assert (r == expected)
where
genProblem = do
n <- chooseInt (1, 5)
vectorOf n clause
clause = do
k <- frequency [(3, pure 1), (1, pure 2)]
ls <- vectorOf k literal
pure (case ls of [l] -> l; _ -> App Or ls)
literal = do
a <- atom
frequency [(3, pure a), (1, pure (App Not [a]))]
atom = do
lhs <- linear
rhs <- linear
elements [le lhs rhs, lt lhs rhs, eq lhs rhs]
linear = do
terms <- sublistOf ["x", "y", "z"]
coeffs <- vectorOf (length terms) (elements [-3, -2, -1, 1, 2, 3])
c <- chooseInteger (-4, 4)
pure $ case [mul k (var n) | (k, n) <- zip coeffs terms] of
[] -> int c
ts -> add (int c : ts)