Skip to content

Commit 3bdb527

Browse files
committed
add AbstractExternalSolverRuleApp to allow other external solvers to close goals
1 parent 8e28439 commit 3bdb527

File tree

3 files changed

+195
-40
lines changed

3 files changed

+195
-40
lines changed

key.core/src/main/java/de/uka/ilkd/key/proof/Goal.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import de.uka.ilkd.key.rule.*;
2323
import de.uka.ilkd.key.rule.inst.SVInstantiations;
2424
import de.uka.ilkd.key.rule.merge.MergeRule;
25-
import de.uka.ilkd.key.smt.SMTRuleApp;
2625
import de.uka.ilkd.key.strategy.AutomatedRuleApplicationManager;
2726
import de.uka.ilkd.key.strategy.QueueRuleApplicationManager;
2827
import de.uka.ilkd.key.strategy.Strategy;
@@ -627,7 +626,7 @@ public ImmutableList<Goal> apply(final RuleApp ruleApp) {
627626
} else {
628627
proof.replace(this, goalList);
629628
if (ruleApp instanceof TacletApp tacletApp && tacletApp.taclet().closeGoal()
630-
|| ruleApp instanceof SMTRuleApp) {
629+
|| ruleApp instanceof AbstractExternalSolverRuleApp) {
631630
// the first new goal is the one to be closed
632631
proof.closeGoal(goalList.head());
633632
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
}

key.core/src/main/java/de/uka/ilkd/key/smt/SMTRuleApp.java

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@
99
import de.uka.ilkd.key.java.Services;
1010
import de.uka.ilkd.key.logic.*;
1111
import de.uka.ilkd.key.proof.Goal;
12-
import de.uka.ilkd.key.rule.AbstractBuiltInRuleApp;
12+
import de.uka.ilkd.key.rule.AbstractExternalSolverRuleApp;
1313
import de.uka.ilkd.key.rule.BuiltInRule;
1414
import de.uka.ilkd.key.rule.RuleApp;
1515

1616
import org.key_project.logic.Name;
1717
import org.key_project.util.collection.ImmutableList;
1818

1919
/**
20-
* The rule application that is used when a goal is closed by means of an external solver. So far it
20+
* The rule application that is used when a goal is closed by means of an SMT solver. So far it
2121
* stores the rule that that has been used and a title containing some information for the user.
2222
*/
23-
public class SMTRuleApp extends AbstractBuiltInRuleApp {
24-
23+
public class SMTRuleApp extends AbstractExternalSolverRuleApp {
2524
public static final SMTRule RULE = new SMTRule();
26-
private final String title;
27-
private final String successfulSolverName;
2825

2926
/**
3027
* Create a new rule app without ifInsts (will be null).
@@ -37,39 +34,31 @@ public class SMTRuleApp extends AbstractBuiltInRuleApp {
3734
this(rule, pio, null, successfulSolverName);
3835
}
3936

40-
SMTRuleApp(SMTRule rule, PosInOccurrence pio, ImmutableList<PosInOccurrence> unsatCore,
37+
SMTRuleApp(ExternalSolverRule rule, PosInOccurrence pio,
38+
ImmutableList<PosInOccurrence> unsatCore,
4139
String successfulSolverName) {
42-
super(rule, pio, unsatCore);
43-
this.title = "SMT: " + successfulSolverName;
44-
this.successfulSolverName = successfulSolverName;
40+
super(rule, pio, unsatCore, successfulSolverName, "SMT: " + successfulSolverName);
4541
}
4642

4743
@Override
4844
public SMTRuleApp replacePos(PosInOccurrence newPos) {
4945
return new SMTRuleApp(RULE, newPos, ifInsts, successfulSolverName);
5046
}
5147

52-
public String getTitle() {
53-
return title;
54-
}
55-
56-
public String getSuccessfulSolverName() {
57-
return successfulSolverName;
58-
}
59-
6048
@Override
6149
public BuiltInRule rule() {
6250
return RULE;
6351
}
6452

65-
@Override
66-
public String displayName() {
67-
return title;
68-
}
69-
70-
public static class SMTRule implements BuiltInRule {
53+
public static class SMTRule implements ExternalSolverRule {
7154
public static final Name name = new Name("SMTRule");
7255

56+
@Override
57+
public ExternalSolverRule newRule() {
58+
return new SMTRule();
59+
}
60+
61+
@Override
7362
public SMTRuleApp createApp(String successfulSolverName) {
7463
return new SMTRuleApp(this, null, successfulSolverName);
7564
}
@@ -81,6 +70,7 @@ public SMTRuleApp createApp(String successfulSolverName) {
8170
* @param unsatCore formulas required to prove the result
8271
* @return rule application instance
8372
*/
73+
@Override
8474
public SMTRuleApp createApp(String successfulSolverName,
8575
ImmutableList<PosInOccurrence> unsatCore) {
8676
return new SMTRuleApp(this, null, unsatCore, successfulSolverName);
@@ -91,13 +81,6 @@ public SMTRuleApp createApp(PosInOccurrence pos, TermServices services) {
9181
return new SMTRuleApp(this, null, "");
9282
}
9383

94-
95-
@Override
96-
public boolean isApplicable(Goal goal, PosInOccurrence pio) {
97-
return false;
98-
}
99-
100-
10184
/**
10285
* Create a new goal (to be closed in {@link Goal#apply(RuleApp)} directly afterwards)
10386
* with the same sequent as the given one.
@@ -115,16 +98,12 @@ public ImmutableList<Goal> apply(Goal goal, Services services, RuleApp ruleApp)
11598
return goal.split(1);
11699
}
117100

118-
@Override
119-
public boolean isApplicableOnSubTerms() {
120-
return false;
121-
}
122-
123101
@Override
124102
public String displayName() {
125103
return "SMT";
126104
}
127105

106+
@Override
128107
public String toString() {
129108
return displayName();
130109
}
@@ -133,9 +112,9 @@ public String toString() {
133112
public Name name() {
134113
return name;
135114
}
136-
137115
}
138116

117+
@Override
139118
public SMTRuleApp setTitle(String title) {
140119
return new SMTRuleApp(RULE, pio, ifInsts, title);
141120
}
@@ -168,5 +147,4 @@ public SMTRuleApp tryToInstantiate(Goal goal) {
168147
}
169148
return app.setIfInsts(ImmutableList.fromList(ifInsts));
170149
}
171-
172150
}

0 commit comments

Comments
 (0)