hsmt-0.1.0.0: test/HSMT/IncrementalSpec.hs
{-# LANGUAGE RankNTypes #-}
-- | Incremental solving: unsatisfiable cores, assumptions, resource limits
-- and sequences of checks over one session.
module HSMT.IncrementalSpec (tests, testsWith) where
import Control.Monad (forM)
import Data.List (sort)
import qualified Data.Text as T
import GHC.Clock (getMonotonicTime)
import Test.Tasty
import Test.Tasty.HUnit hiding (assert)
import HSMT
import HSMT.SMT.Engine (EngineResult (..))
import HSMT.SMTLib.Run
answer :: Result ctx -> String
answer = \case
Sat _ -> "sat"
Unsat _ -> "unsat"
Unknown why -> "unknown: " <> T.unpack why
-- | Pigeonhole: n + 1 pigeons in n holes, as Boolean constraints.
pigeonhole :: Int -> SMT ctx ()
pigeonhole n = do
ps <- forM [0 .. n] $ \i -> forM [1 .. n] $ \j -> declare @'BoolS (T.pack ("p" <> show i <> "_" <> show j))
mapM_ (assert . orB) ps
sequence_
[ assert (notB (ps !! i !! j) .|| notB (ps !! k !! j))
| j <- [0 .. n - 1]
, i <- [0 .. n]
, k <- [i + 1 .. n]
]
tests :: TestTree
tests = testsWith defaultConfig
-- | The tests with the given configuration (and its backend).
testsWith :: Config -> TestTree
testsWith base =
testGroup
"incremental solving"
[ testGroup
"unsatisfiable cores"
[ testCase "a core is unsatisfiable on its own" $ do
core <- runSMT base $ do
p <- declare @'BoolS "p"
q <- declare @'BoolS "q"
r <- declare @'BoolS "r"
let as = [p, r .|| q, notB p .|| notB q, r .=> p, notB r]
mapM_ assert as
c <- checkSat >>= liftCore
pure (map (`elem` as) (coreAssertions c), length (coreAssertions c))
-- Every element is an assertion, and the five assertions are
-- needed except possibly r => p.
all id (fst core) @? "core elements are assertions"
assertBool "core size" (snd core >= 4)
, testCase "minimal cores drop irrelevant assertions" $ do
same <- runSMT (base {cfgMinimizeCores = True}) $ do
x <- declare @'IntS "x"
y <- declare @'IntS "y"
z <- declare @'IntS "z"
let relevant = [x .> 5, x + y .< 3, y .== 0]
mapM_ assert [z .== 1, x .> 5, y .< 100, x + y .< 3, y .== 0, z .> 0]
c <- checkSat >>= liftCore
pure (sort (coreAssertions c) == sort relevant)
same @?= True
, testCase "minimal cores in every theory" $ do
rs <- runSMT (base {cfgMinimizeCores = True}) $ do
f <- declareFun @'[ 'IntS] @'IntS "f"
a <- declare @('ArrayS ('BVS 4) 'BoolS) "a"
b <- declare @('BVS 8) "b"
s <- declare @'StringS "s"
x <- declare @'IntS "x"
p <- declare @'BoolS "p"
q <- declare @'BoolS "q"
-- Satisfiable together, and with any one theory group.
let filler = [x .> 0, p, b `bvULt` bv 200, strLen s .< 10, select a (bv 3)]
groups =
[ [f x .== 3, x .== 3, f 3 .> 3]
, [select a (bv 1) .== select a (bv 2), notB (select a (bv 2)), select a (bv 1)]
, [b `bvULt` bv 10, b `bvUGt` bv 9]
, [strLen s .== x, x .== 3, prefixOf "abcd" s]
, [q, q .=> contains s "z", s .== "abc"]
]
forM groups $ \g -> scoped $ do
mapM_ assert (filler <> g)
c <- checkSat >>= liftCore
pure (sort (coreAssertions c) == sort g)
rs @?= replicate 5 True
, testCase "unsatisfiable assertions without core minimization" $ do
r <- runSMT base $ do
assert (bool False)
map show . coreAssertions <$> (checkSat >>= liftCore)
r @?= ["App (BoolConst False) []"]
]
, testGroup
"assumptions"
[ testCase "assumptions hold for one check" $ do
r <- runSMT base $ do
x <- declare @'IntS "x"
assert (x .> 0)
c <- checkSatAssuming [x .< 0, x .== 7] >>= liftCore
later <- checkSat
pure (coreAssertions c == [x .> 0], coreAssumptions c == [x .< 0], answer later)
r @?= (True, True, "sat")
, testCase "tracking literals" $ do
r <- runSMT (base {cfgMinimizeCores = True}) $ do
x <- declare @'IntS "x"
ts <- forM [1 .. 4 :: Int] $ \i -> declare @'BoolS (T.pack ("t" <> show i))
let constraints = [x .> 10, x .< 20, x .== 5, x .> 0]
sequence_ [assert (t .=> c) | (t, c) <- zip ts constraints]
c <- checkSatAssuming ts >>= liftCore
s <- checkSatAssuming [ts !! 0, ts !! 1, ts !! 3]
pure (map (`elemIndex'` ts) (coreAssumptions c), answer s)
r @?= ([Just 0, Just 2], "sat")
, testCase "an assumption that is an assertion" $ do
r <- runSMT base $ do
p <- declare @'BoolS "p"
assert p
c <- checkSatAssuming [notB p, p] >>= liftCore
pure (length (coreAssertions c) + length (coreAssumptions c) >= 2)
r @?= True
]
, testGroup
"resource limits"
[ testCase "a timeout answers unknown and the session goes on" $ do
start <- getMonotonicTime
(r1, r2) <- runSMT base {cfgTimeout = Just 200} $ do
p <- declare @'BoolS "p"
r2 <- scoped (pigeonhole 11 >> checkSat)
assert p
r1 <- checkSat
pure (answer r1, answer r2)
elapsed <- subtract start <$> getMonotonicTime
(r1, r2) @?= ("sat", "unknown: timeout")
assertBool ("took " <> show elapsed <> " s") (elapsed < 3)
, testCase "a conflict limit answers unknown" $ do
r <- runSMT base {cfgConflictLimit = Just 5} $ do
pigeonhole 6
answer <$> checkSat
r @?= "unknown: resource limit reached"
, testCase "limits leave easy checks alone" $ do
r <- runSMT base {cfgConflictLimit = Just 1000, cfgTimeout = Just 5000} $ do
pigeonhole 4
answer <$> checkSat
r @?= "unsat"
]
, testGroup
"sessions"
[ testCase "many checks over one session" $ do
rs <- runSMT base $ do
x <- declare @'IntS "x"
f <- declareFun @'[ 'IntS] @'IntS "f"
assert (f x .> x)
vs <- forM [1 .. 40 :: Integer] $ \i -> scoped $ do
assert (x .== fromInteger i)
checkSat >>= \case
Sat m -> pure (eval m (f x) > i)
_ -> pure False
last' <- scoped (assert (f x .< x) >> answer <$> checkSat)
pure (and vs, last')
rs @?= (True, "unsat")
, testCase "theories that appear later" $ do
rs <- runSMT base $ do
p <- declare @'BoolS "p"
assert p
r1 <- checkSat
x <- declare @'IntS "x"
assert (p .=> x .> 3)
r2 <- checkSat
s <- declare @'StringS "s"
assert (strLen s .== x)
assert (x .< 5)
r3 <-
checkSat >>= \case
Sat m -> pure (T.length (eval m s) == 4)
_ -> pure False
f <- declareFun @'[ 'StringS] @'BoolS "f"
assert (f s .&& notB (f (s <+> "")))
r4 <- checkSat
pure (answer r1, answer r2, r3, answer r4)
rs @?= ("sat", "sat", True, "unsat")
, testCase "longer strings in later checks" $ do
rs <- runSMT base $ do
s <- declare @'StringS "s"
r1 <- scoped (assert (strLen s .== 3) >> answer <$> checkSat)
r2 <- scoped (assert (strLen s .== 20) >> answer <$> checkSat)
r3 <- scoped (assert (strLen s .== 2 .&& prefixOf "abc" s) >> answer <$> checkSat)
pure (r1, r2, r3)
rs @?= ("sat", "sat", "unsat")
]
, testCase "SMT-LIB: named assertions, assumptions and cores" $ do
let script =
T.unlines
[ "(set-option :smt.core.minimize true)"
, "(declare-const x Int)"
, "(declare-const p Bool)"
, "(assert (! (> x 3) :named a1))"
, "(assert (! p :named a2))"
, "(assert (< x 100))"
, "(check-sat-assuming ((< x 2) (and p (= x 5))))"
, "(get-unsat-core)"
, "(check-sat)"
, "(get-unsat-core)"
, "(assert (not a2))"
, "(check-sat)"
, "(get-unsat-core)"
]
runScriptResponsesWith (cfgBackend base) True script >>= \case
[ CheckResult (EUnsat _)
, UnsatCore core1
, CheckResult (ESat _)
, CommandError _
, CheckResult (EUnsat _)
, UnsatCore core2
] -> do
-- Both are minimal; the engine finds the first.
assertBool ("core: " <> show core1) $
core1 `elem` [["a1", "(< x 2)"], ["(< x 2)", "(and p (= x 5))"]]
core2 @?= ["a2"]
other -> assertFailure (show other)
]
where
liftCore r = case r of
Unsat c -> pure c
_ -> error ("expected unsat, got " <> answer r)
elemIndex' t ts = lookup t (zip ts [0 :: Int ..])