Skip to content

Commit 4b24eba

Browse files
Attempt no. 2 at fixing rule-substitution (#3939)
Fixes #3912? There were three further issues that I identified: * When we have a `VarX` in both the rule and configuration, internally they are distinguished by a different constructor. However, when externalising, this tag was lost. This PR prepends "Rule" to any rule variables and "Eq" to any Equation vars, leaving the original name for all config vars. * After match/unification, there is some further processing before the substitution is applied. We are now capturing the simplified/normalised substitution just before it's applied and emitted in a debug message (via `debugCreatedSubstitution`). This should ensure that the substitution is the correct one. * The substitution being returned contained both the mappings from rule variables to subterms of the configuration, as well as learned predicates about equalities between configuration variables and values. The latter are emitted in the `rule-predicate` field and should not be present in `rule-substitution`.
1 parent a96eba0 commit 4b24eba

File tree

7 files changed

+39
-21
lines changed

7 files changed

+39
-21
lines changed

kore/src/Kore/JsonRpc.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ import Kore.Rewrite (
8181
import Kore.Rewrite.ClaimPattern qualified as ClaimPattern
8282
import Kore.Rewrite.RewriteStep (EnableAssumeInitialDefined (..))
8383
import Kore.Rewrite.RewritingVariable (
84-
RewritingVariableName,
84+
RewritingVariableName (..),
8585
getRewritingPattern,
8686
getRewritingVariable,
8787
isSomeConfigVariable,
8888
isSomeEquationVariable,
89+
isSomeRuleVariable,
8990
mkRewritingPattern,
9091
mkRewritingTerm,
9192
)
@@ -386,7 +387,14 @@ respond serverState moduleName runSMT =
386387
else Just $ Kore.Syntax.Json.fromTermLike $ foldl TermLike.mkAnd pr' $ map toEquals predsFromSub
387388
in ( getRewritingPattern p'
388389
, finalPr
389-
, PatternJson.fromSubstitution sort $ Substitution.mapVariables getRewritingVariable sub
390+
, PatternJson.fromSubstitution sort
391+
$ Substitution.mapVariables
392+
( pure $ \case
393+
ConfigVariableName v -> v
394+
RuleVariableName v@SomeVariable.VariableName{base = TermLike.Id{getId = nm, idLocation = loc}} -> v{SomeVariable.base = TermLike.Id{getId = "Rule" <> nm, idLocation = loc}}
395+
EquationVariableName v@SomeVariable.VariableName{base = TermLike.Id{getId = nm, idLocation = loc}} -> v{SomeVariable.base = TermLike.Id{getId = "Eq" <> nm, idLocation = loc}}
396+
)
397+
$ Substitution.filter isSomeRuleVariable sub
390398
, rid
391399
)
392400

kore/src/Kore/Rewrite/RewriteStep.hs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ finalizeAppliedRule ::
108108
RulePattern RewritingVariableName ->
109109
-- | Conditions of applied rule
110110
Condition RewritingVariableName ->
111-
LogicT Simplifier (Pattern RewritingVariableName)
111+
LogicT
112+
Simplifier
113+
( Substitution.Substitution
114+
RewritingVariableName
115+
, Pattern RewritingVariableName
116+
)
112117
finalizeAppliedRule
113118
sideCondition
114119
renamedRule
@@ -133,7 +138,7 @@ finalizeAppliedRule
133138
ruleRHS = Rule.rhs renamedRule
134139

135140
catchSimplifiesToBottom srt = \case
136-
[] -> return $ pure $ mkBottom srt
141+
[] -> return (mempty, pure $ mkBottom srt)
137142
xs -> Logic.scatter xs
138143

139144
{- | Combine all the conditions to apply rule and construct the result.
@@ -153,7 +158,12 @@ constructConfiguration ::
153158
Condition RewritingVariableName ->
154159
-- | Final configuration
155160
Pattern RewritingVariableName ->
156-
LogicT Simplifier (Pattern RewritingVariableName)
161+
LogicT
162+
Simplifier
163+
( Substitution.Substitution
164+
RewritingVariableName
165+
, Pattern RewritingVariableName
166+
)
157167
constructConfiguration
158168
sideCondition
159169
appliedCondition
@@ -181,7 +191,7 @@ constructConfiguration
181191
-- TODO (thomas.tuegel): Should the final term be simplified after
182192
-- substitution?
183193
debugCreatedSubstitution substitution (termLikeSort finalTerm)
184-
return (finalTerm' `Pattern.withCondition` finalCondition)
194+
return (substitution, finalTerm' `Pattern.withCondition` finalCondition)
185195

186196
finalizeAppliedClaim ::
187197
-- | SideCondition containing metadata
@@ -190,7 +200,7 @@ finalizeAppliedClaim ::
190200
ClaimPattern ->
191201
-- | Conditions of applied rule
192202
Condition RewritingVariableName ->
193-
LogicT Simplifier (Pattern RewritingVariableName)
203+
LogicT Simplifier (Substitution.Substitution RewritingVariableName, Pattern RewritingVariableName)
194204
finalizeAppliedClaim sideCondition renamedRule appliedCondition =
195205
-- `constructConfiguration` may simplify the configuration to bottom
196206
-- we want to "catch" this and return a #bottom Pattern
@@ -210,7 +220,7 @@ finalizeAppliedClaim sideCondition renamedRule appliedCondition =
210220
ClaimPattern{right} = renamedRule
211221

212222
catchSimplifiesToBottom srt = \case
213-
[] -> return $ pure $ mkBottom srt
223+
[] -> return (mempty, pure $ mkBottom srt)
214224
xs -> Logic.scatter xs
215225

216226
type UnifyingRuleWithRepresentation representation rule =
@@ -226,7 +236,7 @@ type UnifyingRuleWithRepresentation representation rule =
226236
type FinalizeApplied rule =
227237
rule ->
228238
Condition RewritingVariableName ->
229-
LogicT Simplifier (Pattern RewritingVariableName)
239+
LogicT Simplifier (Substitution.Substitution RewritingVariableName, Pattern RewritingVariableName)
230240

231241
finalizeRule ::
232242
UnifyingRuleWithRepresentation representation rule =>
@@ -256,9 +266,9 @@ finalizeRule
256266
unificationCondition
257267
checkSubstitutionCoverage initial (toRule <$> unifiedRule)
258268
let renamedRule = Conditional.term unifiedRule
259-
final <- finalizeApplied renamedRule applied
269+
(subst, final) <- finalizeApplied renamedRule applied
260270
let result = resetResultPattern initialVariables final
261-
return Step.Result{appliedRule = unifiedRule, result}
271+
return Step.Result{appliedRule = unifiedRule{substitution = subst}, result}
262272

263273
-- | Finalizes a list of applied rules into 'Results'.
264274
type Finalizer rule =

kore/src/Kore/Rewrite/RewritingVariable.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright : (c) Runtime Verification, 2020-2021
33
License : BSD-3-Clause
44
-}
55
module Kore.Rewrite.RewritingVariable (
6-
RewritingVariableName,
6+
RewritingVariableName (..),
77
RewritingVariable,
88
isEquationVariable,
99
isConfigVariable,

test/rpc-server/execute/branching-prune-unsat-remainder/response.golden

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

test/rpc-server/execute/branching/definition.kore

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,23 +715,23 @@ module TEST
715715
\top{SortK{}}())))
716716
[UNIQUE'Unds'ID{}("651bff3fa53d464ac7dd7aa77e1ef6071e14c959eb6df97baa325e2ad300daaa"), org'Stop'kframework'Stop'attributes'Stop'Location{}("Location(2305,8,2305,67)"), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/nix/store/j993pnk64sgpw2qm7408ixnagfkknjzd-k-6.0.181-af105e4141030abe25055854fc02205be4ed223c-maven/include/kframework/builtin/domains.md)"), org'Stop'kframework'Stop'definition'Stop'Production{}("syntax #RuleContent ::= #RuleBody \"requires\" Bool [klabel(#ruleRequires), symbol]")]
717717

718-
// rule `<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("a","State"))~>_DotVar2),`<val>`(V) #as _Gen5),_DotVar0)=>`<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("b","State"))~>_DotVar2),_Gen5),_DotVar0) requires `_==Int_`(V,#token("0","Int")) ensures #token("true","Bool") [UNIQUE_ID(79cf50846fb75eb486ff134a1a00f1546aee170ae548228b79d8898c12c93d2b), label(TEST.AB), org.kframework.attributes.Location(Location(17,14,17,67)), org.kframework.attributes.Source(Source(/home/sam/git/haskell-backend/test/rpc-server/resources/a-to-f/test.k)), org.kframework.definition.Production(syntax #RuleContent ::= #RuleBody "requires" Bool [klabel(#ruleRequires), symbol])]
718+
// rule `<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("a","State"))~>_DotVar2),`<val>`(X) #as _Gen5),_DotVar0)=>`<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("b","State"))~>_DotVar2),_Gen5),_DotVar0) requires `_==Int_`(X,#token("0","Int")) ensures #token("true","Bool") [UNIQUE_ID(79cf50846fb75eb486ff134a1a00f1546aee170ae548228b79d8898c12c93d2b), label(TEST.AB), org.kframework.attributes.Location(Location(17,14,17,67)), org.kframework.attributes.Source(Source(/home/sam/git/haskell-backend/test/rpc-server/resources/a-to-f/test.k)), org.kframework.definition.Production(syntax #RuleContent ::= #RuleBody "requires" Bool [klabel(#ruleRequires), symbol])]
719719
axiom{} \rewrites{SortGeneratedTopCell{}} (
720720
\and{SortGeneratedTopCell{}} (
721-
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'T'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortState{}, SortKItem{}}(\dv{SortState{}}("a")),Var'Unds'DotVar2:SortK{})),\and{SortValCell{}}(Lbl'-LT-'val'-GT-'{}(VarV:SortInt{}),Var'Unds'Gen5:SortValCell{})),Var'Unds'DotVar0:SortGeneratedCounterCell{}),
721+
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'T'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortState{}, SortKItem{}}(\dv{SortState{}}("a")),Var'Unds'DotVar2:SortK{})),\and{SortValCell{}}(Lbl'-LT-'val'-GT-'{}(VarX:SortInt{}),Var'Unds'Gen5:SortValCell{})),Var'Unds'DotVar0:SortGeneratedCounterCell{}),
722722
\equals{SortBool{},SortGeneratedTopCell{}}(
723-
Lbl'UndsEqlsEqls'Int'Unds'{}(VarV:SortInt{},\dv{SortInt{}}("0")),
723+
Lbl'UndsEqlsEqls'Int'Unds'{}(VarX:SortInt{},\dv{SortInt{}}("0")),
724724
\dv{SortBool{}}("true"))),
725725
\and{SortGeneratedTopCell{}} (
726726
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'T'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortState{}, SortKItem{}}(\dv{SortState{}}("b")),Var'Unds'DotVar2:SortK{})),Var'Unds'Gen5:SortValCell{}),Var'Unds'DotVar0:SortGeneratedCounterCell{}), \top{SortGeneratedTopCell{}}()))
727727
[UNIQUE'Unds'ID{}("79cf50846fb75eb486ff134a1a00f1546aee170ae548228b79d8898c12c93d2b"), label{}("TEST.AB"), org'Stop'kframework'Stop'attributes'Stop'Location{}("Location(17,14,17,67)"), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/sam/git/haskell-backend/test/rpc-server/resources/a-to-f/test.k)"), org'Stop'kframework'Stop'definition'Stop'Production{}("syntax #RuleContent ::= #RuleBody \"requires\" Bool [klabel(#ruleRequires), symbol]")]
728728

