| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
let is_empty_subst = Id.Map.is_empty |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
let equal_instances env sigma c1 c2 = |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
Reductionops.is_conv env sigma c1 c2 |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
type 'a tac = 'a Proofview.tactic |
| 97 |
type 'a m = { stream : 'r. ('a -> result -> 'r tac) -> result -> 'r tac } |
| 98 |
|
| 99 |
|
| 100 |
let empty_subst = Id.Map.empty |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 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 |
|
| 118 |
let return (type a) (lhs:a) : a m = |
| 119 |
{ stream = fun k ctx -> k lhs ctx } |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 205 |
|
| 206 |
|
| 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 |
|
| 219 |
|
| 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) |