Skip to content

Commit c9f107d

Browse files
committed
Factor-out and unify failBecauseUnknown
1 parent 25afc73 commit c9f107d

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

booster/library/Booster/SMT/Interface.hs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ finaliseSolver ctxt = do
192192
Log.logMessage ("Closing SMT solver" :: Text)
193193
destroyContext ctxt
194194

195+
failBecauseUnknown ::
196+
forall io.
197+
Log.LoggerMIO io =>
198+
Set Predicate ->
199+
ExceptT SMTError (SMT io) Bool
200+
failBecauseUnknown psToCheck =
201+
smtRun GetReasonUnknown >>= \case
202+
Unknown reason -> do
203+
Log.withContext Log.CtxAbort $
204+
Log.logMessage $
205+
"Returned Unknown. Reason: " <> fromMaybe "UNKNOWN" reason
206+
throwE $ SMTSolverUnknown reason mempty psToCheck
207+
other -> do
208+
let msg = "Unexpected result while calling ':reason-unknown': " <> show other
209+
Log.withContext Log.CtxAbort $ Log.logMessage $ Text.pack msg
210+
throwSMT' msg
211+
195212
{- |
196213
Implementation of get-model request
197214
@@ -405,8 +422,8 @@ checkPredicates ctxt givenPs givenSubst psToCheck
405422
pure Nothing
406423
(Sat, Unsat) -> pure . Just $ True
407424
(Unsat, Sat) -> pure . Just $ False
408-
(Unknown reason, _) -> retry smtGiven sexprsToCheck transState reason
409-
(_, Unknown reason) -> retry smtGiven sexprsToCheck transState reason
425+
(Unknown _, _) -> retry smtGiven sexprsToCheck transState
426+
(_, Unknown _) -> retry smtGiven sexprsToCheck transState
410427
other ->
411428
throwE . GeneralSMTError $
412429
("Unexpected result while checking a condition: " :: Text) <> Text.pack (show other)
@@ -415,16 +432,14 @@ checkPredicates ctxt givenPs givenSubst psToCheck
415432
[DeclareCommand] ->
416433
[SExpr] ->
417434
TranslationState ->
418-
Maybe Text ->
419435
ExceptT SMTError (SMT io) (Maybe Bool)
420-
retry smtGiven sexprsToCheck transState reasonUnknown = do
436+
retry smtGiven sexprsToCheck transState = do
421437
opts <- lift . SMT $ gets (.options)
422438
case opts.retryLimit of
423439
Just x | x > 0 -> do
424440
lift $ hardResetSolver (updateOptionsOnRetry opts)
425441
solve smtGiven sexprsToCheck transState
426-
_ -> failBecauseUnknown reasonUnknown
427-
442+
_ -> failBecauseUnknown psToCheck >> pure Nothing -- Nothing is unreachable and is here to make the type checker happy
428443
translated :: Either Text (([DeclareCommand], [SExpr]), TranslationState)
429444
translated = SMT.runTranslator $ do
430445
let mkSMTEquation v t =
@@ -437,13 +452,6 @@ checkPredicates ctxt givenPs givenSubst psToCheck
437452
mapM (SMT.translateTerm . coerce) $ Set.toList psToCheck
438453
pure (smtSubst <> smtPs, toCheck)
439454

440-
failBecauseUnknown :: Maybe Text -> ExceptT SMTError (SMT io) (Maybe Bool)
441-
failBecauseUnknown reason = do
442-
Log.withContext Log.CtxAbort $
443-
Log.logMessage $
444-
"Returned Unknown. Reason: " <> fromMaybe "UNKNOWN" reason
445-
throwE $ SMTSolverUnknown reason givenPs psToCheck
446-
447455
-- Given the known truth and the expressions to check,
448456
-- interact with the solver to establish the validity of the expressions.
449457
--
@@ -553,17 +561,4 @@ isSat ctxt psToCheck
553561
lift $ hardResetSolver (updateOptionsOnRetry opts)
554562
Log.logMessage ("Retrying with higher timeout" :: Text)
555563
solve'
556-
_ -> failBecauseUnknown
557-
558-
failBecauseUnknown :: ExceptT SMTError (SMT io) Bool
559-
failBecauseUnknown =
560-
smtRun GetReasonUnknown >>= \case
561-
Unknown reason -> do
562-
Log.withContext Log.CtxAbort $
563-
Log.logMessage $
564-
"Returned Unknown. Reason: " <> fromMaybe "UNKNOWN" reason
565-
throwE $ SMTSolverUnknown reason mempty psToCheck
566-
other -> do
567-
let msg = "Unexpected result while calling ':reason-unknown': " <> show other
568-
Log.withContext Log.CtxAbort $ Log.logMessage $ Text.pack msg
569-
throwSMT' msg
564+
_ -> failBecauseUnknown psToCheck

0 commit comments

Comments
 (0)