@@ -22,7 +22,7 @@ import Control.Monad
2222import Control.Monad.IO.Class
2323import Control.Monad.Trans.Except (catchE , except , runExcept , runExceptT , throwE , withExceptT )
2424import Crypto.Hash (SHA256 (.. ), hashWith )
25- import Data.Bifunctor (second )
25+ import Data.Bifunctor (first , second )
2626import Data.Foldable
2727import Data.List (singleton )
2828import Data.Map.Strict (Map )
@@ -38,7 +38,7 @@ import GHC.Records
3838import Numeric.Natural
3939
4040import Booster.CLOptions (RewriteOptions (.. ))
41- import Booster.Definition.Attributes.Base (UniqueId , getUniqueId , uniqueId )
41+ import Booster.Definition.Attributes.Base (getUniqueId , uniqueId )
4242import Booster.Definition.Base (KoreDefinition (.. ))
4343import Booster.Definition.Base qualified as Definition (RewriteRule (.. ))
4444import Booster.LLVM as LLVM (API )
@@ -49,6 +49,7 @@ import Booster.Pattern.Base qualified as Pattern
4949import Booster.Pattern.Implies (runImplies )
5050import Booster.Pattern.Pretty
5151import Booster.Pattern.Rewrite (
52+ AppliedRuleMetadata (.. ),
5253 RewriteConfig (.. ),
5354 RewriteFailed (.. ),
5455 RewriteResult (.. ),
@@ -57,7 +58,9 @@ import Booster.Pattern.Rewrite (
5758 )
5859import Booster.Pattern.Substitution qualified as Substitution
5960import Booster.Pattern.Util (
61+ externaliseRuleMarker ,
6062 freeVariables ,
63+ modifyVarName ,
6164 sortOfPattern ,
6265 sortOfTerm ,
6366 )
@@ -479,11 +482,13 @@ execResponse req (d, traces, rr) unsupported = case rr of
479482 { reason = RpcTypes. Branching
480483 , depth
481484 , logs
482- , state = toExecState p unsupported Nothing
485+ , state = toExecState p Nothing unsupported
483486 , nextStates =
484- Just $
485- map (\ (_, muid, p') -> toExecState p' unsupported (Just muid)) $
486- toList nexts
487+ Just
488+ $ map
489+ ( \ (rewritten, ruleMetadata) -> toExecState rewritten (Just ruleMetadata) unsupported
490+ )
491+ $ toList nexts
487492 , rule = Nothing
488493 , unknownPredicate = Nothing
489494 }
@@ -494,7 +499,7 @@ execResponse req (d, traces, rr) unsupported = case rr of
494499 { reason = RpcTypes. Stuck
495500 , depth
496501 , logs
497- , state = toExecState p unsupported Nothing
502+ , state = toExecState p Nothing unsupported
498503 , nextStates = Nothing
499504 , rule = Nothing
500505 , unknownPredicate = Nothing
@@ -506,7 +511,7 @@ execResponse req (d, traces, rr) unsupported = case rr of
506511 { reason = RpcTypes. Vacuous
507512 , depth
508513 , logs
509- , state = toExecState p unsupported Nothing
514+ , state = toExecState p Nothing unsupported
510515 , nextStates = Nothing
511516 , rule = Nothing
512517 , unknownPredicate = Nothing
@@ -518,8 +523,8 @@ execResponse req (d, traces, rr) unsupported = case rr of
518523 { reason = RpcTypes. CutPointRule
519524 , depth
520525 , logs
521- , state = toExecState p unsupported Nothing
522- , nextStates = Just [toExecState next unsupported Nothing ]
526+ , state = toExecState p Nothing unsupported
527+ , nextStates = Just [toExecState next Nothing unsupported ]
523528 , rule = Just lbl
524529 , unknownPredicate = Nothing
525530 }
@@ -530,7 +535,7 @@ execResponse req (d, traces, rr) unsupported = case rr of
530535 { reason = RpcTypes. TerminalRule
531536 , depth
532537 , logs
533- , state = toExecState p unsupported Nothing
538+ , state = toExecState p Nothing unsupported
534539 , nextStates = Nothing
535540 , rule = Just lbl
536541 , unknownPredicate = Nothing
@@ -542,7 +547,7 @@ execResponse req (d, traces, rr) unsupported = case rr of
542547 { reason = RpcTypes. DepthBound
543548 , depth
544549 , logs
545- , state = toExecState p unsupported Nothing
550+ , state = toExecState p Nothing unsupported
546551 , nextStates = Nothing
547552 , rule = Nothing
548553 , unknownPredicate = Nothing
@@ -559,7 +564,7 @@ execResponse req (d, traces, rr) unsupported = case rr of
559564 (logSuccessfulRewrites, logFailedRewrites)
560565 (RewriteStepFailed failure)
561566 in logs <|> abortRewriteLog
562- , state = toExecState p unsupported Nothing
567+ , state = toExecState p Nothing unsupported
563568 , nextStates = Nothing
564569 , rule = Nothing
565570 , unknownPredicate = Nothing
@@ -582,23 +587,37 @@ execResponse req (d, traces, rr) unsupported = case rr of
582587 xs@ (_ : _) -> Just xs
583588
584589toExecState ::
585- Pattern -> [Syntax. KorePattern ] -> Maybe UniqueId -> RpcTypes. ExecuteState
586- toExecState pat unsupported muid =
590+ Pattern ->
591+ Maybe AppliedRuleMetadata ->
592+ [Syntax. KorePattern ] ->
587593 RpcTypes. ExecuteState
588- { term = addHeader t
589- , predicate = addHeader <$> addUnsupported p
590- , substitution = addHeader <$> s
591- , ruleSubstitution = Nothing
592- , rulePredicate = Nothing
593- , ruleId = getUniqueId <$> muid
594- }
595- where
596- (t, p, s) = externalisePattern pat
597- termSort = externaliseSort $ sortOfPattern pat
598- allUnsupported = Syntax. KJAnd termSort unsupported
599- addUnsupported
600- | null unsupported = id
601- | otherwise = maybe (Just allUnsupported) (Just . Syntax. KJAnd termSort . (: unsupported))
594+ toExecState
595+ pat
596+ mRuleMetadata
597+ unsupported =
598+ RpcTypes. ExecuteState
599+ { term = addHeader t
600+ , predicate = addHeader <$> addUnsupported p
601+ , substitution = addHeader <$> s
602+ , ruleSubstitution = addHeader <$> mruleSubstExt
603+ , rulePredicate = addHeader <$> mrulePredExt
604+ , ruleId = getUniqueId . ruleUniqueId <$> mRuleMetadata
605+ }
606+ where
607+ mrulePredExt = externalisePredicate termSort . rulePredicate <$> mRuleMetadata
608+ mruleSubstExt =
609+ Syntax. KJAnd termSort
610+ . map
611+ (uncurry (externaliseSubstitution termSort) . first (modifyVarName externaliseRuleMarker))
612+ . Map. toList
613+ . ruleSubstitution
614+ <$> mRuleMetadata
615+ (t, p, s) = externalisePattern pat
616+ termSort = externaliseSort $ sortOfPattern pat
617+ allUnsupported = Syntax. KJAnd termSort unsupported
618+ addUnsupported
619+ | null unsupported = id
620+ | otherwise = maybe (Just allUnsupported) (Just . Syntax. KJAnd termSort . (: unsupported))
602621
603622mkLogRewriteTrace ::
604623 (Bool , Bool ) ->
@@ -639,6 +658,11 @@ mkLogRewriteTrace
639658 { reason = " Uncertain about a condition in rule"
640659 , _ruleId = Just $ getUniqueId (uniqueId $ Definition. attributes r)
641660 }
661+ RewriteRemainderPredicate rs _ _ ->
662+ Failure
663+ { reason = " Uncertain about the remainder after applying a rule"
664+ , _ruleId = Just $ getUniqueId (uniqueId $ Definition. attributes (head rs))
665+ }
642666 DefinednessUnclear r _ undefReasons ->
643667 Failure
644668 { reason = " Uncertain about definedness of rule because of: " <> pack (show undefReasons)
0 commit comments