Skip to content

Log rewrite rule remainders #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions booster/tools/booster/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ logLevelToKoreLogEntryMap =
[ "DebugAttemptedRewriteRules"
, "DebugAppliedLabeledRewriteRule"
, "DebugAppliedRewriteRules"
, "DebugRewriteRulesRemainder"
, "DebugTerm"
]
)
Expand Down
1 change: 1 addition & 0 deletions kore/kore.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ library
Kore.Log.DebugRewriteTrace
Kore.Log.DebugSolver
Kore.Log.DebugSubstitutionSimplifier
Kore.Log.DebugRewriteRulesRemainder
Kore.Log.DebugTransition
Kore.Log.DebugUnification
Kore.Log.DebugUnifyBottom
Expand Down
3 changes: 2 additions & 1 deletion kore/src/Kore/Log/DebugAppliedRewriteRules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ instance Entry DebugAppliedRewriteRules where
| null appliedRewriteRules = True
| otherwise = False
oneLineDoc DebugAppliedRewriteRules{appliedRewriteRules}
| null appliedRewriteRules = mempty
| null appliedRewriteRules =
"failed to apply " <> pretty (length appliedRewriteRules) <> " rewrite rules"
| otherwise =
"applied " <> pretty (length appliedRewriteRules) <> " rewrite rules"
oneLineJson DebugAppliedRewriteRules{appliedRewriteRules}
Expand Down
122 changes: 122 additions & 0 deletions kore/src/Kore/Log/DebugRewriteRulesRemainder.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NoStrict #-}
{-# LANGUAGE NoStrictData #-}

{- |
Copyright : (c) Runtime Verification, 2020-2021
License : BSD-3-Clause
-}
module Kore.Log.DebugRewriteRulesRemainder (
DebugRewriteRulesRemainder (..),
debugRewriteRulesRemainder,
) where

import Data.Aeson (Value (Array), object, toJSON, (.=))
import Data.Text qualified as Text
import Data.Vector (fromList)
import Kore.Internal.Conditional qualified as Conditional
import Kore.Internal.Pattern (
Pattern,
)
import Kore.Internal.Predicate (
Predicate,
)
import Kore.Internal.Predicate qualified as Predicate
import Kore.Internal.TermLike qualified as TermLike
import Kore.Internal.Variable (
VariableName,
toVariableName,
)
import Kore.Rewrite.RewritingVariable
import Kore.Syntax.Json qualified as PatternJson
import Kore.Unparser
import Kore.Util (showHashHex)
import Log
import Prelude.Kore
import Pretty (
Pretty (..),
)
import Pretty qualified

{- This log entry will be emitted if, after unifying with rewrite rules,
there is a satisfiable remainder condition -}
data DebugRewriteRulesRemainder = DebugRewriteRulesRemainder
{ configuration :: !(Pattern VariableName)
-- ^ the state the rules unified with
, rulesCount :: !Int
-- ^ how many rules were unified
, remainder :: !(Predicate RewritingVariableName)
-- ^ the condition not covered by the rules
}
deriving stock (Show)

instance Pretty DebugRewriteRulesRemainder where
pretty DebugRewriteRulesRemainder{..} =
Pretty.vsep
[ (Pretty.hsep . catMaybes)
[ Just "The rules"
, Just "produced a remainder"
, Just . pretty $ remainder
]
, "On configuration:"
, Pretty.indent 2 . unparse $ configuration
]

instance Entry DebugRewriteRulesRemainder where
entrySeverity _ = Debug
helpDoc _ = "log rewrite rules remainder"

oneLineContextDoc
DebugRewriteRulesRemainder{} = [remainderContextName]

oneLineContextJson
DebugRewriteRulesRemainder{configuration, rulesCount} =
Array $
fromList
[ toJSON remainderContextName
, object
[ "term" .= showHashHex (hash configuration)
]
, object
[ "rules-count" .= Text.pack (show rulesCount)
]
]

oneLineDoc (DebugRewriteRulesRemainder{rulesCount, remainder}) =
let context = [Pretty.brackets "detail"]
logMsg =
( Pretty.hsep
[ "After applying "
, pretty rulesCount
, " rewrite rules"
, "there is a satisfiable remainder condition: "
, Pretty.group . pretty $ remainder
]
)
in mconcat context <> logMsg

oneLineJson DebugRewriteRulesRemainder{remainder} =
toJSON
. PatternJson.fromPredicate sortBool
. Predicate.mapVariables (pure toVariableName)
$ remainder

remainderContextName :: Text.Text
remainderContextName = "remainder"

sortBool :: TermLike.Sort
sortBool =
(TermLike.SortActualSort $ TermLike.SortActual (TermLike.Id "SortBool" TermLike.AstLocationNone) [])

debugRewriteRulesRemainder ::
MonadLog log =>
Pattern RewritingVariableName ->
Int ->
Predicate RewritingVariableName ->
log ()
debugRewriteRulesRemainder pat rulesCount remainder =
logEntry DebugRewriteRulesRemainder{..}
where
configuration = mapConditionalVariables TermLike.mapVariables pat
mapConditionalVariables mapTermVariables =
Conditional.mapVariables mapTermVariables (pure toVariableName)
4 changes: 4 additions & 0 deletions kore/src/Kore/Log/Registry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ import Kore.Log.DebugProven (
import Kore.Log.DebugRetrySolverQuery (
DebugRetrySolverQuery,
)
import Kore.Log.DebugRewriteRulesRemainder (
DebugRewriteRulesRemainder,
)
import Kore.Log.DebugRewriteTrace (
DebugFinalPatterns,
DebugInitialClaim,
Expand Down Expand Up @@ -229,6 +232,7 @@ entryHelpDocsErr, entryHelpDocsNoErr :: [Pretty.Doc ()]
, mk $ Proxy @DebugTransition
, mk $ Proxy @DebugAppliedRewriteRules
, mk $ Proxy @DebugAppliedLabeledRewriteRule
, mk $ Proxy @DebugRewriteRulesRemainder
, mk $ Proxy @DebugAttemptedRewriteRules
, mk $ Proxy @DebugSubstitutionSimplifier
, mk $ Proxy @WarnFunctionWithoutEvaluators
Expand Down
2 changes: 2 additions & 0 deletions kore/src/Kore/Rewrite/RewriteStep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Kore.Log.DebugAppliedRewriteRules (
debugAppliedRewriteRules,
)
import Kore.Log.DebugCreatedSubstitution (debugCreatedSubstitution)
import Kore.Log.DebugRewriteRulesRemainder (debugRewriteRulesRemainder)
import Kore.Log.DebugRewriteTrace (
debugRewriteTrace,
)
Expand Down Expand Up @@ -323,6 +324,7 @@ finalizeRulesParallel
-- NB: the UNKNOWN case will trigger an exception in SMT.evalPredicate, which will
-- be caught by the top-level code in the RPC server and reported to the client
_ -> do
debugRewriteRulesRemainder initial (length unifiedRules) remainderPredicate
-- remainder condition is SAT: we are safe to explore
-- the remainder branch, i.e. to evaluate the functions in the configuration
-- with the remainder in the path condition and rewrite further
Expand Down
2 changes: 1 addition & 1 deletion kore/src/Log/Entry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class (Show entry, Typeable entry) => Entry entry where

oneLineContextJson :: entry -> JSON.Value
default oneLineContextJson :: entry -> JSON.Value
oneLineContextJson _ = JSON.Array mempty
oneLineContextJson entry = JSON.object ["entry" JSON..= entryTypeText (toEntry entry)]

oneLineContextDoc :: entry -> [Text]
default oneLineContextDoc :: entry -> [Text]
Expand Down
Loading