Debian Codegraph @

plugins/ltac2/tac2match.ml

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 open Context.Named.Declaration
13
14 module NamedDecl = Context.Named.Declaration
15
16 type context = Constr_matching.context
17
18 type result = {
19 subst : Ltac_pretype.patvar_map ;
20 }
21
22 type match_pattern =
23 | MatchPattern of Pattern.constr_pattern
24 | MatchContext of Pattern.constr_pattern
25
26 type match_context_hyps = match_pattern option * match_pattern
27
28 type match_rule = match_context_hyps list * match_pattern
29
30 (** {6 Utilities} *)
31
32 (** Tests whether the substitution [s] is empty. *)
33 let is_empty_subst = Id.Map.is_empty
34
35 (** {6 Non-linear patterns} *)
36
37
38 (** The patterns of Ltac are not necessarily linear. Non-linear
39 pattern are partially handled by the {!Matching} module, however
40 goal patterns are not primitive to {!Matching}, hence we must deal
41 with non-linearity between hypotheses and conclusion. Subterms are
42 considered equal up to the equality implemented in
43 [equal_instances]. *)
44 (* spiwack: it doesn't seem to be quite the same rule for non-linear
45 term patterns and non-linearity between hypotheses and/or
46 conclusion. Indeed, in [Matching], matching is made modulo
47 syntactic equality, and here we merge modulo conversion. It may be
48 a good idea to have an entry point of [Matching] with a partial
49 substitution as argument instead of merging substitution here. That
50 would ensure consistency. *)
51 let equal_instances env sigma c1 c2 =
52 (* How to compare instances? Do we want the terms to be convertible?
53 unifiable? Do we want the universe levels to be relevant?
54 (historically, conv_x is used) *)
55 Reductionops.is_conv env sigma c1 c2
56
57 (** Merges two substitutions. Raises [Not_coherent_metas] when
58 encountering two instances of the same metavariable which are not
59 equal according to {!equal_instances}. *)
60 exception Not_coherent_metas
61 let verify_metas_coherence env sigma s1 s2 =
62 let merge id oc1 oc2 = match oc1, oc2 with
63 | None, None -> None
64 | None, Some c | Some c, None -> Some c
65 | Some c1, Some c2 ->
66 if equal_instances env sigma c1 c2 then Some c1
67 else raise Not_coherent_metas
68 in
69 Id.Map.merge merge s1 s2
70
71 let matching_error =
72 CErrors.UserError Pp.(str "No matching clauses for match.")
73
74 let imatching_error = (matching_error, Exninfo.null)
75
76 (** A functor is introduced to share the environment and the
77 evar_map. They do not change and it would be a pity to introduce
78 closures everywhere just for the occasional calls to
79 {!equal_instances}. *)
80 module type StaticEnvironment = sig
81 val env : Environ.env
82 val sigma : Evd.evar_map
83 end
84 module PatternMatching (E:StaticEnvironment) = struct
85
86
87 (** {6 The pattern-matching monad } *)
88
89
90 (** To focus on the algorithmic portion of pattern-matching, the
91 bookkeeping is relegated to a monad: the composition of the
92 backtracking monad of {!IStream.t} with a "writer" effect. *)
93 (* spiwack: as we don't benefit from the various stream optimisations
94 of Haskell, it may be costly to give the monad in direct style such as
95 here. We may want to use some continuation passing style. *)
96 type 'a tac = 'a Proofview.tactic
97 type 'a m = { stream : 'r. ('a -> result -> 'r tac) -> result -> 'r tac }
98
99 (** The empty substitution. *)
100 let empty_subst = Id.Map.empty
101
102 (** Composes two substitutions using {!verify_metas_coherence}. It
103 must be a monoid with neutral element {!empty_subst}. Raises
104 [Not_coherent_metas] when composition cannot be achieved. *)
105 let subst_prod s1 s2 =
106 if is_empty_subst s1 then s2
107 else if is_empty_subst s2 then s1
108 else verify_metas_coherence E.env E.sigma s1 s2
109
110 (** Merge two writers (and ignore the first value component). *)
111 let merge m1 m2 =
112 try Some {
113 subst = subst_prod m1.subst m2.subst;
114 }
115 with Not_coherent_metas -> None
116
117 (** Monadic [return]: returns a single success with empty substitutions. *)
118 let return (type a) (lhs:a) : a m =
119 { stream = fun k ctx -> k lhs ctx }
120
121 (** Monadic bind: each success of [x] is replaced by the successes
122 of [f x]. The substitutions of [x] and [f x] are composed,
123 dropping the apparent successes when the substitutions are not
124 coherent. *)
125 let (>>=) (type a) (type b) (m:a m) (f:a -> b m) : b m =
126 { stream = fun k ctx -> m.stream (fun x ctx -> (f x).stream k ctx) ctx }
127
128 (** A variant of [(>>=)] when the first argument returns [unit]. *)
129 let (<*>) (type a) (m:unit m) (y:a m) : a m =
130 { stream = fun k ctx -> m.stream (fun () ctx -> y.stream k ctx) ctx }
131
132 (** Failure of the pattern-matching monad: no success. *)
133 let fail (type a) : a m = { stream = fun _ _ ->
134 let info = Exninfo.reify () in
135 Proofview.tclZERO ~info matching_error }
136
137 let run (m : 'a m) =
138 let ctx = {
139 subst = empty_subst ;
140 } in
141 let eval x ctx = Proofview.tclUNIT (x, ctx) in
142 m.stream eval ctx
143
144 (** Chooses in a list, in the same order as the list *)
145 let rec pick (l:'a list) (e, info) : 'a m = match l with
146 | [] -> { stream = fun _ _ -> Proofview.tclZERO ~info e }
147 | x :: l ->
148 { stream = fun k ctx -> Proofview.tclOR (k x ctx) (fun e -> (pick l e).stream k ctx) }
149
150 let pick l = pick l imatching_error
151
152 let put_subst subst : unit m =
153 let s = { subst } in
154 { stream = fun k ctx -> match merge s ctx with
155 | None ->
156 let info = Exninfo.reify () in
157 Proofview.tclZERO ~info matching_error
158 | Some s -> k () s }
159
160 (** {6 Pattern-matching} *)
161
162 let pattern_match_term pat term =
163 match pat with
164 | MatchPattern p ->
165 begin
166 try
167 put_subst (Constr_matching.matches E.env E.sigma p term) <*>
168 return None
169 with Constr_matching.PatternMatchingFailure -> fail
170 end
171 | MatchContext p ->
172
173 let rec map s (e, info) =
174 { stream = fun k ctx -> match IStream.peek s with
175 | IStream.Nil -> Proofview.tclZERO ~info e
176 | IStream.Cons ({ Constr_matching.m_sub = (_, subst); m_ctx }, s) ->
177 let nctx = { subst } in
178 match merge ctx nctx with
179 | None -> (map s (e, info)).stream k ctx
180 | Some nctx -> Proofview.tclOR (k (Some m_ctx) nctx) (fun e -> (map s e).stream k ctx)
181 }
182 in
183 map (Constr_matching.match_subterm E.env E.sigma (Id.Set.empty,p) term) imatching_error
184
185 let hyp_match_type pat hyps =
186 pick hyps >>= fun decl ->
187 let id = NamedDecl.get_id decl in
188 pattern_match_term pat (NamedDecl.get_type decl) >>= fun ctx ->
189 return (id, None, ctx)
190
191 let hyp_match_body_and_type bodypat typepat hyps =
192 pick hyps >>= function
193 | LocalDef (id,body,hyp) ->
194 pattern_match_term bodypat body >>= fun ctx_body ->
195 pattern_match_term typepat hyp >>= fun ctx_typ ->
196 return (id.binder_name, Some ctx_body, ctx_typ)
197 | LocalAssum (id,hyp) -> fail
198
199 let hyp_match pat hyps =
200 match pat with
201 | None, typepat -> hyp_match_type typepat hyps
202 | Some bodypat, typepat -> hyp_match_body_and_type bodypat typepat hyps
203
204 (** [hyp_pattern_list_match pats hyps lhs], matches the list of
205 patterns [pats] against the hypotheses in [hyps], and eventually
206 returns [lhs]. *)
207 let rec hyp_pattern_list_match pats hyps accu =
208 match pats with
209 | pat::pats ->
210 hyp_match pat hyps >>= fun (matched_hyp, _, _ as v) ->
211 let select_matched_hyp decl = Id.equal (NamedDecl.get_id decl) matched_hyp in
212 let hyps = CList.remove_first select_matched_hyp hyps in
213 hyp_pattern_list_match pats hyps (v :: accu)
214 | [] -> return accu
215
216 let rule_match_goal hyps concl = function
217 | (hyppats,conclpat) ->
218 (* the rules are applied from the topmost one (in the concrete
219 syntax) to the bottommost. *)
220 let hyppats = List.rev hyppats in
221 pattern_match_term conclpat concl >>= fun ctx_concl ->
222 hyp_pattern_list_match hyppats hyps [] >>= fun hyps ->
223 return (hyps, ctx_concl)
224
225 end
226
227 let match_goal env sigma concl ~rev rule =
228 let open Proofview.Notations in
229 let hyps = EConstr.named_context env in
230 let hyps = if rev then List.rev hyps else hyps in
231 let module E = struct
232 let env = env
233 let sigma = sigma
234 end in
235 let module M = PatternMatching(E) in
236 M.run (M.rule_match_goal hyps concl rule) >>= fun ((hyps, ctx_concl), subst) ->
237 Proofview.tclUNIT (hyps, ctx_concl, subst.subst)