| 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 |
Require Import Ltac2.Init. |
| 12 |
Require Ltac2.Control. |
| 13 |
|
| 14 |
Ltac2 Type t := pattern. |
| 15 |
|
| 16 |
Ltac2 Type context. |
| 17 |
|
| 18 |
Ltac2 Type match_kind := [ |
| 19 |
| MatchPattern |
| 20 |
| MatchContext |
| 21 |
]. |
| 22 |
|
| 23 |
Ltac2 @ external empty_context : context := |
| 24 |
"rocq-runtime.plugins.ltac2" "pattern_empty_context". |
| 25 |
(** A trivial context only made of the hole. *) |
| 26 |
|
| 27 |
Ltac2 @ external matches : t -> constr -> (ident * constr) list := |
| 28 |
"rocq-runtime.plugins.ltac2" "pattern_matches". |
| 29 |
(** If the term matches the pattern, returns the bound variables. If it doesn't, |
| 30 |
fail with [Match_failure]. Panics if not focused. *) |
| 31 |
|
| 32 |
Ltac2 @ external matches_subterm : t -> constr -> context * ((ident * constr) list) := |
| 33 |
"rocq-runtime.plugins.ltac2" "pattern_matches_subterm". |
| 34 |
(** Returns a stream of results corresponding to all of the subterms of the term |
| 35 |
that matches the pattern as in [matches]. The stream is encoded as a |
| 36 |
backtracking value whose last exception is [Match_failure]. The additional |
| 37 |
value compared to [matches] is the context of the match, to be filled with |
| 38 |
the instantiate function. *) |
| 39 |
|
| 40 |
Ltac2 @ external matches_vect : t -> constr -> constr array := |
| 41 |
"rocq-runtime.plugins.ltac2" "pattern_matches_vect". |
| 42 |
(** Internal version of [matches] that does not return the identifiers. *) |
| 43 |
|
| 44 |
Ltac2 @ external matches_subterm_vect : t -> constr -> context * constr array := |
| 45 |
"rocq-runtime.plugins.ltac2" "pattern_matches_subterm_vect". |
| 46 |
(** Internal version of [matches_subterms] that does not return the identifiers. *) |
| 47 |
|
| 48 |
Ltac2 @ external matches_goal : |
| 49 |
bool -> |
| 50 |
((match_kind * t) option * (match_kind * t)) list -> |
| 51 |
(match_kind * t) -> |
| 52 |
ident array * context array * context array * constr array * context := |
| 53 |
"rocq-runtime.plugins.ltac2" "pattern_matches_goal". |
| 54 |
(** Given a list of patterns [hpats] for hypotheses and one pattern [cpat] for the |
| 55 |
conclusion, [matches_goal rev hpats cpat] produces (a stream of) tuples of: |
| 56 |
- An array of idents, whose size is the length of [hpats], corresponding to the |
| 57 |
name of matched hypotheses. |
| 58 |
- An array of contexts, whose size is the number of [hpats] which have non empty body pattern, |
| 59 |
corresponding to the contexts matched for every body pattern. |
| 60 |
In case the match kind of a body pattern was [MatchPattern], |
| 61 |
the corresponding context is ensured to be empty. |
| 62 |
- An array of contexts, whose size is the length of [hpats], corresponding to |
| 63 |
the contexts matched for every hypothesis pattern. In case the match kind of |
| 64 |
a hypothesis was [MatchPattern], the corresponding context is ensured to be empty. |
| 65 |
- An array of terms, whose size is the total number of pattern variables without |
| 66 |
duplicates. Terms are ordered by identifier order, e.g. ?a comes before ?b. |
| 67 |
- A context corresponding to the conclusion, which is ensured to be empty if |
| 68 |
the kind of [cpat] was [MatchPattern]. |
| 69 |
This produces a backtracking stream of results containing all the possible |
| 70 |
result combinations. The order of considered hypotheses is reversed if [rev] |
| 71 |
is true. |
| 72 |
*) |
| 73 |
|
| 74 |
Ltac2 @ external instantiate : context -> constr -> constr := |
| 75 |
"rocq-runtime.plugins.ltac2" "pattern_instantiate". |
| 76 |
(** Fill the hole of a context with the given term. *) |
| 77 |
|
| 78 |
(** Implementation of Ltac matching over terms and goals *) |
| 79 |
|
| 80 |
Ltac2 Type 'a constr_matching := (match_kind * t * (context -> constr array -> 'a)) list. |
| 81 |
|
| 82 |
Ltac2 lazy_match0 t (pats:'a constr_matching) := |
| 83 |
let rec interp m := match m with |
| 84 |
| [] => Control.zero Match_failure |
| 85 |
| p :: m => |
| 86 |
let next _ := interp m in |
| 87 |
let (knd, pat, f) := p in |
| 88 |
let p := match knd with |
| 89 |
| MatchPattern => |
| 90 |
(fun _ => |
| 91 |
let context := empty_context in |
| 92 |
let bind := matches_vect pat t in |
| 93 |
fun _ => f context bind) |
| 94 |
| MatchContext => |
| 95 |
(fun _ => |
| 96 |
let (context, bind) := matches_subterm_vect pat t in |
| 97 |
fun _ => f context bind) |
| 98 |
end in |
| 99 |
Control.plus p next |
| 100 |
end in |
| 101 |
Control.once (fun () => interp pats) (). |
| 102 |
|
| 103 |
Ltac2 multi_match0 t (pats:'a constr_matching) := |
| 104 |
let rec interp e m := match m with |
| 105 |
| [] => Control.zero e |
| 106 |
| p :: m => |
| 107 |
let next e := interp e m in |
| 108 |
let (knd, pat, f) := p in |
| 109 |
let p := match knd with |
| 110 |
| MatchPattern => |
| 111 |
(fun _ => |
| 112 |
let context := empty_context in |
| 113 |
let bind := matches_vect pat t in |
| 114 |
f context bind) |
| 115 |
| MatchContext => |
| 116 |
(fun _ => |
| 117 |
let (context, bind) := matches_subterm_vect pat t in |
| 118 |
f context bind) |
| 119 |
end in |
| 120 |
Control.plus p next |
| 121 |
end in |
| 122 |
interp Match_failure pats. |
| 123 |
|
| 124 |
Ltac2 one_match0 t m := Control.once (fun _ => multi_match0 t m). |
| 125 |
|
| 126 |
Ltac2 Type 'a goal_matching := |
| 127 |
((((match_kind * t) option * (match_kind * t)) list * (match_kind * t)) * |
| 128 |
(ident array -> context array -> context array -> constr array -> context -> 'a)) list. |
| 129 |
|
| 130 |
Ltac2 lazy_goal_match0 rev (pats:'a goal_matching) := |
| 131 |
let rec interp m := match m with |
| 132 |
| [] => Control.zero Match_failure |
| 133 |
| p :: m => |
| 134 |
let next _ := interp m in |
| 135 |
let (pat, f) := p in |
| 136 |
let (phyps, pconcl) := pat in |
| 137 |
let cur _ := |
| 138 |
let (hids, hbctx, hctx, subst, cctx) := matches_goal rev phyps pconcl in |
| 139 |
fun _ => f hids hbctx hctx subst cctx |
| 140 |
in |
| 141 |
Control.plus cur next |
| 142 |
end in |
| 143 |
Control.once (fun () => interp pats) (). |
| 144 |
|
| 145 |
Ltac2 multi_goal_match0 rev (pats:'a goal_matching) := |
| 146 |
let rec interp e m := match m with |
| 147 |
| [] => Control.zero e |
| 148 |
| p :: m => |
| 149 |
let next e := interp e m in |
| 150 |
let (pat, f) := p in |
| 151 |
let (phyps, pconcl) := pat in |
| 152 |
let cur _ := |
| 153 |
let (hids, hbctx, hctx, subst, cctx) := matches_goal rev phyps pconcl in |
| 154 |
f hids hbctx hctx subst cctx |
| 155 |
in |
| 156 |
Control.plus cur next |
| 157 |
end in |
| 158 |
interp Match_failure pats. |
| 159 |
|
| 160 |
Ltac2 one_goal_match0 rev pats := Control.once (fun _ => multi_goal_match0 rev pats). |