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: Make datatype cycle detection independent of auto-init #4997

Merged
merged 24 commits into from
Jan 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5d45706
chore: Add documentation and sanity assertions
RustanLeino Jan 18, 2024
c868719
Compute IsObviouslyEmpty for inductive datatypes
RustanLeino Jan 18, 2024
d205272
Fix typo in comment
RustanLeino Jan 19, 2024
4036d59
Don’t use cache (that conflates all type-parrameter instantiations)
RustanLeino Jan 19, 2024
013546b
Also check cycles going through result type of total arrows
RustanLeino Jan 19, 2024
6fea52f
Add tests, and adjust for ordering for checks
RustanLeino Jan 19, 2024
a1f78c5
Simplify tests
RustanLeino Jan 19, 2024
bc9354b
Rename test file
RustanLeino Jan 19, 2024
98a3240
Add verification tests to confirm not auto-init
RustanLeino Jan 19, 2024
d6343ed
Set grounding-ctor-type-parameters and test compilation
RustanLeino Jan 19, 2024
8079558
Fix typo in comment
RustanLeino Jan 19, 2024
2fe447a
Merge branch 'master' into issue-4939
RustanLeino Jan 19, 2024
77f6046
Add release notes
RustanLeino Jan 19, 2024
2018422
Merge branch 'master' into issue-4939
RustanLeino Jan 19, 2024
5d1aec4
Merge branch 'master' into issue-4939
RustanLeino Jan 19, 2024
aec8edc
Fix whitespace
RustanLeino Jan 19, 2024
e660898
Don’t bother with cyclicity test if datatype had refinement error dur…
RustanLeino Jan 19, 2024
0cbc0dd
Change no-instance-because-of-datatype-cycle error into warning
RustanLeino Jan 22, 2024
a1275b1
Add a test that shows an empty datatype is provably empty
RustanLeino Jan 22, 2024
fc44f01
chore: Improve C#
RustanLeino Jan 23, 2024
2a660b5
fix: Box function-body results and let-RHS from datatype to trait
RustanLeino Jan 23, 2024
53ee5a4
Merge branch 'master' into issue-4939
RustanLeino Jan 23, 2024
84a81d5
Merge branch 'master' into issue-4939
ssomayyajula Jan 23, 2024
aad6805
Merge branch 'master' into issue-4939
RustanLeino Jan 24, 2024
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
Prev Previous commit
Next Next commit
Also check cycles going through result type of total arrows
  • Loading branch information
RustanLeino committed Jan 19, 2024
commit 013546b1ca199c424c14a23d6c3797145f80177a
32 changes: 25 additions & 7 deletions Source/DafnyCore/Resolver/ModuleResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,13 +1272,27 @@ public partial class ModuleResolver {
FillInPostConditionsAndBodiesOfPrefixLemmas(declarations);
}

// Perform the stratosphere check on inductive datatypes, and compute to what extent the inductive datatypes require equality support
foreach (var dtd in declarations.ConvertAll(decl => decl as IndDatatypeDecl).Where(dtd => dtd != null)) {
if (AreThereAnyObviousSignsOfEmptiness(UserDefinedType.FromTopLevelDecl(dtd.tok, dtd), new HashSet<IndDatatypeDecl>())) {
reporter.Error(MessageSource.Resolver, dtd,
$"because of cyclic dependencies among constructor argument types, no instances of datatype '{dtd.Name}' can be constructed");
// An inductive datatype is allowed to be defined as an empty type. For example, in
// predicate P(x: int) { false }
// type Subset = x: int | P(x) witness *
// datatype Record = Record(Subset)
// Record is an empty type, because Subset is, since P(x) is always false. But if P(x)
// was instead defined to be true for some x's, then Record would be nonempty. Determining whether or
// not Record is empty goes well beyond the syntactic checks of the type system.
//
// However, if a datatype is empty because of some "obvious" cycle among datatype definitions, then
// that is both detectable by syntactic checks and likely unintended by the programmer. Therefore,
// we search for such type declarations and give error messages if something is found.
if (reporter.Count(ErrorLevel.Error) == prevErrorCount) {
foreach (var dtd in declarations.ConvertAll(decl => decl as IndDatatypeDecl).Where(dtd => dtd != null)) {
if (AreThereAnyObviousSignsOfEmptiness(UserDefinedType.FromTopLevelDecl(dtd.tok, dtd), new HashSet<IndDatatypeDecl>())) {
reporter.Error(MessageSource.Resolver, dtd,
$"because of cyclic dependencies among constructor argument types, no instances of datatype '{dtd.Name}' can be constructed");
ssomayyajula marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// Perform the stratosphere check on inductive datatypes, and compute to what extent the inductive datatypes require equality support
foreach (var dtd in datatypeDependencies.TopologicallySortedComponents()) {
if (datatypeDependencies.GetSCCRepresentative(dtd) == dtd) {
// do the following check once per SCC, so call it on each SCC representative
Expand Down Expand Up @@ -2853,10 +2867,14 @@ public ReportOtherAdditionalInformation_Visitor(ModuleResolver resolver)
}

private bool AreThereAnyObviousSignsOfEmptiness(Type type, ISet<IndDatatypeDecl> beingVisited) {
type = type.NormalizeExpand(); // cut through type proxies, type synonyms, but being mindful of what's in scope
type = type.NormalizeExpandKeepConstraints(); // cut through type proxies, type synonyms, but being mindful of what's in scope
if (type is UserDefinedType { ResolvedClass: var cl} udt) {
Contract.Assert(cl != null);
if (cl is NewtypeDecl newtypeDecl) {
if (ArrowType.IsTotalArrowTypeName(cl.Name)) {
return AreThereAnyObviousSignsOfEmptiness(udt.TypeArgs.Last(), beingVisited);
} else if (cl is SubsetTypeDecl subsetTypeDecl) {
return AreThereAnyObviousSignsOfEmptiness(subsetTypeDecl.RhsWithArgument(udt.TypeArgs), beingVisited);
} else if (cl is NewtypeDecl newtypeDecl) {
return AreThereAnyObviousSignsOfEmptiness(newtypeDecl.RhsWithArgument(udt.TypeArgs), beingVisited);
}
if (cl is IndDatatypeDecl datatypeDecl) {
Expand Down