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

(Idea) Implement built-in Dafny types in Dafny source code #1775

Open
robin-aws opened this issue Feb 1, 2022 · 6 comments
Open

(Idea) Implement built-in Dafny types in Dafny source code #1775

robin-aws opened this issue Feb 1, 2022 · 6 comments
Assignees
Labels
kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: code-generation Support for transpiling Dafny to another language. If relevant, add a `lang:` tag part: runtime Happens in Dafny's runtime (Add a `lang` tag if relevant) priority: not yet Will reconsider working on this when we're looking for work

Comments

@robin-aws
Copy link
Member

Take seq<T> for example. The implementation in the C# and Java runtimes are very similar, and do not simply wrap an existing type in the target language runtime because the lazy evaluation of concatenation is very important for reasonable performance. If this was implemented once in a Dafny prelude instead using classes and traits, and references to seq<T> rewritten in the compiler, we would get several benefits:

  1. Formally-verified correctness.
  2. No additional per-backend work to support some concepts, unless there was a need to further optimize the default implementation.
  3. A mechanism to ensure Dafny contributors "dogfood" the language and implement/maintain non-trivial Dafny code.

This also may apply to some of the types tracked by the Builtins class, such as tuples (which can be thought of as built-in anonymous datatypes).

Related: #1259

@robin-aws robin-aws added kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: code-generation Support for transpiling Dafny to another language. If relevant, add a `lang:` tag part: runtime Happens in Dafny's runtime (Add a `lang` tag if relevant) labels Feb 1, 2022
@keyboardDrummer
Copy link
Member

"If this was implemented once in a Dafny prelude instead using classes and traits, and references to seq rewritten in the compiler"

How come it needs to be defined both in a prelude, and rewritten in the compiler? Are you suggesting to implement Seq in Dafny using arrays and then not having a runtime specific part for it? If so, then what would you need to rewrite in the compiler?

@cpitclaudel
Copy link
Member

How come it needs to be defined both in a prelude

I think Robin is suggesting to have a class Seq<T> somewhere and rewrite references from seq<T> to DafnyPrelude.Seq<T>.

It would be nice to be able to just define seq<T> in a regular Dafny file and just include that, though.

@robin-aws
Copy link
Member Author

I just expect this would need at least some compiler rewriting, since you can't just declare seq<T> to be an alias for a reference type in a regular Dafny prelude file. Is IS valid to do that substitution as part of a lowering transformation phase though.

@keyboardDrummer
Copy link
Member

Wouldn't the compiler rewrite be to have an implicit opened import of DafnyPrelude, instead of a rewrite specific to seq ?

@robin-aws
Copy link
Member Author

I should not have used the term "prelude" in this case, as I'm not proposing Dafny source code that is directly include-d and import-d, because there isn't a way to declare something like type seq<T> = Sequence<T>.

Sequences are currently implemented in each runtime via one or more target language classes or similar datatypes, such as ISequence<T>, Sequence<T>, ArraySequence<T>, ConcatSequence<T> etc. in C#. I'm only proposing a similar common implementation in Dafny code, which will be compiled to each target language as externs with similar names. Then it is only a matter of tweaking code generation for each language to target the Dafny-generated types instead.

@robin-aws
Copy link
Member Author

This is another improvement for the implementation of sequences that would be much nicer to do once in Dafny rather than N times in each runtime: #2859

robin-aws added a commit that referenced this issue Feb 11, 2023
…untime (#3167)

Addresses #2390 for Go, via the strategy described in #1775.

~The only outstanding TODO I'm concerned with is the special-casing in
the Go compiler needed to allow `Sequence<T>` to specify a custom
equality predicate. I'm open to suggestions on how to improve that: at a
minimum it should be checking the full type names, so that we can safely
use the fact that the top-level `dafny` module is reserved, but I'm
hoping there's a cleaner solution.~
Adds an internal `--bootstrapping` option only for use when compiling
Dafny source used in the Dafny implementation itself.

Also adds a `--platform` option to `package.py`: while debugging earlier
attempts to change the build process, I realized that packaging for Mac
OS was often picking the wrong platform.

<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>
@MikaelMayer MikaelMayer added the priority: not yet Will reconsider working on this when we're looking for work label Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: code-generation Support for transpiling Dafny to another language. If relevant, add a `lang:` tag part: runtime Happens in Dafny's runtime (Add a `lang` tag if relevant) priority: not yet Will reconsider working on this when we're looking for work
Projects
None yet
Development

No branches or pull requests

4 participants