Skip to content

Commit b22c9f8

Browse files
committed
Only declare variables once
1 parent 1d5d78a commit b22c9f8

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

booster/library/Booster/SMT/Interface.hs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ throwUnknown reason premises preds = throw $ SMTSolverUnknown reason premises pr
6363
smtTranslateError :: Text -> a
6464
smtTranslateError = throw . SMTTranslationError
6565

66+
{- | declare-const all introduced variables (free in predicates
67+
as well as abstraction variables) before sending assertions
68+
-}
69+
declareVariables :: Log.LoggerMIO io => TranslationState -> SMT io ()
70+
declareVariables transState = do
71+
mapM_
72+
SMT.runCmd
73+
[ DeclareConst (mkComment trm) smtId (SMT.smtSort $ sortOfTerm trm)
74+
| (trm, smtId) <- Map.assocs transState.mappings
75+
]
76+
6677
{- | Start and initialise an SMT solver instance for use in rewriting:
6778
- translate the sort declarations from @KoreDefiniton@ to SMT
6879
- start the solver process
@@ -165,14 +176,16 @@ getModelFor ctxt ps subst
165176
Log.logMessage $ "SMT translation error: " <> errMsg
166177
smtTranslateError errMsg
167178
| Right (smtAsserts, transState) <- translated = Log.withContext "smt" $ do
168-
evalSMT ctxt $ solve smtAsserts transState
179+
evalSMT ctxt $ do
180+
declareVariables transState
181+
solve smtAsserts transState
169182
where
170183
solve ::
171184
[DeclareCommand] -> TranslationState -> SMT io (Either Response (Map Variable Term))
172185
solve smtAsserts transState = do
173186
opts <- SMT $ gets (.options)
174187
Log.logMessage $ "Checking, constraint count " <> pack (show $ Map.size subst + length ps)
175-
satResponse <- interactWithSolver smtAsserts transState
188+
satResponse <- interactWithSolver smtAsserts
176189
Log.logMessage ("Solver returned " <> (Text.pack $ show satResponse))
177190
case satResponse of
178191
Error msg -> do
@@ -215,18 +228,10 @@ getModelFor ctxt ps subst
215228
mapM (\(Predicate p) -> Assert (mkComment p) <$> SMT.translateTerm p) ps
216229
pure $ smtSubst <> smtPs
217230

218-
interactWithSolver :: [DeclareCommand] -> TranslationState -> SMT io Response
219-
interactWithSolver smtAsserts transState = do
231+
interactWithSolver :: [DeclareCommand] -> SMT io Response
232+
interactWithSolver smtAsserts = do
220233
runCmd_ SMT.Push -- assuming the prelude has been run already,
221234

222-
-- declare-const all introduced variables (free in predicates
223-
-- as well as abstraction variables) before sending assertions
224-
mapM_
225-
runCmd
226-
[ DeclareConst (mkComment trm) smtId (SMT.smtSort $ sortOfTerm trm)
227-
| (trm, smtId) <- Map.assocs transState.mappings
228-
]
229-
230235
-- assert the given predicates
231236
mapM_ runCmd smtAsserts
232237

@@ -329,6 +334,7 @@ checkPredicates ctxt givenPs givenSubst psToCheck
329334
pure Nothing
330335
| Right ((smtGiven, sexprsToCheck), transState) <- translated = Log.withContext "smt" $ do
331336
evalSMT ctxt $ do
337+
declareVariables transState
332338
solve smtGiven sexprsToCheck transState
333339
where
334340
solve ::
@@ -348,7 +354,7 @@ checkPredicates ctxt givenPs givenSubst psToCheck
348354
]
349355
Log.logMessage . Pretty.renderOneLineText $
350356
hsep ("Predicates to check:" : map pretty (Set.toList psToCheck))
351-
result <- runMaybeT $ interactWihtSolver smtGiven sexprsToCheck transState
357+
result <- runMaybeT $ interactWihtSolver smtGiven sexprsToCheck
352358
Log.logMessage $
353359
"Check of Given ∧ P and Given ∧ !P produced "
354360
<> (Text.pack $ show result)
@@ -398,18 +404,10 @@ checkPredicates ctxt givenPs givenSubst psToCheck
398404
other -> throwSMT' $ "Unexpected result while calling ':reason-unknown': " <> show other
399405

400406
interactWihtSolver ::
401-
[DeclareCommand] -> [SExpr] -> TranslationState -> MaybeT (SMT io) (Response, Response)
402-
interactWihtSolver smtGiven sexprsToCheck transState = do
407+
[DeclareCommand] -> [SExpr] -> MaybeT (SMT io) (Response, Response)
408+
interactWihtSolver smtGiven sexprsToCheck = do
403409
smtRun_ Push
404410

405-
-- declare-const all introduced variables (free in predicates
406-
-- as well as abstraction variables) before sending assertions
407-
mapM_
408-
smtRun
409-
[ DeclareConst (mkComment trm) smtId (SMT.smtSort $ sortOfTerm trm)
410-
| (trm, smtId) <- Map.assocs transState.mappings
411-
]
412-
413411
-- assert ground truth
414412
mapM_ smtRun smtGiven
415413

0 commit comments

Comments
 (0)