11package liquidjava .diagnostics .errors ;
22
3- import java .util .ArrayList ;
43import java .util .List ;
4+ import java .util .Set ;
55import java .util .stream .Collectors ;
66
77import liquidjava .diagnostics .TranslationTable ;
1010import liquidjava .rj_language .ast .formatter .VariableFormatter ;
1111import liquidjava .rj_language .opt .VCSimplificationResult ;
1212import liquidjava .smt .Counterexample ;
13+ import liquidjava .utils .Pair ;
1314import spoon .reflect .cu .SourcePosition ;
1415
1516/**
@@ -34,7 +35,7 @@ public RefinementError(SourcePosition position, SourcePosition declarationPositi
3435 position , translationTable , customMessage );
3536 this .expected = expected ;
3637 this .found = found ;
37- this .counterexample = counterexample ;
38+ this .counterexample = filterCounterexample ( counterexample ) ;
3839 this .declarationPosition = declarationPosition ;
3940 }
4041
@@ -45,38 +46,13 @@ public SourcePosition getDeclarationPosition() {
4546
4647 @ Override
4748 public String getDetails () {
48- String counterexampleString = getCounterExampleString ();
49- if (counterexampleString == null )
49+ if (counterexample .isEmpty ())
5050 return "" ;
51- return "Counterexample: " + counterexampleString ;
52- }
53-
54- public String getCounterExampleString () {
55- if (counterexample == null || counterexample .assignments ().isEmpty ())
56- return null ;
5751
58- List <String > foundVarNames = new ArrayList <>();
59- Expression foundExpression = getFound ().getImplication ().toPredicate ().getExpression ();
60- Expression expectedExpression = expected .getExpression ();
61- foundExpression .getVariableNames (foundVarNames );
62- // also keep resolved static-final constants (e.g. Integer.MAX_VALUE) referenced by either side of the
63- // subtyping check, so the counterexample maps the symbolic name back to its compile-time value
64- foundExpression .getResolvedConstantNames (foundVarNames );
65- expectedExpression .getResolvedConstantNames (foundVarNames );
66- List <String > foundAssignments = foundExpression .getConjuncts ().stream ().map (Expression ::toString ).toList ();
6752 String counterexampleString = counterexample .assignments ().stream ()
68- // only include variables that appear in the found value and are not already fixed there
69- .filter (a -> foundVarNames .contains (a .first ())
70- && !foundAssignments .contains (a .first () + " == " + a .second ()))
71- // format as "var == value"
7253 .map (a -> VariableFormatter .format (a .first ()) + " == " + a .second ())
73- // join with "&&"
7454 .collect (Collectors .joining (" && " ));
75-
76- if (counterexampleString .isEmpty ())
77- return null ;
78-
79- return counterexampleString ;
55+ return "Counterexample: " + counterexampleString ;
8056 }
8157
8258 public Counterexample getCounterexample () {
@@ -90,4 +66,18 @@ public Predicate getExpected() {
9066 public VCSimplificationResult getFound () {
9167 return found ;
9268 }
69+
70+ // Filters counterexample assignments only in found VC and sorts them in the order of its binders
71+ private Counterexample filterCounterexample (Counterexample counterexample ) {
72+ List <String > binderNames = getFound ().getBinders ();
73+ Set <String > knownAssignments = getFound ().getImplication ().toPredicate ().getExpression ().getConjuncts ().stream ()
74+ .map (Expression ::toString ).collect (Collectors .toSet ());
75+ List <Pair <String , String >> assignments = counterexample .assignments ().stream ()
76+ .filter (a -> binderNames .contains (a .first ()))
77+ .filter (a -> !knownAssignments .contains (a .first () + " == " + a .second ()))
78+ .sorted ((a , b ) -> Integer .compare (binderNames .indexOf (a .first ()), binderNames .indexOf (b .first ())))
79+ .toList ();
80+
81+ return new Counterexample (assignments );
82+ }
9383}
0 commit comments