Skip to content

Commit aff60f1

Browse files
beautify counter example
1 parent ed463a9 commit aff60f1

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

  • liquidjava-verifier/src/main/java/liquidjava/diagnostics

liquidjava-verifier/src/main/java/liquidjava/diagnostics/DebugLog.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static void smtStart(Predicate premises, Predicate conclusion) {
6969
System.out.println(SMT_TAG + " " + c);
7070
}
7171
System.out.println(SMT_TAG + SEPARATOR);
72-
System.out.println(SMT_TAG + " " + formatConclusion(conclusion));
72+
System.out.println(SMT_TAG + " " + formatConclusion(conclusion));
7373
}
7474

7575
/**
@@ -222,8 +222,44 @@ public static void smtSat(Object counterexample) {
222222
if (!enabled()) {
223223
return;
224224
}
225-
System.out.println(SMT_TAG + " result: " + Colors.RED + "SAT (subtype fails)" + Colors.RESET
226-
+ "; counterexample: " + counterexample);
225+
String header = SMT_TAG + " result: " + Colors.RED + "SAT (subtype fails)" + Colors.RESET;
226+
String pretty = formatCounterexample(counterexample);
227+
if (pretty == null) {
228+
System.out.println(header);
229+
} else if (pretty.contains("\n")) {
230+
System.out.println(header + Colors.GREY + " — counterexample:" + Colors.RESET);
231+
System.out.println(pretty);
232+
} else {
233+
System.out.println(header + Colors.GREY + " — counterexample: " + Colors.RESET + pretty);
234+
}
235+
}
236+
237+
/**
238+
* Render a {@link liquidjava.smt.Counterexample} as {@code lhs = value} pairs. Single assignment goes inline;
239+
* multiple assignments are listed one per indented line. Returns {@code null} when there is nothing useful to show
240+
* — caller prints just the SAT header.
241+
*/
242+
private static String formatCounterexample(Object counterexample) {
243+
if (!(counterexample instanceof liquidjava.smt.Counterexample ce)) {
244+
return counterexample == null ? null : counterexample.toString();
245+
}
246+
var pairs = ce.assignments();
247+
if (pairs == null || pairs.isEmpty()) {
248+
return null;
249+
}
250+
if (pairs.size() == 1) {
251+
var p = pairs.get(0);
252+
return p.first() + " = " + p.second();
253+
}
254+
StringBuilder sb = new StringBuilder();
255+
for (int i = 0; i < pairs.size(); i++) {
256+
var p = pairs.get(i);
257+
if (i > 0) {
258+
sb.append('\n');
259+
}
260+
sb.append(SMT_TAG).append(" ").append(p.first()).append(" = ").append(p.second());
261+
}
262+
return sb.toString();
227263
}
228264

229265
public static void smtUnknown() {

0 commit comments

Comments
 (0)