|
| 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.rule; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import de.uka.ilkd.key.java.Services; |
| 10 | +import de.uka.ilkd.key.logic.*; |
| 11 | +import de.uka.ilkd.key.proof.Goal; |
| 12 | + |
| 13 | +import org.key_project.logic.Name; |
| 14 | +import org.key_project.util.collection.ImmutableList; |
| 15 | + |
| 16 | +/** |
| 17 | + * The rule application that is used when a goal is closed by means of an external solver. So far it |
| 18 | + * stores the rule that that has been used and a title containing some information for the user. |
| 19 | + * <p> |
| 20 | + * {@link de.uka.ilkd.key.smt.SMTRuleApp} |
| 21 | + */ |
| 22 | +public abstract class AbstractExternalSolverRuleApp extends AbstractBuiltInRuleApp { |
| 23 | + protected final ExternalSolverRule rule; |
| 24 | + protected final String title; |
| 25 | + protected final String successfulSolverName; |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates a new AbstractExternalSolverRuleApp, |
| 29 | + * |
| 30 | + * @param rule the ExternalSolverRule to apply |
| 31 | + * @param pio the position in the term to apply the rule to |
| 32 | + * @param unsatCore an unsat core consisting of the formulas that are needed to prove the goal |
| 33 | + * (optional null) |
| 34 | + * @param successfulSolverName the name of the solver used to find the proof |
| 35 | + * @param title the title of this rule app |
| 36 | + */ |
| 37 | + protected AbstractExternalSolverRuleApp(ExternalSolverRule rule, PosInOccurrence pio, |
| 38 | + ImmutableList<PosInOccurrence> unsatCore, |
| 39 | + String successfulSolverName, String title) { |
| 40 | + super(rule, pio, unsatCore); |
| 41 | + this.rule = rule.newRule(); |
| 42 | + this.title = title; |
| 43 | + this.successfulSolverName = successfulSolverName; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the title of this rule application |
| 48 | + * |
| 49 | + * @return title of this application |
| 50 | + */ |
| 51 | + public String getTitle() { |
| 52 | + return title; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Gets the name of the successful solver |
| 57 | + * |
| 58 | + * @return name of the successful solver |
| 59 | + */ |
| 60 | + public String getSuccessfulSolverName() { |
| 61 | + return successfulSolverName; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public BuiltInRule rule() { |
| 66 | + return rule; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String displayName() { |
| 71 | + return title; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Interface for the rules of external solvers |
| 76 | + */ |
| 77 | + public interface ExternalSolverRule extends BuiltInRule { |
| 78 | + Name name = new Name("ExternalSolverRule"); |
| 79 | + |
| 80 | + ExternalSolverRule newRule(); |
| 81 | + |
| 82 | + AbstractExternalSolverRuleApp createApp(String successfulSolverName); |
| 83 | + |
| 84 | + /** |
| 85 | + * Create a new rule application with the given solver name and unsat core. |
| 86 | + * |
| 87 | + * @param successfulSolverName solver that produced this result |
| 88 | + * @param unsatCore formulas required to prove the result |
| 89 | + * @return rule application instance |
| 90 | + */ |
| 91 | + AbstractExternalSolverRuleApp createApp(String successfulSolverName, |
| 92 | + ImmutableList<PosInOccurrence> unsatCore); |
| 93 | + |
| 94 | + @Override |
| 95 | + AbstractExternalSolverRuleApp createApp(PosInOccurrence pos, TermServices services); |
| 96 | + |
| 97 | + |
| 98 | + @Override |
| 99 | + default boolean isApplicable(Goal goal, PosInOccurrence pio) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Create a new goal (to be closed in {@link Goal#apply(RuleApp)} directly afterwards) |
| 106 | + * with the same sequent as the given one. |
| 107 | + * |
| 108 | + * @param goal the Goal on which to apply <tt>ruleApp</tt> |
| 109 | + * @param services the Services with the necessary information about the java programs |
| 110 | + * @param ruleApp the rule application to be executed |
| 111 | + * @return a list with an identical goal as the given <tt>goal</tt> |
| 112 | + */ |
| 113 | + @Override |
| 114 | + default ImmutableList<Goal> apply(Goal goal, Services services, RuleApp ruleApp) { |
| 115 | + if (goal.proof().getInitConfig().getJustifInfo().getJustification(newRule()) == null) { |
| 116 | + goal.proof().getInitConfig().registerRule(newRule(), () -> false); |
| 117 | + } |
| 118 | + return goal.split(1); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + default boolean isApplicableOnSubTerms() { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + default String displayName() { |
| 128 | + return "ExternalSolver"; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + String toString(); |
| 133 | + |
| 134 | + @Override |
| 135 | + default Name name() { |
| 136 | + return name; |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the title (needs to create a new instance for this) |
| 143 | + * |
| 144 | + * @param title new title for rule app |
| 145 | + * @return copy of this with the new title |
| 146 | + */ |
| 147 | + public abstract AbstractExternalSolverRuleApp setTitle(String title); |
| 148 | + |
| 149 | + @Override |
| 150 | + public AbstractExternalSolverRuleApp setIfInsts(ImmutableList<PosInOccurrence> ifInsts) { |
| 151 | + setMutable(ifInsts); |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Create a new RuleApp with the same pio (in this case, that will probably be null as the |
| 157 | + * AbstractExternalSolver rule is applied to the complete sequent) as this one. |
| 158 | + * Add all top level formulas of the goal |
| 159 | + * to the RuleApp's ifInsts. |
| 160 | + * |
| 161 | + * @param goal the goal to instantiate the current RuleApp on |
| 162 | + * @return a new RuleApp with the same pio and all top level formulas of the goal as ifInsts |
| 163 | + */ |
| 164 | + @Override |
| 165 | + public AbstractExternalSolverRuleApp tryToInstantiate(Goal goal) { |
| 166 | + AbstractExternalSolverRuleApp app = rule.createApp(pio, goal.proof().getServices()); |
| 167 | + Sequent seq = goal.sequent(); |
| 168 | + List<PosInOccurrence> ifInsts = new ArrayList<>(); |
| 169 | + for (SequentFormula ante : seq.antecedent()) { |
| 170 | + ifInsts.add(new PosInOccurrence(ante, PosInTerm.getTopLevel(), true)); |
| 171 | + } |
| 172 | + for (SequentFormula succ : seq.succedent()) { |
| 173 | + ifInsts.add(new PosInOccurrence(succ, PosInTerm.getTopLevel(), false)); |
| 174 | + } |
| 175 | + return app.setIfInsts(ImmutableList.fromList(ifInsts)); |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments