{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE Rank2Types #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.SBV.Control.Query (
send, ask, retrieveResponse
, CheckSatResult(..), checkSat, checkSatUsing, checkSatAssuming, checkSatAssumingWithUnsatisfiableSet
, getUnsatCore, getProof, getAssignment, getOption, freshVar, freshVar_, push, pop, getAssertionStackDepth
, inNewAssertionStack, echo, caseSplit, resetAssertions, exit, getAssertions, getValue, getUninterpretedValue, getModel, getSMTResult
, getLexicographicOptResults, getIndependentOptResults, getParetoOptResults, getAllSatResult, getUnknownReason
, SMTOption(..), SMTInfoFlag(..), SMTErrorBehavior(..), SMTReasonUnknown(..), SMTInfoResponse(..), getInfo
, Logic(..), Assignment(..)
, ignoreExitCode, timeout
, (|->)
, mkSMTResult
, io
) where
import Control.Monad (unless, when, zipWithM)
import Control.Monad.State.Lazy (get)
import Data.IORef (readIORef)
import Data.List (unzip3, intercalate, nubBy, sortBy, elemIndex)
import Data.Maybe (listToMaybe, catMaybes)
import Data.Function (on)
import Data.SBV.Core.Data
import Data.SBV.Core.Symbolic (QueryState(..), Query(..), SMTModel(..), SMTResult(..), State(..), incrementInternalCounter)
import Data.SBV.Utils.SExpr
import Data.SBV.Utils.Boolean
import Data.SBV.Control.Types
import Data.SBV.Control.Utils
data Assignment = Assign SVal CW
noSurrounding :: Char -> String -> String
noSurrounding c (c':cs@(_:_)) | c == c' && c == last cs = init cs
noSurrounding _ s = s
unQuote :: String -> String
unQuote = noSurrounding '"'
unBar :: String -> String
unBar = noSurrounding '|'
fromECon :: SExpr -> Maybe String
fromECon (ECon s) = Just s
fromECon _ = Nothing
stringsOf :: SExpr -> [String]
stringsOf (ECon s) = [s]
stringsOf (ENum (i, _)) = [show i]
stringsOf (EReal r) = [show r]
stringsOf (EFloat f) = [show f]
stringsOf (EDouble d) = [show d]
stringsOf (EApp ss) = concatMap stringsOf ss
serialize :: Bool -> SExpr -> String
serialize removeQuotes = go
where go (ECon s) = if removeQuotes then unQuote s else s
go (ENum (i, _)) = show i
go (EReal r) = show r
go (EFloat f) = show f
go (EDouble d) = show d
go (EApp [x]) = go x
go (EApp ss) = "(" ++ unwords (map go ss) ++ ")"
getInfo :: SMTInfoFlag -> Query SMTInfoResponse
getInfo flag = do
let cmd = "(get-info " ++ show flag ++ ")"
bad = unexpected "getInfo" cmd "a valid get-info response" Nothing
isAllStatistics AllStatistics = True
isAllStatistics _ = False
isAllStat = isAllStatistics flag
render = serialize True
grabAllStat k v = (render k, render v)
grabAllStats (EApp xs) = walk xs
where walk [] = []
walk [t] = [grabAllStat t (ECon "")]
walk (t : v : rest) = grabAllStat t v : walk rest
grabAllStats o = [grabAllStat o (ECon "")]
r <- ask cmd
parse r bad $ \pe ->
if isAllStat
then return $ Resp_AllStatistics $ grabAllStats pe
else case pe of
ECon "unsupported" -> return Resp_Unsupported
EApp [ECon ":assertion-stack-levels", ENum (i, _)] -> return $ Resp_AssertionStackLevels i
EApp (ECon ":authors" : ns) -> return $ Resp_Authors (map render ns)
EApp [ECon ":error-behavior", ECon "immediate-exit"] -> return $ Resp_Error ErrorImmediateExit
EApp [ECon ":error-behavior", ECon "continued-execution"] -> return $ Resp_Error ErrorContinuedExecution
EApp (ECon ":name" : o) -> return $ Resp_Name (render (EApp o))
EApp [ECon ":reason-unknown", ECon "memout"] -> return $ Resp_ReasonUnknown UnknownMemOut
EApp [ECon ":reason-unknown", ECon "incomplete"] -> return $ Resp_ReasonUnknown UnknownIncomplete
EApp (ECon ":reason-unknown" : o) -> return $ Resp_ReasonUnknown (UnknownOther (render (EApp o)))
EApp (ECon ":version" : o) -> return $ Resp_Version (render (EApp o))
EApp (ECon s : o) -> return $ Resp_InfoKeyword s (map render o)
_ -> bad r Nothing
getOption :: (a -> SMTOption) -> Query (Maybe SMTOption)
getOption f = case f undefined of
DiagnosticOutputChannel{} -> askFor "DiagnosticOutputChannel" ":diagnostic-output-channel" $ string DiagnosticOutputChannel
ProduceAssertions{} -> askFor "ProduceAssertions" ":produce-assertions" $ bool ProduceAssertions
ProduceAssignments{} -> askFor "ProduceAssignments" ":produce-assignments" $ bool ProduceAssignments
ProduceProofs{} -> askFor "ProduceProofs" ":produce-proofs" $ bool ProduceProofs
ProduceUnsatAssumptions{} -> askFor "ProduceUnsatAssumptions" ":produce-unsat-assumptions" $ bool ProduceUnsatAssumptions
ProduceUnsatCores{} -> askFor "ProduceUnsatCores" ":produce-unsat-cores" $ bool ProduceUnsatCores
RandomSeed{} -> askFor "RandomSeed" ":random-seed" $ integer RandomSeed
ReproducibleResourceLimit{} -> askFor "ReproducibleResourceLimit" ":reproducible-resource-limit" $ integer ReproducibleResourceLimit
SMTVerbosity{} -> askFor "SMTVerbosity" ":verbosity" $ integer SMTVerbosity
OptionKeyword nm _ -> askFor ("OptionKeyword" ++ nm) nm $ stringList (OptionKeyword nm)
SetLogic{} -> error "Data.SBV.Query: SMTLib does not allow querying value of the logic!"
SetInfo{} -> error "Data.SBV.Query: SMTLib does not allow querying value of meta-info!"
where askFor sbvName smtLibName continue = do
let cmd = "(get-option " ++ smtLibName ++ ")"
bad = unexpected ("getOption " ++ sbvName) cmd "a valid option value" Nothing
r <- ask cmd
parse r bad $ \case ECon "unsupported" -> return Nothing
e -> continue e (bad r)
string c (ECon s) _ = return $ Just $ c s
string _ e k = k $ Just ["Expected string, but got: " ++ show (serialize False e)]
bool c (ENum (0, _)) _ = return $ Just $ c False
bool c (ENum (1, _)) _ = return $ Just $ c True
bool _ e k = k $ Just ["Expected boolean, but got: " ++ show (serialize False e)]
integer c (ENum (i, _)) _ = return $ Just $ c i
integer _ e k = k $ Just ["Expected integer, but got: " ++ show (serialize False e)]
stringList c e _ = return $ Just $ c $ stringsOf e
getUnknownReason :: Query String
getUnknownReason = do ru <- getInfo ReasonUnknown
case ru of
Resp_Unsupported -> return "No reason provided."
Resp_ReasonUnknown r -> return $ case r of
UnknownMemOut -> "Out of memory."
UnknownIncomplete -> "Incomplete."
UnknownOther s -> s
_ -> return $ "Unexpected reason value received: " ++ show ru
getSMTResult :: Query SMTResult
getSMTResult = do cfg <- getConfig
cs <- checkSat
case cs of
Unsat -> return $ Unsatisfiable cfg
Sat -> Satisfiable cfg <$> getModel
Unk -> Unknown cfg <$> getUnknownReason
classifyModel :: SMTConfig -> SMTModel -> SMTResult
classifyModel cfg m = case filter (not . isRegularCW . snd) (modelObjectives m) of
[] -> Satisfiable cfg m
_ -> SatExtField cfg m
getLexicographicOptResults :: Query SMTResult
getLexicographicOptResults = do cfg <- getConfig
cs <- checkSat
case cs of
Unsat -> return $ Unsatisfiable cfg
Sat -> classifyModel cfg <$> getModelWithObjectives
Unk -> Unknown cfg <$> getUnknownReason
where getModelWithObjectives = do objectiveValues <- getObjectiveValues
m <- getModel
return m {modelObjectives = objectiveValues}
getIndependentOptResults :: [String] -> Query [(String, SMTResult)]
getIndependentOptResults objNames = do cfg <- getConfig
cs <- checkSat
case cs of
Unsat -> return [(nm, Unsatisfiable cfg) | nm <- objNames]
Sat -> continue (classifyModel cfg)
Unk -> do ur <- Unknown cfg <$> getUnknownReason
return [(nm, ur) | nm <- objNames]
where continue classify = do objectiveValues <- getObjectiveValues
nms <- zipWithM getIndependentResult [0..] objNames
return [(n, classify (m {modelObjectives = objectiveValues})) | (n, m) <- nms]
getIndependentResult :: Int -> String -> Query (String, SMTModel)
getIndependentResult i s = do m <- getModelAtIndex (Just i)
return (s, m)
getParetoOptResults :: Maybe Int -> Query (Bool, [SMTResult])
getParetoOptResults (Just i)
| i <= 0 = return (True, [])
getParetoOptResults mbN = do cfg <- getConfig
cs <- checkSat
case cs of
Unsat -> return (False, [])
Sat -> continue (classifyModel cfg)
Unk -> do ur <- getUnknownReason
return (False, [ProofError cfg [ur]])
where continue classify = do m <- getModel
(limReached, fronts) <- getParetoFronts (subtract 1 <$> mbN) [m]
return (limReached, reverse (map classify fronts))
getParetoFronts :: Maybe Int -> [SMTModel] -> Query (Bool, [SMTModel])
getParetoFronts (Just i) sofar | i <= 0 = return (True, sofar)
getParetoFronts mbi sofar = do cs <- checkSat
let more = getModel >>= \m -> getParetoFronts (subtract 1 <$> mbi) (m : sofar)
case cs of
Unsat -> return (False, sofar)
Sat -> more
Unk -> more
getModel :: Query SMTModel
getModel = getModelAtIndex Nothing
getModelAtIndex :: Maybe Int -> Query SMTModel
getModelAtIndex mbi = do
State{runMode} <- get
cfg <- getConfig
inps <- getQuantifiedInputs
rm <- io $ readIORef runMode
let vars :: [NamedSymVar]
vars = case rm of
m@CodeGen -> error $ "SBV.getModel: Model is not available in mode: " ++ show m
m@Concrete -> error $ "SBV.getModel: Model is not available in mode: " ++ show m
SMTMode _ isSAT _ ->
let allModelInputs = if isSAT then takeWhile ((/= ALL) . fst) inps
else takeWhile ((== ALL) . fst) inps
sortByNodeId :: [NamedSymVar] -> [NamedSymVar]
sortByNodeId = sortBy (compare `on` (\(SW _ n, _) -> n))
in sortByNodeId [nv | (_, nv@(_, n)) <- allModelInputs, not (isNonModelVar cfg n)]
assocs <- mapM (\(sw, n) -> (n, ) <$> getValueCW mbi sw) vars
return SMTModel { modelObjectives = []
, modelAssocs = assocs
}
getObjectiveValues :: Query [(String, GeneralizedCW)]
getObjectiveValues = do let cmd = "(get-objectives)"
bad = unexpected "getObjectiveValues" cmd "a list of objective values" Nothing
r <- ask cmd
inputs <- map snd <$> getQuantifiedInputs
parse r bad $ \case EApp (ECon "objectives" : es) -> do mbVals <- mapM (getObjValue (bad r) inputs) es
return $ catMaybes mbVals
_ -> bad r Nothing
where
getObjValue :: (forall a. Maybe [String] -> Query a) -> [NamedSymVar] -> SExpr -> Query (Maybe (String, GeneralizedCW))
getObjValue bailOut inputs expr =
case expr of
EApp [_] -> return Nothing
EApp [ECon nm, v] -> case listToMaybe [p | p@(sw, _) <- inputs, show sw == nm] of
Nothing -> return Nothing
Just (sw, actualName) -> grab sw v >>= \val -> return $ Just (actualName, val)
_ -> dontUnderstand (show expr)
where dontUnderstand s = bailOut $ Just [ "Unable to understand solver output."
, "While trying to process: " ++ s
]
grab :: SW -> SExpr -> Query GeneralizedCW
grab s topExpr = extract topExpr
where k = kindOf s
isFromInteger sw = or [f sw | f <- [isBoolean, isBounded, isInteger, isReal, isFloat, isDouble]]
getUIIndex (KUserSort _ (Right xs)) i = i `elemIndex` xs
getUIIndex _ _ = Nothing
guard True v = return v
guard False _ = dontUnderstand (show topExpr)
extract (ENum i) = guard (isFromInteger s) $ RegularCW $ mkConstCW k (fst i)
extract (EReal i) = guard (isReal s) $ RegularCW $ CW KReal (CWAlgReal i)
extract (EFloat i) = guard (isFloat s) $ RegularCW $ CW KFloat (CWFloat i)
extract (EDouble i) = guard (isDouble s) $ RegularCW $ CW KDouble (CWDouble i)
extract (ECon i)
| isUninterpreted s = return $ RegularCW $ CW k (CWUserSort (getUIIndex k i, i))
extract val = ExtendedCW <$> cvt (simplify val)
cvt :: SExpr -> Query ExtCW
cvt (ECon "oo") = return $ Infinite k
cvt (ECon "epsilon") = return $ Epsilon k
cvt (EApp [ECon "interval", x, y]) = Interval <$> cvt x <*> cvt y
cvt (ENum (i, _)) = return $ BoundedCW $ mkConstCW k i
cvt (EReal r) = return $ BoundedCW $ CW k $ CWAlgReal r
cvt (EFloat f) = return $ BoundedCW $ CW k $ CWFloat f
cvt (EDouble d) = return $ BoundedCW $ CW k $ CWDouble d
cvt (EApp [ECon "+", x, y]) = AddExtCW <$> cvt x <*> cvt y
cvt (EApp [ECon "*", x, y]) = MulExtCW <$> cvt x <*> cvt y
cvt e = dontUnderstand (show e)
simplify :: SExpr -> SExpr
simplify (EApp [ECon "to_real", n]) = n
simplify (EApp xs) = EApp (map simplify xs)
simplify e = e
checkSatAssuming :: [SBool] -> Query CheckSatResult
checkSatAssuming sBools = fst <$> checkSatAssumingHelper False sBools
checkSatAssumingWithUnsatisfiableSet :: [SBool] -> Query (CheckSatResult, Maybe [SBool])
checkSatAssumingWithUnsatisfiableSet = checkSatAssumingHelper True
checkSatAssumingHelper :: Bool -> [SBool] -> Query (CheckSatResult, Maybe [SBool])
checkSatAssumingHelper getAssumptions sBools = do
let mkAssumption st = do swsOriginal <- mapM (\sb -> do sw <- sbvToSW st sb
return (sw, sb)) sBools
let swbs = [p | p@(sw, _) <- nubBy ((==) `on` fst) swsOriginal, sw /= trueSW]
uniqueSWBs <- mapM (\(sw, sb) -> do unique <- incrementInternalCounter st
return (sw, (unique, sb))) swbs
let translate (sw, (unique, sb)) = (nm, decls, (proxy, sb))
where nm = show sw
proxy = "__assumption_proxy_" ++ nm ++ "_" ++ show unique
decls = [ "(declare-const " ++ proxy ++ " Bool)"
, "(assert (= " ++ proxy ++ " " ++ nm ++ "))"
]
return $ map translate uniqueSWBs
assumptions <- inNewContext mkAssumption
let (origNames, declss, proxyMap) = unzip3 assumptions
let cmd = "(check-sat-assuming (" ++ unwords (map fst proxyMap) ++ "))"
bad = unexpected "checkSatAssuming" cmd "one of sat/unsat/unknown"
$ Just [ "Make sure you use:"
, ""
, " setOption $ ProduceUnsatAssumptions True"
, ""
, "to tell the solver to produce unsat assumptions."
]
mapM_ (send True) $ concat declss
r <- ask cmd
let grabUnsat
| getAssumptions = do as <- getUnsatAssumptions origNames proxyMap
return (Unsat, Just as)
| True = return (Unsat, Nothing)
parse r bad $ \case ECon "sat" -> return (Sat, Nothing)
ECon "unsat" -> grabUnsat
ECon "unknown" -> return (Unk, Nothing)
_ -> bad r Nothing
getAssertionStackDepth :: Query Int
getAssertionStackDepth = queryAssertionStackDepth <$> getQueryState
inNewAssertionStack :: Query a -> Query a
inNewAssertionStack q = do push 1
r <- q
pop 1
return r
push :: Int -> Query ()
push i
| i <= 0 = error $ "Data.SBV: push requires a strictly positive level argument, received: " ++ show i
| True = do depth <- getAssertionStackDepth
send True $ "(push " ++ show i ++ ")"
modifyQueryState $ \s -> s{queryAssertionStackDepth = depth + i}
pop :: Int -> Query ()
pop i
| i <= 0 = error $ "Data.SBV: pop requires a strictly positive level argument, received: " ++ show i
| True = do depth <- getAssertionStackDepth
if i > depth
then error $ "Data.SBV: Illegally trying to pop " ++ shl i ++ ", at current level: " ++ show depth
else do QueryState{queryConfig} <- getQueryState
if not (supportsGlobalDecls (capabilities (solver queryConfig)))
then error $ unlines [ ""
, "*** Data.SBV: Backend solver does not support global-declarations."
, "*** Hence, calls to 'pop' are not supported."
, "***"
, "*** Request this as a feature for the underlying solver!"
]
else do send True $ "(pop " ++ show i ++ ")"
modifyQueryState $ \s -> s{queryAssertionStackDepth = depth - i}
where shl 1 = "one level"
shl n = show n ++ " levels"
caseSplit :: Bool -> [(String, SBool)] -> Query (Maybe (String, SMTResult))
caseSplit printCases cases = do cfg <- getConfig
go cfg (cases ++ [("Coverage", bnot (bOr (map snd cases)))])
where msg = when printCases . io . putStrLn
go _ [] = return Nothing
go cfg ((n,c):ncs) = do let notify s = msg $ "Case " ++ n ++ ": " ++ s
notify "Starting"
r <- checkSatAssuming [c]
case r of
Unsat -> do notify "Unsatisfiable"
go cfg ncs
Sat -> do notify "Satisfiable"
res <- Satisfiable cfg <$> getModel
return $ Just (n, res)
Unk -> do notify "Unknown"
res <- Unknown cfg <$> getUnknownReason
return $ Just (n, res)
resetAssertions :: Query ()
resetAssertions = do send True "(reset-assertions)"
modifyQueryState $ \s -> s{queryAssertionStackDepth = 0}
echo :: String -> Query ()
echo s = do let cmd = "(echo \"" ++ concatMap sanitize s ++ "\")"
_ <- ask cmd
return ()
where sanitize '"' = "\"\""
sanitize c = [c]
exit :: Query ()
exit = do send True "(exit)"
modifyQueryState $ \s -> s{queryAssertionStackDepth = 0}
getUnsatCore :: Query [String]
getUnsatCore = do
let cmd = "(get-unsat-core)"
bad = unexpected "getUnsatCore" cmd "an unsat-core response"
$ Just [ "Make sure you use:"
, ""
, " setOption $ ProduceUnsatCores True"
, ""
, "so the solver will be ready to compute unsat cores,"
, "and that there is a model by first issuing a 'checkSat' call."
]
r <- ask cmd
parse r bad $ \case
EApp es | Just xs <- mapM fromECon es -> return $ map unBar xs
_ -> bad r Nothing
getProof :: Query String
getProof = do
let cmd = "(get-proof)"
bad = unexpected "getProof" cmd "a get-proof response"
$ Just [ "Make sure you use:"
, ""
, " setOption $ ProduceProofs True"
, ""
, "to make sure the solver is ready for producing proofs."
, "and that there is a proof by first issuing a 'checkSat' call."
]
r <- ask cmd
parse r bad $ \_ -> return r
getAssertions :: Query [String]
getAssertions = do
let cmd = "(get-assertions)"
bad = unexpected "getAssertions" cmd "a get-assertions response"
$ Just [ "Make sure you use:"
, ""
, " setOption $ ProduceAssertions True"
, ""
, "to make sure the solver is ready for producing assertions."
]
render = serialize False
r <- ask cmd
parse r bad $ \pe -> case pe of
EApp xs -> return $ map render xs
_ -> return [render pe]
getAssignment :: Query [(String, Bool)]
getAssignment = do
let cmd = "(get-assignment)"
bad = unexpected "getAssignment" cmd "a get-assignment response"
$ Just [ "Make sure you use:"
, ""
, " setOption $ ProduceAssignments True"
, ""
, "to make sure the solver is ready for producing assignments,"
, "and that there is a model by first issuing a 'checkSat' call."
]
grab (EApp [ECon s, ENum (0, _)]) = Just (unQuote s, False)
grab (EApp [ECon s, ENum (1, _)]) = Just (unQuote s, True)
grab _ = Nothing
r <- ask cmd
parse r bad $ \case EApp ps | Just vs <- mapM grab ps -> return vs
_ -> bad r Nothing
infix 1 |->
(|->) :: SymWord a => SBV a -> a -> Assignment
SBV a |-> v = case literal v of
SBV (SVal _ (Left cw)) -> Assign a cw
r -> error $ "Data.SBV: Impossible happened in |->: Cannot construct a CW with literal: " ++ show r
mkSMTResult :: [Assignment] -> Query SMTResult
mkSMTResult asgns = do
QueryState{queryConfig} <- getQueryState
inps <- getQuantifiedInputs
let grabValues st = do let extract (Assign s n) = sbvToSW st (SBV s) >>= \sw -> return (sw, n)
modelAssignment <- mapM extract asgns
let userSS = map fst modelAssignment
missing, extra, dup :: [String]
missing = [n | (EX, (s, n)) <- inps, s `notElem` userSS]
extra = [show s | s <- userSS, s `notElem` map (fst . snd) inps]
dup = let walk [] = []
walk (n:ns)
| n `elem` ns = show n : walk (filter (/= n) ns)
| True = walk ns
in walk userSS
unless (null (missing ++ extra ++ dup)) $ do
let misTag = "*** Missing inputs"
dupTag = "*** Duplicate bindings"
extTag = "*** Extra bindings"
maxLen = maximum $ 0
: [length misTag | not (null missing)]
++ [length extTag | not (null extra)]
++ [length dupTag | not (null dup)]
align s = s ++ replicate (maxLen - length s) ' ' ++ ": "
error $ unlines $ [""
, "*** Data.SBV: Query model construction has a faulty assignment."
, "***"
]
++ [ align misTag ++ intercalate ", " missing | not (null missing)]
++ [ align extTag ++ intercalate ", " extra | not (null extra) ]
++ [ align dupTag ++ intercalate ", " dup | not (null dup) ]
++ [ "***"
, "*** Data.SBV: Check your query result construction!"
]
let findName s = case [nm | (_, (i, nm)) <- inps, s == i] of
[nm] -> nm
[] -> error "*** Data.SBV: Impossible happened: Cannot find " ++ show s ++ " in the input list"
nms -> error $ unlines [ ""
, "*** Data.SBV: Impossible happened: Multiple matches for: " ++ show s
, "*** Candidates: " ++ unwords nms
]
return [(findName s, n) | (s, n) <- modelAssignment]
assocs <- inNewContext grabValues
let m = SMTModel { modelObjectives = []
, modelAssocs = assocs
}
return $ Satisfiable queryConfig m