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: Generate dummy constraint for newtypes to enable axiom generation #4190

Merged
merged 9 commits into from
Jun 30, 2023

Conversation

fabiomadge
Copy link
Collaborator

Fixes #4188.

@fabiomadge
Copy link
Collaborator Author

fabiomadge commented Jun 16, 2023

Before merging we need to figure out how to pass the wellformedness check, now that we don't conveniently skip it anymore. The test fails, because we lose track of the witness.

@fabiomadge fabiomadge marked this pull request as draft June 16, 2023 01:15
// RUN: %baredafny verify %args "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

newtype Int = nat
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest making two more variations of this example, namely the ones mentioned in the Issue discussion (newtype Int = int and newtype Int = n: nat | true, perhaps also with newtype Int = x: int | 0 <= x).

Comment on lines 1277 to 1285
dd.Var ??= new BoundVar(dd.tok, "x", dd.BaseType) { IsTypeExplicit = true };
dd.Constraint ??= Expression.CreateBoolLiteral(dd.tok, true);

AddWellformednessCheck(dd);
currentModule = dd.EnclosingModuleDefinition;
// Add $Is and $IsAlloc axioms for the newtype
AddRedirectingTypeDeclAxioms(false, dd, dd.FullName);
AddRedirectingTypeDeclAxioms(true, dd, dd.FullName);
currentModule = null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well identified! These type axioms are what's missing. However, it's not a good idea to change the AST at this time. Also, I think it's fine to continue to omit a well-formedness check for these var-less newtypes (hence, addressing your question in the PR comment).

So, I suggest making the if (dd.Var != null) control only whether or not AddWellformednessCheck is called. Then, do the other lines in either case (as you have in your new L1281-L1285).

Then, adjust AddRedirectingTypeDeclAxioms so that it's able to handle the dd.Var == null case. Instead of dd.Var.Type (which occurs many times in that method), use baseType defined as

var baseType = dd.Var != null ? dd.Var.Type : ((NewtypeDecl)(object)dd).BaseType;

One of the method's preconditions thus needs to change to

Contract.Requires((dd.Var != null && dd.Constraint != null) || dd is NewtypeDecl);

There are two other occurrences of dd.Var that you'll need to handle in some way.

FInally, if dd.Constraint is null, use Bpl.Expr.True instead of etran.TrExpr(dd.Constraint).

@fabiomadge fabiomadge marked this pull request as ready for review June 24, 2023 02:10
Revert unnecessary change.
@fabiomadge fabiomadge enabled auto-merge (squash) June 29, 2023 00:19
@fabiomadge fabiomadge merged commit 90982be into dafny-lang:master Jun 30, 2023
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dafny unable to produce arbitrary witness element from set of nats in custom datatype
2 participants