Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Dafny server API for counterexamples #5847

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Source/DafnyCore/CounterExampleGeneration/PartialState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,42 @@ private Expression GetCompactConjunction(List<Expression> conjuncts) {
return new BinaryExpr(Token.NoToken, BinaryExpr.Opcode.And, left, right);
}

public Dictionary<string, string> AsVariableValueDictionary() {
var variableToValue = new Dictionary<PartialValue, Expression>();
var variables = ExpandedVariableSet().ToArray();
var constraintSet = new HashSet<Constraint>();

foreach (var variable in variables) {
foreach (var constraint in variable.Constraints) {
constraintSet.Add(constraint);
}
}

Constraint.ResolveAndOrder(variableToValue, constraintSet.ToList(), true, true);

var variableValuesDictionary = new Dictionary<string, string>();
foreach (var variable in variables) {
var variableValue = variableToValue[variable].ToString();
foreach (var constraint in variable.Constraints.OfType<DefinitionConstraint>()
.Where(constraint => constraint.DefinedValue == variable &&
(constraint is not LiteralExprConstraint) &&
(constraint is not DatatypeValueConstraint) &&
(constraint is not SeqDisplayConstraint))) {
if (constraint.ReferencedValues.Any(value => !variableToValue.ContainsKey(value))) {
continue;
}
var variableNameAsExpression = constraint.RightHandSide(variableToValue);
var variableNameAsString = variableNameAsExpression.ToString();
if (variableValue == variableNameAsString) {
continue;
}
variableValuesDictionary[variableNameAsString] = variableValue + ",";
}
}

return variableValuesDictionary;
}

/// <summary>
/// Convert this counterexample state into an assumption that could be inserted in Dafny source code
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
namespace Microsoft.Dafny.LanguageServer.Handlers.Custom {
public class CounterExampleItem {
public Position Position { get; }
public IDictionary<string, string> Variables { get; }

public string Assumption { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend the Assumption field to be used by the IDE? Otherwise better not to have it and reduce bandwidth.


public CounterExampleItem(Position position, string assumption) {
public CounterExampleItem(Position position, IDictionary<string, string> variables, string assumption) {
Position = position;
Variables = variables;
Assumption = assumption;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ private IEnumerable<CounterExampleItem> GetCounterExamples(DafnyModel model) {
private CounterExampleItem GetCounterExample(PartialState state) {
return new(
new Position(state.GetLineId() - 1, state.GetCharId()),
state.AsAssumption().ToString()
state.AsVariableValueDictionary(),
state.AsAssumption().ToString()
);
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/dev/news/5847.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug preventing counterexamples from being displayed in the IDE
Loading