diff options
author | Rutger Broekhoff | 2025-07-07 21:52:08 +0200 |
---|---|---|
committer | Rutger Broekhoff | 2025-07-07 21:52:08 +0200 |
commit | ba61dfd69504ec6263a9dee9931d93adeb6f3142 (patch) | |
tree | d6c9b78e50eeab24e0c1c09ab45909a6ae3fd5db /README.md | |
download | verified-dyn-lang-interp-ba61dfd69504ec6263a9dee9931d93adeb6f3142.tar.gz verified-dyn-lang-interp-ba61dfd69504ec6263a9dee9931d93adeb6f3142.zip |
Initialize repository
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 383 |
1 files changed, 383 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad7b1b7 --- /dev/null +++ b/README.md | |||
@@ -0,0 +1,383 @@ | |||
1 | Artifact for "Verified Interpreters for Dynamic Languages with Applications to the Nix Expression Language" | ||
2 | =========================================================================================================== | ||
3 | |||
4 | This is the accompanying artifact for the following paper: | ||
5 | > Rutger Broekhoff and Robbert Krebbers. 2025. Verified Interpreters for Dynamic Languages with Applications to the Nix Expression Language. _Proc. ACM Program. Lang._ 9, ICFP, Article 268 (August 2025), 29 pages. https://doi.org/10.1145/3747537 | ||
6 | |||
7 | This artifact primarily consists of two components: | ||
8 | 1. A Rocq formalization of the languages presented in the paper (LambdaLang; § 2, DynLang; § 3, EvalLang; § 3.4, NixLang; § 4). | ||
9 | This includes operational semantics and properties, an interpreter and correctness proof, plus some tests and examples. | ||
10 | 2. An OCaml front-end for NixLang (§ 5), which elaborates Nix source files into NixLang. | ||
11 | These are then evaluated using the NixLang interpreter, which is derived from the Rocq sources using Rocq's program extraction functionality. | ||
12 | |||
13 | > N.B.: If you are using the VM image that we provide for evaluating the artifact, then all necessary dependencies will already be installed using `opam`. | ||
14 | > You can directly proceed with using the [Dune project and Makefile](#dune-project-and-makefile) or [using the CLI](#usage-of-the-cli). | ||
15 | |||
16 | For the purposes of evaluating the artifact, we suggest the following steps: | ||
17 | 1. [Building](#building) (this must certainly be done first). | ||
18 | 2. Comparing that the mechanized semantics and interpreters of the defined languages match those presented in the paper. | ||
19 | The mechanization of all languages follows a [common structure](#structure-of-the-mechanization), but we also have a [table](#index-of-definitions-and-theories) that points you directly to the relevant definitions. | ||
20 | 3. Comparing that the mechanized theorems correspond to those presented in the paper. | ||
21 | We have a [table](#index-of-definitions-and-theories) that lists all theorems in the paper and their corresponding version in the formalization. | ||
22 | The paper mentions some properties/proofs in passing, we list the corresponding mechanized versions for these as well (but it may be less relevant to verify these, as they are primarily used as stepping stones for the major theorems presented in the paper). | ||
23 | 4. Exercising the Nix test suite on our interpreter and verifying that this gives the same results as presented in the paper (§ 5). | ||
24 | 5. Trying out some Nix programs in our REPL. | ||
25 | The paper (§ 1, § 4.1, § 5) contains some examples that might be interesting to try out. | ||
26 | |||
27 | You may also be interested in the [axioms used](#use-of-axioms) by the mechanization, or in reproducing the interpreter coverage number (91.77%, § 5, p. 22) and Table 1 (§ 5 p. 22). | ||
28 | How to do this depends on the build method that you are using, see the followings section. | ||
29 | |||
30 | > N.B.: We have only verified this artifact to function on Linux (Arch Linux, Debian) and macOS. It does not work on Windows. | ||
31 | |||
32 | ## Building | ||
33 | |||
34 | As mentioned at the start, this project consists of two parts: the Rocq mechanization and the front-end of the Nix interpreter. | ||
35 | The front-end of the Nix interpreter, which uses the makes use of the NixLang interpreter (written in Rocq, extracted to OCaml), is built using [Dune](https://dune.build/). | ||
36 | The Dune project also takes care of checking all of the parts of the Rocq mechanization, and extracting the NixLang interpreter to OCaml. | ||
37 | However, for convenience, we have a separate (classic) `Makefile` which checks only the Rocq sources (this may be more familiar to some, and will explicitly print the files being checked). | ||
38 | (Do note that Dune will refuse to build if the `.vo` files resulting from the checking process are not cleaned up; this can be done using `make clean`.) | ||
39 | |||
40 | *Both parts require dependencies.* | ||
41 | We have two main ways of managing the dependencies of the project: either using Nix or `opam`. | ||
42 | |||
43 | If you are familiar with Nix and have it available to you, it will likely be most easy to use. | ||
44 | If you are just interested in using Nix to build the project (so not directly interacting with the Dune project / Makefile), look [here](#building-with-nix). | ||
45 | If you are instead interested in interacting with `dune`, Makefile and/or other scripts in this repository, you should probably use the Nix devshell instead. | ||
46 | For that, look [here](#managing-dependencies-nix-devshell). | ||
47 | With the devshell set up, you can then proceed with using the [Dune project and Makefile](#dune-project-and-makefile). | ||
48 | |||
49 | If you cannot or do not want to use Nix, there is always still `opam`. | ||
50 | Look [here](#managing-dependencies-opam) to see how to install project dependencies with `opam`. | ||
51 | With the dependencies installed using `opam`, you can then proceed with using the [Dune project and Makefile](#dune-project-and-makefile). | ||
52 | |||
53 | ### Building with Nix | ||
54 | |||
55 | > N.B.: Only do this if you are solely interested in building/testing and/or using the artifact. If you are interested in being able to manually use the `Makefile` and Dune project, you should use a Nix devshell instead; look [here](#nix-devshell) for instructions. | ||
56 | |||
57 | Run `nix-build` (or equivalently `nix-build ./default.nix`) to build and test the artifact. This will take a few minutes. | ||
58 | The resulting CLI should then be available as `./result/bin/mininix`. | ||
59 | See how to use the CLI [below](#usage-of-the-cli). | ||
60 | For some more details on the test suite, look [here](#use-of-the-nix-language-tests). | ||
61 | |||
62 | Run `nix-build ./axioms.nix` to generate the list of axioms used by the formalization. | ||
63 | This is equivalent to running `make validate`. | ||
64 | The output of `coqchk` should be printed directly, but will also be stored at `./result/coqchk-output`. | ||
65 | See [below](#use-of-axioms) for more information on the axioms used. | ||
66 | |||
67 | In the paper, we report a 91.77% coverage for the interpreter code extracted from Rocq. | ||
68 | To generate the coverage report, run `nix-build ./coverage.nix`. | ||
69 | The coverage report should then be available as `./result/coverage/report-plain` (a text file, look for `lib/extraction/interp.ml` for the interpreter coverage). | ||
70 | A HTML version is also generated, the report for the interpreter should then be available under `./result/coverage/html/lib/extraction/interp.ml.html`. | ||
71 | |||
72 | To generate the line counts for the Rocq development (for comparison with Table 1 in the article), run `nix-build ./cloc.nix`. | ||
73 | The resulting report should be available as a text file `./result/formalization-loc-report`. | ||
74 | |||
75 | ### Managing dependencies: Nix devshell | ||
76 | |||
77 | The Nix devshell gives you a shell where all required dependencies are installed for you, so you do not have to fiddle with `opam`, but can still use the Dune project and Makefile that we provide. | ||
78 | There are a few ways that you can use the devshell. | ||
79 | |||
80 | Enter the devshell by running `nix-shell`. | ||
81 | You can then proceed with using the [Dune project and Makefile](#dune-project-and-makefile) *inside of the Nix devshell*. | ||
82 | |||
83 | ### Managing dependencies: opam | ||
84 | |||
85 | First, ensure that your `opam` repositories are up-to-date: | ||
86 | ```sh | ||
87 | opam update | ||
88 | ``` | ||
89 | |||
90 | For maximum flexibility and reproducibility, we recommend running the following | ||
91 | command to create a new opam switch in which the pinned dependencies are | ||
92 | installed (this includes the required Rocq, Flocq, Rocq-std++ versions etc.): | ||
93 | |||
94 | ```sh | ||
95 | # Leave out the --locked flag if not on Linux! | ||
96 | opam switch create ./ --repos default,rocq-released=https://rocq-prover.org/opam/released --deps-only --locked | ||
97 | ``` | ||
98 | |||
99 | It may be necessary to activate the newly set up opam switch as follows (if | ||
100 | you are not sure and opam instructs you to run this after creating the switch: | ||
101 | _do_ run it): | ||
102 | |||
103 | ```sh | ||
104 | eval $(opam env) | ||
105 | ``` | ||
106 | |||
107 | The [Dune project `Makefile`](#dune-project-and-makefile) can then be used as usual. | ||
108 | |||
109 | ### Dune project and `Makefile` | ||
110 | |||
111 | #### The Makefile (just the formalization) | ||
112 | |||
113 | If you are solely interested in the Rocq sources, you may make use of the `Makefile` to check and build Rocq sources and dependencies, provided that you have the appropriate [Rocq](https://rocq-prover.org/), [Flocq](https://flocq.gitlabpages.inria.fr/) and [Rocq-std++](https://gitlab.mpi-sws.org/iris/stdpp) versions available (8.20.1, 4.2.0/4.2.1 and 1.11.0 resp.). | ||
114 | When using a Nix devshell or an `opam` switch with the required dependencies installed (instructions above), this should work automatically. | ||
115 | |||
116 | Run `make` to check all files in the formalization. | ||
117 | Run `make validate` to print the axioms used. | ||
118 | See [below](#use-of-axioms) for more information on the axioms used. | ||
119 | |||
120 | #### The Dune project (formalization + front-end) | ||
121 | |||
122 | > N.B.: Dune will produce errors if `.vo` files generated by the `Makefile` are present. | ||
123 | > Remove these by running `make clean`. | ||
124 | > The Dune project will then build. | ||
125 | |||
126 | This step assumes that you have dependencies installed and available, either using `opam` or a Nix devshell. | ||
127 | See the relevant sections above. | ||
128 | |||
129 | To check all proofs and build the CLI, use `dune build -p mininix`. | ||
130 | To then run the CLI, use `./_build/default/bin/main.exe` (see how to use it [below](#usage-of-the-cli)). | ||
131 | To execute the test suite, use `dune test` (this may take a few minutes). | ||
132 | For some more details on the test suite, look [here](#use-of-the-nix-language-tests). | ||
133 | |||
134 | To generate the coverage report, run `./coverage.sh` (this will take a few minutes). | ||
135 | The report will then be printed directly (look for `lib/extraction/interp.ml`). | ||
136 | A detailed HTML report should also be available under `_coverage/html/lib/extraction/interp.ml.html`. | ||
137 | |||
138 | To generate the line counts for the Rocq development (for comparison with Table 1 in the article), make sure that `cloc` and `jq` are installed (if not using the provided Nix shell). | ||
139 | Then run `./cloc-rocq.sh` to print the report. | ||
140 | |||
141 | ## Usage of the CLI | ||
142 | |||
143 | Depending on how you built the CLI, it will be available to you as `./_build/default/bin/main.exe` (Dune) or `./result/bin/mininix` (Nix). | ||
144 | In these examples, replace `<cli>` with the actual path of the CLI binary. | ||
145 | |||
146 | - As a Nix REPL: `<cli> repl`. | ||
147 | Various meta-commands are available: `:quit`, `:run`, `:set` and `:settings`. | ||
148 | Autocomplete for these meta-commands is available; it is possible to cycle through suggestions using <kbd>TAB</kbd>. | ||
149 | Input can be canceled using <kbd>Ctrl+C</kbd> and the REPL can be quit using `:quit` or <kbd>Ctrl+D</kbd>. | ||
150 | The REPL evaluates in deep mode by default. | ||
151 | This can be changed by using `:set eval_strategy shallow`. | ||
152 | The REPL does not handle newlines in user input (a newline is processed as a request to process input). | ||
153 | |||
154 | The expected format of user input is the Nix language (although, as mentioned in the paper, e.g. derivations are not supported). | ||
155 | The paper (§ 1, § 4.1, § 5) contains some examples that might be interesting to try out. | ||
156 | - For evaluating single files: `<cli> eval FILENAME`. | ||
157 | This command evaluates in shallow mode by default. | ||
158 | You can change to deep mode by using the flag `-strict`. | ||
159 | An import tree definition file can be passed using the `-importsdef` flag. | ||
160 | See [below](#import-tree-definitions). | ||
161 | |||
162 | ## The Rocq mechanization | ||
163 | |||
164 | See how to check/build the mechanization [here](#build). | ||
165 | This section describes the different parts of the mechanization, and how it relates to the different parts of the article. | ||
166 | |||
167 | ### Use of axioms | ||
168 | |||
169 | See how to list the axioms used [above](#build) (look for the dependency management / build method that you are using). | ||
170 | (In case that you are using the Makefile (so dependencies are managed using `opam` / the Nix devshell): run `make validate`. If you are using Nix but not the devshell: use `nix-build ./axioms.nix` (the axioms used will also be written to `result/coqchk-output`).) | ||
171 | |||
172 | Use of four axioms will be reported, namely: | ||
173 | ``` | ||
174 | Coq.Logic.FunctionalExtensionality.functional_extensionality_dep | ||
175 | Coq.Reals.ClassicalDedekindReals.sig_not_dec | ||
176 | Coq.Reals.ClassicalDedekindReals.sig_forall_dec | ||
177 | Coq.Logic.Classical_Prop.classic | ||
178 | ``` | ||
179 | *These are not axioms that we directly make use of.* | ||
180 | Instead, they are marked as used because we import [Flocq](https://flocq.gitlabpages.inria.fr/) (so NixLang can support IEEE 754 floating point numbers, see `theories/nix/floats.v`), which imports the classical `Reals` module from the standard library. | ||
181 | Running `coqchk` on the `Reals` module from the standard library gives the same list of axioms as shown above. | ||
182 | |||
183 | ### Structure of the mechanization | ||
184 | |||
185 | There are two files that are shared by all four languages: | ||
186 | - `theories/res.v`: contains the `res` monad. See also Fig 2. on p. 6. | ||
187 | - `theories/utils.v`: contains some generic lemmas that are useful for all languages. | ||
188 | A decent amount of these are about finite maps, which we use heavily. | ||
189 | |||
190 | There is a general structure for all four languages that we formalize: | ||
191 | - `operational.v`: the definition of the operational semantics. | ||
192 | + Contains an inductive type `expr` for expressions. | ||
193 | + Contains a `step` relation that describes the small-step op. sem. | ||
194 | + Contains a `subst` function that gives parallel substitution (as used by `step`). | ||
195 | - `operational_props.v`: properties of the operational semantics. | ||
196 | + Contains a lemma `step_det`, proving that the small-step op. sem. is deterministic. | ||
197 | - `interp.v`: the definition of the interpreter. | ||
198 | + Contains the definition of values `val`, thunks `thunk` and environments `env`. | ||
199 | + Contains an `interp` function that takes some expression, environment and amount of fuel, and returns a result (timeout, fail or some value). | ||
200 | (In NixLang, we end up not using `interp` directly, but instead use `interp'` which wraps `interp` and allows specifying whether to evaluate in shallow/deep mode, cf. `⟦e⟧^{δ,E}_μ` (§ 4.4, p. 19).) | ||
201 | - `interp_proofs.v`: soundness and completeness of interpreter w.r.t. operational semantics, in three main theorems: | ||
202 | + `interp_sound_complete_ret_string` (or `interp_sound_complete_ret_lit` for NixLang) proving the soundness and completeness for programs that reduce to strings (or literals in general in NixLang). | ||
203 | + `interp_sound_complete_fail` proving the soundness and completeness for programs that fault. | ||
204 | + `interp_sound_complete_no_fuel` proving the soundness and completeness for programs that loop. | ||
205 | For LambdaLang, an extra condition here is that the programs that we are considering must be closed. | ||
206 | |||
207 | There is also always a generalized version of `interp_sound_complete_ret_string/lit`, namely `interp_sound_complete_ret`, which states that the interpreter is sound and complete w.r.t. the operational semantics for (in case of LambdaLang closed) programs that reduce to a value. | ||
208 | |||
209 | Specific to the different languages are the following files: | ||
210 | - In `theories/dynlang` (for DynLang, § 3): | ||
211 | + `equiv.v`, the equivalence of LambdaLang and DynLang for closed LambdaLang terms. | ||
212 | - In `theories/evallang` (for EvalLang, § 3.4): | ||
213 | + `tests.v`, some tests of the EvalLang expression parser and interpreter. | ||
214 | - In `theories/nix` (for NixLang, § 4): | ||
215 | + `floats.v`: our Flocq instantiation with some helper functions. | ||
216 | + `notations.v`: some notations to make writing NixLang programs in Rocq easier. | ||
217 | + `tests.v`: some example NixLang programs to test the functionality of the interpreter with. | ||
218 | + `wp.v`: the definition of our proof-of-concept weakest precondition-based program logic, derived rules (see § 5). | ||
219 | + `wp_examples.v`: examples of use of our WP-based program logic (see § 5). | ||
220 | |||
221 | ### Index of definitions and theories | ||
222 | |||
223 | **Relevant definitions.** | ||
224 | |||
225 | | In paper | File and line number | Name | | ||
226 | |----------------------------------------------------|---------------------------------------|--------------------------------| | ||
227 | | Shared result monad (p. 6) | `theories/res.v:4` | `res` | | ||
228 | | LambdaLang syntax (p. 6) | `theories/lambda/operational.v:6` | `expr` | | ||
229 | | LambdaLang operational semantics (p. 6) | `theories/lambda/operational.v:21` | `step` | | ||
230 | | LambdaLang final expressions (p. 6) | `theories/lambda/operational.v:28` | `final` | | ||
231 | | LambdaLang parallel substitution (p. 6) | `theories/lambda/operational.v:12` | `subst` | | ||
232 | | LambdaLang interpreter (p. 6) | `theories/lambda/interp.v:34` | `interp` | | ||
233 | | LambdaLang interpreter data structures (p. 6) | `theories/lambda/interp.v:7-14` | `thunk`, `env`, `val` | | ||
234 | | DynLang syntax (p. 9) | `theories/dynlang/operational.v:6` | `expr` | | ||
235 | | DynLang operational semantics (p. 9) | `theories/dynlang/operational.v:21` | `step` | | ||
236 | | DynLang final expressions (p. 9) | `theories/dynlang/operational.v:31` | `final` | | ||
237 | | DynLang parallel substitution (p. 9) | `theories/dynlang/operational.v:12` | `subst` | | ||
238 | | DynLang interpreter (p. 9) | `theories/dynlang/interp.v:39` | `interp` | | ||
239 | | DynLang interpreter data structures (p. 9) | `theories/dynlang/interp.v:7-13` | `thunk`, `env`, `val` | | ||
240 | | EvalLang syntax (p. 11) | `theories/evallang/operational.v:7` | `expr` | | ||
241 | | EvalLang expression parser (p. 11) | `theories/evallang/operational.v:103` | `parse` | | ||
242 | | EvalLang operational semantics (p. 11) | `theories/evallang/operational.v:119` | `step` | | ||
243 | | EvalLang final expressions (p. 11) | `theories/evallang/operational.v:130` | `final` | | ||
244 | | EvalLang interpreter (p. 11) | `theories/evallang/interp.v:42` | `interp` | | ||
245 | | EvalLang interpreter data structures (p. 11) | `theories/evallang/interp.v:7-13` | `thunk`, `env`, `val` | | ||
246 | | NixLang syntax (p. 14) | `theories/nix/operational.v:67` | `expr` | | ||
247 | | NixLang operational semantics (p. 14) | `theories/nix/operational.v:444` | `step` | | ||
248 | | NixLang evaluation contexts (p. 14) | `theories/nix/operational.v:418` | `ctx1` | | ||
249 | | NixLang final expressions (p. 14) | `theories/nix/operational.v:139` | `final` | | ||
250 | | NixLang binary operator semantics (p. 15) | `theories/nix/operational.v:297` | `sem_bin_op` | | ||
251 | | NixLang argument matching (p. 15) | `theories/nix/operational.v:398` | `matches` | | ||
252 | | NixLang substitution (p. 15) | `theories/nix/operational.v:112` | `subst` | | ||
253 | | NixLang interpreter (p. 18) | `theories/nix/interp.v:329` | `interp` | | ||
254 | | NixLang interpreter data structures (p. 18) | `theories/nix/interp.v:5-21` | `val`, `thunk`, `tattr`, `env` | | ||
255 | | NixLang interpreter variant with mode arg. (p. 19) | `theories/nix/interp.v:348` | `interp'` | | ||
256 | |||
257 | **Corresponding theorems.** | ||
258 | |||
259 | | Theorem in paper | File and line number | Name | | ||
260 | |---------------------------------|---------------------------------------|------------------------------------| | ||
261 | | Theorem 2.1, Item 1 (p. 7) | `theories/lambda/interp_proofs.v:575` | `interp_sound_complete_ret_string` | | ||
262 | | Theorem 2.1, Item 2 (p. 7) | `theories/lambda/interp_proofs.v:585` | `interp_sound_complete_fail` | | ||
263 | | Theorem 2.1, Item 3 (p. 7) | `theories/lambda/interp_proofs.v:594` | `interp_sound_complete_no_fuel` | | ||
264 | | Lemma 2.2 (p. 8) | `theories/lambda/interp_proofs.v:516` | `interp_sound_open` | | ||
265 | | Lemma 2.3 (p. 8) | `theories/lambda/interp_proofs.v:402` | `interp_step` | | ||
266 | | Lemma 2.4 (p. 8) | `theories/lambda/interp_proofs.v:297` | `interp_proper` | | ||
267 | | Theorem 3.1, Item 1 (p. 11) | `theories/dynlang/equiv.v:120` | `interp_equiv_ret_string` | | ||
268 | | Theorem 3.1, Item 2 (p. 11) | `theories/dynlang/equiv.v:131` | `interp_equiv_fail` | | ||
269 | | Theorem 3.1, Item 3 (p. 11) | `theories/dynlang/equiv.v:142` | `interp_equiv_no_fuel` | | ||
270 | | Theorem 4.1, Item 1 (p. 20) (*) | `theories/nix/interp_proofs.v:2655` | `interp_sound_complete_ret_lit` | | ||
271 | | Theorem 4.1, Item 2 (p. 20) | `theories/nix/interp_proofs.v:2665` | `interp_sound_complete_fail` | | ||
272 | | Theorem 4.1, Item 3 (p. 20) | `theories/nix/interp_proofs.v:2673` | `interp_sound_complete_no_fuel` | | ||
273 | | Lemma 4.2 (p. 20) | `theories/nix/interp_proofs.v:2619` | `interp_sound_open'` | | ||
274 | | Lemma 4.3 (p. 20) | `theories/nix/interp_proofs.v:2029` | `interp_step'` | | ||
275 | |||
276 | (*): The theorem in the formalization is stronger than the theorem presented in the paper. The latter is a trivial specialization of the former. | ||
277 | |||
278 | **Other claims in the paper.** | ||
279 | |||
280 | - § 2, LambdaLang: | ||
281 | + p. 7: Theorem 2.1, Item 1 generalizes to any final value. | ||
282 | See `theories/lambda/interp_proofs.v:563`, `interp_sound_complete_ret`. | ||
283 | This also holds for DynLang, EvalLang and NixLang. | ||
284 | These languages have a theory `interp_sound_complete_ret` in their respective `interp_proofs.v` files as well. | ||
285 | + p. 7: LambdaLang has a deterministic operational semantics. See `theories/lambda/operational_props.v:19`, `step_det`. | ||
286 | This also holds for DynLang, EvalLang and NixLang. | ||
287 | These languages have a theory `step_det` in their respective `operational_props.v` files as well. | ||
288 | - § 3, DynLang: | ||
289 | + p. 10: A variant of the main theorem of DynLang (Theorem 2.1) also holds for DynLang. | ||
290 | The primary difference here is that the closedness conditions are not needed for DynLang. | ||
291 | For a variant of Theorem 2.1, Item 1, see `theories/dynlang/interp_proofs.v:391`, `interp_sound_complete_ret_string`. | ||
292 | For a variant of Theorem 2.1, Item 2, see `theories/dynlang/interp_proofs.v:400`, `interp_sound_complete_fail`. | ||
293 | For a variant of Theorem 2.1, Item 3, see `theories/dynlang/interp_proofs.v:408`, `interp_sound_complete_no_fuel`. | ||
294 | A variant of the generalized version of Theorem 2.1, Item 1 also holds for DynLang. | ||
295 | See `theories/dynlang/interp_proofs.v:381`, `interp_sound_complete_ret`. | ||
296 | - § 4, NixLang: | ||
297 | + p. 19/20: Theorem 4.1, Item 1 generalizes to any final value. | ||
298 | + p. 20: Lemma 4.2 follows from mutual induction on four properties. | ||
299 | For these four properties and their corresponding proof, see `theories/nix/interp_proofs.v:2267-2282` (`interp_sound_open`, `interp_thunk_sound`, `interp_app_sound`, `force_deep_sound`). | ||
300 | + p. 20: Lemma 4.3 follows from mutual induction on two properties. | ||
301 | For these two properties and their corresponding proof, see `theories/nix/interp_proofs.v:1519`, `interp_step`. | ||
302 | - § 5, Evaluation (Program logic, p. 22): | ||
303 | + We can derive structural rules for WP (the application rule is presented). | ||
304 | See `theories/nix/wp.v:55`, `App_wp` for the presented application rule. | ||
305 | In general, `theories/nix/wp.v` contains all derived rules for WP. | ||
306 | + We prove the total correctness of three variants of the recursive program from § 4.1: | ||
307 | * See `theories/nix/wp_examples.v:84`, `even_rec_attr_wp'` for | ||
308 | ```nix | ||
309 | rec { f = x: if x = 0 then true else !(f (x - 1)); }.f n | ||
310 | ``` | ||
311 | * See `theories/nix/wp_examples.v:132`, `even_rec_functor_wp'` for | ||
312 | ```nix | ||
313 | { "__functor " = r: x: if x == 0 then true else !(r (x - 1)); } n | ||
314 | ``` | ||
315 | * See `theories/nix/wp_examples.v:157`, `even_rec_default_wp'` for | ||
316 | ```nix | ||
317 | ({ f ? (x: if x == 0 then true else !(f (x - 1))) }: f) {} n | ||
318 | ``` | ||
319 | + We prove the total correctness of the following program for any non-recursive attribute set `e`: | ||
320 | ```nix | ||
321 | let x = 1; in with e; with { y = 2; }; x == y | ||
322 | ``` | ||
323 | See `theories/nix/wp_examples.v:11`, `test_wp`. | ||
324 | |||
325 | ## Import tree definitions | ||
326 | |||
327 | We do not claim to have (proper) support the `import` feature of Nix, but we do support it in a bare-bones fashion in order to be able to run the test suite. | ||
328 | In the test suite, there is a file `lib.nix`, which a few test cases load by using `with import ./lib.nix; ...` (or something similar). | ||
329 | To support this, we use a so-called import tree definition file, which pre-declares all files that may be imported. | ||
330 | In the test suite, for example, we have created a `test/testdata/importdef.sexp` file, which looks like this: | ||
331 | ``` | ||
332 | (deps ./lib.nix) | ||
333 | ``` | ||
334 | This means that the file `lib.nix` should be available to be imported for all files in the `test/testdata` folder. | ||
335 | Such `importdef.sexp` files are discovered automatically by walking up the directory tree from the path of the file to be evaluated (or in case of the REPL, from the current working directory). | ||
336 | The general syntax (`<forest>`) is as follows: `(deps <tree>)`, where `<tree>` is either `<filename>` or `(<filename> <forest>)` | ||
337 | |||
338 | In general, the primitive imports mechanism works as follows: | ||
339 | |||
340 | - All path expressions (_e.g.,_ `./lib.nix`) in the Nix source file are converted to strings representing their absolute path. | ||
341 | - We load all dependencies and evaluate them to NixLang values. | ||
342 | - We put all of these values in a NixLang attribute set (`VAttr`, see `theories/nix/interp.v:14`), where we associate the NixLang value of each imported file with the original filename (as an absolute path). | ||
343 | - We then create a NixLang function `import <filename>` (using `VClo`, see `theories/nix/interp.v:9`) that takes a string and returns the value for that file (if indeed forward-declared in the import tree definition). | ||
344 | - This function is then inserted into the global environment of the file that we want to evaluate. | ||
345 | |||
346 | This process is performed recursively, hence recursive imports are also supported. | ||
347 | |||
348 | ## Use of the Nix language tests | ||
349 | |||
350 | When exercising the official Nix language tests on our extracted interpreter, we do not exactly consider all tests in the Nix test suite. | ||
351 | Since we are only interested in tests for the interpreter, and not so much in tests for the parser, we only consider the `eval-*` test files in the `test/testdata` folder. | ||
352 | There are two types of tests that we have here: tests where evaluation should succeed, and tests where evaluation should fail. | ||
353 | This is simply marked in the filename; Nix files that should fail to evaluate are named `eval-fail-*.nix` and files that should evaluate successfully are named `eval-okay-*.nix`. | ||
354 | For the former, there are expected error outputs that the interpreter should produce, but we are not interested in reproducing these (nor would this be realistic with our setup). | ||
355 | |||
356 | For the latter, there are also expected output expressions, which can be found in matching `eval-okay-*.exp`. | ||
357 | However, there are two `eval-okay-*.nix` files for which no such matching `eval-okay-*.exp` file exists: | ||
358 | - `eval-okay-tail-call-1.nix`: a file `eval-okay-tail-call-1.exp-disabled` exists; we hence do not consider this test. | ||
359 | - `eval-okay-xml.nix`: a file `eval-okay-xml.exp.xml` exists, but we are not interested in converting the resulting term to XML to validate the result of this test. | ||
360 | |||
361 | The total amount of `eval-fail-*.nix` files and pairs of `eval-okay-*.nix` and `eval-okay-*.exp` files informs our total count of interpreter tests that we consider to begin with, namely 182 (§ 5, p. 21). | ||
362 | For these tests again, we explicitly list 74 tests that should be ignored in `./test/test_mininix.ml`, since they cover functionality of Nix that is out of scope for our paper. | ||
363 | We end up with 108 tests that we exercise our interpreter on. | ||
364 | Our interpreter passes 103 of these tests, and times out for five tests due to its call-by-name nature. | ||
365 | Some more details can be found in the paper, in § 5. | ||
366 | |||
367 | ## General project structure | ||
368 | |||
369 | - The `theories` folder contains the Rocq formalization. See the [structure of the mechanization](#structure-of-the-mechanization). | ||
370 | - The `bin` folder contains the entrypoint for the command-line interface. | ||
371 | - The `lib` folder contains the Nix parser, elaborator and interpreter extraction | ||
372 | + The `nix` subfolder contains an adapted version of the parser and pretty-printer from nixformat (licensed under ISC) by Denis Korzunov. | ||
373 | See https://github.com/d2km/nixformat. | ||
374 | Some improvements were made to the parser and pretty-printer. | ||
375 | The file `elaborator.ml`, not part of nixformat, in this folder is concerned with desugaring attribute paths among other things; you may consider this a 'normalizing' stage, which allows the elaborator to our core language to be more concise. | ||
376 | + The `mininix` subfolder is the most important here. It contains, among | ||
377 | other files: | ||
378 | - `nix2mininix.ml`, elaboration of Nix into our core language | ||
379 | - `builtins.nix`, our implementation of builtins in Nix, in our core Nix language, where we liberally use 'core' builtins (available as binary operations) that are specific to the core language | ||
380 | - `conv.ml`, conversion between numeric types in OCaml and Rocq/Flocq | ||
381 | - `import.ml`, very bare-bones support for imports, used in the test suite | ||
382 | + The `extraction` folder contains a file that extracts the core language interpreter and some auxiliary functions to OCaml | ||
383 | - The `explorer` folder contains some utilities for converting the Rocq Mechanization to a static site. | ||