Debian Codegraph @

dev/doc/proof-engine.md

raw symbols SCIP
1 Tutorial on the new proof engine for ML tactic writers
2 ======================================================
3
4 Starting from Coq 8.5, a new proof engine has been introduced, replacing the old
5 meta-based engine which had a lot of drawbacks, ranging from expressivity to
6 soundness, the major one being that the type of tactics was transparent. This
7 was pervasively abused and made virtually impossible to tweak the implementation
8 of the engine.
9
10 The old engine is deprecated and is slowly getting removed from the source code.
11
12 The new engine relies on a monadic API defined in the `Proofview` module. Helper
13 functions and higher-level operations are defined in the `Tacmach` and
14 `Tacticals` modules, and end-user tactics are defined amongst other in the
15 `Tactics` module.
16
17 At the root of the engine is a representation of proofs as partial terms that
18 can contain typed holes, called evars, short for *existential variable*. An evar
19 is essentially defined by its context and return type, that we will write
20 `?e : [Γ ⊢ _ : A]`. An evar `?e` must be applied to a substitution `σ` of type
21 `Γ` (i.e. a list of terms) to produce a term of type `A`, which is done by
22 applying `EConstr.mkEvar`, and which we will write `?e{σ}`.
23
24 The engine monad features a notion of global state called `evar_map`, defined in
25 the `Evd` module, which is the structure containing the incremental refinement
26 of evars. `Evd` is a low-level API and its use is discouraged in favour of the
27 `Evarutil` module which provides more abstract primitives.
28
29 In addition to this state, the monad also features a goal state, that is
30 an ordered list of current holes to be filled. While these holes are referred
31 to as goals at a high-enough level, they are actually no more than evars. The
32 API provided to deal with these holes can be found in the `Proofview.Goal`
33 module. Tactics are naturally operating on several goals at once, so that it is
34 usual to use the `Proofview.Goal.enter` function and its variants to dispatch a
35 tactic to each of the goals under focus.
36
37 Primitive tactics by term refining
38 -------------------------------------
39
40 A typical low-level tactic will be defined by plugging partial terms in the
41 goal holes thanks to the `Refine` module, and in particular to the
42 `Refine.refine` primitive.
43
44 ```ocaml
45 val refine : typecheck:bool -> (Evd.evar_map -> Evd.evar_map * EConstr.t) -> unit tactic
46 (** In [refine ~typecheck t], [t] is a term with holes under some
47 [evar_map] context. The term [t] is used as a partial solution
48 for the current goal (refine is a goal-dependent tactic), the
49 new holes created by [t] become the new subgoals. Exceptions
50 raised during the interpretation of [t] are caught and result in
51 tactic failures. If [typecheck] is [true] [t] is type-checked beforehand. *)
52 ```
53
54 What the function does is first evaluate the `t` argument in the
55 current proof state, and then use the resulting term as a filler for
56 the proof under focus. All evars that have been created by the
57 invocation of this thunk are then turned into new goals added in the
58 order of their creation.
59
60 To see how we can use it, let us have a look at an idealized example, the `cut`
61 tactic. Assuming `X` is a type, `cut X` fills the current goal `[Γ ⊢ _ : A]`
62 with a term `let x : X := ?e2{Γ} in ?e1{Γ} x` where `x` is a fresh variable and
63 `?e1 : [Γ ⊢ _ : X -> A]` and `?e2 : [Γ ⊢ _ : X]`. The current goal is solved and
64 two new holes `[e1, e2]` are added to the goal state in this order.
65
66 ```ocaml
67 let cut c =
68 Proofview.Goal.enter begin fun gl ->
69 (* In this block, we focus on one goal at a time indicated by gl *)
70 let env = Proofview.Goal.env gl in
71 (* Get the context of the goal, essentially [Γ] *)
72 let concl = Proofview.Goal.concl gl in
73 (* Get the conclusion [A] of the goal *)
74 let hyps = Tacmach.pf_ids_set_of_hyps gl in
75 (* List of hypotheses from the context of the goal *)
76 let id = Namegen.next_name_away Anonymous hyps in
77 (* Generate a fresh identifier *)
78 let t = mkArrowR c (Vars.lift 1 concl) in
79 (* Build [X -> A]. Note the lifting of [A] due to being on the right hand
80 side of the arrow. *)
81 Refine.refine ~typecheck:true begin fun sigma ->
82 (* All evars generated by this block will be added as goals *)
83 let sigma, f = Evarutil.new_evar env sigma t in
84 (* Generate ?e1 : [Γ ⊢ _ : X -> A], add it to sigma, and return the
85 term [f := Γ ⊢ ?e1{Γ} : X -> A] with the updated sigma. The identity
86 substitution for [Γ] is extracted from the [env] argument, so that
87 one must be careful to pass the correct context here in order for the
88 resulting term to be well-typed. The [p] return value is a proof term
89 used to enforce sigma monotonicity. *)
90 let sigma, x = Evarutil.new_evar env sigma c in
91 (* Generate ?e2 : [Γ ⊢ _ : X] in sigma and return
92 [x := Γ ⊢ ?e2{Γ} : X]. *)
93 let r = mkLetIn (Context.annotR (Name id), x, c, mkApp (Vars.lift 1 f, [|mkRel 1|])) in
94 (* Build [r := Γ ⊢ let id : X := ?e2{Γ} in ?e1{Γ} id : A] *)
95 (sigma, r)
96 end
97 end
98 ```
99
100 The `Evarutil.new_evar` function is the preferred way to generate evars in
101 tactics. It returns a ready-to-use term, so that one does not have to call
102 the `mkEvar` primitive. There are lower-level variants whose use is dedicated to
103 special use cases, *e.g.* whenever one wants a non-identity substitution. One
104 should take care to call it with the proper `env` argument so that the evar
105 and term it generates make sense in the context they will be plugged in.
106
107 For the sake of completeness, the old engine was relying on the `Tacmach.refine`
108 function to provide a similar feature. Nonetheless, it was using untyped metas
109 instead of evars, so that it had to mangle the argument term to actually produce
110 the term that would be put into the hole. For instance, to work around the
111 untypedness, some metas had to be coerced with a cast to enforce their type,
112 otherwise leading to runtime errors. This was working for very simple
113 instances, but was unreliable for everything else.
114
115 High-level composition of tactics
116 ------------------------------------
117
118 It is possible to combine low-level refinement tactics to create more powerful
119 abstractions. While it was the standard way of doing things in the old engine
120 to overcome its technical limitations (namely that one was forced to go through
121 a limited set of derivation rules), it is recommended to generate proofs as
122 much as possible by refining in ML tactics when it is possible and easy enough.
123 Indeed, this prevents dependence on fragile constructions such as unification.
124
125 Obviously, it does not forbid the use of tacticals to mimic what one would do
126 in Ltac. Each Ltac primitive has a corresponding ML counterpart with simple
127 semantics. A list of such tacticals can be found in the `Tacticals` module. Most
128 of them are a porting of the tacticals from the old engine to the new one, so
129 that if they share the same name they are expected to have the same semantics.