hsmt-0.1.0.0: test/HSMT/RecordSpec.hs
module HSMT.RecordSpec (tests) where
import Data.Text (Text)
import qualified Data.Text as T
import Test.Tasty
import Test.Tasty.HUnit
import HSMT.SMT.Engine
import HSMT.SMTLib.Run
tests :: TestTree
tests =
testGroup
"Records"
[ testCase "selection and construction" $ do
let base = [point, "(declare-const p P)", "(declare-const x Int)"]
expect (base <> ["(assert (not (= (px (mkP x true)) x)))"]) ["unsat"]
expect (base <> ["(assert (= p (mkP x true)))", "(assert (not (py p)))"]) ["unsat"]
expect (base <> ["(assert (= (mkP x true) (mkP 3 true)))", "(check-sat)", "(assert (distinct x 3))"]) ["sat", "unsat"]
, testCase "extensionality" $ do
let base = [point, "(declare-const p P)", "(declare-const q P)"]
-- Records with equal fields are equal.
expect (base <> ["(assert (= (px p) (px q)))", "(assert (= (py p) (py q)))", "(assert (distinct p q))"]) ["unsat"]
expect (base <> ["(assert (distinct p (mkP (px p) (py p))))"]) ["unsat"]
expect (base <> ["(assert (= (px p) (px q)))", "(assert (distinct p q))", "(check-sat)", "(assert (py p))", "(assert (py q))"]) ["sat", "unsat"]
, testCase "finite record sorts" $ do
let pair = "(declare-datatypes ((B 0)) (((mkB (b1 Bool) (b2 (_ BitVec 1))))))"
consts n = ["(declare-const x" <> tshow i <> " B)" | i <- [1 .. n]]
names n = T.unwords ["x" <> tshow i | i <- [1 .. n :: Int]]
expect (pair : consts 4 <> ["(assert (distinct " <> names 4 <> "))"]) ["sat"]
expect (pair : consts 5 <> ["(assert (distinct " <> names 5 <> "))"]) ["unsat"]
-- The empty record has a single value.
expect
["(declare-datatypes ((E 0)) (((mkE))))", "(declare-const e E)", "(declare-const f E)", "(assert (distinct e f))"]
["unsat"]
, testCase "nested records" $ do
let base =
[ point
, "(declare-datatypes ((S 0)) (((mkS (pos P) (tag Int)))))"
, "(declare-const s S)"
, "(declare-const t S)"
]
expect (base <> ["(assert (= (pos s) (pos t)))", "(assert (= (tag s) (tag t)))", "(assert (distinct s t))"]) ["unsat"]
expect (base <> ["(assert (= s t))", "(assert (distinct (px (pos s)) (px (pos t))))"]) ["unsat"]
expect (base <> ["(assert (= (px (pos s)) (+ (tag s) 1)))", "(assert (> (tag s) (px (pos s))))"]) ["unsat"]
, testCase "uninterpreted functions over records" $ do
let base = [point, "(declare-const p P)", "(declare-const q P)", "(declare-fun f (P) Int)", "(declare-fun g (Int) P)"]
expect (base <> ["(assert (= (px p) (px q)))", "(assert (= (py p) (py q)))", "(assert (distinct (f p) (f q)))"]) ["unsat"]
expect (base <> ["(assert (distinct (f p) (f q)))", "(assert (= (px p) (px q)))"]) ["sat"]
expect (base <> ["(assert (= (g 1) (g 2)))", "(assert (distinct (px (g 1)) (px (g 2))))"]) ["unsat"]
, testCase "arrays of records" $ do
let base = [point, "(declare-const a (Array Int P))", "(declare-const i Int)", "(declare-const j Int)"]
expect
(base <> ["(assert (not (= (px (select (store a i (mkP 5 false)) i)) 5)))"])
["unsat"]
expect
(base <> ["(assert (distinct (px (select a i)) (px (select a j))))", "(check-sat)", "(assert (= i j))"])
["sat", "unsat"]
, testCase "arrays indexed by records" $ do
let pair = "(declare-datatypes ((B 0)) (((mkB (b1 Bool) (b2 (_ BitVec 1))))))"
k v = "((as const (Array B Int)) " <> v <> ")"
full = foldl (\acc ix -> "(store " <> acc <> " " <> ix <> " 1)") (k "0") ["(mkB " <> b <> " " <> w <> ")" | b <- ["true", "false"], w <- ["#b0", "#b1"]]
-- Overwriting every index changes the default.
expect [pair, "(assert (= " <> full <> " " <> k "1" <> "))"] ["sat"]
expect
[ pair
, "(declare-const a (Array B Int))"
, "(declare-const i B)"
, "(assert (= (select a i) 5))"
, "(assert (= (select (store a (mkB true #b0) 2) i) 2))"
, "(check-sat)"
, "(assert (not (b1 i)))"
]
["sat", "unsat"]
-- An infinite record index sort.
expect
[ point
, "(declare-const a (Array P Int))"
, "(declare-const p P)"
, "(declare-const x Int)"
, "(assert (distinct (select (store a (mkP x true) 1) p) (select a p)))"
, "(check-sat)"
, "(assert (distinct (px p) x))"
]
["sat", "unsat"]
]
where
point = "(declare-datatypes ((P 0)) (((mkP (px Int) (py Bool)))))"
tshow :: Int -> Text
tshow = T.pack . show
-- | Run a script (with model validation) and compare its answers; a final
-- @check-sat@ is appended.
expect :: [Text] -> [Text] -> Assertion
expect cmds answers =
runScriptText True (T.unlines (cmds <> ["(check-sat)"])) >>= \case
Left err -> assertFailure (T.unpack err)
Right rs -> map answer rs @?= answers
where
answer = \case
ESat _ -> "sat"
EUnsat _ -> "unsat"
EUnknown why -> "unknown: " <> why