hsmt-0.1.0.0: test/HSMT/SATSpec.hs
module HSMT.SATSpec (tests) where
import Control.Monad
import Data.IORef
import Data.List (nub)
import Test.QuickCheck.Monadic (assert, monadicIO, run)
import Test.Tasty
import Test.Tasty.HUnit hiding (assert)
import Test.Tasty.QuickCheck
import HSMT.SAT.Dimacs
import HSMT.SAT.Solver
import HSMT.SAT.Types
-- | A small random CNF in DIMACS numbering.
data SmallCnf = SmallCnf Int [[Int]]
deriving (Show)
instance Arbitrary SmallCnf where
arbitrary = do
n <- chooseInt (1, 8)
m <- chooseInt (0, 35)
cs <- replicateM m $ do
k <- chooseInt (1, 4)
replicateM k $ do
v <- chooseInt (1, n)
s <- arbitrary
pure (if s then v else negate v)
pure (SmallCnf n cs)
shrink (SmallCnf n cs) = [SmallCnf n cs' | cs' <- shrinkList (shrinkList (const [])) cs]
assignments :: Int -> [[Bool]]
assignments n = replicateM n [False, True]
satisfiesClause :: [Bool] -> [Int] -> Bool
satisfiesClause asg = any (\l -> (asg !! (abs l - 1)) == (l > 0))
bruteForce :: Int -> [[Int]] -> Maybe [Bool]
bruteForce n cs = case filter (\a -> all (satisfiesClause a) cs) (assignments n) of
(a : _) -> Just a
[] -> Nothing
loadSolver :: Int -> [[Int]] -> IO Solver
loadSolver n cs = do
s <- newSolver
_ <- loadCnf s (Cnf n cs)
pure s
prop_agreesWithBruteForce :: SmallCnf -> Property
prop_agreesWithBruteForce (SmallCnf n cs) = monadicIO $ do
s <- run (loadSolver n cs)
r <- run (solve s)
model <- run (getModel s)
case bruteForce n cs of
Nothing -> assert (r == Unsat)
Just _ -> do
assert (r == Sat)
assert (all (satisfiesClause (take n model)) cs)
prop_assumptions :: SmallCnf -> Property
prop_assumptions (SmallCnf n cs) = forAll (assumptionsFor n) $ \as -> monadicIO $ do
s <- run (loadSolver n cs)
r <- run (solveWith s defaultSolveOptions (map toLit as))
let withUnits = cs ++ [[a] | a <- as]
case bruteForce n withUnits of
Just _ -> do
assert (r == Sat)
model <- run (getModel s)
assert (all (satisfiesClause (take n model)) withUnits)
Nothing -> do
assert (r == Unsat)
failed <- run (failedAssumptions s)
let failedInts = map fromLit failed
-- The failed assumptions are assumptions, and are enough for unsat.
assert (all (`elem` as) failedInts)
assert (bruteForce n (cs ++ [[a] | a <- failedInts]) == Nothing)
-- The solver stays usable after an Unsat under assumptions.
r' <- run (solve s)
assert (r' == if bruteForce n cs == Nothing then Unsat else Sat)
where
assumptionsFor k = do
m <- chooseInt (0, k)
nub <$> replicateM m (do v <- chooseInt (1, k); b <- arbitrary; pure (if b then v else negate v))
toLit :: Int -> Lit
toLit l = mkLit (abs l - 1) (l < 0)
fromLit :: Lit -> Int
fromLit l = if litSign l then negate (litVar l + 1) else litVar l + 1
-- | Pigeonhole principle: n+1 pigeons in n holes is unsatisfiable.
pigeonhole :: Int -> Cnf
pigeonhole n =
let var p h = p * n + h + 1
atLeastOne = [[var p h | h <- [0 .. n - 1]] | p <- [0 .. n]]
atMostOne =
[[negate (var p h), negate (var q h)] | h <- [0 .. n - 1], p <- [0 .. n], q <- [p + 1 .. n]]
in Cnf ((n + 1) * n) (atLeastOne ++ atMostOne)
-- | A toy theory: at most one variable of a given set is true. It either
-- only reports conflicts or also propagates through lemmas.
amoTheory :: Solver -> [Var] -> Bool -> IO TheoryHooks
amoTheory s vars propagating = do
st <- newIORef [[]]
let check = do
trues <- concat <$> readIORef st
case trues of
(a : b : _) -> pure (TheoryConflict [a, b])
[t] | propagating -> do
lemmas <- fmap concat . forM vars $ \v -> do
val <- litValueNow s (posLit v)
pure [[negLit v, litNeg t] | v /= litVar t, val == LUndef]
pure (if null lemmas then TheoryOk else TheoryLemmas (map removable lemmas))
_ -> pure TheoryOk
pure
TheoryHooks
{ thNewLevel = modifyIORef' st ([] :)
, thBacktrack = \lvl -> modifyIORef' st (\xs -> drop (length xs - (lvl + 1)) xs)
, thAssert = \l -> when (not (litSign l) && litVar l `elem` vars) $
modifyIORef' st (\case (x : xs) -> (l : x) : xs; [] -> [[l]])
, thPropagate = check
, thFinalCheck = check
}
prop_theory :: Bool -> SmallCnf -> Property
prop_theory propagating (SmallCnf n cs) = forAll (sublistOf [1 .. n]) $ \amo -> monadicIO $ do
s <- run (loadSolver n cs)
run (amoTheory s (map (subtract 1) amo) propagating >>= setTheory s)
r <- run (solve s)
let amoClauses = [[negate a, negate b] | a <- amo, b <- amo, a < b]
case bruteForce n (cs ++ amoClauses) of
Nothing -> assert (r == Unsat)
Just _ -> do
assert (r == Sat)
model <- run (getModel s)
assert (all (satisfiesClause (take n model)) (cs ++ amoClauses))
tests :: TestTree
tests =
testGroup
"SAT"
[ testProperty "agrees with brute force" (withMaxSuccess 3000 prop_agreesWithBruteForce)
, testProperty "assumptions and failed assumptions" (withMaxSuccess 2000 prop_assumptions)
, testProperty "theory conflicts" (withMaxSuccess 1500 (prop_theory False))
, testProperty "theory propagation lemmas" (withMaxSuccess 1500 (prop_theory True))
, testCase "pigeonhole 7 is unsat" $ do
s <- newSolver
ok <- loadCnf s (pigeonhole 7)
r <- if ok then solve s else pure Unsat
r @?= Unsat
, testCase "conflict limit gives Unknown" $ do
s <- newSolver
_ <- loadCnf s (pigeonhole 9)
r <- solveWith s defaultSolveOptions {optConflictLimit = Just 10} []
r @?= Unknown
, testCase "incremental clause addition" $ do
s <- loadSolver 3 [[1, 2, 3]]
(@?= Sat) =<< solve s
forM_ [[-1], [-2]] (addClause s . map toLit)
(@?= Sat) =<< solve s
(@?= True) =<< modelValue s 2
_ <- addClause s [toLit (-3)]
(@?= Unsat) =<< solve s
, testCase "DIMACS parsing" $
parseDimacs "c comment\np cnf 3 2\n1 -2 0\n2 3\n0\n"
@?= Right (Cnf 3 [[1, -2], [2, 3]])
]