| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Tacexpr |
| 13 |
open Mod_subst |
| 14 |
open Genarg |
| 15 |
open Stdarg |
| 16 |
open Tacarg |
| 17 |
open Tactypes |
| 18 |
open Tactics |
| 19 |
open Globnames |
| 20 |
open Patternops |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
let subst_quantified_hypothesis _ x = x |
| 28 |
|
| 29 |
let subst_declared_or_quantified_hypothesis _ x = x |
| 30 |
|
| 31 |
let subst_glob_constr_and_expr subst (c, e) = |
| 32 |
(Detyping.subst_glob_constr (Global.env()) subst c, e) |
| 33 |
|
| 34 |
let subst_glob_constr = subst_glob_constr_and_expr |
| 35 |
|
| 36 |
let subst_binding subst = |
| 37 |
CAst.map (fun (b,c) -> |
| 38 |
subst_quantified_hypothesis subst b,subst_glob_constr subst c) |
| 39 |
|
| 40 |
let subst_bindings subst = function |
| 41 |
| NoBindings -> NoBindings |
| 42 |
| ImplicitBindings l -> ImplicitBindings (List.map (subst_glob_constr subst) l) |
| 43 |
| ExplicitBindings l -> ExplicitBindings (List.map (subst_binding subst) l) |
| 44 |
|
| 45 |
let subst_glob_with_bindings subst (c,bl) = |
| 46 |
(subst_glob_constr subst c, subst_bindings subst bl) |
| 47 |
|
| 48 |
let subst_glob_with_bindings_arg subst (clear,c) = |
| 49 |
(clear,subst_glob_with_bindings subst c) |
| 50 |
|
| 51 |
let rec subst_intro_pattern subst = CAst.map (function |
| 52 |
| IntroAction p -> IntroAction (subst_intro_pattern_action subst p) |
| 53 |
| IntroNaming _ | IntroForthcoming _ as x -> x) |
| 54 |
|
| 55 |
and subst_intro_pattern_action subst = let open CAst in function |
| 56 |
| IntroApplyOn ({loc;v=t},pat) -> |
| 57 |
IntroApplyOn (make ?loc @@ subst_glob_constr subst t,subst_intro_pattern subst pat) |
| 58 |
| IntroOrAndPattern l -> |
| 59 |
IntroOrAndPattern (subst_intro_or_and_pattern subst l) |
| 60 |
| IntroInjection l -> IntroInjection (List.map (subst_intro_pattern subst) l) |
| 61 |
| IntroWildcard | IntroRewrite _ as x -> x |
| 62 |
|
| 63 |
and subst_intro_or_and_pattern subst = function |
| 64 |
| IntroAndPattern l -> |
| 65 |
IntroAndPattern (List.map (subst_intro_pattern subst) l) |
| 66 |
| IntroOrPattern ll -> |
| 67 |
IntroOrPattern (List.map (List.map (subst_intro_pattern subst)) ll) |
| 68 |
|
| 69 |
let subst_destruction_arg subst = function |
| 70 |
| clear,ElimOnConstr c -> clear,ElimOnConstr (subst_glob_with_bindings subst c) |
| 71 |
| clear,ElimOnAnonHyp n as x -> x |
| 72 |
| clear,ElimOnIdent id as x -> x |
| 73 |
|
| 74 |
let subst_and_short_name f (c,n) = |
| 75 |
|
| 76 |
(f c,None) |
| 77 |
|
| 78 |
let subst_located f = Loc.map f |
| 79 |
|
| 80 |
let subst_reference subst = |
| 81 |
Locusops.or_var_map (subst_located (subst_kn subst)) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
let subst_global_reference subst = |
| 88 |
Locusops.or_var_map (subst_located (subst_global_reference subst)) |
| 89 |
|
| 90 |
let subst_evaluable subst = |
| 91 |
let subst_eval_ref = Tacred.subst_evaluable_reference subst in |
| 92 |
Locusops.or_var_map (subst_and_short_name subst_eval_ref) |
| 93 |
|
| 94 |
let subst_constr_with_occurrences subst (l,c) = (l,subst_glob_constr subst c) |
| 95 |
|
| 96 |
let subst_glob_constr_or_pattern subst (bvars,c,p) = |
| 97 |
let env = Global.env () in |
| 98 |
let sigma = Evd.from_env env in |
| 99 |
(bvars,subst_glob_constr subst c,subst_uninstantiated_pattern env sigma subst p) |
| 100 |
|
| 101 |
let subst_glob_red_expr subst = |
| 102 |
Redops.map_red_expr_gen |
| 103 |
(subst_glob_constr subst) |
| 104 |
(subst_evaluable subst) |
| 105 |
(subst_glob_constr subst) |
| 106 |
(Redexpr.subst_user_red_expr subst) |
| 107 |
|
| 108 |
let subst_raw_may_eval subst = function |
| 109 |
| ConstrEval (r,c) -> ConstrEval (subst_glob_red_expr subst r,subst_glob_constr subst c) |
| 110 |
| ConstrContext (locid,c) -> ConstrContext (locid,subst_glob_constr subst c) |
| 111 |
| ConstrTypeOf c -> ConstrTypeOf (subst_glob_constr subst c) |
| 112 |
| ConstrTerm c -> ConstrTerm (subst_glob_constr subst c) |
| 113 |
|
| 114 |
let subst_match_pattern subst = function |
| 115 |
| Subterm (ido,pc) -> Subterm (ido,(subst_glob_constr_or_pattern subst pc)) |
| 116 |
| Term pc -> Term (subst_glob_constr_or_pattern subst pc) |
| 117 |
|
| 118 |
let rec subst_match_goal_hyps subst = function |
| 119 |
| Hyp (locs,mp) :: tl -> |
| 120 |
Hyp (locs,subst_match_pattern subst mp) |
| 121 |
:: subst_match_goal_hyps subst tl |
| 122 |
| Def (locs,mv,mp) :: tl -> |
| 123 |
Def (locs,subst_match_pattern subst mv, subst_match_pattern subst mp) |
| 124 |
:: subst_match_goal_hyps subst tl |
| 125 |
| [] -> [] |
| 126 |
|
| 127 |
let rec subst_atomic subst (t:glob_atomic_tactic_expr) = match t with |
| 128 |
|
| 129 |
| TacIntroPattern (ev,l) -> TacIntroPattern (ev,List.map (subst_intro_pattern subst) l) |
| 130 |
| TacApply (a,ev,cb,cl) -> |
| 131 |
TacApply (a,ev,List.map (subst_glob_with_bindings_arg subst) cb, |
| 132 |
List.map (on_snd (Option.map (subst_intro_pattern subst))) cl) |
| 133 |
| TacElim (ev,cb,cbo) -> |
| 134 |
TacElim (ev,subst_glob_with_bindings_arg subst cb, |
| 135 |
Option.map (subst_glob_with_bindings subst) cbo) |
| 136 |
| TacCase (ev,cb) -> TacCase (ev,subst_glob_with_bindings_arg subst cb) |
| 137 |
| TacMutualFix (id,n,l) -> |
| 138 |
TacMutualFix(id,n,List.map (fun (id,n,c) -> (id,n,subst_glob_constr subst c)) l) |
| 139 |
| TacMutualCofix (id,l) -> |
| 140 |
TacMutualCofix (id, List.map (fun (id,c) -> (id,subst_glob_constr subst c)) l) |
| 141 |
| TacAssert (ev,b,otac,na,c) -> |
| 142 |
TacAssert (ev,b,Option.map (Option.map (subst_tactic subst)) otac,na, |
| 143 |
subst_glob_constr subst c) |
| 144 |
| TacGeneralize cl -> |
| 145 |
TacGeneralize (List.map (on_fst (subst_constr_with_occurrences subst))cl) |
| 146 |
| TacLetTac (ev,id,c,clp,b,eqpat) -> |
| 147 |
TacLetTac (ev,id,subst_glob_constr subst c,clp,b,eqpat) |
| 148 |
|
| 149 |
|
| 150 |
| TacInductionDestruct (isrec,ev,(l,el)) -> |
| 151 |
let l' = List.map (fun (c,ids,cls) -> |
| 152 |
subst_destruction_arg subst c, ids, cls) l in |
| 153 |
let el' = Option.map (subst_glob_with_bindings subst) el in |
| 154 |
TacInductionDestruct (isrec,ev,(l',el')) |
| 155 |
|
| 156 |
|
| 157 |
| TacReduce (r,cl) -> TacReduce (subst_glob_red_expr subst r, cl) |
| 158 |
| TacChange (check,op,c,cl) -> |
| 159 |
TacChange (check,Option.map (subst_glob_constr subst) op, |
| 160 |
subst_glob_constr subst c, cl) |
| 161 |
|
| 162 |
|
| 163 |
| TacRewrite (ev,l,cl,by) -> |
| 164 |
TacRewrite (ev, |
| 165 |
List.map (fun (b,m,c) -> |
| 166 |
b,m,subst_glob_with_bindings_arg subst c) l, |
| 167 |
cl,Option.map (subst_tactic subst) by) |
| 168 |
| TacInversion (DepInversion (k,c,l),hyp) -> |
| 169 |
TacInversion (DepInversion (k,Option.map (subst_glob_constr subst) c,l),hyp) |
| 170 |
| TacInversion (NonDepInversion _,_) as x -> x |
| 171 |
| TacInversion (InversionUsing (c,cl),hyp) -> |
| 172 |
TacInversion (InversionUsing (subst_glob_constr subst c,cl),hyp) |
| 173 |
|
| 174 |
and subst_tactic subst = CAst.map (function |
| 175 |
| TacAtom t -> TacAtom (subst_atomic subst t) |
| 176 |
| TacFun tacfun -> TacFun (subst_tactic_fun subst tacfun) |
| 177 |
| TacLetIn (r,l,u) -> |
| 178 |
let l = List.map (fun (n,b) -> (n,subst_tacarg subst b)) l in |
| 179 |
TacLetIn (r,l,subst_tactic subst u) |
| 180 |
| TacMatchGoal (lz,lr,lmr) -> |
| 181 |
TacMatchGoal (lz,lr, subst_match_rule subst lmr) |
| 182 |
| TacMatch (lz,c,lmr) -> |
| 183 |
TacMatch (lz,subst_tactic subst c,subst_match_rule subst lmr) |
| 184 |
| TacId _ | TacFail _ as x -> x |
| 185 |
| TacProgress tac -> TacProgress (subst_tactic subst tac:glob_tactic_expr) |
| 186 |
| TacAbstract (tac,s) -> TacAbstract (subst_tactic subst tac,s) |
| 187 |
| TacThen (t1,t2) -> |
| 188 |
TacThen (subst_tactic subst t1, subst_tactic subst t2) |
| 189 |
| TacDispatch tl -> TacDispatch (List.map (subst_tactic subst) tl) |
| 190 |
| TacExtendTac (tf,t,tl) -> |
| 191 |
TacExtendTac (Array.map (subst_tactic subst) tf, |
| 192 |
subst_tactic subst t, |
| 193 |
Array.map (subst_tactic subst) tl) |
| 194 |
| TacThens (t,tl) -> |
| 195 |
TacThens (subst_tactic subst t, List.map (subst_tactic subst) tl) |
| 196 |
| TacThens3parts (t1,tf,t2,tl) -> |
| 197 |
TacThens3parts (subst_tactic subst t1,Array.map (subst_tactic subst) tf, |
| 198 |
subst_tactic subst t2,Array.map (subst_tactic subst) tl) |
| 199 |
| TacDo (n,tac) -> TacDo (n,subst_tactic subst tac) |
| 200 |
| TacTimeout (n,tac) -> TacTimeout (n,subst_tactic subst tac) |
| 201 |
| TacTime (s,tac) -> TacTime (s,subst_tactic subst tac) |
| 202 |
| TacTry tac -> TacTry (subst_tactic subst tac) |
| 203 |
| TacRepeat tac -> TacRepeat (subst_tactic subst tac) |
| 204 |
| TacOr (tac1,tac2) -> |
| 205 |
TacOr (subst_tactic subst tac1,subst_tactic subst tac2) |
| 206 |
| TacOnce tac -> |
| 207 |
TacOnce (subst_tactic subst tac) |
| 208 |
| TacExactlyOnce tac -> |
| 209 |
TacExactlyOnce (subst_tactic subst tac) |
| 210 |
| TacIfThenCatch (tac,tact,tace) -> |
| 211 |
TacIfThenCatch ( |
| 212 |
subst_tactic subst tac, |
| 213 |
subst_tactic subst tact, |
| 214 |
subst_tactic subst tace) |
| 215 |
| TacOrelse (tac1,tac2) -> |
| 216 |
TacOrelse (subst_tactic subst tac1,subst_tactic subst tac2) |
| 217 |
| TacFirst l -> TacFirst (List.map (subst_tactic subst) l) |
| 218 |
| TacSolve l -> TacSolve (List.map (subst_tactic subst) l) |
| 219 |
| TacArg a -> TacArg (subst_tacarg subst a) |
| 220 |
| TacSelect (s, tac) -> TacSelect (s, subst_tactic subst tac) |
| 221 |
|
| 222 |
|
| 223 |
| TacAlias (s,l) -> |
| 224 |
let s = subst_kn subst s in |
| 225 |
TacAlias (s,List.map (subst_tacarg subst) l) |
| 226 |
| TacML (opn,l) -> TacML (opn,List.map (subst_tacarg subst) l) |
| 227 |
) |
| 228 |
|
| 229 |
and subst_tactic_fun subst (var,body) = (var,subst_tactic subst body) |
| 230 |
|
| 231 |
and subst_tacarg subst = function |
| 232 |
| Reference r -> Reference (subst_reference subst r) |
| 233 |
| ConstrMayEval c -> ConstrMayEval (subst_raw_may_eval subst c) |
| 234 |
| TacCall { CAst.loc; v=(f,l) } -> |
| 235 |
TacCall CAst.(make ?loc (subst_reference subst f, List.map (subst_tacarg subst) l)) |
| 236 |
| TacFreshId _ as x -> x |
| 237 |
| TacPretype c -> TacPretype (subst_glob_constr subst c) |
| 238 |
| TacNumgoals -> TacNumgoals |
| 239 |
| Tacexp t -> Tacexp (subst_tactic subst t) |
| 240 |
| TacGeneric (isquot,arg) -> TacGeneric (isquot,subst_genarg subst arg) |
| 241 |
|
| 242 |
|
| 243 |
and subst_match_rule subst = function |
| 244 |
| (All tc)::tl -> |
| 245 |
(All (subst_tactic subst tc))::(subst_match_rule subst tl) |
| 246 |
| (Pat (rl,mp,tc))::tl -> |
| 247 |
let hyps = subst_match_goal_hyps subst rl in |
| 248 |
let pat = subst_match_pattern subst mp in |
| 249 |
Pat (hyps,pat,subst_tactic subst tc) |
| 250 |
::(subst_match_rule subst tl) |
| 251 |
| [] -> [] |
| 252 |
|
| 253 |
and subst_genarg subst (GenArg (Glbwit wit, x)) = |
| 254 |
match wit with |
| 255 |
| ListArg wit -> |
| 256 |
let map x = |
| 257 |
let ans = subst_genarg subst (in_gen (glbwit wit) x) in |
| 258 |
out_gen (glbwit wit) ans |
| 259 |
in |
| 260 |
in_gen (glbwit (wit_list wit)) (List.map map x) |
| 261 |
| OptArg wit -> |
| 262 |
let ans = match x with |
| 263 |
| None -> in_gen (glbwit (wit_opt wit)) None |
| 264 |
| Some x -> |
| 265 |
let s = out_gen (glbwit wit) (subst_genarg subst (in_gen (glbwit wit) x)) in |
| 266 |
in_gen (glbwit (wit_opt wit)) (Some s) |
| 267 |
in |
| 268 |
ans |
| 269 |
| PairArg (wit1, wit2) -> |
| 270 |
let p, q = x in |
| 271 |
let p = out_gen (glbwit wit1) (subst_genarg subst (in_gen (glbwit wit1) p)) in |
| 272 |
let q = out_gen (glbwit wit2) (subst_genarg subst (in_gen (glbwit wit2) q)) in |
| 273 |
in_gen (glbwit (wit_pair wit1 wit2)) (p, q) |
| 274 |
| ExtraArg s -> |
| 275 |
Gensubst.generic_substitute subst (in_gen (glbwit wit) x) |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
let () = |
| 280 |
Gensubst.register_subst0 wit_int_or_var (fun _ v -> v); |
| 281 |
Gensubst.register_subst0 wit_nat_or_var (fun _ v -> v); |
| 282 |
Gensubst.register_subst0 wit_ref subst_global_reference; |
| 283 |
Gensubst.register_subst0 wit_smart_global subst_global_reference; |
| 284 |
Gensubst.register_subst0 wit_pre_ident (fun _ v -> v); |
| 285 |
Gensubst.register_subst0 wit_ident (fun _ v -> v); |
| 286 |
Gensubst.register_subst0 wit_hyp (fun _ v -> v); |
| 287 |
Gensubst.register_subst0 wit_intropattern subst_intro_pattern [@warning "-3"]; |
| 288 |
Gensubst.register_subst0 wit_simple_intropattern subst_intro_pattern; |
| 289 |
Gensubst.register_subst0 wit_tactic subst_tactic; |
| 290 |
Gensubst.register_subst0 wit_ltac_in_term (fun s (used_ntnvars,tac) -> used_ntnvars, subst_tactic s tac); |
| 291 |
Gensubst.register_subst0 wit_ltac subst_tactic; |
| 292 |
Gensubst.register_subst0 wit_constr subst_glob_constr; |
| 293 |
Gensubst.register_subst0 wit_clause_dft_concl (fun _ v -> v); |
| 294 |
Gensubst.register_subst0 wit_uconstr (fun subst c -> subst_glob_constr subst c); |
| 295 |
Gensubst.register_subst0 wit_open_constr (fun subst c -> subst_glob_constr subst c); |
| 296 |
Gensubst.register_subst0 Redexpr.wit_red_expr subst_glob_red_expr; |
| 297 |
Gensubst.register_subst0 wit_quant_hyp subst_declared_or_quantified_hypothesis; |
| 298 |
Gensubst.register_subst0 wit_bindings subst_bindings; |
| 299 |
Gensubst.register_subst0 wit_constr_with_bindings subst_glob_with_bindings; |
| 300 |
Gensubst.register_subst0 wit_destruction_arg subst_destruction_arg; |
| 301 |
() |