Commit b3f70a8
authored
feat: Prelude extractor (#5621)
## Description
This PR adds a way to extract a Dafny model into set of Boogie
declarations. This new feature can be of general use, but its main
application is to justify various axioms in Dafny's `DafnyPrelude.bpl`
by providing a model for them.
## Impact on the Dafny tool
The model for Dafny's underlying type `Seq` is given in
`Source/DafnyCore/Prelude/Sequences.dfy`.
The file `Source/DafnyCore/DafnyPrelude.bpl` should no longer be edited
by hand. Instead, it is now generated from
`Source/DafnyCore/Prelude/PreludeCore.bpl` and from the model
`Sequences.dfy` (and other models in the future, for example for
multisets).
To generate a new version of `DafnyPrelude.bpl`, run `make extract` in
the `Source/DafnyCore` folder. This will run `make` in the
`Source/DafnyCore/Prelude` folder and will then copy the generated
`Prelude/DafnyPrelude.bpl` to `DafnyPrelude.bpl`.
## CLI option
This PR adds a CLI option `--extract:<file>` (where `<file>` is allowed
to be `-` to denote standard output, just like for the various print
options). This option works with the `dafny verify` command.
Caveats:
This gets the job done, but is probably not the best way of doing it.
For instance, one could imagine a `dafny extract` command that performs
this task.
I'm unsure of the code I added to handle this CLI option. In particular,
I don't understand if I have added it just for the `dafny verify`
command or also for other commands, and I don't understand if I have
made the option available via the language server (or if we care).
## Changes to `DafnyPrelude.bpl`
This PR contains changes to `DafnyPrelude.bpl`. The claim is that these
changes make no semantic difference. The changes are:
* differences in whitespace
* other changes to format the code like the Boogie `/print` option does
(for example, `s: Seq, t: Seq` instead of `s, t: Seq`)
* in the `Seq` part of `DafnyPrelude.bpl`, the declarations that are not
part of the model in `Sequences.dfy` have
been moved to the end, so that the extracted declarations can be in one
contiguous part of the file
The third of these bullets do change the order in which things are sent
to the SMT solver, and thus this may impact some users.
## Extract mechanism
Upon successful verification, the new extract mechanism added by this PR
will visit the AST of the given program. For any module marked with
`{:extract}`, the extract-worthy material from the module is output. The
output declarations will be in the same order as they appear textually
in the module (in particular, the fact that module-level Dafny
declarations are collected in an internal class `_default` has no
bearing on the output order).
Three kinds of declarations are extract-worthy:
* A type declaration `A<X, Y, Z>` that bears an attribute
`{:extract_name B}` is extracted into a Boogie type declaration `type B
_ _ _;`.
The definition of the type is ignored. (The intended usage for an
extracted type is that the Dafny program give a definition for the type,
which goes to show the existence of such a type.)
* A function declaration `F(x: X, y: Y): Z` that bears an attribute
`{:extract_name G}` is extracted into a Boogie function declaration
`function G(x: X, y: Y): Z;`.
The body of the Dafny function is ignored. (The intended usage for an
extracted function is that the Dafny program give a definition for the
function, which goes to show the existence of such a function.)
* A lemma declaration `L(x: X, y: Y) requires P ensures Q` that bears an
attribute `{:extract_pattern ...}` or an attribute `{:extract_used_by
...}` is extracted into a Boogie `axiom`. The axiom has the basic form
`axiom (forall x: X, y: Y :: P ==> Q);`.
If the lemma has an attribute `{:extract_used_by F}`, then the axiom
will be emitted into the `uses` clause of the Boogie function generated
for Dafny function `F`.
If the lemma has no in-parameters, the axiom is just `P ==> Q`.
If the lemma has in-parameters, then any attribute `{:extract_pattern E,
F, G}` adds a matching pattern `{ E, F, G }` to the emitted quantifier.
Also, any attribute `{:extract_attribute "name", E, F, G}` adds an
attribute `{:name E, F, G}` to the quantifier.
## Expressions
The pre- and postconditions of extracted lemmas turn into analogous
Boogie expressions, and the types of function/lemma parameters and bound
variables are extracted into analogous Boogie types. The intended usage
of the extract mechanism is that these expressions and types do indeed
have analogous Boogie types.
At this time, only a limited set of expressions and types are supported,
but more can be added in the future.
Any `forall` and `exists` quantifiers in expressions are allowed to use
`:extract_pattern` and `:extract_attribute` attributes, as described
above for lemmas.
Some extracted expressions are simplified. For example, `true && !!P` is
simplified to `P`.
## Soundness
The Dafny program that is used as input for the extraction is treated
like any other Dafny program. The intended usage of the extraction
mechanism is to prove parts of the axiomatization in `DafnyPrelude.bpl`
to be logically consistent. Whether or not the extracted Boogie
declarations meet this goal depends on the given Dafny program. For
example, if the given Dafny program formalizes sequences in terms of
maps and formalizes maps in terms of sequences, then the extraction
probably does not provide guarantees of consistency.
<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>1 parent d869f97 commit b3f70a8
File tree
21 files changed
+2922
-163
lines changed- Source
- DafnyCore
- Backends
- Prelude
- Verifier
- DafnyDriver/Commands
- IntegrationTests/TestFiles/LitTests/LitTest
- dafny0
- dafny1
- git-issues
- server
- docs/dev/news
21 files changed
+2922
-163
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| 323 | + | |
323 | 324 | | |
324 | 325 | | |
325 | 326 | | |
| |||
0 commit comments