|
| 1 | +/* This file is part of KeY - https://key-project.org |
| 2 | + * KeY is licensed under the GNU General Public License Version 2 |
| 3 | + * SPDX-License-Identifier: GPL-2.0-only */ |
| 4 | +package de.uka.ilkd.key.strategy.feature; |
| 5 | + |
| 6 | +import de.uka.ilkd.key.proof.BuiltInRuleAppIndex; |
| 7 | +import de.uka.ilkd.key.proof.BuiltInRuleIndex; |
| 8 | +import de.uka.ilkd.key.proof.Goal; |
| 9 | +import de.uka.ilkd.key.proof.Node; |
| 10 | +import de.uka.ilkd.key.proof.Proof; |
| 11 | +import de.uka.ilkd.key.proof.TacletIndex; |
| 12 | +import de.uka.ilkd.key.proof.TacletIndexKit; |
| 13 | +import de.uka.ilkd.key.proof.calculus.JavaDLSequentKit; |
| 14 | +import de.uka.ilkd.key.rule.TacletApp; |
| 15 | +import de.uka.ilkd.key.rule.TacletForTests; |
| 16 | + |
| 17 | +import org.key_project.logic.PosInTerm; |
| 18 | +import org.key_project.prover.proof.rulefilter.TacletFilter; |
| 19 | +import org.key_project.prover.sequent.PosInOccurrence; |
| 20 | +import org.key_project.prover.sequent.Sequent; |
| 21 | +import org.key_project.prover.sequent.SequentFormula; |
| 22 | +import org.key_project.prover.strategy.costbased.MutableState; |
| 23 | +import org.key_project.prover.strategy.costbased.RuleAppCost; |
| 24 | +import org.key_project.prover.strategy.costbased.TopRuleAppCost; |
| 25 | +import org.key_project.prover.strategy.costbased.feature.Feature; |
| 26 | +import org.key_project.util.collection.ImmutableList; |
| 27 | +import org.key_project.util.collection.ImmutableSLList; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.BeforeAll; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests duplicate detection of {@link NonDuplicateAppFeature} and {@link EqNonDuplicateAppFeature}, |
| 37 | + * which go through the fingerprint-bucketed {@link AppliedRuleAppsNameCache}. The two key |
| 38 | + * properties: an application already performed on the branch is rejected (cost |
| 39 | + * {@link TopRuleAppCost}), and the fingerprint bucketing never hides a real duplicate nor invents |
| 40 | + * one for the same taclet applied at a <em>different</em> position (a different bucket). |
| 41 | + */ |
| 42 | +public class TestNonDuplicateAppFeature { |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + public static void setUp() { |
| 46 | + TacletForTests.parse(); |
| 47 | + } |
| 48 | + |
| 49 | + /** Root sequent {@code ==> A -> B, B -> A} (two distinct succedent formulas). */ |
| 50 | + private static Sequent twoImplications() { |
| 51 | + ImmutableList<SequentFormula> succ = ImmutableSLList.<SequentFormula>nil() |
| 52 | + .append(new SequentFormula(TacletForTests.parseTerm("A -> B"))) |
| 53 | + .append(new SequentFormula(TacletForTests.parseTerm("B -> A"))); |
| 54 | + return JavaDLSequentKit.createSequent(ImmutableSLList.nil(), succ); |
| 55 | + } |
| 56 | + |
| 57 | + private static Goal goalFor(Node n, TacletIndex idx) { |
| 58 | + return new Goal(n, idx, new BuiltInRuleAppIndex(new BuiltInRuleIndex()), |
| 59 | + n.proof().getServices()); |
| 60 | + } |
| 61 | + |
| 62 | + private static PosInOccurrence topSucc(Node n, int idx) { |
| 63 | + return new PosInOccurrence(n.sequent().succedent().get(idx), PosInTerm.getTopLevel(), |
| 64 | + false); |
| 65 | + } |
| 66 | + |
| 67 | + private static TacletApp appAt(Goal goal, PosInOccurrence pos) { |
| 68 | + return goal.ruleAppIndex().getTacletAppAt(TacletFilter.TRUE, pos, null).head(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Build {@code root --(imp_right at succedent #applyIdx)--> child(leaf)} and return the feature |
| 73 | + * cost of applying imp_right at succedent #candIdx on the child leaf. |
| 74 | + */ |
| 75 | + private static RuleAppCost cost(Feature feature, int applyIdx, int candIdx) { |
| 76 | + Proof proof = new Proof("TestNonDuplicateAppFeature", TacletForTests.initConfig()); |
| 77 | + Node root = new Node(proof, twoImplications()); |
| 78 | + proof.setRoot(root); |
| 79 | + |
| 80 | + TacletIndex idx = TacletIndexKit.getKit().createTacletIndex(); |
| 81 | + idx.add(TacletForTests.lookupTaclet("imp_right")); |
| 82 | + Goal rootGoal = goalFor(root, idx); |
| 83 | + |
| 84 | + final PosInOccurrence applyPos = topSucc(root, applyIdx); |
| 85 | + final PosInOccurrence candPos = topSucc(root, candIdx); |
| 86 | + final TacletApp applied = appAt(rootGoal, applyPos); |
| 87 | + final TacletApp candidate = candIdx == applyIdx ? applied : appAt(rootGoal, candPos); |
| 88 | + |
| 89 | + root.setAppliedRuleApp(applied); |
| 90 | + Node child = new Node(proof, root.sequent(), root); |
| 91 | + root.add(child); |
| 92 | + |
| 93 | + return feature.computeCost(candidate, candPos, goalFor(child, idx), new MutableState()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void nonDuplicateRejectsAlreadyAppliedApp() { |
| 98 | + assertSame(TopRuleAppCost.INSTANCE, cost(NonDuplicateAppFeature.INSTANCE, 0, 0), |
| 99 | + "re-applying the same taclet at the same position must be rejected as a duplicate"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void nonDuplicateAcceptsAppAtDifferentPosition() { |
| 104 | + assertNotSame(TopRuleAppCost.INSTANCE, cost(NonDuplicateAppFeature.INSTANCE, 1, 0), |
| 105 | + "the same taclet at a different position is not a duplicate (different fingerprint bucket)"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void eqNonDuplicateRejectsAlreadyAppliedApp() { |
| 110 | + assertSame(TopRuleAppCost.INSTANCE, cost(EqNonDuplicateAppFeature.INSTANCE, 0, 0), |
| 111 | + "eqEquals variant must also reject an already-applied application"); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void eqNonDuplicateAcceptsAppAtDifferentPosition() { |
| 116 | + assertNotSame(TopRuleAppCost.INSTANCE, cost(EqNonDuplicateAppFeature.INSTANCE, 1, 0), |
| 117 | + "eqEquals variant must accept the same taclet at a different position"); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Apply a rewrite (TestApplyTaclet_contradiction, {@code find(b->c)}) at one occurrence of |
| 122 | + * {@code A -> B} in {@code ==> (A -> B) & (A -> B)}, then return the cost of applying it at the |
| 123 | + * <em>other</em> occurrence: same focus term, different sequent position. This is the case the |
| 124 | + * fingerprint must not get wrong -- the modulo-position variant treats it as a duplicate, the |
| 125 | + * plain variant does not. |
| 126 | + */ |
| 127 | + private static RuleAppCost crossPositionCost(Feature feature) { |
| 128 | + Proof proof = new Proof("TestNonDuplicateAppFeature", TacletForTests.initConfig()); |
| 129 | + SequentFormula conj = new SequentFormula(TacletForTests.parseTerm("(A -> B) & (A -> B)")); |
| 130 | + Node root = new Node(proof, |
| 131 | + JavaDLSequentKit.createSequent(ImmutableSLList.nil(), ImmutableSLList.singleton(conj))); |
| 132 | + proof.setRoot(root); |
| 133 | + |
| 134 | + TacletIndex idx = TacletIndexKit.getKit().createTacletIndex(); |
| 135 | + idx.add(TacletForTests.lookupTaclet("TestApplyTaclet_contradiction")); |
| 136 | + Goal rootGoal = goalFor(root, idx); |
| 137 | + |
| 138 | + final SequentFormula f = root.sequent().succedent().get(0); |
| 139 | + final PosInOccurrence leftConjunct = |
| 140 | + new PosInOccurrence(f, PosInTerm.getTopLevel().down(0), false); |
| 141 | + final PosInOccurrence rightConjunct = |
| 142 | + new PosInOccurrence(f, PosInTerm.getTopLevel().down(1), false); |
| 143 | + |
| 144 | + root.setAppliedRuleApp(appAt(rootGoal, leftConjunct)); |
| 145 | + Node child = new Node(proof, root.sequent(), root); |
| 146 | + root.add(child); |
| 147 | + |
| 148 | + return feature.computeCost(appAt(rootGoal, rightConjunct), rightConjunct, |
| 149 | + goalFor(child, idx), |
| 150 | + new MutableState()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void modPositionRejectsSameFocusAtDifferentPosition() { |
| 155 | + assertSame(TopRuleAppCost.INSTANCE, |
| 156 | + crossPositionCost(NonDuplicateAppModPositionFeature.INSTANCE), |
| 157 | + "modulo-position variant must treat the same focus term at a different position as a duplicate"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void nonDuplicateAllowsSameFocusAtDifferentPosition() { |
| 162 | + assertNotSame(TopRuleAppCost.INSTANCE, crossPositionCost(NonDuplicateAppFeature.INSTANCE), |
| 163 | + "plain variant must not treat a different position as a duplicate even with equal focus term"); |
| 164 | + } |
| 165 | +} |
0 commit comments