Debian Codegraph @

vernac/declare.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 open Names
12
13 (** This module provides the functions to declare new
14 variables, parameters, constants and inductive types in the global
15 environment. It also updates some accessory tables such as [Nametab]
16 (name resolution), [Impargs], and [Notations]. *)
17
18 (** We provide three main entry points:
19
20 - one-go functions, that will register a constant in one go, suited
21 for non-interactive definitions where the term is given.
22
23 - two-phase [start/save] functions which will create an
24 interactive proof, allow its modification using tactics, and saving
25 when complete.
26
27 - program mode API, that allow to declare a constant with holes, to
28 be fullfilled later.
29
30 Note that the API in this file is still in a state of flux, don't
31 hesitate to contact the maintainers if you have any question.
32
33 Additionally, this file does contain some low-level functions, marked
34 as such; these functions are unstable and should not be used unless you
35 already know what they are doing.
36
37 *)
38
39 (** Declaration hooks, to be run when a constant is saved. Use with
40 care, as imperative effects may become not supported in the
41 future. *)
42 module Hook : sig
43 type 'a g
44
45 type t = unit g
46
47 (** Hooks allow users of the API to perform arbitrary actions at
48 proof/definition saving time. For example, to register a constant
49 as a Coercion, perform some cleanup, update the search database,
50 etc... *)
51 module S : sig
52 type t =
53 { uctx : UState.t
54 (** [ustate]: universe constraints obtained when the term was closed *)
55 ; obls : (Id.t * Constr.t) list
56 (** [(n1,t1),...(nm,tm)]: association list between obligation
57 name and the corresponding defined term (might be a constant,
58 but also an arbitrary term in the Expand case of obligations) *)
59 ; scope : Locality.definition_scope
60 (** [scope]: Locality of the original declaration *)
61 ; dref : GlobRef.t
62 (** [dref]: identifier of the original declaration *)
63 }
64 end
65
66 val make_g : (S.t -> 'a -> 'a) -> 'a g
67 val make : (S.t -> unit) -> t
68 val call : ?hook:t -> S.t -> unit
69 end
70
71 (** {2 One-go, non-interactive declaration API } *)
72
73 (** Information for a single top-level named constant *)
74 module CInfo : sig
75 type 'constr t
76
77 val make
78 : ?loc:Loc.t
79 -> name:Id.t
80 -> typ:'constr
81 -> ?args:Name.t list
82 -> ?impargs:Impargs.manual_implicits
83 -> unit
84 -> 'constr t
85
86 (* Used only in Vernacentries, may disappear from public API *)
87 val to_constr : Evd.evar_map -> EConstr.t t -> Constr.t t
88
89 (* Used only in RecLemmas, may disappear from public API *)
90 val get_typ : 'constr t -> 'constr
91
92 end
93
94 (** Information for a declaration, interactive or not, includes
95 parameters shared by mutual constants *)
96 module Info : sig
97
98 type t
99
100 (** Note that [opaque] doesn't appear here as it is not known at the
101 start of the proof in the interactive case. *)
102 val make
103 : ?poly:PolyFlags.t
104 -> ?inline : bool
105 -> ?kind : Decls.logical_kind
106 (** Theorem, etc... *)
107 -> ?udecl : UState.universe_decl
108 -> ?scope : Locality.definition_scope
109 (** locality *)
110 -> ?clearbody:bool
111 -> ?hook : Hook.t
112 (** Callback to be executed after saving the constant *)
113 -> ?typing_flags:Declarations.typing_flags
114 -> ?user_warns : Globnames.extended_global_reference UserWarn.with_qf
115 -> ?ntns : Metasyntax.notation_interpretation_decl list
116 -> unit
117 -> t
118
119 end
120
121 (** Declares a non-interactive constant; [body] and [types] will be
122 normalized w.r.t. the passed [evar_map] [sigma]. Universes should
123 be handled properly, including minimization and restriction. Note
124 that [sigma] is checked for unresolved evars, thus you should be
125 careful not to submit open terms *)
126 val declare_definition
127 : info:Info.t
128 -> cinfo:EConstr.t option CInfo.t
129 -> opaque:bool
130 -> body:EConstr.t
131 -> ?using:Vernacexpr.section_subset_expr
132 -> Evd.evar_map
133 -> GlobRef.t
134
135 val declare_mutual_definitions
136 : info:Info.t
137 -> cinfo: Constr.t CInfo.t list
138 -> opaque:bool
139 -> eff:Evd.side_effects
140 -> uctx:UState.t
141 -> bodies:Constr.t list
142 -> possible_guard:Pretyping.possible_guard * Sorts.relevance list
143 -> ?using:Vernacexpr.section_subset_expr
144 -> unit
145 -> Names.GlobRef.t list
146
147 (** {2 Declaration of interactive constants } *)
148
149 (** [save] / [save_admitted] can update obligations state, so we need
150 to expose the state here *)
151 module OblState : sig
152
153 type t
154 val empty : t
155
156 module View : sig
157 module Obl : sig
158 type t = private
159 { name : Id.t
160 ; loc : Loc.t option
161 ; status : bool * Evar_kinds.obligation_definition_status
162 ; solved : bool
163 }
164 end
165
166 type t = private
167 { opaque : bool
168 ; remaining : int
169 ; obligations : Obl.t array
170 }
171 end
172
173 val view : t -> View.t Id.Map.t
174
175 end
176
177 (** [Declare.Proof.t] Construction of constants using interactive proofs. *)
178 module Proof : sig
179
180 type t
181
182 (** [start_proof ~info ~cinfo sigma] starts a proof of [cinfo].
183 The proof is started in the evar map [sigma] (which
184 can typically contain universe constraints) *)
185 val start
186 : info:Info.t
187 -> cinfo:EConstr.t CInfo.t
188 -> ?using:Id.Set.t
189 -> Evd.evar_map
190 -> t
191
192 (** [start_{derive,equations}] are functions meant to handle
193 interactive proofs with multiple goals, they should be considered
194 experimental until we provide a more general API encompassing
195 both of them. Please, get in touch with the developers if you
196 would like to experiment with multi-goal dependent proofs so we
197 can use your input on the design of the new API. *)
198 val start_derive : name:Id.t -> info:Info.t -> cinfo:unit CInfo.t list -> Proofview.telescope -> t
199
200 val start_equations :
201 name:Id.t
202 -> info:Info.t
203 -> hook:(pm:OblState.t -> Constant.t list -> Evd.evar_map -> OblState.t)
204 -> types:(Environ.env * Evar.t * Evd.undefined Evd.evar_info * EConstr.named_context * Evd.econstr) list
205 -> Evd.evar_map
206 -> Proofview.telescope
207 -> t
208
209 (** Pretty much internal, used by the Lemma vernaculars *)
210 val start_definition
211 : info:Info.t
212 -> cinfo:EConstr.t CInfo.t
213 -> ?using:Vernacexpr.section_subset_expr
214 -> Evd.evar_map
215 -> t
216
217 (** Pretty much internal, used by mutual Lemma / Fixpoint vernaculars *)
218 val start_mutual_definitions
219 : info:Info.t
220 -> cinfo:Constr.t CInfo.t list
221 -> bodies:Constr.t option list
222 -> possible_guard:(Pretyping.possible_guard * Sorts.relevance list)
223 -> ?using:Vernacexpr.section_subset_expr
224 -> Evd.evar_map
225 -> t
226
227 (** Pretty much internal, used by mutual Lemma / Fixpoint vernaculars with #[refine] *)
228 val start_mutual_definitions_refine
229 : info:Info.t
230 -> cinfo:EConstr.t CInfo.t list
231 -> bodies:EConstr.t option list
232 -> possible_guard:(Pretyping.possible_guard * Evd.erelevance list)
233 -> ?using:Vernacexpr.section_subset_expr
234 -> Evd.evar_map
235 -> t
236
237 (** Qed a proof *)
238 val save
239 : pm:OblState.t
240 -> proof:t
241 -> opaque:Vernacexpr.opacity_flag
242 -> idopt:Names.lident option
243 -> OblState.t * GlobRef.t list
244
245 (** For proofs known to have [Regular] ending, no need to touch program state. *)
246 val save_regular
247 : proof:t
248 -> opaque:Vernacexpr.opacity_flag
249 -> idopt:Names.lident option
250 -> GlobRef.t list
251
252 (** Admit a proof *)
253 val save_admitted : pm:OblState.t -> proof:t -> OblState.t
254
255 (** [by env tac] applies tactic [tac] to the 1st subgoal of the current
256 focused proof.
257 Returns [false] if an unsafe tactic has been used. *)
258 val by : Environ.env -> unit Proofview.tactic -> t -> t * bool
259
260 (** Operations on ongoing proofs *)
261 val get : t -> Proof.t
262 val get_name : t -> Names.Id.t
263
264 val fold : f:(Proof.t -> 'a) -> t -> 'a
265 val map : f:(Proof.t -> Proof.t) -> t -> t
266 val map_fold : f:(Proof.t -> Proof.t * 'a) -> t -> t * 'a
267 val map_fold_endline : f:(Gentactic.glob_generic_tactic option -> Proof.t -> Proof.t * 'a) -> t -> t * 'a
268
269 (** Sets the tactic to be used when a tactic line is closed with [...] *)
270 val set_endline_tactic : Gentactic.glob_generic_tactic -> t -> t
271
272 val definition_scope : t -> Locality.definition_scope
273
274 (** Sets the section variables assumed by the proof, returns its closure
275 * (w.r.t. type dependencies and let-ins covered by it) *)
276 val set_proof_using : t -> Vernacexpr.section_subset_expr -> Constr.named_context * t
277
278 (** Gets the set of variables declared to be used by the proof. None means
279 no "Proof using" or #[using] was given *)
280 val get_used_variables : t -> Id.Set.t option
281
282 (** Compacts the representation of the proof by pruning all intermediate
283 terms *)
284 val compact : t -> t
285
286 (** Update the proof's universe information typically after a
287 side-effecting command (e.g. a sublemma definition) has been run
288 inside it. *)
289 val update_sigma_univs : UGraph.t -> t -> t
290
291 val get_open_goals : t -> int
292
293 (** Helpers to obtain proof state when in an interactive proof *)
294
295 (** [get_goal_context n] returns the context of the [n]th subgoal of
296 the current focused proof or raises a [UserError] if there is no
297 focused proof or if there is no more subgoals *)
298
299 val get_goal_context : t -> int -> Evd.evar_map * Environ.env
300
301 (** [get_current_context ()] returns the context of the
302 current focused goal. If there is no focused goal but there
303 is a proof in progress, it returns the corresponding evar_map.
304 If there is no pending proof then it returns the current global
305 environment and empty evar_map. *)
306 val get_current_context : t -> Evd.evar_map * Environ.env
307
308 (** {2 Proof delay API, warning, internal, not stable} *)
309
310 (* Intermediate step necessary to delegate the future.
311 * Both access the current proof state. The former is supposed to be
312 * chained with a computation that completed the proof *)
313 type closed_proof_output
314
315 (** Requires a complete proof. *)
316 val return_proof : t -> closed_proof_output
317
318 (** XXX: This is an internal, low-level API and could become scheduled
319 for removal from the public API, use higher-level declare APIs
320 instead *)
321 type proof_object
322
323 val close_proof : ?warn_incomplete:bool -> opaque:Vernacexpr.opacity_flag -> keep_body_ucst_separate:bool -> t -> proof_object
324 val close_future_proof : feedback_id:Stateid.t -> t -> closed_proof_output Future.computation -> proof_object
325
326 (** Special cases for delayed proofs, in this case we must provide the
327 proof information so the proof won't be forced. *)
328 val save_lemma_admitted_delayed :
329 pm:OblState.t
330 -> proof:proof_object
331 -> OblState.t
332
333 val save_lemma_proved_delayed
334 : pm:OblState.t
335 -> proof:proof_object
336 -> idopt:Names.lident option
337 -> OblState.t
338
339 exception NotGuarded of
340 Environ.env * Evd.evar_map *
341 (Environ.env * int * EConstr.t Type_errors.pcofix_guard_error) option *
342 (Environ.env * int * int list * EConstr.t Type_errors.pfix_guard_error) list *
343 EConstr.rec_declaration
344
345 val control_only_guard : t -> unit
346
347 end
348
349 (** {2 low-level, internal API, avoid using unless you have special needs } *)
350
351 (** Proof entries represent a proof that has been finished, but still
352 not registered with the kernel.
353
354 XXX: This is an internal, low-level API and could become scheduled
355 for removal from the public API, use higher-level declare APIs
356 instead *)
357 type proof_entry
358 type parameter_entry
359 type primitive_entry
360 type symbol_entry
361
362 val definition_entry
363 : ?opaque:bool
364 -> ?using:Names.Id.Set.t
365 -> ?inline:bool
366 -> ?types:Constr.types
367 -> ?univs:UState.named_universes_entry
368 -> Constr.constr
369 -> proof_entry
370
371 val parameter_entry
372 : ?inline:int
373 -> ?univs:UState.named_universes_entry
374 -> Constr.constr
375 -> parameter_entry
376
377 val primitive_entry
378 : ?types:(Constr.types * UState.named_universes_entry)
379 -> CPrimitives.op_or_type
380 -> primitive_entry
381
382 val symbol_entry
383 : ?univs:UState.named_universes_entry
384 -> unfold_fix:bool
385 -> Constr.types
386 -> symbol_entry
387
388 (** XXX: This is an internal, low-level API and could become scheduled
389 for removal from the public API, use higher-level declare APIs
390 instead *)
391 val declare_entry
392 : ?loc:Loc.t
393 -> name:Id.t
394 -> ?scope:Locality.definition_scope
395 -> kind:Decls.logical_kind
396 -> ?user_warns:Globnames.extended_global_reference UserWarn.with_qf
397 -> ?hook:Hook.t
398 -> impargs:Impargs.manual_implicits
399 -> uctx:UState.t
400 -> proof_entry
401 -> GlobRef.t
402
403 (** Declaration of section variables and local definitions *)
404 type variable_declaration =
405 | SectionLocalDef of {
406 clearbody : bool;
407 entry : proof_entry;
408 }
409 | SectionLocalAssum of {
410 typ : Constr.types;
411 impl : Glob_term.binding_kind;
412 univs : UState.named_universes_entry;
413 }
414
415 (** Declaration of local constructions (Variable/Hypothesis/Local) *)
416 val declare_variable
417 : name:variable
418 -> kind:Decls.logical_kind
419 -> typing_flags:Declarations.typing_flags option
420 -> variable_declaration
421 -> unit
422
423 (** Declaration of global constructions
424 i.e. Definition/Theorem/Axiom/Parameter/...
425
426 XXX: This is an internal, low-level API and could become scheduled
427 for removal from the public API, use higher-level declare APIs
428 instead *)
429 type constant_entry =
430 | DefinitionEntry of proof_entry
431 | ParameterEntry of parameter_entry
432 | PrimitiveEntry of primitive_entry
433 | SymbolEntry of symbol_entry
434
435 val prepare_parameter
436 : poly:PolyFlags.t
437 -> udecl:UState.universe_decl
438 -> types:EConstr.types
439 -> Evd.evar_map
440 -> Evd.evar_map * parameter_entry
441
442 (** [declare_constant id cd] declares a global declaration
443 (constant/parameter) with name [id] in the current section; it returns
444 the full path of the declaration
445
446 XXX: This is an internal, low-level API and could become scheduled
447 for removal from the public API, use higher-level declare APIs
448 instead *)
449 val declare_constant
450 : ?loc:Loc.t
451 -> ?local:Locality.import_status
452 -> name:Id.t
453 -> kind:Decls.logical_kind
454 -> ?typing_flags:Declarations.typing_flags
455 -> ?user_warns:Globnames.extended_global_reference UserWarn.with_qf
456 -> constant_entry
457 -> Constant.t
458
459 (** Like [declare_definition] but also returns the universes and universe constraints added to the
460 global environment *)
461 val declare_definition_full
462 : info:Info.t
463 -> cinfo:EConstr.t option CInfo.t
464 -> opaque:bool
465 -> body:EConstr.t
466 -> ?using:Vernacexpr.section_subset_expr
467 -> Evd.evar_map
468 -> GlobRef.t * Univ.ContextSet.t
469
470 (** Declaration messages, for internal use *)
471
472 (** XXX: Scheduled for removal from public API, do not use *)
473 val definition_message : Id.t -> unit
474 val assumption_message : Id.t -> unit
475 val fixpoint_message : int array option -> Id.t list -> unit
476
477 (** {2 Program mode API} *)
478
479 (** Rocq's Program mode support. This mode extends declarations of
480 constants and fixpoints with [Program Definition] and [Program
481 Fixpoint] to support incremental construction of terms using
482 delayed proofs, called "obligations"
483
484 The mode also provides facilities for managing and auto-solving
485 sets of obligations.
486
487 The basic code flow of programs/obligations is as follows:
488
489 - [add_definition] / [add_mutual_definitions] are called from the
490 respective [Program] vernacular command interpretation; at this
491 point the only extra work we do is to prepare the new definition
492 [d] using [RetrieveObl], which consists in turning unsolved evars
493 into obligations. [d] is not sent to the kernel yet, as it is not
494 complete and cannot be typchecked, but saved in a special
495 data-structure. Auto-solving of obligations is tried at this stage
496 (see below)
497
498 - [next_obligation] will retrieve the next obligation
499 ([RetrieveObl] sorts them by topological order) and will try to
500 solve it. When all obligations are solved, the original constant
501 [d] is grounded and sent to the kernel for addition to the global
502 environment. Auto-solving of obligations is also triggered on
503 obligation completion.
504
505 {2} Solving of obligations: Solved obligations are stored as regular
506 global declarations in the global environment, usually with name
507 [constant_obligation_number] where [constant] is the original
508 [constant] and [number] is the corresponding (internal) number.
509
510 Solving an obligation can trigger a bit of a complex cascaded
511 callback path; closing an obligation can indeed allow all other
512 obligations to be closed, which in turn may trigged the declaration
513 of the original constant. Care must be taken, as this can modify
514 [Global.env] in arbitrarily ways. Current code takes some care to
515 refresh the [env] in the proper boundaries, but the invariants
516 remain delicate.
517
518 {2} Saving of obligations: as open obligations use the regular proof
519 mode, a `Qed` will call `Lemmas.save_lemma` first. For this reason
520 obligations code is split in two: this file, [Obligations], taking
521 care of the top-level vernac commands, and [Declare], which is
522 called by `Lemmas` to close an obligation proof and eventually to
523 declare the top-level [Program]ed constant.
524
525 *)
526
527 module Obls : sig
528
529 type fixpoint_kind = IsFixpoint of lident option list | IsCoFixpoint
530
531 (** Check obligations are properly solved before closing the
532 [what_for] section / module *)
533 val check_solved_obligations : pm:OblState.t -> what_for:Pp.t -> unit
534 val default_tactic : unit Proofview.tactic ref
535
536 (** Prepare API, to be removed once we provide the corresponding 1-step API *)
537 val prepare_obligations
538 : name:Id.t
539 -> PolyFlags.t
540 -> ?types:EConstr.t
541 -> body:EConstr.t
542 -> Environ.env
543 -> Evd.evar_map
544 -> Constr.constr * Constr.types * UState.t * RetrieveObl.obligation_name_lifter * RetrieveObl.obligation_info
545
546 (** Start a [Program Definition c] proof. [uctx] [udecl] [impargs]
547 [kind] [scope] [poly] etc... come from the interpretation of the
548 vernacular; `obligation_info` was generated by [RetrieveObl] It
549 will return whether all the obligations were solved; if so, it will
550 also register [c] with the kernel. *)
551 val add_definition :
552 pm:OblState.t
553 -> info:Info.t
554 -> cinfo:Constr.types CInfo.t
555 -> opaque:bool
556 -> uctx:UState.t
557 -> ?body:Constr.t
558 -> ?tactic:unit Proofview.tactic
559 -> ?reduce:(Constr.t -> Constr.t)
560 -> ?using:Vernacexpr.section_subset_expr
561 -> ?obl_hook: OblState.t Hook.g
562 -> RetrieveObl.obligation_info
563 -> OblState.t
564
565 (* XXX: unify with MutualEntry *)
566
567 (** Start a [Program Fixpoint] declaration, similar to the above,
568 except it takes a list now. *)
569 val add_mutual_definitions :
570 pm:OblState.t
571 -> info:Info.t
572 -> cinfo:Constr.types CInfo.t list
573 -> opaque:bool
574 -> uctx:UState.t
575 -> bodies:Constr.t list
576 -> possible_guard:(Pretyping.possible_guard * Sorts.relevance list)
577 -> ?tactic:unit Proofview.tactic
578 -> ?reduce:(Constr.t -> Constr.t)
579 -> ?using:Vernacexpr.section_subset_expr
580 -> ?obl_hook: OblState.t Hook.g
581 -> RetrieveObl.obligation_info list
582 -> OblState.t
583
584 (** Implementation of the [Obligation n of id with tac] command *)
585 val obligation :
586 int * Names.Id.t option
587 -> pm:OblState.t
588 -> Gentactic.glob_generic_tactic option
589 -> Proof.t
590
591 (** Implementation of the [Next Obligation of id with tac] and [Final Obligation of id with tac] commands *)
592 val next_obligation :
593 pm:OblState.t -> ?final:bool -> Names.Id.t option -> Gentactic.glob_generic_tactic option -> Proof.t
594
595 (** Implementation of the [Solve Obligations of id with tac] command *)
596 val solve_obligations :
597 pm:OblState.t -> Names.Id.t option -> unit Proofview.tactic option -> OblState.t
598
599 (** Implementation of the [Solve All Obligations with tac] command *)
600 val solve_all_obligations : pm:OblState.t -> unit Proofview.tactic option -> OblState.t
601
602 (** Implementation of the [Obligations of id] command *)
603 val show_obligations : pm:OblState.t -> ?msg:bool -> Names.Id.t option -> unit
604
605 (** Implementation of the [Preterm of id] command *)
606 val show_term : pm:OblState.t -> Names.Id.t option -> Pp.t
607
608 (** Implementation of the [Admit Obligations of id] command *)
609 val admit_obligations : pm:OblState.t -> Names.Id.t option -> OblState.t
610
611 val check_program_libraries : unit -> unit
612
613 val program_inference_hook : Environ.env -> Evd.evar_map -> Evar.t -> (Evd.evar_map * EConstr.t) option
614
615 end
616
617 val is_local_constant : Constant.t -> bool
618 (** [true] on constants declared Local and not from the current
619 interactive module (or its parents). *)
620
621 (** {6 For internal support, do not use} *)
622
623 module Internal : sig
624
625 (* Libobject exports *)
626 module Constant : sig
627 type t
628 val tag : (Id.t * t) Libobject.Dyn.tag
629 val kind : t -> Decls.logical_kind
630 end
631
632 val objVariable : Id.t Libobject.Dyn.tag
633
634 (** [export_side_effects eff] makes the side effects [eff] global. This
635 usually happens at the end of a proof (during Qed or Defined), but
636 one may need to declare them by hand, for example because the
637 tactic was run as part of a command *)
638 val export_side_effects : Evd.side_effects -> unit
639
640 val register_side_effects : Proof.t -> Proof.t
641
642 end