|
| 1 | +package liquidjava.diagnostics; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedHashMap; |
| 5 | +import java.util.LinkedHashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import liquidjava.api.CommandLineLauncher; |
| 11 | +import liquidjava.rj_language.Predicate; |
| 12 | +import liquidjava.rj_language.ast.BinaryExpression; |
| 13 | +import liquidjava.rj_language.ast.Expression; |
| 14 | +import liquidjava.rj_language.ast.GroupExpression; |
| 15 | +import liquidjava.utils.Utils; |
| 16 | +import spoon.reflect.cu.SourcePosition; |
| 17 | + |
| 18 | +/** |
| 19 | + * Centralised debug-mode logging for verification activity. Output is gated on the global {@code --debug} / {@code -d} |
| 20 | + * flag and is purely informational. |
| 21 | + * |
| 22 | + * <p> |
| 23 | + * Layers of output, from outermost to innermost: |
| 24 | + * <ul> |
| 25 | + * <li>{@link #info} — verification context (caller-level predicates, source position).</li> |
| 26 | + * <li>{@link #smtStart} — premises and conclusion as fed to Z3.</li> |
| 27 | + * <li>{@link #smtUnsat} / {@link #smtSat} / {@link #smtUnknown} — solver outcome.</li> |
| 28 | + * </ul> |
| 29 | + */ |
| 30 | +public final class DebugLog { |
| 31 | + |
| 32 | + private static final String INFO_TAG = Colors.YELLOW + "[INFO]" + Colors.RESET; |
| 33 | + private static final String SMT_TAG = Colors.BLUE + "[SMT]" + Colors.RESET; |
| 34 | + |
| 35 | + private DebugLog() { |
| 36 | + } |
| 37 | + |
| 38 | + public static boolean enabled() { |
| 39 | + return CommandLineLauncher.cmdArgs.debugMode; |
| 40 | + } |
| 41 | + |
| 42 | + public static void info(String verb, Object expected, Object found, SourcePosition position) { |
| 43 | + if (!enabled()) { |
| 44 | + return; |
| 45 | + } |
| 46 | + String exp = Utils.getExpressionFromPosition(position); |
| 47 | + String where = position.getFile().getName() + ":" + position.getLine(); |
| 48 | + System.out.println(INFO_TAG + " " + verb + ": " + found + " <: " + expected); |
| 49 | + System.out.println(INFO_TAG + " at " + where + (exp != null ? " on '" + exp + "'" : "")); |
| 50 | + } |
| 51 | + |
| 52 | + public static void smtStart(Predicate premises, Predicate conclusion) { |
| 53 | + if (!enabled()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + printPremisesByVariable(premises); |
| 57 | + System.out.println(SMT_TAG + " conclusion: " + conclusion); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Splits the premise conjunction into top-level conjuncts and groups them by the variable they constrain. Conjuncts |
| 62 | + * mentioning a single variable are printed under that variable; conjuncts mentioning zero or several variables are |
| 63 | + * printed under a {@code <shared>} bucket. |
| 64 | + */ |
| 65 | + private static void printPremisesByVariable(Predicate premises) { |
| 66 | + Expression root = premises.getExpression(); |
| 67 | + List<Expression> conjuncts = new ArrayList<>(); |
| 68 | + flattenConjunction(root, conjuncts); |
| 69 | + |
| 70 | + Map<String, List<String>> grouped = new LinkedHashMap<>(); |
| 71 | + for (Expression c : conjuncts) { |
| 72 | + List<String> names = new ArrayList<>(); |
| 73 | + c.getVariableNames(names); |
| 74 | + Set<String> unique = new LinkedHashSet<>(names); |
| 75 | + String key; |
| 76 | + if (unique.isEmpty()) { |
| 77 | + key = ""; |
| 78 | + } else if (unique.size() == 1) { |
| 79 | + key = unique.iterator().next(); |
| 80 | + } else { |
| 81 | + key = "<shared>"; |
| 82 | + } |
| 83 | + grouped.computeIfAbsent(key, k -> new ArrayList<>()).add(c.toString()); |
| 84 | + } |
| 85 | + |
| 86 | + System.out.println(SMT_TAG + " premises:"); |
| 87 | + for (Map.Entry<String, List<String>> entry : grouped.entrySet()) { |
| 88 | + String prefix = entry.getKey().isEmpty() ? "" : entry.getKey() + ": "; |
| 89 | + for (String c : entry.getValue()) { |
| 90 | + System.out.println(SMT_TAG + " " + prefix + c); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static void flattenConjunction(Expression e, List<Expression> out) { |
| 96 | + if (e instanceof GroupExpression g) { |
| 97 | + flattenConjunction(g.getExpression(), out); |
| 98 | + return; |
| 99 | + } |
| 100 | + if (e instanceof BinaryExpression b && "&&".equals(b.getOperator())) { |
| 101 | + flattenConjunction(b.getFirstOperand(), out); |
| 102 | + flattenConjunction(b.getSecondOperand(), out); |
| 103 | + return; |
| 104 | + } |
| 105 | + out.add(e); |
| 106 | + } |
| 107 | + |
| 108 | + public static void smtUnsat() { |
| 109 | + if (!enabled()) { |
| 110 | + return; |
| 111 | + } |
| 112 | + System.out.println(SMT_TAG + " result: " + Colors.GREEN + "UNSAT (subtype holds)" + Colors.RESET); |
| 113 | + } |
| 114 | + |
| 115 | + public static void smtSat(Object counterexample) { |
| 116 | + if (!enabled()) { |
| 117 | + return; |
| 118 | + } |
| 119 | + System.out.println(SMT_TAG + " result: " + Colors.RED + "SAT (subtype fails)" + Colors.RESET |
| 120 | + + "; counterexample: " + counterexample); |
| 121 | + } |
| 122 | + |
| 123 | + public static void smtUnknown() { |
| 124 | + if (!enabled()) { |
| 125 | + return; |
| 126 | + } |
| 127 | + System.out.println(SMT_TAG + " result: " + Colors.YELLOW + "UNKNOWN (treated as OK)" + Colors.RESET); |
| 128 | + } |
| 129 | +} |
0 commit comments