Skip to content

Commit fe44659

Browse files
authored
made Rewriter::prove return RewriterResult (runtimeverification#562)
* made Rewriter::prove return RewriterResult * Added KEMException.TERMINATED_WITH_ERRORS_EXIT_CODE = 113
1 parent 90ee840 commit fe44659

File tree

10 files changed

+65
-28
lines changed

10 files changed

+65
-28
lines changed

haskell-backend/src/main/java/org/kframework/backend/haskell/HaskellRewriter.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.kframework.kompile.CompiledDefinition;
1616
import org.kframework.kompile.KompileOptions;
1717
import org.kframework.kore.K;
18-
import org.kframework.kore.KApply;
1918
import org.kframework.kore.KORE;
2019
import org.kframework.kore.KVariable;
2120
import org.kframework.kore.Sort;
@@ -239,7 +238,7 @@ public K search(K initialConfiguration, Optional<Integer> depth, Optional<Intege
239238
}
240239

241240
@Override
242-
public K prove(Module rules, Rule boundaryPattern) {
241+
public RewriterResult prove(Module rules, Rule boundaryPattern) {
243242
Module kompiledModule = KoreBackend.getKompiledModule(module);
244243
ModuleToKORE converter = new ModuleToKORE(kompiledModule, files, def.topCellInitializer, kompileOptions);
245244
String kompiledString = KoreBackend.getKompiledString(converter, files, false);
@@ -281,26 +280,28 @@ public K prove(Module rules, Rule boundaryPattern) {
281280
globalOptions.debugWarnings = true; // sets this so the kprove directory is not removed.
282281
System.out.println(String.join(" ", koreCommand));
283282
kprint.options.output = OutputModes.NONE;
284-
return KORE.KApply(KLabels.ML_FALSE);
283+
return new RewriterResult(Optional.empty(), Optional.of(0),KORE.KApply(KLabels.ML_FALSE));
285284
}
286285
if (globalOptions.verbose) {
287286
System.err.println("Executing " + args);
288287
}
288+
int exit;
289289
try {
290290
File korePath = koreDirectory == null ? null : new File(koreDirectory);
291-
if (executeCommandBasic(korePath, koreCommand) != 0) {
292-
kem.registerCriticalWarning("Haskell backend returned non-zero exit code");
293-
}
294-
K outputK = new KoreParser(files.resolveKoreToKLabelsFile(), rules.sortAttributesFor()).parseFile(koreOutputFile);
295-
return outputK;
291+
exit = executeCommandBasic(korePath, koreCommand);
296292
} catch (IOException e) {
297293
throw KEMException.criticalError("I/O Error while executing", e);
298294
} catch (InterruptedException e) {
299295
throw KEMException.criticalError("Interrupted while executing", e);
296+
}
297+
K outputK;
298+
try {
299+
outputK = new KoreParser(files.resolveKoreToKLabelsFile(), rules.sortAttributesFor()).parseFile(koreOutputFile);
300300
} catch (ParseError parseError) {
301301
kem.registerCriticalWarning("Error parsing haskell backend output", parseError);
302-
return KORE.KApply(KLabels.ML_FALSE);
302+
outputK = KORE.KApply(KLabels.ML_FALSE);
303303
}
304+
return new RewriterResult(Optional.empty(), Optional.of(exit), outputK);
304305
}
305306

306307
@Override

java-backend/src/main/java/org/kframework/backend/java/symbolic/InitializeRewriter.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.kframework.kil.Attribute;
2828
import org.kframework.kompile.KompileOptions;
2929
import org.kframework.kore.K;
30+
import org.kframework.kore.KApply;
31+
import org.kframework.kore.KORE;
3032
import org.kframework.kprove.KProveOptions;
3133
import org.kframework.krun.KRunOptions;
3234
import org.kframework.krun.api.io.FileSystem;
@@ -210,7 +212,7 @@ public Tuple2<RewriterResult, K> executeAndMatch(K k, Optional<Integer> depth, R
210212
}
211213

212214
@Override
213-
public K prove(Module mod, @Nullable Rule boundaryPattern) {
215+
public RewriterResult prove(Module mod, @Nullable Rule boundaryPattern) {
214216
rewritingContext.stateLog.open("prove-" + Integer.toString(Math.abs(mod.hashCode())));
215217
rewritingContext.setExecutionPhase(false);
216218
List<Rule> rules = stream(mod.rules())
@@ -263,9 +265,16 @@ public K prove(Module mod, @Nullable Rule boundaryPattern) {
263265

264266
K result = proofResults.stream()
265267
.map(constrainedTerm -> (K) constrainedTerm.term())
266-
.reduce(((k1, k2) -> KApply(KLabels.ML_AND, k1, k2))).orElse(KApply(KLabels.ML_TRUE));
268+
.reduce(((k1, k2) -> KORE.KApply(KLabels.ML_AND, k1, k2))).orElse(KORE.KApply(KLabels.ML_TRUE));
269+
int exit;
270+
if (result instanceof KApply) {
271+
KApply kapp = (KApply) result;
272+
exit = kapp.klabel().name().equals("#True") ? 0 : 1;
273+
} else {
274+
exit = 1;
275+
}
267276
rewritingContext.stateLog.close();
268-
return result;
277+
return new RewriterResult(Optional.empty(), Optional.of(exit), result);
269278
}
270279

271280
@Override
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
*/
3+
4+
module VERIFICATION
5+
imports IMP
6+
7+
syntax Id ::= "a" [token]
8+
syntax Id ::= "b" [token]
9+
syntax Id ::= "max" [token]
10+
endmodule
11+
12+
13+
module MAX-SPEC
14+
imports VERIFICATION
15+
imports IMP
16+
17+
rule
18+
<k>
19+
if (a <= b)
20+
{ max = b; }
21+
else
22+
{ max = a; }
23+
=> .K
24+
</k>
25+
<state>
26+
a |-> A:Int
27+
b |-> B:Int
28+
max |-> (_ => ?M:Int)
29+
</state>
30+
ensures (A >=Int B andBool ?M ==Int A) orBool (B >=Int A andBool ?M ==Int B)
31+
32+
endmodule
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#True

kernel/src/main/java/org/kframework/kprove/KProve.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
import com.google.inject.Inject;
55
import org.apache.commons.io.FilenameUtils;
6+
import org.kframework.RewriterResult;
67
import org.kframework.attributes.Source;
78
import org.kframework.compile.Backend;
89
import org.kframework.definition.Definition;
910
import org.kframework.definition.Module;
1011
import org.kframework.definition.Rule;
1112
import org.kframework.kompile.CompiledDefinition;
1213
import org.kframework.kompile.Kompile;
13-
import org.kframework.kore.K;
14-
import org.kframework.kore.KApply;
1514
import org.kframework.krun.KRun;
1615
import org.kframework.rewriter.Rewriter;
1716
import org.kframework.unparser.KPrint;
@@ -58,16 +57,9 @@ public int run(KProveOptions options, CompiledDefinition compiledDefinition, Bac
5857
Module specModule = compiled._2();
5958
Rule boundaryPattern = buildBoundaryPattern(compiledDefinition);
6059

61-
K results = rewriter.prove(specModule, boundaryPattern);
62-
int exit;
63-
if (results instanceof KApply) {
64-
KApply kapp = (KApply) results;
65-
exit = kapp.klabel().name().equals("#True") ? 0 : 1;
66-
} else {
67-
exit = 1;
68-
}
69-
kprint.prettyPrint(compiled._1(), compiled._1().getModule("LANGUAGE-PARSING").get(), s -> kprint.outputFile(s), results);
70-
return exit;
60+
RewriterResult results = rewriter.prove(specModule, boundaryPattern);
61+
kprint.prettyPrint(compiled._1(), compiled._1().getModule("LANGUAGE-PARSING").get(), s -> kprint.outputFile(s), results.k());
62+
return results.exitCode().orElse(KEMException.TERMINATED_WITH_ERRORS_EXIT_CODE);
7163
}
7264

7365
private static Module getModule(String defModule, Map<String, Module> modules, Definition oldDef) {

kernel/src/main/java/org/kframework/main/FrontEnd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public int main() {
6666
}
6767
} catch (KEMException e) {
6868
// terminated with errors, so we need to return nonzero error code.
69-
retval = 113;
69+
retval = KEMException.TERMINATED_WITH_ERRORS_EXIT_CODE;
7070
if (globalOptions.debug()) {
7171
e.printStackTrace();
7272
} else {

kore/src/main/java/org/kframework/utils/errorsystem/KEMException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* @author dwightguth
1414
*/
1515
public class KEMException extends RuntimeException {
16+
public static final int TERMINATED_WITH_ERRORS_EXIT_CODE = 113;
17+
1618
public final KException exception;
1719

1820
KEMException(KException e) {

kore/src/main/scala/org/kframework/rewriter/Rewriter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ trait Rewriter {
3838

3939
def executeAndMatch(k: kore.K, depth: Optional[Integer], rule: Rule): Tuple2[RewriterResult, kore.K]
4040

41-
def prove(rules: Module, boundaryPattern: Rule): kore.K
41+
def prove(rules: Module, boundaryPattern: Rule): RewriterResult
4242

4343
def equivalence(firstDef: Rewriter, secondDef: Rewriter, firstSpec: Module, secondSpec: Module): Boolean
4444
}

llvm-backend/src/main/java/org/kframework/backend/llvm/LLVMRewriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public K search(K initialConfiguration, Optional<Integer> depth, Optional<Intege
113113
}
114114

115115
@Override
116-
public K prove(Module rules, Rule boundaryPattern) {
116+
public RewriterResult prove(Module rules, Rule boundaryPattern) {
117117
throw new UnsupportedOperationException();
118118
}
119119

ocaml-backend/src/main/java/org/kframework/backend/ocaml/OcamlRewriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public K search(K initialConfiguration, Optional<Integer> depth, Optional<Intege
118118
}
119119

120120
@Override
121-
public K prove(Module rules, Rule boundaryPattern) {
121+
public RewriterResult prove(Module rules, Rule boundaryPattern) {
122122
throw new UnsupportedOperationException();
123123
}
124124

0 commit comments

Comments
 (0)