|
| 1 | +{-# LANGUAGE RecordWildCards #-} |
| 2 | +{-# LANGUAGE NoStrict #-} |
| 3 | +{-# LANGUAGE NoStrictData #-} |
| 4 | + |
| 5 | +{- | |
| 6 | +Copyright : (c) Runtime Verification, 2020-2021 |
| 7 | +License : BSD-3-Clause |
| 8 | +-} |
| 9 | +module Kore.Log.DebugRewriteRulesRemainder ( |
| 10 | + DebugRewriteRulesRemainder (..), |
| 11 | + debugRewriteRulesRemainder, |
| 12 | +) where |
| 13 | + |
| 14 | +import Data.Aeson (Value (Array), object, toJSON, (.=)) |
| 15 | +import Data.Text qualified as Text |
| 16 | +import Data.Vector (fromList) |
| 17 | +import Kore.Internal.Conditional qualified as Conditional |
| 18 | +import Kore.Internal.Pattern ( |
| 19 | + Pattern, |
| 20 | + ) |
| 21 | +import Kore.Internal.Predicate ( |
| 22 | + Predicate, |
| 23 | + ) |
| 24 | +import Kore.Internal.Predicate qualified as Predicate |
| 25 | +import Kore.Internal.TermLike qualified as TermLike |
| 26 | +import Kore.Internal.Variable ( |
| 27 | + VariableName, |
| 28 | + toVariableName, |
| 29 | + ) |
| 30 | +import Kore.Rewrite.RewritingVariable |
| 31 | +import Kore.Syntax.Json qualified as PatternJson |
| 32 | +import Kore.Unparser |
| 33 | +import Kore.Util (showHashHex) |
| 34 | +import Log |
| 35 | +import Prelude.Kore |
| 36 | +import Pretty ( |
| 37 | + Pretty (..), |
| 38 | + ) |
| 39 | +import Pretty qualified |
| 40 | + |
| 41 | +{- This log entry will be emitted if, after unifying with rewrite rules, |
| 42 | + there is a satisfiable remainder condition -} |
| 43 | +data DebugRewriteRulesRemainder = DebugRewriteRulesRemainder |
| 44 | + { configuration :: !(Pattern VariableName) |
| 45 | + -- ^ the state the rules unified with |
| 46 | + , rulesCount :: !Int |
| 47 | + -- ^ how many rules were unified |
| 48 | + , remainder :: !(Predicate RewritingVariableName) |
| 49 | + -- ^ the condition not covered by the rules |
| 50 | + } |
| 51 | + deriving stock (Show) |
| 52 | + |
| 53 | +instance Pretty DebugRewriteRulesRemainder where |
| 54 | + pretty DebugRewriteRulesRemainder{..} = |
| 55 | + Pretty.vsep |
| 56 | + [ (Pretty.hsep . catMaybes) |
| 57 | + [ Just "The rules" |
| 58 | + , Just "produced a remainder" |
| 59 | + , Just . pretty $ remainder |
| 60 | + ] |
| 61 | + , "On configuration:" |
| 62 | + , Pretty.indent 2 . unparse $ configuration |
| 63 | + ] |
| 64 | + |
| 65 | +instance Entry DebugRewriteRulesRemainder where |
| 66 | + entrySeverity _ = Debug |
| 67 | + helpDoc _ = "log rewrite rules remainder" |
| 68 | + |
| 69 | + oneLineContextDoc |
| 70 | + DebugRewriteRulesRemainder{} = [remainderContextName] |
| 71 | + |
| 72 | + oneLineContextJson |
| 73 | + DebugRewriteRulesRemainder{configuration, rulesCount} = |
| 74 | + Array $ |
| 75 | + fromList |
| 76 | + [ toJSON remainderContextName |
| 77 | + , object |
| 78 | + [ "term" .= showHashHex (hash configuration) |
| 79 | + ] |
| 80 | + , object |
| 81 | + [ "rules-count" .= Text.pack (show rulesCount) |
| 82 | + ] |
| 83 | + ] |
| 84 | + |
| 85 | + oneLineDoc (DebugRewriteRulesRemainder{rulesCount, remainder}) = |
| 86 | + let context = [Pretty.brackets "detail"] |
| 87 | + logMsg = |
| 88 | + ( Pretty.hsep |
| 89 | + [ "After applying " |
| 90 | + , pretty rulesCount |
| 91 | + , " rewrite rules" |
| 92 | + , "there is a satisfiable remainder condition: " |
| 93 | + , Pretty.group . pretty $ remainder |
| 94 | + ] |
| 95 | + ) |
| 96 | + in mconcat context <> logMsg |
| 97 | + |
| 98 | + oneLineJson DebugRewriteRulesRemainder{remainder} = |
| 99 | + toJSON |
| 100 | + . PatternJson.fromPredicate sortBool |
| 101 | + . Predicate.mapVariables (pure toVariableName) |
| 102 | + $ remainder |
| 103 | + |
| 104 | +remainderContextName :: Text.Text |
| 105 | +remainderContextName = "remainder" |
| 106 | + |
| 107 | +sortBool :: TermLike.Sort |
| 108 | +sortBool = |
| 109 | + (TermLike.SortActualSort $ TermLike.SortActual (TermLike.Id "SortBool" TermLike.AstLocationNone) []) |
| 110 | + |
| 111 | +debugRewriteRulesRemainder :: |
| 112 | + MonadLog log => |
| 113 | + Pattern RewritingVariableName -> |
| 114 | + Int -> |
| 115 | + Predicate RewritingVariableName -> |
| 116 | + log () |
| 117 | +debugRewriteRulesRemainder pat rulesCount remainder = |
| 118 | + logEntry DebugRewriteRulesRemainder{..} |
| 119 | + where |
| 120 | + configuration = mapConditionalVariables TermLike.mapVariables pat |
| 121 | + mapConditionalVariables mapTermVariables = |
| 122 | + Conditional.mapVariables mapTermVariables (pure toVariableName) |
0 commit comments