Debian Codegraph @

plugins/ssrmatching/ssrmatching.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 (* (c) Copyright 2006-2015 Microsoft Corporation and Inria. *)
12
13 open Environ
14 open Evd
15 open Constr
16 open Genintern
17
18 (** ******** Small Scale Reflection pattern matching facilities ************* *)
19
20 (** Pattern parsing *)
21
22 type ssrtermkind = | InParens | WithAt | NoFlag | Cpattern
23
24 (** The type of context patterns, the patterns of the [set] tactic and
25 [:] tactical. These are patterns that identify a precise subterm. *)
26 type cpattern =
27 { kind : ssrtermkind
28 ; pattern : Genintern.glob_constr_and_expr
29 ; interpretation : Geninterp.interp_sign option }
30 val pr_cpattern : cpattern -> Pp.t
31
32 (** Pattern interpretation and matching *)
33
34 exception NoMatch
35 exception NoProgress
36
37 (** AST for [rpattern] (and consequently [cpattern]) *)
38 type ('inpat, 'term) ssrpattern =
39 | T of 'term
40 | In_T of 'term
41 | X_In_T of 'inpat
42 | In_X_In_T of 'inpat
43 | E_In_X_In_T of 'term * 'inpat
44 | E_As_X_In_T of 'term * 'inpat
45
46 type in_pattern
47
48 type pattern = {
49 pat_sigma : Evd.evar_map;
50 pat_pat : (in_pattern, EConstr.t) ssrpattern;
51 }
52
53 val pp_pattern : env -> pattern -> Pp.t
54
55 (** The type of rewrite patterns, the patterns of the [rewrite] tactic.
56 These patterns also include patterns that identify all the subterms
57 of a context (i.e. "in" prefix) *)
58 type rpattern = (cpattern * cpattern, cpattern) ssrpattern
59 val pr_rpattern : rpattern -> Pp.t
60
61 (** Extracts the redex and applies to it the substitution part of the pattern.
62 @raise Anomaly if called on [In_T] or [In_X_In_T] *)
63 val redex_of_pattern :
64 pattern -> (Evd.evar_map * EConstr.t) option
65
66 (** [interp_rpattern ise gl rpat] "internalizes" and "interprets" [rpat]
67 in the current [Ltac] interpretation signature [ise] and tactic input [gl]*)
68 val interp_rpattern :
69 Environ.env -> Evd.evar_map ->
70 rpattern ->
71 pattern
72
73 (** [interp_cpattern ise gl cpat ty] "internalizes" and "interprets" [cpat]
74 in the current [Ltac] interpretation signature [ise] and tactic input [gl].
75 [ty] is an optional type for the redex of [cpat] *)
76 val interp_cpattern :
77 Environ.env -> Evd.evar_map ->
78 cpattern -> (glob_constr_and_expr * Geninterp.interp_sign) option ->
79 pattern
80
81 (** The set of occurrences to be matched. The boolean is set to true
82 * to signal the complement of this set (i.e. \{-1 3\}) *)
83 type occ = (bool * int list) option
84
85 (** [subst e p t i]. [i] is the number of binders
86 traversed so far, [p] the term from the pattern, [t] the matched one *)
87 type subst = Environ.env -> EConstr.t -> EConstr.t -> int -> EConstr.t
88
89 (** [eval_pattern b env sigma t pat occ subst] maps [t] calling [subst] on every
90 [occ] occurrence of [pat]. The [int] argument is the number of
91 binders traversed. If [pat] is [None] then then subst is called on [t].
92 [t] must live in [env] and [sigma], [pat] must have been interpreted in
93 (an extension of) [sigma].
94 @raise NoMatch if [pat] has no occurrence and [b] is [true] (default [false])
95 @return [t] where all [occ] occurrences of [pat] have been mapped using
96 [subst] *)
97 val eval_pattern :
98 ?raise_NoMatch:bool ->
99 env -> evar_map -> EConstr.t ->
100 pattern option -> occ -> subst ->
101 EConstr.t
102
103 (** [fill_occ_pattern b env sigma t pat occ h] is a simplified version of
104 [eval_pattern].
105 It replaces all [occ] occurrences of [pat] in [t] with Rel [h].
106 [t] must live in [env] and [sigma], [pat] must have been interpreted in
107 (an extension of) [sigma].
108 @raise NoMatch if [pat] has no occurrence and [b] is [true] (default [false])
109 @return the instance of the redex of [pat] that was matched and [t]
110 transformed as described above. *)
111 val fill_occ_pattern :
112 ?raise_NoMatch:bool ->
113 env -> evar_map -> EConstr.t ->
114 pattern -> occ -> int ->
115 EConstr.t Evd.in_ustate * EConstr.t
116
117 (** Variant of the above function where we fix [h := 1] and return
118 [redex_of_pattern pat] if [pat] has no occurrence. *)
119 val fill_rel_occ_pattern :
120 env -> evar_map -> EConstr.t -> pattern -> occ ->
121 evar_map * EConstr.t * EConstr.t
122
123 (** *************************** Low level APIs ****************************** *)
124
125 (* The primitive matching facility. It matches of a term with holes, like
126 the T pattern above, and calls a continuation on its occurrences. *)
127
128 type ssrdir = L2R | R2L
129 val pr_dir_side : ssrdir -> Pp.t
130
131 (** patterns for a term with wildcards *)
132 type tpatterns
133
134 val empty_tpatterns : Evd.evar_map -> tpatterns
135
136 (** [mk_tpattern env sigma0 sigma_p ok p_origin dir t] compiles a term [t]
137 living in [env] [sigma] (an extension of [sigma0]) intro a [tpattern].
138 The [tpattern] can hold a (proof) term [p] and a diction [dir]. The [ok]
139 callback is used to filter occurrences.
140 @return the compiled [tpattern] and its [evar_map]
141 @raise UserEerror is the pattern is a wildcard *)
142 val mk_tpattern :
143 ?p_origin:ssrdir * EConstr.t ->
144 ?ok:(EConstr.t -> evar_map -> bool) ->
145 rigid:(Evar.t -> bool) ->
146 env ->
147 EConstr.t ->
148 ssrdir -> EConstr.t ->
149 tpatterns -> tpatterns
150
151 (** [findP env t i k] is a stateful function that finds the next occurrence
152 of a tpattern and calls the callback [k] to map the subterm matched.
153 The [int] argument passed to [k] is the number of binders traversed so far
154 plus the initial value [i].
155 @return [t] where the subterms identified by the selected occurrences of
156 the patter have been mapped using [k]
157 @raise NoMatch if the raise_NoMatch flag given to [mk_tpattern_matcher] is
158 [true] and if the pattern did not match
159 @raise UserEerror if the raise_NoMatch flag given to [mk_tpattern_matcher] is
160 [false] and if the pattern did not match *)
161 type find_P =
162 Environ.env -> EConstr.t -> int -> k:subst -> EConstr.t
163
164 (** [conclude ()] asserts that all mentioned occurrences have been visited.
165 @return the instance of the pattern, the evarmap after the pattern
166 instantiation, the proof term and the ssrdit stored in the tpattern
167 @raise UserEerror if too many occurrences were specified *)
168 type conclude =
169 unit -> EConstr.t * ssrdir * (bool * evar_map * UState.t * EConstr.t)
170
171 (** [mk_tpattern_matcher b o sigma0 occ sigma_tplist] creates a pair
172 a function [find_P] and [conclude] with the behaviour explained above.
173 The flag [b] (default [false]) changes the error reporting behaviour
174 of [find_P] if none of the [tpattern] matches. The argument [o] can
175 be passed to tune the [UserError] eventually raised (useful if the
176 pattern is coming from the LHS/RHS of an equation) *)
177 val mk_tpattern_matcher :
178 ?all_instances:bool ->
179 ?raise_NoMatch:bool ->
180 ?upats_origin:ssrdir * EConstr.t ->
181 evar_map -> occ -> tpatterns -> find_P * conclude
182
183 (** Example of [mk_tpattern_matcher] to implement
184 [rewrite \{occ\}\[in t\]rules].
185 It first matches "in t" (called [pat]), then in all matched subterms
186 it matches the LHS of the rules using [find_R].
187 [concl0] is the initial goal, [concl] will be the goal where some terms
188 are replaced by a De Bruijn index. The [rw_progress] extra check
189 selects only occurrences that are not rewritten to themselves (e.g.
190 an occurrence "x + x" rewritten with the commutativity law of addition
191 is skipped)
192 {[
193 let find_R, conclude = match pat with
194 | Some (_, In_T _) ->
195 let aux (sigma, pats) (d, r, lhs, rhs) =
196 let sigma, pat =
197 mk_tpattern env0 sigma0 (sigma, r) (rw_progress rhs) d lhs in
198 sigma, pats @ [pat] in
199 let rpats = List.fold_left aux (r_sigma, []) rules in
200 let find_R, end_R = mk_tpattern_matcher sigma0 occ rpats in
201 find_R ~k:(fun _ _ h -> mkRel h),
202 fun cl -> let rdx, d, r = end_R () in (d,r),rdx
203 | _ -> ... in
204 let concl = eval_pattern env0 sigma0 concl0 pat occ find_R in
205 let (d, r), rdx = conclude concl in
206 ]} *)
207
208 (* convenience shortcut: [fill_occ_term env concl sigma occ (sigma,t)] returns
209 * [concl] where [occ] occurrences of [t] have been replaced
210 * by [Rel 1] and the instance of [t] *)
211
212 val fill_occ_term : Environ.env -> Evd.evar_map -> EConstr.t -> occ -> evar_map * EConstr.t -> EConstr.t * EConstr.t
213
214 (** Helpers to make stateful closures. Example: a [find_P] function may be
215 called many times, but the pattern instantiation phase is performed only the
216 first time. The corresponding [conclude] has to return the instantiated
217 pattern redex. Since it is up to [find_P] to raise [NoMatch] if the pattern
218 has no instance, [conclude] considers it an anomaly if the pattern did
219 not match *)
220
221 (** [do_once r f] calls [f] and updates the ref only once *)
222 val do_once : 'a option ref -> (unit -> 'a) -> unit
223
224 (** [assert_done r] return the content of r.
225 @raise Anomaly is r is [None] *)
226 val assert_done : 'a option ref -> 'a
227
228 (** Very low level APIs.
229 these are calls to evarconv's [the_conv_x] followed by
230 [solve_unif_constraints_with_heuristics].
231 In case of failure they raise [NoMatch] *)
232
233 val unify_HO : env -> evar_map -> EConstr.constr -> EConstr.constr -> evar_map
234
235 (** Some more low level functions needed to implement the full SSR language
236 on top of the former APIs *)
237 val tag_of_cpattern : cpattern -> ssrtermkind
238 val loc_of_cpattern : cpattern -> Loc.t option
239 val id_of_pattern : evar_map -> pattern -> Names.Id.t option
240 val is_wildcard : cpattern -> bool
241 val cpattern_of_id : Names.Id.t -> cpattern
242 val pr_constr_pat : env -> evar_map -> constr -> Pp.t
243 val pr_econstr_pat : env -> evar_map -> econstr -> Pp.t
244
245 val ssrinstancesof : cpattern -> unit Proofview.tactic
246
247 (** Functions used for grammar extensions. Do not use. *)
248
249 module Internal :
250 sig
251 val wit_rpatternty : (rpattern, rpattern, rpattern) Genarg.genarg_type
252 val glob_rpattern : Genintern.glob_sign -> rpattern -> rpattern
253 val subst_rpattern : Mod_subst.substitution -> rpattern -> rpattern
254 val interp_rpattern : Geninterp.interp_sign -> env -> evar_map -> rpattern -> rpattern
255 val pr_rpattern : rpattern -> Pp.t
256 val mk_rpattern : (cpattern * cpattern, cpattern) ssrpattern -> rpattern
257 val mk_lterm : Constrexpr.constr_expr -> Geninterp.interp_sign option -> cpattern
258 val mk_term : ssrtermkind -> Constrexpr.constr_expr -> Geninterp.interp_sign option -> cpattern
259
260 val glob_cpattern : Genintern.glob_sign -> cpattern -> cpattern
261 val subst_ssrterm : Mod_subst.substitution -> cpattern -> cpattern
262 val interp_ssrterm : Geninterp.interp_sign -> env -> evar_map -> cpattern -> cpattern
263 val pr_ssrterm : cpattern -> Pp.t
264 end
265
266 (* eof *)