-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No flatten for most backends (#5528)
### Description Do not flatten matches for most backends. Such flattening can cause the generated code size to be the square of the input code size. Removing the flattening leads to a huge decrease in the size of the generated C# code inside the Dafny codebase. It goes from ~14K lines to ~7K. The compilation of nested matches is done as follows: ```dafny datatype TX = X(x1arg: TY, x2arg: TY) datatype TY = Y(yarg: int) datatype TZ = Z(zarg: TW) datatype TW = W(warg: int) method M(a: TX) { match a case X(Y(b),Z(W(c)) => <body1> case r => <body2> } ``` Is roughly compiled into ```dafny // Same datatypes method M(a: TX) { var unmatched := true; if (unmatched && a is X) { var x1arg1 = ((X)a).1; if (x1 is Y) { var b = ((Y)x1arg1).1; var x2arg2 := ((X)a).2; if (x2 is Z) { var zarg1 := ((Z)x2arg2).1; if (x4 is W) { var c := ((W)zarg1).1; unmatched := false; <body1> } } } } if (unmatched) { var r := a; <body2> } } ``` #### Caveats ##### Maintainability To reduce the required work, Java and Dafny back-ends still compile using flattened matches Ideally the transformation would be a Dafny-to-Dafny source transformation, instead of a customization of SinglePassCompiler. However, Dafny does not allow using statements in expression contexts, and this is needed for the transformation. I think it would be good to have an intermediate Dafny that does allow this, similar to what @cpitclaudel 's Dafny-in-Dafny compiler allowed, and then to define the rewrite that this PR introduces using a Dafny source translation. ##### Improvement For C# we could generate much nicer code, since C# allows declaring new variables inside expressions using `x is T xAsType` expressions. We could get rid of the nested `if` statements and the `unmatched` variable. However, I'll leave this for future work. ### How has this been tested? - Performance change. No additional tests added. <small>By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
- Loading branch information
1 parent
fe3b56c
commit 5b048ff
Showing
35 changed files
with
7,176 additions
and
14,203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 109 additions & 109 deletions
218
Source/DafnyCore.Test/GeneratedFromDafny/DafnyToRustCompilerCoverage_RASTCoverage.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.