1515import liquidjava .rj_language .ast .Enum ;
1616import liquidjava .rj_language .ast .Expression ;
1717import liquidjava .rj_language .ast .Var ;
18- import liquidjava .rj_language .ast .formatter .VariableFormatter ;
18+ import liquidjava .processor .context .PlacementInCode ;
19+ import liquidjava .rj_language .ast .formatter .DisplayNames ;
20+ import liquidjava .rj_language .ast .formatter .ExpressionFormatter ;
1921import liquidjava .rj_language .opt .VCSimplificationResult ;
2022import liquidjava .smt .Counterexample ;
2123import liquidjava .utils .GeneratedNames ;
@@ -41,19 +43,27 @@ public class RefinementError extends LJError {
4143 private final VCSimplificationResult found ;
4244 private final Counterexample counterexample ;
4345 private final SourcePosition declarationPosition ;
46+ private final DisplayNames displayNames ;
4447
4548 public RefinementError (SourcePosition position , SourcePosition declarationPosition , Predicate expected ,
4649 VCSimplificationResult found , TranslationTable translationTable , Counterexample counterexample ,
4750 String customMessage ) {
48- super ("Refinement Error" , message (expected , found ), position , translationTable , customMessage );
51+ this (position , declarationPosition , expected , found , translationTable , counterexample , customMessage ,
52+ displayNames (expected , found , translationTable ));
53+ }
54+
55+ private RefinementError (SourcePosition position , SourcePosition declarationPosition , Predicate expected ,
56+ VCSimplificationResult found , TranslationTable translationTable , Counterexample counterexample ,
57+ String customMessage , DisplayNames displayNames ) {
58+ super ("Refinement Error" , message (expected , found , displayNames ), position , translationTable , customMessage );
4959 this .expected = expected ;
5060 this .found = found ;
5161 this .counterexample = filterCounterexample (counterexample );
5262 this .declarationPosition = declarationPosition ;
63+ this .displayNames = displayNames ;
5364 if (!this .counterexample .isEmpty ()) {
5465 String counterexampleString = this .counterexample .assignments ().stream ()
55- .map (a -> VariableFormatter .format (a .first ()) + " == " + a .second ())
56- .collect (Collectors .joining (" && " ));
66+ .map (a -> displayNames .get (a .first ()) + " == " + a .second ()).collect (Collectors .joining (" && " ));
5767 setCounterexampleStr ("Counterexample: " + counterexampleString );
5868 }
5969 setCategory (categoryOf (expected , found ));
@@ -64,13 +74,35 @@ public RefinementError(SourcePosition position, SourcePosition declarationPositi
6474
6575 // Only the facts of found about the values expected constrains; path conditions about other values stay in the
6676 // derivation
67- private static String message (Predicate expected , VCSimplificationResult found ) {
68- Expression shown = linkedToExpected ( found . getImplication (). toPredicate (). getExpression (),
69- expected .getExpression ());
77+ private static String message (Predicate expected , VCSimplificationResult found , DisplayNames names ) {
78+ Expression shown = shownFound ( expected , found );
79+ String expectedString = ExpressionFormatter . format ( expected .getExpression (), names );
7080 if (isTrue (shown ))
71- return String .format ("expected %s but could not prove it" , expected .getExpression ().toDisplayString ());
72- return String .format ("%s is not a subtype of %s" , shown .toDisplayString (),
73- expected .getExpression ().toDisplayString ());
81+ return String .format ("expected %s but could not prove it" , expectedString );
82+ return String .format ("%s is not a subtype of %s" , ExpressionFormatter .format (shown , names ), expectedString );
83+ }
84+
85+ /** The facts of found that the message shows */
86+ static Expression shownFound (Predicate expected , VCSimplificationResult found ) {
87+ return linkedToExpected (found .getImplication ().toPredicate ().getExpression (), expected .getExpression ());
88+ }
89+
90+ /**
91+ * The names of an error's variables: those of its message come first, then those only the steps of found show, each
92+ * with the line its value comes from
93+ */
94+ static DisplayNames displayNames (Predicate expected , VCSimplificationResult found ,
95+ TranslationTable translationTable ) {
96+ List <String > shown = new ArrayList <>();
97+ shownFound (expected , found ).getVariableNames (shown );
98+ expected .getExpression ().getVariableNames (shown );
99+ VCSimplificationResult original = original (found );
100+ List <String > others = original .getBinders ();
101+ original .getImplication ().toPredicate ().getExpression ().getVariableNames (others );
102+ return DisplayNames .of (shown , others , name -> {
103+ PlacementInCode placement = translationTable == null ? null : translationTable .get (name );
104+ return placement == null ? null : placement .getLine ();
105+ });
74106 }
75107
76108 /** Unknown when what was found says nothing about the values the expected refinement constrains */
@@ -97,6 +129,11 @@ public VCSimplificationResult getFound() {
97129 return found ;
98130 }
99131
132+ /** The names this error gives its variables, for every view of it to show them alike */
133+ public DisplayNames getDisplayNames () {
134+ return displayNames ;
135+ }
136+
100137 // Keeps the counterexample assignments to the terms of expected and of the found facts linked to them, except
101138 // values found already states, so there is none when found fixes every value expected depends on. The terms of
102139 // expected come first, those found says nothing about before the rest, then the linked terms in the order of the
@@ -144,10 +181,14 @@ private static boolean states(Expression term, Expression value, Pair<String, St
144181
145182 // Binders of the VC chain before simplification, which keeps the variables in the order they were introduced
146183 private List <String > originalBinders () {
147- VCSimplificationResult original = getFound ();
184+ return original (getFound ()).getBinders ();
185+ }
186+
187+ private static VCSimplificationResult original (VCSimplificationResult found ) {
188+ VCSimplificationResult original = found ;
148189 while (original .getOrigin () != null )
149190 original = original .getOrigin ();
150- return original . getBinders () ;
191+ return original ;
151192 }
152193
153194 // 0 for terms of expected found says nothing about, whose values alone break it, 1 for the other terms of
0 commit comments