{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Data.SBV.Provers.Prover (
SMTSolver(..), SMTConfig(..), Predicate, Provable(..), Goal
, ThmResult(..), SatResult(..), AllSatResult(..), SafeResult(..), OptimizeResult(..), SMTResult(..)
, isSatisfiable, isSatisfiableWith, isTheorem, isTheoremWith
, prove, proveWith
, sat, satWith
, allSat, allSatWith
, safe, safeWith, isSafe
, optimize, optimizeWith
, isVacuous, isVacuousWith
, SatModel(..), Modelable(..), displayModels, extractModels
, getModelDictionaries, getModelValues, getModelUninterpretedValues
, boolector, cvc4, yices, z3, mathSAT, abc, defaultSMTCfg
, compileToSMTLib, generateSMTBenchmarks
, internalSATCheck
) where
import Data.Char (isSpace)
import Data.List (intercalate, nub)
import Control.Monad (when, unless)
import System.FilePath (addExtension, splitExtension)
import System.Time (getClockTime)
import System.IO (hGetBuffering, hSetBuffering, stdout, hFlush, BufferMode(..))
import System.IO.Unsafe (unsafeInterleaveIO)
import Control.Concurrent.Async (async, wait, cancel, waitAny, Async)
import GHC.Stack.Compat
#if !MIN_VERSION_base(4,9,0)
import GHC.SrcLoc.Compat
#endif
import qualified Data.Set as Set (toList)
import Data.SBV.Core.Data
import Data.SBV.Core.Symbolic
import Data.SBV.SMT.SMT
import Data.SBV.SMT.SMTLib
import Data.SBV.Utils.TDiff
import Control.DeepSeq (rnf)
import Control.Exception (bracket)
import qualified Data.SBV.Provers.Boolector as Boolector
import qualified Data.SBV.Provers.CVC4 as CVC4
import qualified Data.SBV.Provers.Yices as Yices
import qualified Data.SBV.Provers.Z3 as Z3
import qualified Data.SBV.Provers.MathSAT as MathSAT
import qualified Data.SBV.Provers.ABC as ABC
mkConfig :: SMTSolver -> SMTLibVersion -> [String] -> SMTConfig
mkConfig s smtVersion tweaks = SMTConfig { verbose = False
, timing = NoTiming
, sBranchTimeOut = Nothing
, timeOut = Nothing
, printBase = 10
, printRealPrec = 16
, smtFile = Nothing
, solver = s
, solverTweaks = tweaks
, smtLibVersion = smtVersion
, optimizeArgs = []
, satCmd = "(check-sat)"
, isNonModelVar = const False
, roundingMode = RoundNearestTiesToEven
, useLogic = Nothing
, getUnsatCore = False
}
boolector :: SMTConfig
boolector = mkConfig Boolector.boolector SMTLib2 []
cvc4 :: SMTConfig
cvc4 = mkConfig CVC4.cvc4 SMTLib2 []
yices :: SMTConfig
yices = mkConfig Yices.yices SMTLib2 []
z3 :: SMTConfig
z3 = mkConfig Z3.z3 SMTLib2 ["(set-option :smt.mbqi true) ; use model based quantifier instantiation"]
mathSAT :: SMTConfig
mathSAT = mkConfig MathSAT.mathSAT SMTLib2 []
abc :: SMTConfig
abc = mkConfig ABC.abc SMTLib2 []
defaultSMTCfg :: SMTConfig
defaultSMTCfg = z3
type Predicate = Symbolic SBool
type Goal = Symbolic ()
class Provable a where
forAll_ :: a -> Predicate
forAll :: [String] -> a -> Predicate
forSome_ :: a -> Predicate
forSome :: [String] -> a -> Predicate
instance Provable Predicate where
forAll_ = id
forAll [] = id
forAll xs = error $ "SBV.forAll: Extra unmapped name(s) in predicate construction: " ++ intercalate ", " xs
forSome_ = id
forSome [] = id
forSome xs = error $ "SBV.forSome: Extra unmapped name(s) in predicate construction: " ++ intercalate ", " xs
instance Provable SBool where
forAll_ = return
forAll _ = return
forSome_ = return
forSome _ = return
instance (SymWord a, Provable p) => Provable (SBV a -> p) where
forAll_ k = forall_ >>= \a -> forAll_ $ k a
forAll (s:ss) k = forall s >>= \a -> forAll ss $ k a
forAll [] k = forAll_ k
forSome_ k = exists_ >>= \a -> forSome_ $ k a
forSome (s:ss) k = exists s >>= \a -> forSome ss $ k a
forSome [] k = forSome_ k
instance (HasKind a, HasKind b, Provable p) => Provable (SArray a b -> p) where
forAll_ k = declNewSArray (\t -> "array_" ++ show t) Nothing >>= \a -> forAll_ $ k a
forAll (s:ss) k = declNewSArray (const s) Nothing >>= \a -> forAll ss $ k a
forAll [] k = forAll_ k
forSome_ _ = error "SBV.forSome: Existential arrays are not currently supported."
forSome _ _ = error "SBV.forSome: Existential arrays are not currently supported."
instance (HasKind a, HasKind b, Provable p) => Provable (SFunArray a b -> p) where
forAll_ k = declNewSFunArray Nothing >>= \a -> forAll_ $ k a
forAll (_:ss) k = declNewSFunArray Nothing >>= \a -> forAll ss $ k a
forAll [] k = forAll_ k
forSome_ _ = error "SBV.forSome: Existential arrays are not currently supported."
forSome _ _ = error "SBV.forSome: Existential arrays are not currently supported."
instance (SymWord a, SymWord b, Provable p) => Provable ((SBV a, SBV b) -> p) where
forAll_ k = forall_ >>= \a -> forAll_ $ \b -> k (a, b)
forAll (s:ss) k = forall s >>= \a -> forAll ss $ \b -> k (a, b)
forAll [] k = forAll_ k
forSome_ k = exists_ >>= \a -> forSome_ $ \b -> k (a, b)
forSome (s:ss) k = exists s >>= \a -> forSome ss $ \b -> k (a, b)
forSome [] k = forSome_ k
instance (SymWord a, SymWord b, SymWord c, Provable p) => Provable ((SBV a, SBV b, SBV c) -> p) where
forAll_ k = forall_ >>= \a -> forAll_ $ \b c -> k (a, b, c)
forAll (s:ss) k = forall s >>= \a -> forAll ss $ \b c -> k (a, b, c)
forAll [] k = forAll_ k
forSome_ k = exists_ >>= \a -> forSome_ $ \b c -> k (a, b, c)
forSome (s:ss) k = exists s >>= \a -> forSome ss $ \b c -> k (a, b, c)
forSome [] k = forSome_ k
instance (SymWord a, SymWord b, SymWord c, SymWord d, Provable p) => Provable ((SBV a, SBV b, SBV c, SBV d) -> p) where
forAll_ k = forall_ >>= \a -> forAll_ $ \b c d -> k (a, b, c, d)
forAll (s:ss) k = forall s >>= \a -> forAll ss $ \b c d -> k (a, b, c, d)
forAll [] k = forAll_ k
forSome_ k = exists_ >>= \a -> forSome_ $ \b c d -> k (a, b, c, d)
forSome (s:ss) k = exists s >>= \a -> forSome ss $ \b c d -> k (a, b, c, d)
forSome [] k = forSome_ k
instance (SymWord a, SymWord b, SymWord c, SymWord d, SymWord e, Provable p) => Provable ((SBV a, SBV b, SBV c, SBV d, SBV e) -> p) where
forAll_ k = forall_ >>= \a -> forAll_ $ \b c d e -> k (a, b, c, d, e)
forAll (s:ss) k = forall s >>= \a -> forAll ss $ \b c d e -> k (a, b, c, d, e)
forAll [] k = forAll_ k
forSome_ k = exists_ >>= \a -> forSome_ $ \b c d e -> k (a, b, c, d, e)
forSome (s:ss) k = exists s >>= \a -> forSome ss $ \b c d e -> k (a, b, c, d, e)
forSome [] k = forSome_ k
instance (SymWord a, SymWord b, SymWord c, SymWord d, SymWord e, SymWord f, Provable p) => Provable ((SBV a, SBV b, SBV c, SBV d, SBV e, SBV f) -> p) where
forAll_ k = forall_ >>= \a -> forAll_ $ \b c d e f -> k (a, b, c, d, e, f)
forAll (s:ss) k = forall s >>= \a -> forAll ss $ \b c d e f -> k (a, b, c, d, e, f)
forAll [] k = forAll_ k
forSome_ k = exists_ >>= \a -> forSome_ $ \b c d e f -> k (a, b, c, d, e, f)
forSome (s:ss) k = exists s >>= \a -> forSome ss $ \b c d e f -> k (a, b, c, d, e, f)
forSome [] k = forSome_ k
instance (SymWord a, SymWord b, SymWord c, SymWord d, SymWord e, SymWord f, SymWord g, Provable p) => Provable ((SBV a, SBV b, SBV c, SBV d, SBV e, SBV f, SBV g) -> p) where
forAll_ k = forall_ >>= \a -> forAll_ $ \b c d e f g -> k (a, b, c, d, e, f, g)
forAll (s:ss) k = forall s >>= \a -> forAll ss $ \b c d e f g -> k (a, b, c, d, e, f, g)
forAll [] k = forAll_ k
forSome_ k = exists_ >>= \a -> forSome_ $ \b c d e f g -> k (a, b, c, d, e, f, g)
forSome (s:ss) k = exists s >>= \a -> forSome ss $ \b c d e f g -> k (a, b, c, d, e, f, g)
forSome [] k = forSome_ k
prove :: Provable a => a -> IO ThmResult
prove = proveWith defaultSMTCfg
sat :: Provable a => a -> IO SatResult
sat = satWith defaultSMTCfg
allSat :: Provable a => a -> IO AllSatResult
allSat = allSatWith defaultSMTCfg
optimize :: Provable a => a -> IO OptimizeResult
optimize = optimizeWith defaultSMTCfg
safe :: SExecutable a => a -> IO [SafeResult]
safe = safeWith defaultSMTCfg
isVacuous :: Provable a => a -> IO Bool
isVacuous = isVacuousWith defaultSMTCfg
isTheoremWith :: Provable a => SMTConfig -> Maybe Int -> a -> IO (Maybe Bool)
isTheoremWith cfg mbTo p = do r <- proveWith cfg{timeOut = mbTo} p
case r of
ThmResult Unsatisfiable{} -> return $ Just True
ThmResult Satisfiable{} -> return $ Just False
ThmResult TimeOut{} -> return Nothing
_ -> error $ "SBV.isTheorem: Received:\n" ++ show r
isSatisfiableWith :: Provable a => SMTConfig -> Maybe Int -> a -> IO (Maybe Bool)
isSatisfiableWith cfg mbTo p = do r <- satWith cfg{timeOut = mbTo} p
case r of
SatResult Satisfiable{} -> return $ Just True
SatResult Unsatisfiable{} -> return $ Just False
SatResult TimeOut{} -> return Nothing
_ -> error $ "SBV.isSatisfiable: Received: " ++ show r
isTheorem :: Provable a => Maybe Int -> a -> IO (Maybe Bool)
isTheorem = isTheoremWith defaultSMTCfg
isSatisfiable :: Provable a => Maybe Int -> a -> IO (Maybe Bool)
isSatisfiable = isSatisfiableWith defaultSMTCfg
compileToSMTLib :: Provable a => SMTLibVersion
-> Bool
-> a
-> IO String
compileToSMTLib version isSat a = do
t <- getClockTime
let comments = ["Created on " ++ show t]
cvt = case version of
SMTLib2 -> toSMTLib2
SMTProblem{smtLibPgm} <- simulate cvt defaultSMTCfg isSat comments a
let out = show (smtLibPgm defaultSMTCfg NoCase)
return $ out ++ "\n(check-sat)\n"
generateSMTBenchmarks :: Provable a => Bool -> FilePath -> a -> IO ()
generateSMTBenchmarks isSat f a = mapM_ gen [minBound .. maxBound]
where gen v = do s <- compileToSMTLib v isSat a
let fn = f `addExtension` smtLibVersionExtension v
writeFile fn s
putStrLn $ "Generated " ++ show v ++ " benchmark " ++ show fn ++ "."
bufferSanity :: Bool -> IO a -> IO a
bufferSanity False a = a
bufferSanity True a = bracket before after (const a)
where before = do b <- hGetBuffering stdout
hSetBuffering stdout LineBuffering
return b
after b = do hFlush stdout
hSetBuffering stdout b
hFlush stdout
objectiveCheck :: Bool -> [Objective a] -> String -> IO ()
objectiveCheck False [] _ = return ()
objectiveCheck False os w = error $ unlines $ ("\n*** Unsupported call to " ++ show w ++ " in the presence of objective(s):")
: [ "***\t" ++ intercalate ", " (map objectiveName os)
, "*** Use \"optimize\" to optimize for these objectives instead of " ++ show w
]
objectiveCheck True [] w = error $ "*** Unsupported call to " ++ w ++ " when no objectives are present. Use \"sat\" for plain satisfaction"
objectiveCheck True _ _ = return ()
getConverter :: SMTConfig -> SMTLibConverter
getConverter SMTConfig{smtLibVersion} = case smtLibVersion of
SMTLib2 -> toSMTLib2
proveWith :: Provable a => SMTConfig -> a -> IO ThmResult
proveWith config a = do simRes@SMTProblem{tactics, objectives} <- simulate (getConverter config) config False [] a
objectiveCheck False objectives "prove"
let hasPar = any isParallelCaseAnywhere tactics
bufferSanity hasPar $ applyTactics config (False, hasPar) (wrap, unwrap) [] tactics objectives $ callSolver False "Checking Theoremhood.." [] mwrap simRes
where wrap = ThmResult
unwrap (ThmResult r) = r
mwrap [r] = wrap r
mwrap xs = error $ "SBV.proveWith: Backend solver returned a non-singleton answer:\n" ++ show (map ThmResult xs)
satWith :: Provable a => SMTConfig -> a -> IO SatResult
satWith config a = do simRes@SMTProblem{tactics, objectives} <- simulate (getConverter config) config True [] a
objectiveCheck False objectives "sat"
let hasPar = any isParallelCaseAnywhere tactics
bufferSanity hasPar $ applyTactics config (True, hasPar) (wrap, unwrap) [] tactics objectives $ callSolver True "Checking Satisfiability.." [] mwrap simRes
where wrap = SatResult
unwrap (SatResult r) = r
mwrap [r] = wrap r
mwrap xs = error $ "SBV.satWith: Backend solver returned a non-singleton answer:\n" ++ show (map SatResult xs)
optimizeWith :: Provable a => SMTConfig -> a -> IO OptimizeResult
optimizeWith config a = do
msg "Optimizing.."
sbvPgm@SMTProblem{objectives, tactics} <- simulate (getConverter config) config True [] a
objectiveCheck True objectives "optimize"
let hasPar = any isParallelCaseAnywhere tactics
style = case nub [s | OptimizePriority s <- tactics] of
[] -> Lexicographic
[s] -> s
ss -> error $ "SBV: Multiple optimization priorities found: " ++ intercalate ", " (map show ss) ++ ". Please use only one."
optimizer = case style of
Lexicographic -> optLexicographic
Independent -> optIndependent
Pareto -> optPareto
optimizer hasPar config sbvPgm
where msg = when (verbose config) . putStrLn . ("** " ++)
optLexicographic :: Bool -> SMTConfig -> SMTProblem -> IO OptimizeResult
optLexicographic hasPar config sbvPgm@SMTProblem{objectives, tactics} = do
result <- bufferSanity hasPar $ applyTactics config (True, hasPar) (id, id) [] tactics objectives $ callSolver True "Lexicographically optimizing.." [] mwrap sbvPgm
return $ LexicographicResult result
where mwrap [r] = r
mwrap xs = error $ "SBV.optLexicographic: Backend solver returned a non-singleton answer:\n" ++ show (map SatResult xs)
optIndependent :: Bool -> SMTConfig -> SMTProblem -> IO OptimizeResult
optIndependent hasPar config sbvPgm@SMTProblem{objectives, tactics} = do
let ns = map objectiveName objectives
result <- bufferSanity hasPar $ applyTactics config (True, hasPar) (wrap ns, unwrap) [] tactics objectives $ callSolver True "Independently optimizing.." [] mwrap sbvPgm
return $ IndependentResult result
where wrap :: [String] -> SMTResult -> [(String, SMTResult)]
wrap ns r = zip ns (repeat r)
unwrap :: [(String, SMTResult)] -> SMTResult
unwrap xs = case [r | (_, r@Satisfiable{}) <- xs] ++ [r | (_, r@SatExtField{}) <- xs] ++ map snd xs of
(r:_) -> r
[] -> error "SBV.optIndependent: Impossible happened: Received no results!"
mwrap xs
| lobs == lxs = zip (map objectiveName objectives) xs
| True = error $ "SBV.optIndependent: Expected " ++ show lobs ++ " objective results, but received: " ++ show lxs ++ ":\n" ++ show (map SatResult xs)
where lxs = length xs
lobs = length objectives
optPareto :: Bool -> SMTConfig -> SMTProblem -> IO OptimizeResult
optPareto hasPar config sbvPgm@SMTProblem{objectives, tactics} = do
result <- bufferSanity hasPar $ applyTactics config (True, hasPar) (wrap, unwrap) [] tactics objectives $ callSolver True "Pareto optimizing.." [] id sbvPgm
return $ ParetoResult result
where wrap :: SMTResult -> [SMTResult]
wrap r = [r]
unwrap :: [SMTResult] -> SMTResult
unwrap xs = case [r | r@Satisfiable{} <- xs] ++ [r | r@SatExtField{} <- xs] ++ xs of
(r:_) -> r
[] -> error "SBV.optPareto: Impossible happened: Received no results!"
applyTactics :: SMTConfig
-> (Bool, Bool)
-> (SMTResult -> res, res -> SMTResult)
-> [(String, (String, SW))]
-> [Tactic SW]
-> [Objective (SW, SW)]
-> (SMTConfig -> Maybe (OptimizeStyle, Int) -> CaseCond -> IO res)
-> IO res
applyTactics cfgIn (isSat, hasPar) (wrap, unwrap) levels tactics objectives cont
= do
when (hasObjectives && not isSat) $ error "SBV: Optimization is only available for sat calls."
when (hasObjectives && hasCaseSplits) $ error "SBV: Optimization and case-splits are not supported together."
let mbOptInfo
| not hasObjectives = Nothing
| True = Just (optimizePriority, length objectives)
if hasObjectives
then cont (finalOptConfig objectives) mbOptInfo (Opt objectives)
else do
mbRes <- if not shouldCheckConstrVacuity
then return Nothing
else constraintVacuityCheck isSat finalConfig mbOptInfo (wrap, unwrap) cont
case mbRes of
Just r -> return r
Nothing -> if null caseSplits
then cont finalConfig mbOptInfo (CasePath (map (snd . snd) levels))
else caseSplit finalConfig mbOptInfo shouldCheckCaseVacuity (parallelCase, hasPar) isSat (wrap, unwrap) levels chatty cases cont
where (caseSplits, checkCaseVacuity, parallelCases, checkConstrVacuity, timeOuts, checkUsing, useLogics, useSolvers, optimizePriorities)
= foldr (flip classifyTactics) ([], [], [], [], [], [], [], [], []) tactics
classifyTactics (a, b, c, d, e, f, g, h, i) = \case
t@CaseSplit{} -> (t:a, b, c, d, e, f, g, h, i)
t@CheckCaseVacuity{} -> ( a, t:b, c, d, e, f, g, h, i)
t@ParallelCase{} -> ( a, b, t:c, d, e, f, g, h, i)
t@CheckConstrVacuity{} -> ( a, b, c, t:d, e, f, g, h, i)
t@StopAfter{} -> ( a, b, c, d, t:e, f, g, h, i)
t@CheckUsing{} -> ( a, b, c, d, e, t:f, g, h, i)
t@UseLogic{} -> ( a, b, c, d, e, f, t:g, h, i)
t@UseSolver{} -> ( a, b, c, d, e, f, g, t:h, i)
t@OptimizePriority{} -> ( a, b, c, d, e, f, g, h, t:i)
hasObjectives = not $ null objectives
hasCaseSplits = not $ null cases
parallelCase = not $ null parallelCases
optimizePriority = case [s | OptimizePriority s <- optimizePriorities] of
[] -> Lexicographic
[s] -> s
ss -> error $ "SBV.OptimizePriority: Multiple optimization priorities found, at most one is allowed: " ++ intercalate "," (map show ss)
shouldCheckCaseVacuity = case [b | CheckCaseVacuity b <- checkCaseVacuity] of
[] -> True
bs -> or bs
shouldCheckConstrVacuity = or [b | CheckConstrVacuity b <- checkConstrVacuity]
(chatty, cases) = let (vs, css) = unzip [(v, cs) | CaseSplit v cs <- caseSplits] in (or (verbose cfgIn : vs), concat css)
grabStops c = case [i | StopAfter i <- timeOuts] of
[] -> c
xs -> c {timeOut = Just (maximum xs)}
grabCheckUsing c = case [s | CheckUsing s <- checkUsing] of
[] -> c
[s] -> c {satCmd = "(check-sat-using " ++ s ++ ")"}
ss -> c {satCmd = "(check-sat-using (then " ++ unwords ss ++ "))"}
grabUseLogic c = case [l | UseLogic l <- useLogics] of
[] -> c
ss -> c { useLogic = Just (last ss) }
configToUse = case [s | UseSolver s <- useSolvers] of
[] -> cfgIn
[s] -> s
ss -> error $ "SBV.UseSolver: Multiple UseSolver tactics found, at most one is allowed: " ++ intercalate "," (map show ss)
finalConfig = grabUseLogic . grabCheckUsing . grabStops $ configToUse
finalOptConfig goals = finalConfig { optimizeArgs = optimizeArgs finalConfig ++ optimizerDirectives }
where optimizerDirectives
| hasObjectives = map minmax goals ++ style optimizePriority
| True = []
minmax (Minimize _ (_, v)) = "(minimize " ++ show v ++ ")"
minmax (Maximize _ (_, v)) = "(maximize " ++ show v ++ ")"
minmax (AssertSoft nm (_, v) mbp) = "(assert-soft " ++ show v ++ penalize mbp ++ ")"
where penalize DefaultPenalty = ""
penalize (Penalty w mbGrp)
| w <= 0 = error $ unlines [ "SBV.AssertSoft: Goal " ++ show nm ++ " is assigned a non-positive penalty: " ++ shw
, "All soft goals must have > 0 penalties associated."
]
| True = " :weight " ++ shw ++ maybe "" group mbGrp
where shw = show (fromRational w :: Double)
group g = " :id " ++ g
style Lexicographic = []
style Independent = ["(set-option :opt.priority box)"]
style Pareto = [ "(set-option :opt.priority pareto)"
, "(set-option :opt.print_model true)"
]
constraintVacuityCheck :: forall res.
Bool
-> SMTConfig
-> Maybe (OptimizeStyle, Int)
-> (SMTResult -> res, res -> SMTResult)
-> (SMTConfig -> Maybe (OptimizeStyle, Int) -> CaseCond -> IO res)
-> IO (Maybe res)
constraintVacuityCheck True _ _ _ _ = return Nothing
constraintVacuityCheck False config d (wrap, unwrap) f = do
res <- f config d CstrVac
case unwrap res of
Satisfiable{} -> return Nothing
_ -> return $ Just $ wrap vacuityFailResult
where vacuityFailResult = ProofError config [ "Constraint vacuity check failed."
, "User given constraints are not satisfiable."
]
caseSplit :: forall res.
SMTConfig
-> Maybe (OptimizeStyle, Int)
-> Bool
-> (Bool, Bool)
-> Bool
-> (SMTResult -> res, res -> SMTResult)
-> [(String, (String, SW))]
-> Bool
-> [(String, SW, [Tactic SW])]
-> (SMTConfig -> Maybe (OptimizeStyle, Int) -> CaseCond -> IO res)
-> IO res
caseSplit config mbOptInfo checkVacuity (runParallel, hasPar) isSAT (wrap, unwrap) level chatty cases cont
| runParallel = goParallel tasks
| True = goSerial tasks
where tasks = zip caseNos cases
lids = map fst level
noOfCases = length cases
casePad = length (show noOfCases)
tagLength = maximum $ map length $ "Coverage" : [s | (s, _, _) <- cases]
showTag t = take tagLength (t ++ repeat ' ')
shCaseId i = let si = show i in replicate (casePad - length si) ' ' ++ si
caseNos = map shCaseId [(1::Int) .. ]
tag tagChar = replicate 2 tagChar ++ replicate (2 * length level) tagChar
mkCaseNameBase s i = "Case " ++ intercalate "." (lids ++ [i]) ++ ": " ++ showTag s
mkCovNameBase = "Coverage " ++ replicate (casePad - 1) ' ' ++ "X"
mkCaseName tagChar s i = tag tagChar ++ ' ' : mkCaseNameBase s i
mkCovName tagChar = tag tagChar ++ ' ' : mkCovNameBase
startCase :: Bool -> Maybe (String, String) -> IO ()
startCase multi mbis
| not chatty = return ()
| Just (i, s) <- mbis = printer $ mkCaseName tagChar s i ++ start
| True = printer $ mkCovName tagChar ++ start
where line = multi || hasPar
printer | line = putStrLn
| True = putStr
tagChar | line = '>'
| True = '*'
start = " [Started]"
vacuityMsg :: Maybe Bool -> Bool -> (String, String) -> IO ()
vacuityMsg mbGood multi (i, s)
| not chatty = return ()
| line = putStrLn $ mkCaseName '=' s i ++ msg
| True = printer msg
where line = multi || hasPar
printer
| failed = putStrLn
| True = putStr
(failed, msg) = case mbGood of
Nothing -> (False, " [Vacuity Skipped]")
Just True -> (False, " [Vacuity OK]")
Just False -> (True, " [Vacuity Failed]")
endCase :: Bool -> Maybe (String, String) -> String -> IO ()
endCase multi mbis msg
| not chatty = return ()
| not line = putStrLn $ ' ' : msg
| Just (i, s) <- mbis = putStrLn $ mkCaseName '<' s i ++ ' ' : msg
| True = putStrLn $ mkCovName '<' ++ ' ' : msg
where line = multi || hasPar
goSerial :: [(String, (String, SW, [Tactic SW]))] -> IO res
goSerial []
= do let multi = runParallel
startCase multi Nothing
res <- cont config mbOptInfo (CaseCov (map (snd . snd) level) [c | (_, c, _) <- cases])
decideSerial multi Nothing (unwrap res) (return res)
goSerial ((i, (nm, cond, ts)):cs)
= do let multi = not . null $ [() | CaseSplit{} <- ts]
mbis = Just (i, nm)
startCase multi mbis
continue <- if isSAT
then return True
else if checkVacuity
then do res <- cont config mbOptInfo (CaseVac (map (snd . snd) level) cond)
case unwrap res of
Satisfiable{} -> vacuityMsg (Just True) multi (i, nm) >> return True
_ -> vacuityMsg (Just False) multi (i, nm) >> return False
else vacuityMsg Nothing multi (i, nm) >> return True
if continue
then do res <- applyTactics config (isSAT, hasPar) (wrap, unwrap) (level ++ [(i, (nm, cond))]) ts [] cont
decideSerial multi mbis (unwrap res) (goSerial cs)
else return $ wrap $ vacuityFailResult (i, nm)
vacuityFailResult cur = ProofError config $ [ "Vacuity check failed."
, "Case constraint not satisfiable. Leading path:"
]
++ map (" " ++) (align ([(i, n) | (i, (n, _)) <- level] ++ [cur]))
++ ["HINT: Try \"CheckCaseVacuity False\" tactic to ignore case vacuity checks."]
where align :: [(String, String)] -> [String]
align path = map join cpath
where len = maximum (0 : map (length . fst) cpath)
join (c, n) = reverse (take len (reverse c ++ repeat ' ')) ++ ": " ++ n
cpath = [(intercalate "." (reverse ls), j) | (ls, j) <- cascade [] path]
trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
cascade _ [] = []
cascade sofar ((i, j) : rest) = let new = trim i : sofar in (new, j) : cascade new rest
decideSerial
| isSAT = decideSerialSAT
| True = decideSerialProof
diag Unsatisfiable{} = "[Unsatisfiable]"
diag Satisfiable {} = "[Satisfiable]"
diag SatExtField {} = "[Satisfiable in Field Extension]"
diag Unknown {} = "[Unknown]"
diag ProofError {} = "[ProofError]"
diag TimeOut {} = "[TimeOut]"
decideSerialSAT :: Bool -> Maybe (String, String) -> SMTResult -> IO res -> IO res
decideSerialSAT multi mbis r@Satisfiable{} _ = endCase multi mbis (diag r) >> return (wrap r)
decideSerialSAT multi mbis r@ProofError{} _ = endCase multi mbis (diag r) >> return (wrap r)
decideSerialSAT multi mbis r k = endCase multi mbis (diag r) >> k
decideSerialProof :: Bool -> Maybe (String, String) -> SMTResult -> IO res -> IO res
decideSerialProof multi mbis Unsatisfiable{} k = endCase multi mbis "[Proved]" >> k
decideSerialProof multi mbis r _ = endCase multi mbis "[Failed]" >> return (wrap r)
goParallel :: [(String, (String, SW, [Tactic SW]))] -> IO res
goParallel cs = do
when chatty $ putStrLn $ topName '>' "[Starting]"
let mkTask (i, (nm, cond, ts)) =
let caseProof = do continue <- if isSAT
then return True
else if checkVacuity
then do res <- cont config mbOptInfo (CaseVac (map (snd . snd) level) cond)
case unwrap res of
Satisfiable{} -> return True
_ -> return False
else return True
if continue
then unwrap `fmap` applyTactics config (isSAT, hasPar) (wrap, unwrap) (level ++ [(i, (nm, cond))]) ts [] cont
else return $ vacuityFailResult (i, nm)
in (mkCaseNameBase nm i, caseProof)
let cov = unwrap `fmap` cont config mbOptInfo (CaseCov (map (snd . snd) level) [c | (_, c, _) <- cases])
(decidingTag, res) <- decideParallel $ map mkTask cs ++ [(mkCovNameBase, cov)]
let trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
let caseMsg
| isSAT = satMsg
| True = proofMsg
where addTag x = x ++ " (at " ++ trim decidingTag ++ ")"
(satMsg, proofMsg) = case res of
Unsatisfiable{} -> ("[Unsatisfiable]", "[Proved]")
Satisfiable{} -> (addTag "[Satisfiable]", addTag "[Failed]")
_ -> let d = diag res in (addTag d, addTag d)
when chatty $ putStrLn $ topName '<' caseMsg
return $ wrap res
where topName c w = tag c ++ topTag ++ " Parallel case split: " ++ range ++ ": " ++ w
topTag = " Case" ++ s ++ intercalate "." lids ++ dot ++ "[1-" ++ show (length cs + 1) ++ "]:"
where dot | null lids = ""
| True = "."
s | null cs = " "
| True = "s "
range = case cs of
[] -> "Coverage"
[_] -> "One case and coverage"
xs -> show (length xs) ++ " cases and coverage"
decideParallel :: [(String, IO SMTResult)] -> IO (String, SMTResult)
decideParallel caseTasks = mapM try caseTasks >>= pick
where try (nm, task) = async $ task >>= \r -> return (nm, r)
pick :: [Async (String, SMTResult)] -> IO (String, SMTResult)
pick [] = error "SBV.caseSplit.decideParallel: Impossible happened, ran out of proofs!"
pick [a] = wait a
pick as = do (d, r) <- waitAny as
let others = filter (/= d) as
continue = pick others
stop = mapM_ cancel others >> return r
case snd r of
Unsatisfiable{} -> continue
Satisfiable{} -> stop
SatExtField{} -> stop
ProofError{} -> stop
Unknown{} -> if isSAT then continue else stop
TimeOut{} -> if isSAT then continue else stop
safeWith :: SExecutable a => SMTConfig -> a -> IO [SafeResult]
safeWith cfg a = do
res@Result{resAssertions=asserts} <- runSymbolic (True, cfg) $ sName_ a >>= output
mapM (verify res) asserts
where locInfo (Just ps) = Just $ let loc (f, sl) = concat [srcLocFile sl, ":", show (srcLocStartLine sl), ":", show (srcLocStartCol sl), ":", f]
in intercalate ",\n " (map loc ps)
locInfo _ = Nothing
verify res (msg, cs, cond) = do result <- runProofOn (getConverter cfg) cfg True [] pgm >>= \p -> callSolver True msg [] mwrap p cfg Nothing NoCase
return $ SafeResult (locInfo (getCallStack `fmap` cs), msg, result)
where pgm = res { resInputs = [(EX, n) | (_, n) <- resInputs res]
, resOutputs = [cond]
}
mwrap [r] = r
mwrap xs = error $ "SBV.safeWith: Backend solver returned a non-singleton answer:\n" ++ show (map SatResult xs)
isSafe :: SafeResult -> Bool
isSafe (SafeResult (_, _, result)) = case result of
Unsatisfiable{} -> True
Satisfiable{} -> False
SatExtField{} -> False
Unknown{} -> False
ProofError{} -> False
TimeOut{} -> False
isVacuousWith :: Provable a => SMTConfig -> a -> IO Bool
isVacuousWith config a = do
Result ki tr uic is cs ts as uis ax asgn cstr tactics goals asserts _out <- runSymbolic (True, config) $ forAll_ a >>= output
case cstr of
[] -> return False
_ -> do let is' = [(EX, i) | (_, i) <- is]
res' = Result ki tr uic is' cs ts as uis ax asgn cstr tactics goals asserts [trueSW]
result <- runProofOn (getConverter config) config True [] res' >>= \p -> callSolver True "Checking Vacuity.." [] mwrap p config Nothing NoCase
case result of
Unsatisfiable{} -> return True
Satisfiable{} -> return False
SatExtField{} -> error "SBV: isVacuous: Solver returned a model in the extension field!"
Unknown{} -> error "SBV: isVacuous: Solver returned unknown!"
ProofError _ ls -> error $ "SBV: isVacuous: error encountered:\n" ++ unlines ls
TimeOut _ -> error "SBV: isVacuous: time-out."
where mwrap [r] = r
mwrap xs = error $ "SBV.isVacuousWith: Backend solver returned a non-singleton answer:\n" ++ show (map SatResult xs)
allSatWith :: Provable a => SMTConfig -> a -> IO AllSatResult
allSatWith config p = do
msg "Checking Satisfiability, all solutions.."
sbvPgm@SMTProblem{smtInputs=qinps, kindsUsed=ki} <- simulate (getConverter config) config True [] p
let usorts = [s | us@(KUserSort s _) <- Set.toList ki, isFree us]
where isFree (KUserSort _ (Left _)) = True
isFree _ = False
unless (null usorts) $ msg $ "SBV.allSat: Uninterpreted sorts present: " ++ unwords usorts
++ "\n SBV will use equivalence classes to generate all-satisfying instances."
results <- unsafeInterleaveIO $ go sbvPgm (1::Int) []
let w = ALL `elem` map fst qinps
return $ AllSatResult (w, results)
where msg = when (verbose config) . putStrLn . ("** " ++)
go sbvPgm = loop
where hasPar = any isParallelCaseAnywhere (tactics sbvPgm)
loop !n nonEqConsts = do
curResult <- invoke nonEqConsts hasPar n sbvPgm
case curResult of
Nothing -> return []
Just (SatResult r) -> let cont model = do let modelOnlyAssocs = [v | v@(x, _) <- modelAssocs model, not (isNonModelVar config x)]
rest <- unsafeInterleaveIO $ loop (n+1) (modelOnlyAssocs : nonEqConsts)
return (r : rest)
in case r of
Unsatisfiable{} -> return []
Satisfiable _ (SMTModel _ []) -> return [r]
Satisfiable _ model -> cont model
SatExtField _ (SMTModel _ []) -> return [r]
SatExtField _ model@(SMTModel [] _) -> cont model
SatExtField{} -> return []
Unknown{} -> return [r]
ProofError{} -> return [r]
TimeOut{} -> return [r]
invoke nonEqConsts hasPar n simRes@SMTProblem{smtInputs, tactics, objectives} = do
objectiveCheck False objectives "allSat"
msg $ "Looking for solution " ++ show n
case addNonEqConstraints (smtLibVersion config) (roundingMode config) smtInputs nonEqConsts of
Nothing ->
return Nothing
Just refutedModels -> do
let wrap = SatResult
unwrap (SatResult r) = r
mwrap [r] = wrap r
mwrap xs = error $ "SBV.allSatWith: Backend solver returned a non-singleton answer:\n" ++ show (map SatResult xs)
res <- bufferSanity hasPar $ applyTactics (updateName (n-1) config) (True, hasPar) (wrap, unwrap) [] tactics objectives
$ callSolver True "Checking Satisfiability.." refutedModels mwrap simRes
return $ Just res
updateName i cfg = cfg{smtFile = upd `fmap` smtFile cfg}
where upd nm = let (b, e) = splitExtension nm in b ++ "_allSat_" ++ show i ++ e
callSolver :: Bool -> String -> [String] -> ([SMTResult] -> b) -> SMTProblem -> SMTConfig -> Maybe (OptimizeStyle, Int) -> CaseCond -> IO b
callSolver isSat checkMsg refutedModels wrap SMTProblem{smtInputs, smtSkolemMap, smtLibPgm} config mbOptInfo caseCond = do
let msg = when (verbose config) . putStrLn . ("** " ++)
finalPgm = intercalate "\n" (pgm ++ refutedModels) where SMTLibPgm _ pgm = smtLibPgm config caseCond
msg checkMsg
msg $ "Generated SMTLib program:\n" ++ (finalPgm ++ intercalate "\n" ("" : optimizeArgs config ++ [satCmd config]))
smtAnswer <- engine (solver config) config isSat mbOptInfo smtInputs smtSkolemMap finalPgm
msg "Done.."
return $ wrap smtAnswer
simulate :: Provable a => SMTLibConverter -> SMTConfig -> Bool -> [String] -> a -> IO SMTProblem
simulate converter config isSat comments predicate = do
let msg = when (verbose config) . putStrLn . ("** " ++)
isTiming = timing config
msg "Starting symbolic simulation.."
res <- timeIf isTiming ProblemConstruction $ runSymbolic (isSat, config) $ (if isSat then forSome_ else forAll_) predicate >>= output
msg $ "Generated symbolic trace:\n" ++ show res
msg "Translating to SMT-Lib.."
runProofOn converter config isSat comments res
runProofOn :: SMTLibConverter -> SMTConfig -> Bool -> [String] -> Result -> IO SMTProblem
runProofOn converter config isSat comments res =
let isTiming = timing config
in case res of
Result ki _qcInfo _codeSegs is consts tbls arrs uis axs pgm cstrs tacs goals assertions [o@(SW KBool _)] ->
timeIf isTiming Translation
$ let skolemMap = skolemize (if isSat then is else map flipQ is)
where flipQ (ALL, x) = (EX, x)
flipQ (EX, x) = (ALL, x)
skolemize :: [(Quantifier, NamedSymVar)] -> [Either SW (SW, [SW])]
skolemize qinps = go qinps ([], [])
where go [] (_, sofar) = reverse sofar
go ((ALL, (v, _)):rest) (us, sofar) = go rest (v:us, Left v : sofar)
go ((EX, (v, _)):rest) (us, sofar) = go rest (us, Right (v, reverse us) : sofar)
smtScript = converter ki isSat comments is skolemMap consts tbls arrs uis axs pgm cstrs o
result = SMTProblem {smtInputs=is, smtSkolemMap=skolemMap, kindsUsed=ki, smtAsserts=assertions, tactics=tacs, objectives=goals, smtLibPgm=smtScript}
in rnf smtScript `seq` return result
Result{resOutputs = os} -> case length os of
0 -> error $ "Impossible happened, unexpected non-outputting result\n" ++ show res
1 -> error $ "Impossible happened, non-boolean output in " ++ show os
++ "\nDetected while generating the trace:\n" ++ show res
_ -> error $ "User error: Multiple output values detected: " ++ show os
++ "\nDetected while generating the trace:\n" ++ show res
++ "\n*** Check calls to \"output\", they are typically not needed!"
internalSATCheck :: SMTConfig -> SBool -> State -> String -> IO SatResult
internalSATCheck cfg condInPath st msg = do
sw <- sbvToSW st condInPath
() <- forceSWArg sw
Result ki tr uic is cs ts as uis ax asgn cstr tactics goals assertions _ <- extractSymbolicSimulationState st
let
pgm = Result ki tr uic [(EX, n) | (_, n) <- is] cs ts as uis ax asgn cstr tactics goals assertions [sw]
mwrap [r] = SatResult r
mwrap xs = error $ "SBV.internalSATCheck: Backend solver returned a non-singleton answer:\n" ++ show (map SatResult xs)
runProofOn (getConverter cfg) cfg True [] pgm >>= \p -> callSolver True msg [] mwrap p cfg Nothing NoCase
{-# ANN module ("HLint: ignore Reduce duplication" :: String) #-}