Skip to content

Commit

Permalink
fix: Remove multiset cardinality cap in Python (#4014)
Browse files Browse the repository at this point in the history
Fixes #4012.
  • Loading branch information
fabiomadge authored May 18, 2023
1 parent 2d31724 commit 33db038
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Source/DafnyCore/Compilers/Python/Compiler-python.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,10 @@ protected override void EmitUnaryExpr(ResolvedUnaryOp op, Expression expr, bool
ConcreteSyntaxTree wr, ConcreteSyntaxTree wStmts) {
switch (op) {
case ResolvedUnaryOp.Cardinality:
TrParenExpr("len", expr, wr, inLetExprBody, wStmts);
var multiset = expr.Type.AsMultiSetType != null;
if (!multiset) { wr.Write("len"); }
TrParenExpr(expr, wr, inLetExprBody, wStmts);
if (multiset) { wr.Write(".cardinality"); }
break;
case ResolvedUnaryOp.BitwiseNot:
TrParenExpr("~", expr, wr, inLetExprBody, wStmts);
Expand Down
5 changes: 3 additions & 2 deletions Source/DafnyRuntime/DafnyRuntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ class MultiSet(Counter):
def __dafnystr__(self) -> str:
return 'multiset{' + ', '.join(map(string_of, self.elements())) + '}'

def __len__(self):
@property
def cardinality(self):
return reduce(lambda acc, key: acc + self[key], self, 0)

def union(self, other):
Expand Down Expand Up @@ -272,7 +273,7 @@ def issubset(self, other):
return all(self[key] <= other[key] for key in frozenset(self).union(frozenset(other)))

def ispropersubset(self, other):
return self.issubset(other) and len(self) < len(other)
return self.issubset(other) and self.cardinality < other.cardinality

def set(self, key, value):
set = Counter(self)
Expand Down
8 changes: 8 additions & 0 deletions Test/git-issues/git-issue-4012.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %dafny /compile:4 /compileTarget:py "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

method Main() {
var x: multiset<int> := multiset{};
x := x[1 := 18446744073709551616];
print |x|, "\n";
}
3 changes: 3 additions & 0 deletions Test/git-issues/git-issue-4012.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Dafny program verifier finished with 1 verified, 0 errors
18446744073709551616
1 change: 1 addition & 0 deletions docs/dev/news/4014.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove multiset cardinality cap in Python

0 comments on commit 33db038

Please sign in to comment.