@@ -44,7 +44,7 @@ import Booster.Definition.Base qualified as Definition (RewriteRule (..))
44
44
import Booster.LLVM as LLVM (API )
45
45
import Booster.Log
46
46
import Booster.Pattern.ApplyEquations qualified as ApplyEquations
47
- import Booster.Pattern.Base (Pattern (.. ), Sort (SortApp ), Term , Variable )
47
+ import Booster.Pattern.Base (Pattern (.. ), Sort (SortApp ))
48
48
import Booster.Pattern.Base qualified as Pattern
49
49
import Booster.Pattern.Implies (runImplies )
50
50
import Booster.Pattern.Pretty
@@ -55,12 +55,11 @@ import Booster.Pattern.Rewrite (
55
55
RewriteTrace (.. ),
56
56
performRewrite ,
57
57
)
58
+ import Booster.Pattern.Substitution qualified as Substitution
58
59
import Booster.Pattern.Util (
59
60
freeVariables ,
60
61
sortOfPattern ,
61
62
sortOfTerm ,
62
- substituteInPredicate ,
63
- substituteInTerm ,
64
63
)
65
64
import Booster.Prettyprinter (renderText )
66
65
import Booster.SMT.Interface qualified as SMT
@@ -69,6 +68,7 @@ import Booster.Syntax.Json.Externalise
69
68
import Booster.Syntax.Json.Internalise (
70
69
InternalisedPredicates (.. ),
71
70
TermOrPredicates (.. ),
71
+ extractSubstitution ,
72
72
internalisePattern ,
73
73
internaliseTermOrPredicate ,
74
74
logPatternError ,
@@ -131,12 +131,14 @@ respond stateVar request =
131
131
[ req. logSuccessfulRewrites
132
132
, req. logFailedRewrites
133
133
]
134
- -- apply the given substitution before doing anything else
134
+ -- apply the given substitution before doing anything else,
135
+ -- as internalisePattern does not substitute
135
136
let substPat =
136
137
Pattern
137
- { term = substituteInTerm substitution term
138
- , constraints = Set. fromList $ map (substituteInPredicate substitution) preds
138
+ { term = Substitution. substituteInTerm substitution term
139
+ , constraints = Set. fromList $ map (Substitution. substituteInPredicate substitution) preds
139
140
, ceilConditions = ceils
141
+ , substitution
140
142
}
141
143
-- remember all variables used in the substitutions
142
144
substVars =
@@ -166,7 +168,7 @@ respond stateVar request =
166
168
result <-
167
169
performRewrite rewriteConfig substPat
168
170
SMT. finaliseSolver solver
169
- pure $ execResponse req result substitution unsupported
171
+ pure $ execResponse req result unsupported
170
172
RpcTypes. AddModule RpcTypes. AddModuleRequest {_module, nameAsId = nameAsId'} -> Booster.Log. withContext CtxAddModule $ runExceptT $ do
171
173
-- block other request executions while modifying the server state
172
174
state <- liftIO $ takeMVar stateVar
@@ -244,20 +246,14 @@ respond stateVar request =
244
246
RpcError. CouldNotVerifyPattern $
245
247
map patternErrorToRpcError patternErrors
246
248
-- term and predicate (pattern)
247
- Right (TermAndPredicates pat substitution unsupported) -> do
249
+ -- NOTE: the input substitution will have already been applied by internaliseTermOrPredicate
250
+ Right (TermAndPredicates pat unsupported) -> do
248
251
unless (null unsupported) $ do
249
252
withKorePatternContext (KoreJson. KJAnd (externaliseSort $ sortOfPattern pat) unsupported) $ do
250
253
logMessage (" ignoring unsupported predicate parts" :: Text )
251
- -- apply the given substitution before doing anything else
252
- let substPat =
253
- Pattern
254
- { term = substituteInTerm substitution pat. term
255
- , constraints = Set. map (substituteInPredicate substitution) pat. constraints
256
- , ceilConditions = pat. ceilConditions
257
- }
258
- ApplyEquations. evaluatePattern def mLlvmLibrary solver mempty substPat >>= \ case
254
+ ApplyEquations. evaluatePattern def mLlvmLibrary solver mempty pat >>= \ case
259
255
(Right newPattern, _) -> do
260
- let (term, mbPredicate, mbSubstitution) = externalisePattern newPattern substitution
256
+ let (term, mbPredicate, mbSubstitution) = externalisePattern newPattern
261
257
tSort = externaliseSort (sortOfPattern newPattern)
262
258
result = case catMaybes (mbPredicate : mbSubstitution : map Just unsupported) of
263
259
[] -> term
@@ -281,23 +277,24 @@ respond stateVar request =
281
277
withKorePatternContext (KoreJson. KJAnd (externaliseSort $ SortApp " SortBool" [] ) ps. unsupported) $ do
282
278
logMessage (" ignoring unsupported predicate parts" :: Text )
283
279
-- apply the given substitution before doing anything else
284
- let predicates = map (substituteInPredicate ps. substitution) ps. boolPredicates
280
+ let predicates = map (Substitution. substituteInPredicate ps. substitution) ps. boolPredicates
285
281
withContext CtxConstraint $
286
282
ApplyEquations. simplifyConstraints
287
283
def
288
284
mLlvmLibrary
289
285
solver
290
286
mempty
291
- predicates
287
+ ( predicates <> Substitution. asEquations ps . substitution)
292
288
>>= \ case
293
- (Right newPreds , _) -> do
289
+ (Right simplified , _) -> do
294
290
let predicateSort =
295
291
fromMaybe (error " not a predicate" ) $
296
292
sortOfJson req. state. term
293
+ (simplifiedSubstitution, simplifiedPredicates) = extractSubstitution simplified
297
294
result =
298
- map (externalisePredicate predicateSort) newPreds
295
+ map (externalisePredicate predicateSort) ( Set. toList simplifiedPredicates)
299
296
<> map (externaliseCeil predicateSort) ps. ceilPredicates
300
- <> map (uncurry $ externaliseSubstitution predicateSort) (Map. toList ps . substitution )
297
+ <> map (uncurry $ externaliseSubstitution predicateSort) (Map. assocs simplifiedSubstitution )
301
298
<> ps. unsupported
302
299
303
300
pure $ Right (addHeader $ Syntax. KJAnd predicateSort result)
@@ -332,7 +329,7 @@ respond stateVar request =
332
329
-- term and predicates were sent. Only work on predicates
333
330
(boolPs, suppliedSubst) <-
334
331
case things of
335
- TermAndPredicates pat substitution unsupported -> do
332
+ TermAndPredicates pat unsupported -> do
336
333
withContext CtxGetModel $
337
334
logMessage' (" ignoring supplied terms and only checking predicates" :: Text )
338
335
@@ -341,7 +338,7 @@ respond stateVar request =
341
338
logMessage' (" ignoring unsupported predicates" :: Text )
342
339
withContext CtxDetail $
343
340
logMessage (Text. unwords $ map prettyPattern unsupported)
344
- pure (Set. toList pat. constraints, substitution)
341
+ pure (Set. toList pat. constraints, pat . substitution)
345
342
Predicates ps -> do
346
343
unless (null ps. ceilPredicates && null ps. unsupported) $ do
347
344
withContext CtxGetModel $ do
@@ -472,21 +469,20 @@ execStateToKoreJson RpcTypes.ExecuteState{term = t, substitution, predicate} =
472
469
execResponse ::
473
470
RpcTypes. ExecuteRequest ->
474
471
(Natural , Seq (RewriteTrace () ), RewriteResult Pattern ) ->
475
- Map Variable Term ->
476
472
[Syntax. KorePattern ] ->
477
473
Either ErrorObj (RpcTypes. API 'RpcTypes.Res )
478
- execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
474
+ execResponse req (d, traces, rr) unsupported = case rr of
479
475
RewriteBranch p nexts ->
480
476
Right $
481
477
RpcTypes. Execute
482
478
RpcTypes. ExecuteResult
483
479
{ reason = RpcTypes. Branching
484
480
, depth
485
481
, logs
486
- , state = toExecState p originalSubstitution unsupported Nothing
482
+ , state = toExecState p unsupported Nothing
487
483
, nextStates =
488
484
Just $
489
- map (\ (_, muid, p') -> toExecState p' originalSubstitution unsupported (Just muid)) $
485
+ map (\ (_, muid, p') -> toExecState p' unsupported (Just muid)) $
490
486
toList nexts
491
487
, rule = Nothing
492
488
, unknownPredicate = Nothing
@@ -498,7 +494,7 @@ execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
498
494
{ reason = RpcTypes. Stuck
499
495
, depth
500
496
, logs
501
- , state = toExecState p originalSubstitution unsupported Nothing
497
+ , state = toExecState p unsupported Nothing
502
498
, nextStates = Nothing
503
499
, rule = Nothing
504
500
, unknownPredicate = Nothing
@@ -510,7 +506,7 @@ execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
510
506
{ reason = RpcTypes. Vacuous
511
507
, depth
512
508
, logs
513
- , state = toExecState p originalSubstitution unsupported Nothing
509
+ , state = toExecState p unsupported Nothing
514
510
, nextStates = Nothing
515
511
, rule = Nothing
516
512
, unknownPredicate = Nothing
@@ -522,8 +518,8 @@ execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
522
518
{ reason = RpcTypes. CutPointRule
523
519
, depth
524
520
, logs
525
- , state = toExecState p originalSubstitution unsupported Nothing
526
- , nextStates = Just [toExecState next originalSubstitution unsupported Nothing ]
521
+ , state = toExecState p unsupported Nothing
522
+ , nextStates = Just [toExecState next unsupported Nothing ]
527
523
, rule = Just lbl
528
524
, unknownPredicate = Nothing
529
525
}
@@ -534,7 +530,7 @@ execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
534
530
{ reason = RpcTypes. TerminalRule
535
531
, depth
536
532
, logs
537
- , state = toExecState p originalSubstitution unsupported Nothing
533
+ , state = toExecState p unsupported Nothing
538
534
, nextStates = Nothing
539
535
, rule = Just lbl
540
536
, unknownPredicate = Nothing
@@ -546,7 +542,7 @@ execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
546
542
{ reason = RpcTypes. DepthBound
547
543
, depth
548
544
, logs
549
- , state = toExecState p originalSubstitution unsupported Nothing
545
+ , state = toExecState p unsupported Nothing
550
546
, nextStates = Nothing
551
547
, rule = Nothing
552
548
, unknownPredicate = Nothing
@@ -563,7 +559,7 @@ execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
563
559
(logSuccessfulRewrites, logFailedRewrites)
564
560
(RewriteStepFailed failure)
565
561
in logs <|> abortRewriteLog
566
- , state = toExecState p originalSubstitution unsupported Nothing
562
+ , state = toExecState p unsupported Nothing
567
563
, nextStates = Nothing
568
564
, rule = Nothing
569
565
, unknownPredicate = Nothing
@@ -586,8 +582,8 @@ execResponse req (d, traces, rr) originalSubstitution unsupported = case rr of
586
582
xs@ (_ : _) -> Just xs
587
583
588
584
toExecState ::
589
- Pattern -> Map Variable Term -> [Syntax. KorePattern ] -> Maybe UniqueId -> RpcTypes. ExecuteState
590
- toExecState pat sub unsupported muid =
585
+ Pattern -> [Syntax. KorePattern ] -> Maybe UniqueId -> RpcTypes. ExecuteState
586
+ toExecState pat unsupported muid =
591
587
RpcTypes. ExecuteState
592
588
{ term = addHeader t
593
589
, predicate = addHeader <$> addUnsupported p
@@ -597,7 +593,7 @@ toExecState pat sub unsupported muid =
597
593
, ruleId = getUniqueId <$> muid
598
594
}
599
595
where
600
- (t, p, s) = externalisePattern pat sub
596
+ (t, p, s) = externalisePattern pat
601
597
termSort = externaliseSort $ sortOfPattern pat
602
598
allUnsupported = Syntax. KJAnd termSort unsupported
603
599
addUnsupported
0 commit comments