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

feat: Use (wrapped) native strings instead of sequences of characters in Python #2130

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
feat: Use (wrapped) native strings instead of sequences of characters…
… in Python
  • Loading branch information
fabiomadge committed May 10, 2022
commit f53151724da41bd6dcb93786c45bef04f095fabc
7 changes: 5 additions & 2 deletions Source/Dafny/Compilers/Compiler-python.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ protected class ClassWriter : IClassWriter {
var wStmts = wr.Fork();
wr.Write($"raise {DafnyRuntimeModule}.HaltException(");
if (tok != null) {
wr.Write($"{DafnyRuntimeModule}.Seq(\"{Dafny.ErrorReporter.TokenToString(tok)}: \") + ");
wr.Write($"{DafnySeqClass}(\"{Dafny.ErrorReporter.TokenToString(tok)}: \") + ");
}

TrExpr(messageExpr, wr, false, wStmts);
Expand Down Expand Up @@ -716,7 +716,7 @@ protected class ClassWriter : IClassWriter {
wr.Write($"'{(string)e.Value}'");
break;
case StringLiteralExpr str:
wr.Write($"{DafnyRuntimeModule}.Seq(");
wr.Write($"{DafnySeqClass}(");
TrStringLiteral(str, wr);
wr.Write(")");
break;
Expand Down Expand Up @@ -1257,6 +1257,9 @@ protected class ClassWriter : IClassWriter {
wr.Write(open);
TrExprList(elements, wr, inLetExprBody, wStmts, parens: false);
wr.Write(close);
if (ct is SeqType && ct.TypeArgs.TrueForAll(ty => ty.IsCharType)) {
wr.Write(", isStr = True");
}
wr.Write(")");
}

Expand Down
27 changes: 18 additions & 9 deletions Source/DafnyRuntime/DafnyRuntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,26 @@ def _break(name):
def _tail_call():
raise TailCall()

class String(str):
def __new__(cls, *args, **kw):
return super().__new__(cls, *args, **kw)

def __repr__(self):
return self

def __add__(self, other):
return String(super().__add__(other))

class Seq(list):
def __init__(self, __iterable = None, isStr = False):
def __new__(cls, __iterable = None, isStr = False):
if __iterable is None:
__iterable = []
self.isStr = isinstance(__iterable, str) or isStr
super().__init__(__iterable)
if isinstance(__iterable, str):
return String(__iterable)
elif isStr:
return String(''.join(__iterable))
else:
return super().__new__(cls, __iterable)

@property
def Elements(self):
Expand All @@ -54,13 +68,8 @@ def Elements(self):
def UniqueElements(self):
return set(self)

def __repr__(self) -> str:
if self.isStr:
return ''.join(self)
return super().__repr__()

def __add__(self, other):
return Seq(super().__add__(other), isStr=self.isStr and other.isStr)
return Seq(super().__add__(other))

def __hash__(self) -> int:
return hash(tuple(self))
Expand Down
5 changes: 5 additions & 0 deletions Test/comp/Various.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ method LetExpr() {
print (var a := A(0); var A(zero) := a; zero), "\n";
}

method PrintString() {
print [[], [] + "Hallo " + ['W', 'e', 'l', 't', '!']], "\n";
}

method Main() {
Match((0,1));
Countdown(1);
LetExpr();
PrintString();
}
5 changes: 5 additions & 0 deletions Test/comp/Various.dfy.expect
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ match
1
Kablammo!!
0
[, Hallo Welt!]

Dafny program verifier did not attempt verification
match
1
Kablammo!!
0
[, Hallo Welt!]

Dafny program verifier did not attempt verification
match
1
Kablammo!!
0
[, Hallo Welt!]

Dafny program verifier did not attempt verification
match
1
Kablammo!!
0
[, Hallo Welt!]

Dafny program verifier did not attempt verification
match
1
Kablammo!!
0
[, Hallo Welt!]