Debian Codegraph @

kernel/context.mli

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** The modules defined below represent a {e local context}
12 as defined by Chapter 4 in the Reference Manual:
13
14 A {e local context} is an ordered list of of {e local declarations}
15 of names that we call {e variables}.
16
17 A {e local declaration} of some variable can be either:
18 - a {e local assumption}, or
19 - a {e local definition}.
20
21 {e Local assumptions} are denoted in the Reference Manual as [(name : typ)] and
22 {e local definitions} are there denoted as [(name := value : typ)].
23 *)
24
25 open Names
26
27 type ('a,'r) pbinder_annot = { binder_name : 'a; binder_relevance : 'r }
28
29 val eq_annot : ('a -> 'a -> bool) -> ('r -> 'r -> bool) ->
30 ('a,'r) pbinder_annot -> ('a,'r) pbinder_annot -> bool
31
32 val hash_annot : ('a -> int) -> ('a,Sorts.relevance) pbinder_annot -> int
33
34 val map_annot : ('a -> 'b) -> ('a,'r) pbinder_annot -> ('b,'r) pbinder_annot
35
36 val map_annot_relevance : ('r -> 'r) -> ('a,'r) pbinder_annot -> ('a,'r) pbinder_annot
37
38 val map_annot_relevance_het : ('r1 -> 'r2) -> ('a,'r1) pbinder_annot -> ('a,'r2) pbinder_annot
39
40 val make_annot : 'a -> 'r -> ('a,'r) pbinder_annot
41
42 val binder_name : ('a,'r) pbinder_annot -> 'a
43 val binder_relevance : ('a,'r) pbinder_annot -> 'r
44
45 val annotR : 'a -> ('a,Sorts.relevance) pbinder_annot
46 (** Always Relevant *)
47
48 val nameR : Id.t -> (Name.t,Sorts.relevance) pbinder_annot
49 (** Relevant + Name *)
50
51 val anonR : (Name.t,Sorts.relevance) pbinder_annot
52 (** Relevant + Anonymous *)
53
54 (** Representation of contexts that can capture anonymous as well as non-anonymous variables.
55 Individual declarations are then designated by de Bruijn indexes. *)
56 module Rel :
57 sig
58 module Declaration :
59 sig
60 (* local declaration *)
61 type ('constr, 'types, 'r) pt =
62 | LocalAssum of (Name.t,'r) pbinder_annot * 'types (** name, type *)
63 | LocalDef of (Name.t,'r) pbinder_annot * 'constr * 'types (** name, value, type *)
64
65 val get_annot : (_,_,'r) pt -> (Name.t,'r) pbinder_annot
66
67 (** Return the name bound by a given declaration. *)
68 val get_name : ('c, 't,'r) pt -> Name.t
69
70 (** Return [Some value] for local-declarations and [None] for local-assumptions. *)
71 val get_value : ('c, 't, 'r) pt -> 'c option
72
73 (** Return the type of the name bound by a given declaration. *)
74 val get_type : ('c, 't, 'r) pt -> 't
75
76 val get_relevance : ('c, 't, 'r) pt -> 'r
77
78 val set_annot : (Name.t,'r) pbinder_annot -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
79
80 (** Set the name that is bound by a given declaration. *)
81 val set_name : Name.t -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
82
83 (** Set the type of the bound variable in a given declaration. *)
84 val set_type : 't -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
85
86 (** Return [true] iff a given declaration is a local assumption. *)
87 val is_local_assum : ('c, 't, 'r) pt -> bool
88
89 (** Return [true] iff a given declaration is a local definition. *)
90 val is_local_def : ('c, 't, 'r) pt -> bool
91
92 (** Check whether any term in a given declaration satisfies a given predicate. *)
93 val exists : ('c -> bool) -> ('c, 'c, 'r) pt -> bool
94
95 (** Check whether all terms in a given declaration satisfy a given predicate. *)
96 val for_all : ('c -> bool) -> ('c, 'c, 'r) pt -> bool
97
98 (** Check whether the two given declarations are equal. *)
99 val equal : ('r -> 'r -> bool) -> ('c -> 'c -> bool) ->
100 ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt -> bool
101
102 (** Map the name bound by a given declaration. *)
103 val map_name : (Name.t -> Name.t) -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
104
105 (** Map the relevance *)
106 val map_relevance : ('r -> 'r) -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
107
108 (** For local assumptions, this function returns the original local assumptions.
109 For local definitions, this function maps the value in the local definition. *)
110 val map_value : ('c -> 'c) -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
111
112 (** Map the type of the name bound by a given declaration. *)
113 val map_type : ('t -> 't) -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
114
115 (** Map all terms in a given declaration. *)
116 val map_constr : ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
117
118 (** Map all terms in a given declaration. *)
119 val map_constr_with_relevance : ('r -> 'r) -> ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
120
121 (** Map all terms, with an heterogeneous function. *)
122 val map_constr_het : ('r1 -> 'r2) -> ('a -> 'b) -> ('a, 'a, 'r1) pt -> ('b, 'b, 'r2) pt
123
124 (** Perform a given action on all terms in a given declaration. *)
125 val iter_constr : ('c -> unit) -> ('c, 'c, 'r) pt -> unit
126
127 (** Reduce all terms in a given declaration to a single value. *)
128 val fold_constr : ('c -> 'a -> 'a) -> ('c, 'c, 'r) pt -> 'a -> 'a
129
130 val to_tuple : ('c, 't, 'r) pt -> (Name.t,'r) pbinder_annot * 'c option * 't
131
132 (** Turn [LocalDef] into [LocalAssum], identity otherwise. *)
133 val drop_body : ('c, 't, 'r) pt -> ('c, 't, 'r) pt
134 end
135
136 (** Rel-context is represented as a list of declarations.
137 Inner-most declarations are at the beginning of the list.
138 Outer-most declarations are at the end of the list. *)
139 type ('constr, 'types, 'r) pt = ('constr, 'types, 'r) Declaration.pt list
140
141 (** empty rel-context *)
142 val empty : ('c, 't, 'r) pt
143
144 (** Return a new rel-context enriched by with a given inner-most declaration. *)
145 val add : ('c, 't, 'r) Declaration.pt -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
146
147 (** Return the number of {e local declarations} in a given rel-context. *)
148 val length : ('c, 't, 'r) pt -> int
149
150 (** Check whether given two rel-contexts are equal. *)
151 val equal : ('r -> 'r -> bool) -> ('c -> 'c -> bool) ->
152 ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt -> bool
153
154 (** Return the number of {e local assumptions} in a given rel-context. *)
155 val nhyps : ('c, 't, 'r) pt -> int
156
157 (** Return a declaration designated by a given de Bruijn index.
158 @raise Not_found if the designated de Bruijn index outside the range. *)
159 val lookup : int -> ('c, 't, 'r) pt -> ('c, 't, 'r) Declaration.pt
160
161 (** Map all terms in a given rel-context. *)
162 val map : ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
163
164 (** Map all terms in a given rel-context. *)
165 val map_with_relevance : ('r -> 'r) -> ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
166
167 (** Map all terms in a given named-context. *)
168 val map_het : ('r1 -> 'r2) -> ('c -> 'd) -> ('c, 'c, 'r1) pt -> ('d, 'd, 'r2) pt
169
170 (** Map all terms in a given rel-context taking into account the
171 position of the binder in the context starting at 1. *)
172 val map_with_binders : (int -> 'c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
173
174 (** Perform a given action on every declaration in a given rel-context. *)
175 val iter : ('c -> unit) -> ('c, 'c, 'r) pt -> unit
176
177 (** Reduce all terms in a given rel-context to a single value.
178 Innermost declarations are processed first. *)
179 val fold_inside : ('a -> ('c, 't, 'r) Declaration.pt -> 'a) -> init:'a -> ('c, 't, 'r) pt -> 'a
180
181 (** Reduce all terms in a given rel-context to a single value.
182 Outermost declarations are processed first. *)
183 val fold_outside : (('c, 't, 'r) Declaration.pt -> 'a -> 'a) -> ('c, 't, 'r) pt -> init:'a -> 'a
184
185 (** Return the set of all named variables bound in a given rel-context. *)
186 val to_vars : ('c, 't, 'r) pt -> Id.Set.t
187
188 (** Map a given rel-context to a list where each {e local
189 definition} is mapped to [true] and each {e local assumption} is
190 mapped to [false]. The resulting list is in reverse order
191 compared to the order of declarations in the context. *)
192 val to_tags : ('c, 't, 'r) pt -> bool list
193
194 (** Turn all [LocalDef] into [LocalAssum], leave [LocalAssum] unchanged. *)
195 val drop_bodies : ('c, 't, 'r) pt -> ('c, 't, 'r) pt
196
197 (** [chop_nhyps n Γ] returns [Γ'',Γ'] such that [Γ]=[Γ'Γ''], [Γ''] has
198 [n] hypotheses (i.e. [LocalAssum]), excluding local definitions
199 (i.e. [LocalDef]), and [Γ''], if [n] non zero, starts with an
200 hypothesis (i.e., [Γ''] has the form [x:A;Γ'''], i.e., local
201 definitions at the junction of the [n] hypotheses are put in
202 [Γ'] rather than in [Γ''] *)
203 val chop_nhyps : int -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt * ('c, 't, 'r) pt
204
205 (** [instance mk n Γ] builds an instance [args] such that [Γ,Δ ⊢ args:Γ]
206 with n = |Δ| and with the {e local definitions} of [Γ] skipped in
207 [args] where [mk] is used to build the corresponding variables.
208 Example: for [x:T, y:=c, z:U] and [n]=2, it gives [mk 5, mk 3]. *)
209 val instance : (int -> 'v) -> int -> ('c, 't, 'r) pt -> 'v array
210
211 (** [instance_list] is like [instance] but returning a list. *)
212 val instance_list : (int -> 'v) -> int -> ('c, 't, 'r) pt -> 'v list
213
214 val to_extended_vect : (int -> 'r) -> int -> ('c, 't, 'r) pt -> 'r array
215 [@@ocaml.deprecated "(8.15) Use synonymous [Context.Rel.instance]"]
216
217 val to_extended_list : (int -> 'r) -> int -> ('c, 't, 'r) pt -> 'r list
218 [@@ocaml.deprecated "(8.15) Use synonymous [Context.Rel.instance_list]"]
219 end
220
221 (** This module represents contexts that can capture non-anonymous variables.
222 Individual declarations are then designated by the identifiers they bind. *)
223 module Named :
224 sig
225 (** Representation of {e local declarations}. *)
226 module Declaration :
227 sig
228 type ('constr, 'types, 'r) pt =
229 | LocalAssum of (Id.t,'r) pbinder_annot * 'types (** identifier, type *)
230 | LocalDef of (Id.t,'r) pbinder_annot * 'constr * 'types (** identifier, value, type *)
231
232 val get_annot : (_,_,'r) pt -> (Id.t,'r) pbinder_annot
233
234 (** Return the identifier bound by a given declaration. *)
235 val get_id : ('c, 't, 'r) pt -> Id.t
236
237 (** Return [Some value] for local-declarations and [None] for local-assumptions. *)
238 val get_value : ('c, 't, 'r) pt -> 'c option
239
240 (** Return the type of the name bound by a given declaration. *)
241 val get_type : ('c, 't, 'r) pt -> 't
242
243 val get_relevance : ('c, 't, 'r) pt -> 'r
244
245 (** Set the identifier that is bound by a given declaration. *)
246 val set_id : Id.t -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
247
248 (** Set the type of the bound variable in a given declaration. *)
249 val set_type : 't -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
250
251 (** Return [true] iff a given declaration is a local assumption. *)
252 val is_local_assum : ('c, 't, 'r) pt -> bool
253
254 (** Return [true] iff a given declaration is a local definition. *)
255 val is_local_def : ('c, 't, 'r) pt -> bool
256
257 (** Check whether any term in a given declaration satisfies a given predicate. *)
258 val exists : ('c -> bool) -> ('c, 'c, 'r) pt -> bool
259
260 (** Check whether all terms in a given declaration satisfy a given predicate. *)
261 val for_all : ('c -> bool) -> ('c, 'c, 'r) pt -> bool
262
263 (** Check whether the two given declarations are equal. *)
264 val equal : ('r -> 'r -> bool) -> ('c -> 'c -> bool) ->
265 ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt -> bool
266
267 (** Map the identifier bound by a given declaration. *)
268 val map_id : (Id.t -> Id.t) -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
269
270 (** For local assumptions, this function returns the original local assumptions.
271 For local definitions, this function maps the value in the local definition. *)
272 val map_value : ('c -> 'c) -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
273
274 (** Map the type of the name bound by a given declaration. *)
275 val map_type : ('t -> 't) -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
276
277 (** Map all terms in a given declaration. *)
278 val map_constr : ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
279
280 (** Map all terms in a given declaration. *)
281 val map_constr_with_relevance : ('r -> 'r) -> ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
282
283 (** Map all terms, with an heterogeneous function. *)
284 val map_constr_het : ('r1 -> 'r2) -> ('a -> 'b) -> ('a, 'a, 'r1) pt -> ('b, 'b, 'r2) pt
285
286 (** Perform a given action on all terms in a given declaration. *)
287 val iter_constr : ('c -> unit) -> ('c, 'c, 'r) pt -> unit
288
289 (** Reduce all terms in a given declaration to a single value. *)
290 val fold_constr : ('c -> 'a -> 'a) -> ('c, 'c, 'r) pt -> 'a -> 'a
291
292 val to_tuple : ('c, 't, 'r) pt -> (Id.t,'r) pbinder_annot * 'c option * 't
293 val of_tuple : (Id.t,'r) pbinder_annot * 'c option * 't -> ('c, 't, 'r) pt
294
295 (** Turn [LocalDef] into [LocalAssum], identity otherwise. *)
296 val drop_body : ('c, 't, 'r) pt -> ('c, 't, 'r) pt
297
298 (** Convert [Rel.Declaration.t] value to the corresponding [Named.Declaration.t] value.
299 The function provided as the first parameter determines how to translate "names" to "ids". *)
300 val of_rel_decl : (Name.t -> Id.t) -> ('c, 't, 'r) Rel.Declaration.pt -> ('c, 't, 'r) pt
301
302 (** Convert [Named.Declaration.t] value to the corresponding [Rel.Declaration.t] value. *)
303 (* TODO: Move this function to [Rel.Declaration] module and rename it to [of_named]. *)
304 val to_rel_decl : ('c, 't, 'r) pt -> ('c, 't, 'r) Rel.Declaration.pt
305 end
306
307 (** Named-context is represented as a list of declarations.
308 Inner-most declarations are at the beginning of the list.
309 Outer-most declarations are at the end of the list. *)
310 type ('constr, 'types, 'r) pt = ('constr, 'types, 'r) Declaration.pt list
311
312 (** empty named-context *)
313 val empty : ('c, 't, 'r) pt
314
315 (** Return a new named-context enriched by with a given inner-most declaration. *)
316 val add : ('c, 't, 'r) Declaration.pt -> ('c, 't, 'r) pt -> ('c, 't, 'r) pt
317
318 (** Return the number of {e local declarations} in a given named-context. *)
319 val length : ('c, 't, 'r) pt -> int
320
321 (** Return a declaration designated by an identifier of the variable bound in that declaration.
322 @raise Not_found if the designated identifier is not bound in a given named-context. *)
323 val lookup : Id.t -> ('c, 't, 'r) pt -> ('c, 't, 'r) Declaration.pt
324
325 (** Check whether given two named-contexts are equal. *)
326 val equal : ('r -> 'r -> bool) -> ('c -> 'c -> bool) ->
327 ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt -> bool
328
329 (** Map all terms in a given named-context. *)
330 val map : ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
331
332 (** Map all terms in a given rel-context. *)
333 val map_with_relevance : ('r -> 'r) -> ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
334
335 (** Map all terms in a given named-context. *)
336 val map_het : ('r1 -> 'r2) -> ('c -> 'd) -> ('c, 'c, 'r1) pt -> ('d, 'd, 'r2) pt
337
338 (** Perform a given action on every declaration in a given named-context. *)
339 val iter : ('c -> unit) -> ('c, 'c, 'r) pt -> unit
340
341 (** Reduce all terms in a given named-context to a single value.
342 Innermost declarations are processed first. *)
343 val fold_inside : ('a -> ('c, 't, 'r) Declaration.pt -> 'a) -> init:'a -> ('c, 't, 'r) pt -> 'a
344
345 (** Reduce all terms in a given named-context to a single value.
346 Outermost declarations are processed first. *)
347 val fold_outside : (('c, 't, 'r) Declaration.pt -> 'a -> 'a) -> ('c, 't, 'r) pt -> init:'a -> 'a
348
349 (** Return the set of all identifiers bound in a given named-context. *)
350 val to_vars : ('c, 't, 'r) pt -> Id.Set.t
351
352 (** Turn all [LocalDef] into [LocalAssum], leave [LocalAssum] unchanged. *)
353 val drop_bodies : ('c, 't, 'r) pt -> ('c, 't, 'r) pt
354
355 (** [to_instance Ω] builds an instance [args] in reverse order such
356 that [Ω ⊢ args:Ω] where [Ω] is a named-context and with the local
357 definitions of [Ω] skipped. Example: for [id1:T,id2:=c,id3:U], it
358 gives [Var id1, Var id3]. All [idj] are supposed distinct. *)
359 val to_instance : (Id.t -> 'v) -> ('c, 't, 'r) pt -> 'v list
360 [@@ocaml.deprecated "(8.15) [to_instance] was missing a [List.rev] to comply to its specification; rely on [instance] for the correct specification or use [List.rev (instance ...)] for strict compatibility"]
361
362 (** [instance Ω] builds an instance [args] such
363 that [Ω ⊢ args:Ω] where [Ω] is a named-context and with the
364 local definitions of [Ω] skipped. Example: for the context
365 [id1:T,id2:=c,id3:U] (which is internally represented by a list
366 with [id3] at the head), it gives [Var id1, Var id3]. All [idj]
367 are supposed distinct. *)
368 val instance : (Id.t -> 'v) -> ('c, 't, 'r) pt -> 'v array
369
370 (** [instance_list] is like [instance] but returning a list. *)
371 val instance_list : (Id.t -> 'v) -> ('c, 't, 'r) pt -> 'v list
372 end
373
374 module Compacted :
375 sig
376 module Declaration :
377 sig
378 type ('constr, 'types, 'r) pt =
379 | LocalAssum of (Id.t,'r) pbinder_annot list * 'types
380 | LocalDef of (Id.t,'r) pbinder_annot list * 'constr * 'types
381
382 val map_constr : ('c -> 'c) -> ('c, 'c, 'r) pt -> ('c, 'c, 'r) pt
383 val of_named_decl : ('c, 't, 'r) Named.Declaration.pt -> ('c, 't, 'r) pt
384 val to_named_context : ('c, 't, 'r) pt -> ('c, 't, 'r) Named.pt
385 end
386
387 type ('constr, 'types, 'r) pt = ('constr, 'types, 'r) Declaration.pt list
388
389 val fold : (('c, 't, 'r) Declaration.pt -> 'a -> 'a) -> ('c, 't, 'r) pt -> init:'a -> 'a
390 end