729-
// rule `<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("a","State"))~>_DotVar2),`<val>`(V) #as _Gen5),_DotVar0)=>`<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("c","State"))~>_DotVar2),_Gen5),_DotVar0) requires `notBool_`(`_==Int_`(V,#token("0","Int"))) ensures #token("true","Bool") [UNIQUE_ID(6974170fb1496dc2cae5b77afce0c12386d66ce73a4b2344c6512681ba06c70d), label(TEST.AC), org.kframework.attributes.Location(Location(18,14,18,77)), org.kframework.attributes.Source(Source(/home/sam/git/haskell-backend/test/rpc-server/resources/a-to-f/test.k)), org.kframework.definition.Production(syntax #RuleContent ::= #RuleBody "requires" Bool [klabel(#ruleRequires), symbol])]
729+
// rule `<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("a","State"))~>_DotVar2),`<val>`(X) #as _Gen5),_DotVar0)=>`<generatedTop>`(`<T>`(`<k>`(inj{State,KItem}(#token("c","State"))~>_DotVar2),_Gen5),_DotVar0) requires `notBool_`(`_==Int_`(X,#token("0","Int"))) ensures #token("true","Bool") [UNIQUE_ID(6974170fb1496dc2cae5b77afce0c12386d66ce73a4b2344c6512681ba06c70d), label(TEST.AC), org.kframework.attributes.Location(Location(18,14,18,77)), org.kframework.attributes.Source(Source(/home/sam/git/haskell-backend/test/rpc-server/resources/a-to-f/test.k)), org.kframework.definition.Production(syntax #RuleContent ::= #RuleBody "requires" Bool [klabel(#ruleRequires), symbol])]
730730
axiom{} \rewrites{SortGeneratedTopCell{}} (
731731
\and{SortGeneratedTopCell{}} (
732-
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'T'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortState{}, SortKItem{}}(\dv{SortState{}}("a")),Var'Unds'DotVar2:SortK{})),\and{SortValCell{}}(Lbl'-LT-'val'-GT-'{}(VarV:SortInt{}),Var'Unds'Gen5:SortValCell{})),Var'Unds'DotVar0:SortGeneratedCounterCell{}),
732+
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'T'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortState{}, SortKItem{}}(\dv{SortState{}}("a")),Var'Unds'DotVar2:SortK{})),\and{SortValCell{}}(Lbl'-LT-'val'-GT-'{}(VarX:SortInt{}),Var'Unds'Gen5:SortValCell{})),Var'Unds'DotVar0:SortGeneratedCounterCell{}),
733733
\equals{SortBool{},SortGeneratedTopCell{}}(
734-
LblnotBool'Unds'{}(Lbl'UndsEqlsEqls'Int'Unds'{}(VarV:SortInt{},\dv{SortInt{}}("0"))),
734+
LblnotBool'Unds'{}(Lbl'UndsEqlsEqls'Int'Unds'{}(VarX:SortInt{},\dv{SortInt{}}("0"))),
735735
\dv{SortBool{}}("true"))),
736736
\and{SortGeneratedTopCell{}} (
737737
Lbl'-LT-'generatedTop'-GT-'{}(Lbl'-LT-'T'-GT-'{}(Lbl'-LT-'k'-GT-'{}(kseq{}(inj{SortState{}, SortKItem{}}(\dv{SortState{}}("c")),Var'Unds'DotVar2:SortK{})),Var'Unds'Gen5:SortValCell{}),Var'Unds'DotVar0:SortGeneratedCounterCell{}), \top{SortGeneratedTopCell{}}()))

0 commit comments

Comments
 (0)