Debian Codegraph @

dev/doc/universes.md

raw symbols SCIP
1 Notes on universe polymorphism
2 ------------------------------
3
4 The implementation of universe polymorphism introduces a few changes
5 to the API of Rocq. First and foremost, the term language changes, as
6 global references now carry a universe level substitution:
7
8 ~~~ocaml
9 type 'a puniverses = 'a * UVars.Instance.t
10 type pconstant = constant puniverses
11 type pinductive = inductive puniverses
12 type pconstructor = constructor puniverses
13
14 type constr = ...
15 | Const of puniverses
16 | Ind of pinductive
17 | Constr of pconstructor
18 ~~~
19
20 Universes
21 ---------
22
23 Universe instances (an array of levels) gets substituted when
24 unfolding definitions, are used to typecheck and are unified according
25 to the rules in the ITP'14 paper on universe polymorphism in Coq.
26
27 ~~~ocaml
28 type Level.t = Set | Prop | Level of int * dirpath (* hashconsed *)
29 type Instance.t = Level.t array
30 type Universe.t = Level.t list (* hashconsed *)
31 ~~~
32
33 The universe module defines modules and abstract types for levels,
34 universes etc.. Structures are hashconsed (with a hack to take care
35 of the fact that deserialization breaks sharing).
36
37 Definitions (constants, inductives) now carry around not only
38 constraints but also the universes they introduced (a UVars.UContext.t).
39 There is another kind of contexts `Univ.ContextSet.t`, the latter has
40 a set of universes, while the former has serialized the levels in an
41 array, and is used for polymorphic objects. Both have "reified"
42 constraints depending on global and local universes.
43
44 A polymorphic definition is abstract w.r.t. the variables in this
45 context, while a monomorphic one (or template polymorphic) just adds the
46 universes and constraints to the global universe context when it is put
47 in the environment. No other universes than the global ones and the
48 declared local ones are needed to check a declaration, hence the kernel
49 does not produce any constraints anymore, apart from module
50 subtyping.... There are hence two conversion functions now: `check_conv`
51 and `infer_conv`: the former just checks the definition in the current env
52 (in which we usually push_universe_context of the associated context),
53 and `infer_conv` which produces constraints that were not implied by the
54 ambient constraints. Ideally, that one could be put out of the kernel,
55 but currently module subtyping needs it.
56
57 Inference of universes is now done during refinement, and the evar_map
58 carries the incrementally built universe context, starting from the
59 global universe constraints (see `Evd.from_env`). `Evd.conversion` is a
60 wrapper around `infer_conv` that will do the bookkeeping for you, it
61 uses `evar_conv_x`. There is a universe substitution being built
62 incrementally according to the constraints, so one should normalize at
63 the end of a proof (or during a proof) with that substitution just like
64 we normalize evars. There are some nf_* functions in
65 library/universes.ml to do that. Additionally, there is a minimization
66 algorithm in there that can be applied at the end of a proof to simplify
67 the universe constraints used in the term. It is heuristic but
68 validity-preserving. No user-introduced universe (i.e. coming from a
69 user-written anonymous Type) gets touched by this, only the fresh
70 universes generated for each global application. Using
71 ~~~ocaml
72 val pf_constr_of_global : Globnames.global_reference -> (constr -> tactic) -> tactic
73 ~~~
74 Is the way to make a constr out of a global reference in the new API.
75 If they constr is polymorphic, it will add the necessary constraints to
76 the evar_map. Even if a constr is not polymorphic, we have to take care
77 of keeping track of its universes. Typically, using:
78 ~~~ocaml
79 mkApp (coq_id_function, [| A; a |])
80 ~~~
81 and putting it in a proof term is not enough now. One has to somehow
82 show that A's type is in cumululativity relation with id's type
83 argument, incurring a universe constraint. To do this, one can simply
84 call Typing.resolve_evars env evdref c which will do some infer_conv to
85 produce the right constraints and put them in the evar_map. Of course in
86 some cases you might know from an invariant that no new constraint would
87 be produced and get rid of it. Anyway the kernel will tell you if you
88 forgot some. As a temporary way out, `Universes.constr_of_global` allows
89 you to make a constr from any non-polymorphic constant, but it will fail
90 on polymorphic ones.
91
92 Other than that, unification (w_unify and evarconv) now take account of universes and
93 produce only well-typed evar_maps.
94
95 Some syntactic comparisons like the one used in `change` have to be
96 adapted to allow identification up-to-universes (when dealing with
97 polymorphic references), `make_eq_univs_test` is there to help.
98 In constr, there are actually many new comparison functions to deal with
99 that:
100 ~~~ocaml
101 (** [equal a b] is true if [a] equals [b] modulo alpha, casts,
102 and application grouping *)
103 val equal : constr -> constr -> bool
104
105 (** [eq_constr_univs u a b] is [true] if [a] equals [b] modulo alpha, casts,
106 application grouping and the universe equalities in [u]. *)
107 val eq_constr_univs : constr Univ.check_function
108
109 (** [leq_constr_univs u a b] is [true] if [a] is convertible to [b] modulo
110 alpha, casts, application grouping and the universe inequalities in [u]. *)
111 val leq_constr_univs : constr Univ.check_function
112
113 (** [eq_constr_universes a b] [true, c] if [a] equals [b] modulo alpha, casts,
114 application grouping and the universe equalities in [c]. *)
115 val eq_constr_universes : constr -> constr -> bool Univ.universe_constrained
116
117 (** [leq_constr_universes a b] [true, c] if [a] is convertible to [b] modulo
118 alpha, casts, application grouping and the universe inequalities in [c]. *)
119 val leq_constr_universes : constr -> constr -> bool Univ.universe_constrained
120
121 (** [eq_constr_univs a b] [true, c] if [a] equals [b] modulo alpha, casts,
122 application grouping and ignoring universe instances. *)
123 val eq_constr_nounivs : constr -> constr -> bool
124 ~~~
125 The `_univs` versions are doing checking of universe constraints
126 according to a graph, while the `_universes` are producing (non-atomic)
127 universe constraints. The non-atomic universe constraints include the
128 `ULub` constructor: when comparing `f (* u1 u2 *) c` and `f (* u1' u2'
129 *) c` we add ULub constraints on `u1, u1'` and `u2, u2'`. These are
130 treated specially: as unfolding `f` might not result in these
131 unifications, we need to keep track of the fact that failure to satisfy
132 them does not mean that the term are actually equal. This is used in
133 unification but probably not necessary to the average programmer.
134
135 Another issue for ML programmers is that tables of constrs now usually
136 need to take a `constr Univ.in_universe_context_set` instead, and
137 properly refresh the universes context when using the constr, e.g. using
138 Clenv.refresh_undefined_univs clenv or:
139 ~~~ocaml
140 (** Get fresh variables for the universe context.
141 Useful to make tactics that manipulate constrs in universe contexts polymorphic. *)
142 val fresh_universe_context_set_instance : universe_context_set ->
143 universe_level_subst * universe_context_set
144 ~~~
145 The substitution should be applied to the constr(s) under consideration,
146 and the context_set merged with the current evar_map with:
147 ~~~ocaml
148 val merge_context_set : rigid -> evar_map -> Univ.universe_context_set -> evar_map
149 ~~~
150 The `rigid` flag here should be `Evd.univ_flexible` most of the
151 time. This means the universe levels of polymorphic objects in the
152 constr might get instantiated instead of generating equality constraints
153 (Evd.univ_rigid does that).
154
155 On this issue, I recommend forcing commands to take `global_reference`s
156 only. Users can declare their specialized terms used as hints as
157 constants and this is cleaner. Alas, backward-compatibility-wise,
158 this is the only solution I found. In the case of global_references
159 only, it's just a matter of using `Evd.fresh_global` /
160 `pf_constr_of_global` to let the system take care of universes.
161
162
163 The universe graph
164 ------------------
165
166 To accommodate universe polymorphic definitions, the graph structure in
167 kernel/univ.ml was modified. The new API forces every universe to be
168 declared before it is mentioned in any constraint. This forces to
169 declare every universe to be >= Set or > Set. Every universe variable
170 introduced during elaboration is >= Set. Every _global_ universe is now
171 declared explicitly > Set, _after_ typechecking the definition. In
172 polymorphic definitions Type@{i} ranges over Set and any other universe
173 j. However, at instantiation time for polymorphic references, one can
174 try to instantiate a universe parameter with Prop as well, if the
175 instantiated constraints allow it. The graph invariants ensure that
176 no universe i can be set lower than Set, so the chain of universes
177 always bottoms down at Prop < Set.
178
179 Modules
180 -------
181
182 One has to think of universes in modules as being globally declared, so
183 when including a module (type) which declares a type i (e.g. through a
184 parameter), we get back a copy of i and not some fresh universe.
185
186 Incompatibilities
187 -----------------
188
189 Old-style universe polymorphic definitions were implemented by taking
190 advantage of the fact that elaboration (i.e., pretyping and unification)
191 were _not_ universe aware, so some of the constraints generated during
192 pretypechecking would be forgotten. In the current setting, this is not
193 possible, as unification ensures that the substitution is built is
194 entirely well-typed, even w.r.t universes. This means that some terms
195 that type-checked before no longer do, especially projections of the
196 pair:
197 ~~~coq
198 @fst ?x ?y : prod ?x ?y : Type (max(Datatypes.i, Datatypes.j)).
199 ~~~
200 The "template universe polymorphic" variables i and j appear during
201 typing without being refreshed, meaning that they can be lowered (have
202 upper constraints) with user-introduced universes. In most cases this
203 won't work, so ?x and ?y have to be instantiated earlier, either from
204 the type of the actual projected pair term (some t : prod A B) or the
205 typing constraint. Adding the correct type annotations will always fix
206 this.
207
208
209 Unification semantics
210 ---------------------
211
212 In Ltac, matching with:
213
214 - a universe polymorphic constant `c` matches any instance of the
215 constant.
216 - a variable ?x already bound to a term `t` (non-linear pattern) uses
217 strict equality of universes (e.g., Type@{i} and Type@{j} are not
218 equal).
219
220 In tactics:
221
222 - `change foo with bar`, `pattern foo` will unify all instances of `foo`
223 (and convert them with `bar`). This might incur unifications of
224 universes. `change` uses conversion while `pattern` only does
225 syntactic matching up-to unification of universes.
226 - `apply`, `refine` use unification up to universes.