hsmt-0.1.0.0: app/Main.hs
{-# LANGUAGE MultiWayIf #-}
module Main (main) where
import Data.List (isSuffixOf, stripPrefix)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Version (showVersion)
import System.Environment (getArgs)
import System.Exit (exitFailure)
import System.IO (BufferMode (..), hPutStrLn, hSetBuffering, stderr, stdout)
import Text.Read (readMaybe)
import HSMT.Backend (Backend (..), solverFromCommand)
import HSMT.SAT.Dimacs
import HSMT.SAT.Solver
import HSMT.SAT.Types
import HSMT.SMT.Engine (CheckOptions (..), defaultCheckOptions)
import HSMT.SMTLib.Run (renderResponse, runScriptWithBackend)
import qualified Paths_hsmt as Paths
usage :: String
usage =
unlines
[ "usage: hsmt [OPTION]... [FILE]"
, ""
, "Solve an SMT-LIB 2.6 script, or a DIMACS CNF file (FILE.cnf). Without FILE,"
, "or with FILE = -, the script is read from the standard input."
, ""
, " -t MS, --timeout=MS time limit of each check-sat, in milliseconds"
, " --minimize-cores minimize unsatisfiable cores (:smt.core.minimize)"
, " --validate check every model against the assertions"
, " --solver=CMD decide the checks with an external solver (for"
, " example --solver=z3, or --solver='cvc5 --incremental'),"
, " reading its models back; z3 is found through HSMT_Z3"
, " or the PATH"
, " -h, --help show this help"
, " --version show the version"
]
data Options = Options
{ optCheck :: CheckOptions
, optBackend :: Backend
, optFile :: Maybe FilePath
}
parseArgs :: Options -> [String] -> Either String Options
parseArgs o = \case
[] -> Right o
("-t" : ms : rest) -> timeout ms >>= \o' -> parseArgs o' rest
("--solver" : cmd : rest) -> solver cmd >>= \o' -> parseArgs o' rest
(arg : rest)
| Just ms <- stripPrefix "--timeout=" arg -> timeout ms >>= \o' -> parseArgs o' rest
| Just cmd <- stripPrefix "--solver=" arg -> solver cmd >>= \o' -> parseArgs o' rest
| arg == "--minimize-cores" -> parseArgs (check (\c -> c {coMinimizeCore = True})) rest
| arg == "--validate" -> parseArgs (check (\c -> c {coValidate = True})) rest
| arg == "-" || take 1 arg /= "-" -> case optFile o of
Nothing -> parseArgs o {optFile = if arg == "-" then Nothing else Just arg} rest
Just _ -> Left "more than one input file"
| otherwise -> Left ("unknown option: " <> arg)
where
check f = o {optCheck = f (optCheck o)}
solver cmd = case solverFromCommand (words cmd) of
Just s -> Right o {optBackend = External s}
Nothing -> Left "empty solver command"
timeout ms = case readMaybe ms :: Maybe Integer of
Just n | n > 0 -> Right (check (\c -> c {coTimeout = Just (fromInteger n / 1000)}))
_ -> Left ("invalid timeout: " <> ms)
main :: IO ()
main = do
hSetBuffering stdout LineBuffering
args <- getArgs
if
| any (`elem` ["-h", "--help"]) args -> putStr usage
| "--version" `elem` args -> putStrLn ("hsmt " <> showVersion Paths.version)
| otherwise -> case parseArgs (Options defaultCheckOptions Native Nothing) args of
Left err -> hPutStrLn stderr ("hsmt: " <> err) >> hPutStr' usage >> exitFailure
Right o -> case optFile o of
Just path | ".cnf" `isSuffixOf` path -> T.readFile path >>= dimacs
Just path -> T.readFile path >>= smtlib o
Nothing -> T.getContents >>= smtlib o
where
hPutStr' = hPutStrLn stderr
-- | Run a script, printing each response as soon as it is known.
smtlib :: Options -> T.Text -> IO ()
smtlib o = runScriptWithBackend (optBackend o) (optCheck o) (T.putStrLn . renderResponse)
dimacs :: T.Text -> IO ()
dimacs txt = case parseDimacs txt of
Left err -> putStrLn ("error: " <> err) >> exitFailure
Right cnf -> do
s <- newSolver
ok <- loadCnf s cnf
r <- if ok then solve s else pure Unsat
putStrLn $ case r of
Sat -> "s SATISFIABLE"
Unsat -> "s UNSATISFIABLE"
Unknown -> "s UNKNOWN"