Debian Codegraph @

dev/doc/econstr.md

raw symbols SCIP
1 # Evar-insensitive terms (EConstr)
2
3 Evar-insensitive terms were introduced in 8.7, following
4 [CEP #10](https://github.com/coq/ceps/blob/master/text/010-econstr.md). We will
5 not recap the motivations in this document and rather summarize the code changes
6 to perform.
7
8 ## Overview
9
10 The essential datastructures are defined in
11 [the `EConstr` module](/engine/eConstr.mli) module. It defines
12 the tactic counterparts of kernel data structures such as terms
13 (`EConstr.constr`), universes (`EConstr.ESorts.t`) and contexts
14 (`EConstr.*_context`).
15
16 The main difference with kernel-side types is that observing them requires
17 an evar-map at hand in order to normalize evars on the fly. The basic primitive
18 to observe an `EConstr.t` is the following function:
19 ```
20 val kind : Evd.evar_map -> t -> (t, t, ESorts.t, EInstance.t) Constr.kind_of_term
21 (** Same as {!Constr.kind} except that it expands evars and normalizes
22 universes on the fly. *)
23 ```
24
25 Essentially, each time it sees an evar which happens to be defined in the
26 provided evar-map, it replaces it with the corresponding body and carries on.
27
28 Due to universe unification occurring at the tactic level, the same goes for
29 universe instances and sorts. See the `ESort` and `EInstance` modules in
30 `EConstr`.
31
32 This normalization is critical for the soundness of tactics. Before EConstr, a
33 lot of bugs were lurking in the code base, a few still are (most notably in
34 meta-based unification) and failure to respect the guidelines thereafter may
35 result in nasal demons.
36
37 ## Transition path
38
39 ### Types
40
41 As a rule of thumb, all functions living at the tactic level should manipulate
42 `EConstr.t` instead of `Constr.t`, and similarly for the other data structures.
43
44 To ease the transition, the `EConstr` module defines a handful of aliases to
45 shadow the type names from the kernel.
46
47 It is recommended to perform the following replacement in headers.
48 ```ocaml
49 (** Kernel types. You may remove the two following opens if you want. Beware
50 that [kind_of_term] needs to be in scope if you use [EConstr.kind] so that
51 you may still need to open one of the two. *)
52 open Term
53 open Constr
54 (** Tactic types. Open this after to shadow kernel types. *)
55 open EConstr
56 ```
57
58 Note that the `EConstr` module also redefines a `Vars` submodule.
59
60 ### Evar-map-passing
61
62 All functions deconstructing an econstr need to take an evar-map as a parameter.
63 Therefore, you need to pass one as an argument virtually everywhere.
64
65 In the Rocq source code, it is recommended to take the evar-map as a first
66 argument called `sigma`, except if the function also takes an environment in
67 which case it is passed second. Namely, the two typical instances are:
68 ```ocaml
69 let foo sigma c = mycode
70 val foo : Evd.evar_map -> EConstr.t -> Foo.t
71
72 let bar env sigma c = mycode
73 val bar : Environ.env -> Evd.evar_map -> EConstr.t -> Bar.t
74 ```
75
76 The EConstr API makes the code much more sensitive to evar-maps, because a
77 lot of now useless normalizations were removed. Thus one should be cautious of
78 **not** dropping the evar-map when it has been updated, and should rather stick
79 to a strict state-passing discipline. Unsound primitives like
80 `Typing.unsafe_type_of` are also a known source of problems, so you should
81 replace them with the corresponding evar-map-returning function and thread it
82 properly.
83
84 ### Functions
85
86 Many functions from `Constr` and `Term` are redefined to work on econstr in
87 the `EConstr` module, so that it is often enough to perform the `open` as
88 described above to replace them. Their type may differ though, because they now
89 need access to an evar-map. A lot of econstr-manipulating functions are also
90 defined in [`Termops`](/engine/termops.mli).
91
92 Functions manipulating tactic terms and kernel terms share the same name if they
93 are the equivalent one of the other. Do not hesitate to grep Rocq mli files to
94 find the equivalent of a function you want to port if it is neither in `EConstr`
95 nor in `Termops` (this should be very rare).
96
97 ### Conversion
98
99 Sometimes you do not have any other choice than calling kernel-side functions
100 on terms, and conversely to turn a kernel term into a tactic term.
101
102 There are two functions to do so.
103 * `EConstr.of_constr` turns kernel terms into tactic terms. It is currently
104 the physical identity, and thus O(1), but this may change in the future.
105 * `EConstr.to_constr` turns tactic terms into kernel terms. It performs a
106 full-blown normalization of the given term, which is O(n) and potentially
107 costly.
108
109 For performance reasons, avoiding to jump back and forth between kernel and
110 tactic terms is recommended.
111
112 There are also a few unsafe conversion functions that take advantage of the
113 fact that `EConstr.t` is internally the same as `Constr.t`. Namely,
114 `EConstr.Unsafe.to_constr` is the physical identity. It should **not** be used
115 in typical code and is instead provided for efficiency **when you know what you
116 are doing**. Either use it to reimplement low-level functions that happen to
117 be insensitive externally, or use it to provide backward compatibility with
118 broken code that relies on evar-sensitivity. **Do not use it because it is
119 easier than stuffing evar-maps everywhere.** You've been warned.
120
121 ## Notes
122
123 The EConstr branch fixed a lot of eisenbugs linked to lack of normalization
124 everywhere, most notably in unification. It may also have introduced a few, so
125 if you see a change in behaviour *that looks like a bug*, please report it.
126 Obviously, unification is not specified, so it's hard to tell apart, but still.
127
128 Efficiency has been affected as well. We now pay an overhead when observing a
129 term, but at the same time a lot of costly upfront normalizations were removed.