| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Printer |
| 12 |
open CErrors |
| 13 |
open Util |
| 14 |
open Constr |
| 15 |
open Context |
| 16 |
open Termops |
| 17 |
open EConstr |
| 18 |
open Vars |
| 19 |
open Namegen |
| 20 |
open Names |
| 21 |
open Pp |
| 22 |
open Tactics |
| 23 |
open FixTactics |
| 24 |
open Induction |
| 25 |
open Indfun_common |
| 26 |
open Libnames |
| 27 |
open Context.Rel.Declaration |
| 28 |
module RelDecl = Context.Rel.Declaration |
| 29 |
|
| 30 |
|
| 31 |
open UnsafeMonomorphic |
| 32 |
|
| 33 |
let list_chop ?(msg = "") n l = |
| 34 |
try List.chop n l with Failure msg' -> failwith (msg ^ msg') |
| 35 |
|
| 36 |
let pop t = Vars.lift (-1) t |
| 37 |
|
| 38 |
let make_refl_eq constructor type_of_t t = |
| 39 |
|
| 40 |
mkApp (constructor, [|type_of_t; t|]) |
| 41 |
|
| 42 |
type pte_info = |
| 43 |
{proving_tac : Id.t list -> unit Proofview.tactic; is_valid : constr -> bool} |
| 44 |
|
| 45 |
type ptes_info = pte_info Id.Map.t |
| 46 |
|
| 47 |
type 'a dynamic_info = |
| 48 |
{nb_rec_hyps : int; rec_hyps : Id.t list; eq_hyps : Id.t list; info : 'a} |
| 49 |
|
| 50 |
type body_info = constr dynamic_info |
| 51 |
|
| 52 |
let observe_tac s = |
| 53 |
observe_tac ~header:(str "observation") (fun _ _ -> Pp.str s) |
| 54 |
|
| 55 |
let finish_proof dynamic_infos = observe_tac "finish" assumption |
| 56 |
let thin = clear |
| 57 |
let eq_constr sigma u v = EConstr.eq_constr_nounivs sigma u v |
| 58 |
|
| 59 |
let is_trivial_eq sigma t = |
| 60 |
let res = |
| 61 |
try |
| 62 |
match EConstr.kind sigma t with |
| 63 |
| App (f, [|_; t1; t2|]) when eq_constr sigma f (Lazy.force eq) -> |
| 64 |
eq_constr sigma t1 t2 |
| 65 |
| App (f, [|t1; a1; t2; a2|]) when eq_constr sigma f (jmeq ()) -> |
| 66 |
eq_constr sigma t1 t2 && eq_constr sigma a1 a2 |
| 67 |
| _ -> false |
| 68 |
with e when CErrors.noncritical e -> false |
| 69 |
in |
| 70 |
|
| 71 |
res |
| 72 |
|
| 73 |
let rec incompatible_constructor_terms sigma t1 t2 = |
| 74 |
let c1, arg1 = decompose_app_list sigma t1 and c2, arg2 = decompose_app_list sigma t2 in |
| 75 |
(not (eq_constr sigma t1 t2)) |
| 76 |
&& isConstruct sigma c1 && isConstruct sigma c2 |
| 77 |
&& ( (not (eq_constr sigma c1 c2)) |
| 78 |
|| List.exists2 (incompatible_constructor_terms sigma) arg1 arg2 ) |
| 79 |
|
| 80 |
let is_incompatible_eq env sigma t = |
| 81 |
let res = |
| 82 |
try |
| 83 |
match EConstr.kind sigma t with |
| 84 |
| App (f, [|_; t1; t2|]) when eq_constr sigma f (Lazy.force eq) -> |
| 85 |
incompatible_constructor_terms sigma t1 t2 |
| 86 |
| App (f, [|u1; t1; u2; t2|]) when eq_constr sigma f (jmeq ()) -> |
| 87 |
eq_constr sigma u1 u2 && incompatible_constructor_terms sigma t1 t2 |
| 88 |
| _ -> false |
| 89 |
with e when CErrors.noncritical e -> false |
| 90 |
in |
| 91 |
if res then observe (fun () -> str "is_incompatible_eq " ++ pr_leconstr_env env sigma t); |
| 92 |
res |
| 93 |
|
| 94 |
let pf_get_new_id id env = |
| 95 |
next_ident_away id (Id.Set.of_list (Termops.ids_of_named_context env)) |
| 96 |
|
| 97 |
let change_hyp_with_using msg hyp_id t tac = |
| 98 |
Proofview.Goal.enter (fun gl -> |
| 99 |
let prov_id = pf_get_new_id hyp_id (Proofview.Goal.hyps gl) in |
| 100 |
Tacticals.tclTHENS |
| 101 |
( |
| 102 |
assert_by (Name prov_id) t |
| 103 |
(Tacticals.tclCOMPLETE tac)) |
| 104 |
[ Tacticals.tclTHENLIST |
| 105 |
[ |
| 106 |
Tactics.clear [hyp_id] |
| 107 |
; |
| 108 |
rename_hyp [(prov_id, hyp_id)] ] ]) |
| 109 |
|
| 110 |
exception TOREMOVE |
| 111 |
|
| 112 |
let prove_trivial_eq h_id context (constructor, type_of_term, term) = |
| 113 |
let nb_intros = List.length context in |
| 114 |
Tacticals.tclTHENLIST |
| 115 |
[ Tacticals.tclDO nb_intros intro |
| 116 |
; |
| 117 |
Proofview.Goal.enter (fun g -> |
| 118 |
let hyps = Proofview.Goal.hyps g in |
| 119 |
let context_hyps = |
| 120 |
fst |
| 121 |
(list_chop ~msg:"prove_trivial_eq : " nb_intros |
| 122 |
(ids_of_named_context hyps)) |
| 123 |
in |
| 124 |
let context_hyps' = |
| 125 |
mkApp (constructor, [|type_of_term; term|]) |
| 126 |
:: List.map mkVar context_hyps |
| 127 |
in |
| 128 |
let to_refine = applist (mkVar h_id, List.rev context_hyps') in |
| 129 |
Tactics.exact_check to_refine) ] |
| 130 |
|
| 131 |
let find_rectype env sigma c = |
| 132 |
let t, l = decompose_app_list sigma (Reductionops.whd_betaiotazeta env sigma c) in |
| 133 |
match EConstr.kind sigma t with |
| 134 |
| Ind ind -> (t, l) |
| 135 |
| Construct _ -> (t, l) |
| 136 |
| _ -> raise Not_found |
| 137 |
|
| 138 |
let isAppConstruct env sigma t = |
| 139 |
try |
| 140 |
let t', l = find_rectype env sigma t in |
| 141 |
observe |
| 142 |
(fun () -> str "isAppConstruct : " |
| 143 |
++ Printer.pr_leconstr_env env sigma t |
| 144 |
++ str " -> " |
| 145 |
++ Printer.pr_leconstr_env env sigma (applist (t', l)) ); |
| 146 |
true |
| 147 |
with Not_found -> false |
| 148 |
|
| 149 |
exception NoChange |
| 150 |
|
| 151 |
let change_eq env sigma hyp_id (context : rel_context) x t end_of_type = |
| 152 |
let nochange ?t' msg = |
| 153 |
observe |
| 154 |
(fun () -> str ("Not treating ( " ^ msg ^ " )") |
| 155 |
++ pr_leconstr_env env sigma t |
| 156 |
++ str " " |
| 157 |
++ |
| 158 |
match t' with |
| 159 |
| None -> str "" |
| 160 |
| Some t -> Printer.pr_leconstr_env env sigma t ); |
| 161 |
raise NoChange |
| 162 |
in |
| 163 |
let eq_constr c1 c2 = |
| 164 |
try |
| 165 |
ignore (Evarconv.unify_delay env sigma c1 c2); |
| 166 |
true |
| 167 |
with Evarconv.UnableToUnify _ -> false |
| 168 |
in |
| 169 |
if not (noccurn sigma 1 end_of_type) then nochange "dependent"; |
| 170 |
|
| 171 |
if not (isApp sigma t) then nochange "not an equality"; |
| 172 |
let f_eq, args = destApp sigma t in |
| 173 |
let constructor, t1, t2, t1_typ = |
| 174 |
try |
| 175 |
if eq_constr f_eq (Lazy.force eq) then |
| 176 |
let t1 = (args.(1), args.(0)) |
| 177 |
and t2 = (args.(2), args.(0)) |
| 178 |
and t1_typ = args.(0) in |
| 179 |
(Lazy.force refl_equal, t1, t2, t1_typ) |
| 180 |
else if eq_constr f_eq (jmeq ()) then |
| 181 |
(jmeq_refl (), (args.(1), args.(0)), (args.(3), args.(2)), args.(0)) |
| 182 |
else nochange "not an equality" |
| 183 |
with e when CErrors.noncritical e -> nochange "not an equality" |
| 184 |
in |
| 185 |
if not (closed0 sigma (fst t1) && closed0 sigma (snd t1)) then |
| 186 |
nochange "not a closed lhs"; |
| 187 |
let rec compute_substitution sub t1 t2 = |
| 188 |
|
| 189 |
if isRel sigma t2 then ( |
| 190 |
let t2 = destRel sigma t2 in |
| 191 |
try |
| 192 |
let t1' = Int.Map.find t2 sub in |
| 193 |
if not (eq_constr t1 t1') then nochange "twice bound variable"; |
| 194 |
sub |
| 195 |
with Not_found -> |
| 196 |
assert (closed0 sigma t1); |
| 197 |
Int.Map.add t2 t1 sub ) |
| 198 |
else if isAppConstruct env sigma t1 && isAppConstruct env sigma t2 then begin |
| 199 |
let c1, args1 = find_rectype env sigma t1 |
| 200 |
and c2, args2 = find_rectype env sigma t2 in |
| 201 |
if not (eq_constr c1 c2) then nochange "cannot solve (diff)"; |
| 202 |
List.fold_left2 compute_substitution sub args1 args2 |
| 203 |
end |
| 204 |
else if eq_constr t1 t2 then sub |
| 205 |
else |
| 206 |
nochange |
| 207 |
~t':(make_refl_eq constructor (Reductionops.whd_all env sigma t1) t2) |
| 208 |
"cannot solve (diff)" |
| 209 |
in |
| 210 |
let sub = compute_substitution Int.Map.empty (snd t1) (snd t2) in |
| 211 |
let sub = compute_substitution sub (fst t1) (fst t2) in |
| 212 |
let end_of_type_with_pop = pop end_of_type in |
| 213 |
|
| 214 |
let new_end_of_type = |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
let sub = Int.Map.bindings sub in |
| 219 |
List.fold_left |
| 220 |
(fun end_of_type (i, t) -> liftn 1 i (substnl [t] (i - 1) end_of_type)) |
| 221 |
end_of_type_with_pop sub |
| 222 |
in |
| 223 |
let old_context_length = List.length context + 1 in |
| 224 |
let witness_fun = |
| 225 |
mkLetIn |
| 226 |
( make_annot Anonymous ERelevance.relevant |
| 227 |
, make_refl_eq constructor t1_typ (fst t1) |
| 228 |
, t |
| 229 |
, mkApp |
| 230 |
( mkVar hyp_id |
| 231 |
, Array.init old_context_length (fun i -> |
| 232 |
mkRel (old_context_length - i)) ) ) |
| 233 |
in |
| 234 |
let new_type_of_hyp, ctxt_size, witness_fun = |
| 235 |
List.fold_left_i |
| 236 |
(fun i (end_of_type, ctxt_size, witness_fun) decl -> |
| 237 |
try |
| 238 |
let witness = Int.Map.find i sub in |
| 239 |
if is_local_def decl then anomaly (Pp.str "can not redefine a rel!"); |
| 240 |
( pop end_of_type |
| 241 |
, ctxt_size |
| 242 |
, mkLetIn |
| 243 |
( RelDecl.get_annot decl |
| 244 |
, witness |
| 245 |
, RelDecl.get_type decl |
| 246 |
, witness_fun ) ) |
| 247 |
with Not_found -> |
| 248 |
( mkProd_or_LetIn decl end_of_type |
| 249 |
, ctxt_size + 1 |
| 250 |
, mkLambda_or_LetIn decl witness_fun )) |
| 251 |
1 |
| 252 |
(new_end_of_type, 0, witness_fun) |
| 253 |
context |
| 254 |
in |
| 255 |
let new_type_of_hyp = Reductionops.nf_betaiota env sigma new_type_of_hyp in |
| 256 |
let new_ctxt, new_end_of_type = |
| 257 |
decompose_prod_n_decls sigma ctxt_size new_type_of_hyp |
| 258 |
in |
| 259 |
let prove_new_hyp = |
| 260 |
let open Tacticals in |
| 261 |
tclTHEN (tclDO ctxt_size intro) |
| 262 |
(Proofview.Goal.enter (fun g -> |
| 263 |
let all_ids = Tacmach.pf_ids_of_hyps g in |
| 264 |
let new_ids, _ = list_chop ctxt_size all_ids in |
| 265 |
let to_refine = applist (witness_fun, List.rev_map mkVar new_ids) in |
| 266 |
let evm, _ = |
| 267 |
Typing.type_of (Proofview.Goal.env g) (Proofview.Goal.sigma g) |
| 268 |
to_refine |
| 269 |
in |
| 270 |
tclTHEN (Proofview.Unsafe.tclEVARS evm) (Tactics.exact_check to_refine))) |
| 271 |
in |
| 272 |
let simpl_eq_tac = |
| 273 |
change_hyp_with_using "prove_pattern_simplification" hyp_id new_type_of_hyp |
| 274 |
prove_new_hyp |
| 275 |
in |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
(new_ctxt, new_end_of_type, simpl_eq_tac) |
| 291 |
|
| 292 |
let is_property sigma (ptes_info : ptes_info) t_x full_type_of_hyp = |
| 293 |
if isApp sigma t_x then |
| 294 |
let pte, args = destApp sigma t_x in |
| 295 |
if isVar sigma pte && Array.for_all (closed0 sigma) args then |
| 296 |
try |
| 297 |
let info = Id.Map.find (destVar sigma pte) ptes_info in |
| 298 |
info.is_valid full_type_of_hyp |
| 299 |
with Not_found -> false |
| 300 |
else false |
| 301 |
else false |
| 302 |
|
| 303 |
let isLetIn sigma t = |
| 304 |
match EConstr.kind sigma t with LetIn _ -> true | _ -> false |
| 305 |
|
| 306 |
let h_reduce_with_zeta cl = |
| 307 |
reduce (Genredexpr.Cbv {Redops.all_flags with Genredexpr.rDelta = false}) cl |
| 308 |
|
| 309 |
let rewrite_until_var arg_num eq_ids : unit Proofview.tactic = |
| 310 |
let open Tacticals in |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
let test_var g = |
| 316 |
let sigma = Proofview.Goal.sigma g in |
| 317 |
let env = Proofview.Goal.env g in |
| 318 |
let _, args = destApp sigma (Proofview.Goal.concl g) in |
| 319 |
not (isConstruct sigma args.(arg_num) || isAppConstruct env sigma args.(arg_num)) |
| 320 |
in |
| 321 |
let rec do_rewrite eq_ids = |
| 322 |
Proofview.Goal.enter (fun g -> |
| 323 |
if test_var g then Proofview.tclUNIT () |
| 324 |
else |
| 325 |
match eq_ids with |
| 326 |
| [] -> |
| 327 |
anomaly (Pp.str "Cannot find a way to prove recursive property.") |
| 328 |
| eq_id :: eq_ids -> |
| 329 |
tclTHEN |
| 330 |
(tclTRY (Equality.rewriteRL (mkVar eq_id))) |
| 331 |
(do_rewrite eq_ids)) |
| 332 |
in |
| 333 |
do_rewrite eq_ids |
| 334 |
|
| 335 |
let rec_pte_id = Id.of_string "Hrec" |
| 336 |
|
| 337 |
let clean_hyp_with_heq ptes_infos eq_hyps hyp_id env sigma = |
| 338 |
let rocq_False = |
| 339 |
EConstr.of_constr |
| 340 |
(UnivGen.constr_of_monomorphic_global env @@ Rocqlib.lib_ref "core.False.type") |
| 341 |
in |
| 342 |
let rocq_True = |
| 343 |
EConstr.of_constr |
| 344 |
(UnivGen.constr_of_monomorphic_global env @@ Rocqlib.lib_ref "core.True.type") |
| 345 |
in |
| 346 |
let rocq_I = |
| 347 |
EConstr.of_constr |
| 348 |
(UnivGen.constr_of_monomorphic_global env @@ Rocqlib.lib_ref "core.True.I") |
| 349 |
in |
| 350 |
let open Tacticals in |
| 351 |
let rec scan_type context type_of_hyp : unit Proofview.tactic = |
| 352 |
if isLetIn sigma type_of_hyp then |
| 353 |
let real_type_of_hyp = it_mkProd_or_LetIn type_of_hyp context in |
| 354 |
let reduced_type_of_hyp = |
| 355 |
Reductionops.nf_betaiotazeta env sigma real_type_of_hyp |
| 356 |
in |
| 357 |
|
| 358 |
let new_context, new_typ_of_hyp = |
| 359 |
decompose_prod_n_decls sigma (List.length context) reduced_type_of_hyp |
| 360 |
in |
| 361 |
tclTHENLIST |
| 362 |
[ h_reduce_with_zeta (Locusops.onHyp hyp_id) |
| 363 |
; scan_type new_context new_typ_of_hyp ] |
| 364 |
else if isProd sigma type_of_hyp then |
| 365 |
let x, t_x, t' = destProd sigma type_of_hyp in |
| 366 |
let actual_real_type_of_hyp = it_mkProd_or_LetIn t' context in |
| 367 |
if is_property sigma ptes_infos t_x actual_real_type_of_hyp then |
| 368 |
let pte, pte_args = destApp sigma t_x in |
| 369 |
let prove_rec_hyp = |
| 370 |
(Id.Map.find (destVar sigma pte) ptes_infos).proving_tac |
| 371 |
in |
| 372 |
let popped_t' = pop t' in |
| 373 |
let real_type_of_hyp = it_mkProd_or_LetIn popped_t' context in |
| 374 |
let prove_new_type_of_hyp = |
| 375 |
let context_length = List.length context in |
| 376 |
tclTHENLIST |
| 377 |
[ tclDO context_length intro |
| 378 |
; Proofview.Goal.enter (fun g -> |
| 379 |
let hyps = Proofview.Goal.hyps g in |
| 380 |
let context_hyps_ids = |
| 381 |
fst |
| 382 |
(list_chop ~msg:"rec hyp : context_hyps" context_length |
| 383 |
(ids_of_named_context hyps)) |
| 384 |
in |
| 385 |
let rec_pte_id = pf_get_new_id rec_pte_id hyps in |
| 386 |
let to_refine = |
| 387 |
applist |
| 388 |
( mkVar hyp_id |
| 389 |
, List.rev_map mkVar (rec_pte_id :: context_hyps_ids) ) |
| 390 |
in |
| 391 |
|
| 392 |
tclTHENS |
| 393 |
(assert_before (Name rec_pte_id) t_x) |
| 394 |
[ |
| 395 |
prove_rec_hyp eq_hyps |
| 396 |
; |
| 397 |
Tactics.exact_check to_refine ]) ] |
| 398 |
in |
| 399 |
tclTHENLIST |
| 400 |
[ |
| 401 |
change_hyp_with_using "rec_hyp_tac" hyp_id real_type_of_hyp |
| 402 |
prove_new_type_of_hyp |
| 403 |
; scan_type context popped_t' ] |
| 404 |
else if eq_constr sigma t_x rocq_False then |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
raise TOREMOVE |
| 409 |
else if is_incompatible_eq env sigma t_x then raise TOREMOVE |
| 410 |
|
| 411 |
else if |
| 412 |
eq_constr sigma t_x rocq_True |
| 413 |
then |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
let popped_t' = pop t' in |
| 418 |
let real_type_of_hyp = it_mkProd_or_LetIn popped_t' context in |
| 419 |
let prove_trivial = |
| 420 |
let nb_intro = List.length context in |
| 421 |
tclTHENLIST |
| 422 |
[ tclDO nb_intro intro |
| 423 |
; Proofview.Goal.enter (fun g -> |
| 424 |
let hyps = Proofview.Goal.hyps g in |
| 425 |
let context_hyps = |
| 426 |
fst |
| 427 |
(list_chop ~msg:"removing True : context_hyps " nb_intro |
| 428 |
(ids_of_named_context hyps)) |
| 429 |
in |
| 430 |
let to_refine = |
| 431 |
applist |
| 432 |
( mkVar hyp_id |
| 433 |
, List.rev (rocq_I :: List.map mkVar context_hyps) ) |
| 434 |
in |
| 435 |
Tactics.exact_check to_refine) ] |
| 436 |
in |
| 437 |
tclTHENLIST |
| 438 |
[ change_hyp_with_using "prove_trivial" hyp_id real_type_of_hyp |
| 439 |
prove_trivial |
| 440 |
; scan_type context popped_t' ] |
| 441 |
else if is_trivial_eq sigma t_x then |
| 442 |
|
| 443 |
let popped_t' = pop t' in |
| 444 |
let real_type_of_hyp = it_mkProd_or_LetIn popped_t' context in |
| 445 |
let hd, args = destApp sigma t_x in |
| 446 |
let get_args hd args = |
| 447 |
if eq_constr sigma hd (Lazy.force eq) then |
| 448 |
(Lazy.force refl_equal, args.(0), args.(1)) |
| 449 |
else (jmeq_refl (), args.(0), args.(1)) |
| 450 |
in |
| 451 |
tclTHENLIST |
| 452 |
[ change_hyp_with_using "prove_trivial_eq" hyp_id real_type_of_hyp |
| 453 |
( |
| 454 |
prove_trivial_eq hyp_id context (get_args hd args)) |
| 455 |
; scan_type context popped_t' ] |
| 456 |
else |
| 457 |
try |
| 458 |
let new_context, new_t', tac = |
| 459 |
change_eq env sigma hyp_id context x t_x t' |
| 460 |
in |
| 461 |
tclTHEN tac (scan_type new_context new_t') |
| 462 |
with NoChange -> |
| 463 |
|
| 464 |
scan_type (LocalAssum (x, t_x) :: context) t' |
| 465 |
else tclIDTAC |
| 466 |
in |
| 467 |
try (scan_type [] (Typing.type_of_variable env hyp_id), [hyp_id]) |
| 468 |
with TOREMOVE -> (thin [hyp_id], []) |
| 469 |
|
| 470 |
let clean_goal_with_heq ptes_infos continue_tac (dyn_infos : body_info) = |
| 471 |
let open Tacticals in |
| 472 |
Proofview.Goal.enter (fun g -> |
| 473 |
let env = Proofview.Goal.env g in |
| 474 |
let sigma = Proofview.Goal.sigma g in |
| 475 |
let tac, new_hyps = |
| 476 |
List.fold_left |
| 477 |
(fun (hyps_tac, new_hyps) hyp_id -> |
| 478 |
let hyp_tac, new_hyp = |
| 479 |
clean_hyp_with_heq ptes_infos dyn_infos.eq_hyps hyp_id env sigma |
| 480 |
in |
| 481 |
(tclTHEN hyp_tac hyps_tac, new_hyp @ new_hyps)) |
| 482 |
(tclIDTAC, []) dyn_infos.rec_hyps |
| 483 |
in |
| 484 |
let new_infos = |
| 485 |
{dyn_infos with rec_hyps = new_hyps; nb_rec_hyps = List.length new_hyps} |
| 486 |
in |
| 487 |
tclTHENLIST |
| 488 |
[ tac |
| 489 |
; |
| 490 |
continue_tac new_infos ]) |
| 491 |
|
| 492 |
let heq_id = Id.of_string "Heq" |
| 493 |
|
| 494 |
let treat_new_case ptes_infos nb_prod continue_tac term dyn_infos = |
| 495 |
let open Tacticals in |
| 496 |
Proofview.Goal.enter (fun g -> |
| 497 |
let nb_first_intro = nb_prod - 1 - dyn_infos.nb_rec_hyps in |
| 498 |
tclTHENLIST |
| 499 |
[ |
| 500 |
tclDO nb_first_intro |
| 501 |
(intro_avoiding (Id.Set.of_list dyn_infos.rec_hyps)) |
| 502 |
; |
| 503 |
intro_using_then heq_id |
| 504 |
|
| 505 |
(fun _ -> Proofview.tclUNIT ()) |
| 506 |
; onLastHypId (fun heq_id -> |
| 507 |
tclTHENLIST |
| 508 |
[ |
| 509 |
tclMAP introduction dyn_infos.rec_hyps |
| 510 |
; observe_tac "after_introduction" |
| 511 |
(Proofview.Goal.enter (fun g' -> |
| 512 |
let env = Proofview.Goal.env g' in |
| 513 |
let sigma = Proofview.Goal.sigma g' in |
| 514 |
let concl = Proofview.Goal.concl g' in |
| 515 |
|
| 516 |
let new_term_value_eq = |
| 517 |
Tacmach.pf_get_hyp_typ heq_id g' |
| 518 |
in |
| 519 |
|
| 520 |
let new_term_value = |
| 521 |
match EConstr.kind sigma new_term_value_eq with |
| 522 |
| App (f, [|_; _; args2|]) -> args2 |
| 523 |
| _ -> |
| 524 |
observe |
| 525 |
(fun () -> str "cannot compute new term value : " |
| 526 |
++ Termops.Internal.print_constr_env env sigma concl ++ fnl () |
| 527 |
++ str "last hyp is" |
| 528 |
++ pr_leconstr_env env sigma new_term_value_eq ); |
| 529 |
anomaly (Pp.str "cannot compute new term value.") |
| 530 |
in |
| 531 |
tclTYPEOFTHEN term (fun sigma termtyp -> |
| 532 |
let fun_body = |
| 533 |
mkLambda |
| 534 |
( make_annot Anonymous ERelevance.relevant |
| 535 |
, termtyp |
| 536 |
, Termops.replace_term sigma term (mkRel 1) |
| 537 |
dyn_infos.info ) |
| 538 |
in |
| 539 |
let new_body = |
| 540 |
Reductionops.nf_betaiota env sigma |
| 541 |
(mkApp (fun_body, [|new_term_value|])) |
| 542 |
in |
| 543 |
let new_infos = |
| 544 |
{ dyn_infos with |
| 545 |
info = new_body |
| 546 |
; eq_hyps = heq_id :: dyn_infos.eq_hyps } |
| 547 |
in |
| 548 |
clean_goal_with_heq ptes_infos continue_tac |
| 549 |
new_infos))) ]) ]) |
| 550 |
|
| 551 |
let instantiate_hyps_with_args (do_prove : Id.t list -> unit Proofview.tactic) |
| 552 |
hyps args_id = |
| 553 |
let args = Array.of_list (List.map mkVar args_id) in |
| 554 |
let open Tacticals in |
| 555 |
let instantiate_one_hyp hid = |
| 556 |
tclORELSE0 |
| 557 |
|
| 558 |
(Proofview.Goal.enter (fun g -> |
| 559 |
let prov_hid = Tacmach.pf_get_new_id hid g in |
| 560 |
let c = mkApp (mkVar hid, args) in |
| 561 |
|
| 562 |
tclTYPEOFTHEN c (fun _ _ -> |
| 563 |
tclTHENLIST |
| 564 |
[ pose_proof (Name prov_hid) c |
| 565 |
; thin [hid] |
| 566 |
; rename_hyp [(prov_hid, hid)] ]))) |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
(thin [hid]) |
| 577 |
in |
| 578 |
if List.is_empty args_id then |
| 579 |
tclTHENLIST |
| 580 |
[ tclMAP (fun hyp_id -> h_reduce_with_zeta (Locusops.onHyp hyp_id)) hyps |
| 581 |
; do_prove hyps ] |
| 582 |
else |
| 583 |
tclTHENLIST |
| 584 |
[ tclMAP (fun hyp_id -> h_reduce_with_zeta (Locusops.onHyp hyp_id)) hyps |
| 585 |
; tclMAP instantiate_one_hyp hyps |
| 586 |
; Proofview.Goal.enter (fun g -> |
| 587 |
let all_g_hyps_id = |
| 588 |
List.fold_right Id.Set.add |
| 589 |
(Tacmach.pf_ids_of_hyps g) |
| 590 |
Id.Set.empty |
| 591 |
in |
| 592 |
let remaining_hyps = |
| 593 |
List.filter (fun id -> Id.Set.mem id all_g_hyps_id) hyps |
| 594 |
in |
| 595 |
do_prove remaining_hyps) ] |
| 596 |
|
| 597 |
let build_proof (interactive_proof : bool) (fnames : Constant.t list) ptes_infos |
| 598 |
dyn_infos : unit Proofview.tactic = |
| 599 |
let open Tacticals in |
| 600 |
let rec build_proof_aux do_finalize dyn_infos : unit Proofview.tactic = |
| 601 |
Proofview.Goal.enter (fun g -> |
| 602 |
let env = Proofview.Goal.env g in |
| 603 |
let sigma = Proofview.Goal.sigma g in |
| 604 |
|
| 605 |
match EConstr.kind sigma dyn_infos.info with |
| 606 |
| Case (ci, u, pms, ct, iv, t, cb) -> |
| 607 |
let do_finalize_t dyn_info' = |
| 608 |
Proofview.Goal.enter (fun g -> |
| 609 |
let t = dyn_info'.info in |
| 610 |
let dyn_infos = |
| 611 |
{dyn_info' with info = mkCase (ci, u, pms, ct, iv, t, cb)} |
| 612 |
in |
| 613 |
let g_nb_prod = |
| 614 |
nb_prod (Proofview.Goal.sigma g) (Proofview.Goal.concl g) |
| 615 |
in |
| 616 |
tclTYPEOFTHEN t (fun _ type_of_term -> |
| 617 |
let term_eq = |
| 618 |
make_refl_eq (Lazy.force refl_equal) type_of_term t |
| 619 |
in |
| 620 |
tclTHENLIST |
| 621 |
[ Generalize.generalize (term_eq :: List.map mkVar dyn_infos.rec_hyps) |
| 622 |
; thin dyn_infos.rec_hyps |
| 623 |
; pattern_option [(Locus.AllOccurrencesBut [1], t)] None |
| 624 |
; observe_tac "toto" |
| 625 |
(tclTHENLIST |
| 626 |
[ Simple.case t |
| 627 |
; Proofview.Goal.enter (fun g' -> |
| 628 |
let g'_nb_prod = |
| 629 |
nb_prod (Proofview.Goal.sigma g') |
| 630 |
(Proofview.Goal.concl g') |
| 631 |
in |
| 632 |
let nb_instantiate_partial = |
| 633 |
g'_nb_prod - g_nb_prod |
| 634 |
in |
| 635 |
observe_tac "treat_new_case" |
| 636 |
(treat_new_case ptes_infos |
| 637 |
nb_instantiate_partial |
| 638 |
(build_proof do_finalize) t dyn_infos)) |
| 639 |
]) ])) |
| 640 |
in |
| 641 |
build_proof do_finalize_t {dyn_infos with info = t} |
| 642 |
| Lambda (n, t, b) -> ( |
| 643 |
match EConstr.kind sigma (Proofview.Goal.concl g) with |
| 644 |
| Prod _ -> |
| 645 |
tclTHEN intro |
| 646 |
(Proofview.Goal.enter (fun g' -> |
| 647 |
let env = Proofview.Goal.env g' in |
| 648 |
let sigma = Proofview.Goal.sigma g' in |
| 649 |
let open Context.Named.Declaration in |
| 650 |
let id = get_id @@ List.hd @@ Environ.named_context env in |
| 651 |
let new_term = |
| 652 |
Reductionops.nf_betaiota env sigma (mkApp (dyn_infos.info, [|mkVar id|])) |
| 653 |
in |
| 654 |
let new_infos = {dyn_infos with info = new_term} in |
| 655 |
let do_prove new_hyps = |
| 656 |
build_proof do_finalize |
| 657 |
{ new_infos with |
| 658 |
rec_hyps = new_hyps |
| 659 |
; nb_rec_hyps = List.length new_hyps } |
| 660 |
in |
| 661 |
|
| 662 |
instantiate_hyps_with_args do_prove new_infos.rec_hyps [id] |
| 663 |
)) |
| 664 |
| _ -> do_finalize dyn_infos ) |
| 665 |
| Cast (t, _, _) -> build_proof do_finalize {dyn_infos with info = t} |
| 666 |
| Const _ | Var _ | Meta _ | Evar _ | Sort _ | Construct _ | Ind _ |
| 667 |
|Int _ | Float _ | String _ -> |
| 668 |
do_finalize dyn_infos |
| 669 |
| App (_, _) -> ( |
| 670 |
let f, args = decompose_app_list sigma dyn_infos.info in |
| 671 |
match EConstr.kind sigma f with |
| 672 |
| Int _ -> user_err Pp.(str "integer cannot be applied") |
| 673 |
| Float _ -> user_err Pp.(str "float cannot be applied") |
| 674 |
| String _ -> user_err Pp.(str "string cannot be applied") |
| 675 |
| Array _ -> user_err Pp.(str "array cannot be applied") |
| 676 |
| App _ -> |
| 677 |
assert false |
| 678 |
| Proj _ -> assert false |
| 679 |
| Var _ | Construct _ | Rel _ | Evar _ | Meta _ | Ind _ | Sort _ |
| 680 |
|Prod _ -> |
| 681 |
let new_infos = {dyn_infos with info = (f, args)} in |
| 682 |
build_proof_args env sigma do_finalize new_infos |
| 683 |
| Const (c, _) when not (List.exists (fun kn -> Environ.QConstant.equal env c kn) fnames) -> |
| 684 |
let new_infos = {dyn_infos with info = (f, args)} in |
| 685 |
|
| 686 |
build_proof_args env sigma do_finalize new_infos |
| 687 |
| Const _ -> do_finalize dyn_infos |
| 688 |
| Lambda _ -> |
| 689 |
let new_term = Reductionops.nf_beta env sigma dyn_infos.info in |
| 690 |
build_proof do_finalize {dyn_infos with info = new_term} |
| 691 |
| LetIn _ -> |
| 692 |
let new_infos = |
| 693 |
{ dyn_infos with |
| 694 |
info = Reductionops.nf_betaiotazeta env sigma dyn_infos.info } |
| 695 |
in |
| 696 |
tclTHENLIST |
| 697 |
[ tclMAP |
| 698 |
(fun hyp_id -> h_reduce_with_zeta (Locusops.onHyp hyp_id)) |
| 699 |
dyn_infos.rec_hyps |
| 700 |
; h_reduce_with_zeta Locusops.onConcl |
| 701 |
; build_proof do_finalize new_infos ] |
| 702 |
| Cast (b, _, _) -> build_proof do_finalize {dyn_infos with info = b} |
| 703 |
| Case _ | Fix _ | CoFix _ -> |
| 704 |
let new_finalize dyn_infos = |
| 705 |
let new_infos = {dyn_infos with info = (dyn_infos.info, args)} in |
| 706 |
build_proof_args env sigma do_finalize new_infos |
| 707 |
in |
| 708 |
build_proof new_finalize {dyn_infos with info = f} ) |
| 709 |
| Fix _ | CoFix _ -> |
| 710 |
user_err Pp.(str "Anonymous local (co)fixpoints are not handled yet") |
| 711 |
| Proj _ -> user_err Pp.(str "Prod") |
| 712 |
| Prod _ -> do_finalize dyn_infos |
| 713 |
| LetIn _ -> |
| 714 |
let new_infos = |
| 715 |
{ dyn_infos with |
| 716 |
info = Reductionops.nf_betaiotazeta env sigma dyn_infos.info } |
| 717 |
in |
| 718 |
tclTHENLIST |
| 719 |
[ tclMAP |
| 720 |
(fun hyp_id -> h_reduce_with_zeta (Locusops.onHyp hyp_id)) |
| 721 |
dyn_infos.rec_hyps |
| 722 |
; h_reduce_with_zeta Locusops.onConcl |
| 723 |
; build_proof do_finalize new_infos ] |
| 724 |
| Rel _ -> anomaly (Pp.str "Free var in goal conclusion!") |
| 725 |
| Array _ -> CErrors.user_err Pp.(str "Arrays not handled yet")) |
| 726 |
and build_proof do_finalize dyn_infos = |
| 727 |
|
| 728 |
Indfun_common.observe_tac ~header:(str "observation") |
| 729 |
(fun env sigma -> |
| 730 |
str "build_proof with " ++ pr_leconstr_env env sigma dyn_infos.info) |
| 731 |
(build_proof_aux do_finalize dyn_infos) |
| 732 |
and build_proof_args env sigma do_finalize dyn_infos : unit Proofview.tactic = |
| 733 |
|
| 734 |
Proofview.Goal.enter (fun g -> |
| 735 |
let f_args', args = dyn_infos.info in |
| 736 |
let tac = |
| 737 |
match args with |
| 738 |
| [] -> do_finalize {dyn_infos with info = f_args'} |
| 739 |
| arg :: args -> |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
let do_finalize dyn_infos = |
| 745 |
let new_arg = dyn_infos.info in |
| 746 |
|
| 747 |
build_proof_args env sigma do_finalize |
| 748 |
{dyn_infos with info = (mkApp (f_args', [|new_arg|]), args)} |
| 749 |
in |
| 750 |
build_proof do_finalize {dyn_infos with info = arg} |
| 751 |
in |
| 752 |
tac) |
| 753 |
in |
| 754 |
let do_finish_proof dyn_infos = |
| 755 |
clean_goal_with_heq ptes_infos finish_proof dyn_infos |
| 756 |
in |
| 757 |
|
| 758 |
build_proof (clean_goal_with_heq ptes_infos do_finish_proof) dyn_infos |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
type static_fix_info = |
| 763 |
{ idx : int |
| 764 |
; name : Id.t |
| 765 |
; types : types |
| 766 |
; offset : int |
| 767 |
; nb_realargs : int |
| 768 |
; body_with_param : constr |
| 769 |
; num_in_block : int } |
| 770 |
|
| 771 |
let prove_rec_hyp_for_struct fix_info eq_hyps = |
| 772 |
let open Tacticals in |
| 773 |
tclTHEN |
| 774 |
(rewrite_until_var fix_info.idx eq_hyps) |
| 775 |
(Proofview.Goal.enter (fun g -> |
| 776 |
let _, pte_args = |
| 777 |
destApp (Proofview.Goal.sigma g) (Proofview.Goal.concl g) |
| 778 |
in |
| 779 |
let rec_hyp_proof = |
| 780 |
mkApp (mkVar fix_info.name, array_get_start pte_args) |
| 781 |
in |
| 782 |
Tactics.exact_check rec_hyp_proof)) |
| 783 |
|
| 784 |
let prove_rec_hyp fix_info = |
| 785 |
{proving_tac = prove_rec_hyp_for_struct fix_info; is_valid = (fun _ -> true)} |
| 786 |
|
| 787 |
let generalize_non_dep hyp = |
| 788 |
Proofview.Goal.enter (fun g -> |
| 789 |
|
| 790 |
let hyps = [hyp] in |
| 791 |
let env = Proofview.Goal.env g in |
| 792 |
let sigma = Proofview.Goal.sigma g in |
| 793 |
let hyp_typ = Tacmach.pf_get_hyp_typ hyp g in |
| 794 |
let to_revert, _ = |
| 795 |
let open Context.Named.Declaration in |
| 796 |
Environ.fold_named_context_reverse |
| 797 |
(fun (clear, keep) decl -> |
| 798 |
let decl = EConstr.of_named_decl decl in |
| 799 |
let hyp = get_id decl in |
| 800 |
if |
| 801 |
Id.List.mem hyp hyps |
| 802 |
|| List.exists (Termops.occur_var_in_decl env sigma hyp) keep |
| 803 |
|| Termops.occur_var env sigma hyp hyp_typ |
| 804 |
|| Termops.is_section_variable (Global.env ()) hyp |
| 805 |
|
| 806 |
then (clear, decl :: keep) |
| 807 |
else (hyp :: clear, keep)) |
| 808 |
~init:([], []) env |
| 809 |
in |
| 810 |
|
| 811 |
Tacticals.tclTHEN |
| 812 |
( |
| 813 |
Generalize.generalize (List.map mkVar to_revert)) |
| 814 |
( clear to_revert)) |
| 815 |
|
| 816 |
let id_of_decl = RelDecl.get_name %> Nameops.Name.get_id |
| 817 |
let var_of_decl = id_of_decl %> mkVar |
| 818 |
|
| 819 |
let revert idl = |
| 820 |
Tacticals.tclTHEN (Generalize.generalize (List.map mkVar idl)) (clear idl) |
| 821 |
|
| 822 |
let generate_equation_lemma env evd fnames f fun_num nb_params nb_args rec_args_num |
| 823 |
= |
| 824 |
let open Tacticals in |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
let f_def = Environ.lookup_constant (fst (destConst evd f)) env in |
| 829 |
let eq_lhs = |
| 830 |
mkApp |
| 831 |
( f |
| 832 |
, Array.init (nb_params + nb_args) (fun i -> |
| 833 |
mkRel (nb_params + nb_args - i)) ) |
| 834 |
in |
| 835 |
let f_body = match f_def.const_body with |
| 836 |
| Def d -> d |
| 837 |
| OpaqueDef _ | Primitive _ | Symbol _ | Undef _ -> |
| 838 |
CErrors.user_err (Pp.str "Definition without a body") |
| 839 |
in |
| 840 |
let f_body = EConstr.of_constr f_body in |
| 841 |
let params, f_body_with_params = decompose_lambda_n evd nb_params f_body in |
| 842 |
let (_, num), (_, _, bodies) = destFix evd f_body_with_params in |
| 843 |
let fnames_with_params = |
| 844 |
let params = Array.init nb_params (fun i -> mkRel (nb_params - i)) in |
| 845 |
let fnames = |
| 846 |
List.rev (Array.to_list (Array.map (fun f -> mkApp (f, params)) fnames)) |
| 847 |
in |
| 848 |
fnames |
| 849 |
in |
| 850 |
|
| 851 |
|
| 852 |
let f_body_with_params_and_other_fun = |
| 853 |
substl fnames_with_params bodies.(num) |
| 854 |
in |
| 855 |
|
| 856 |
let eq_rhs = |
| 857 |
Reductionops.nf_betaiotazeta env evd |
| 858 |
(mkApp |
| 859 |
( compose_lam params f_body_with_params_and_other_fun |
| 860 |
, Array.init (nb_params + nb_args) (fun i -> |
| 861 |
mkRel (nb_params + nb_args - i)) )) |
| 862 |
in |
| 863 |
|
| 864 |
let (type_ctxt, type_of_f), evd = |
| 865 |
let evd, t = Typing.type_of ~refresh:true env evd f in |
| 866 |
(decompose_prod_n_decls evd (nb_params + nb_args) t, evd) |
| 867 |
in |
| 868 |
let eqn = mkApp (Lazy.force eq, [|type_of_f; eq_lhs; eq_rhs|]) in |
| 869 |
let lemma_type = it_mkProd_or_LetIn eqn type_ctxt in |
| 870 |
|
| 871 |
let f_id = Constant.label (fst (destConst evd f)) in |
| 872 |
let prove_replacement = |
| 873 |
tclTHENLIST |
| 874 |
[ tclDO (nb_params + rec_args_num + 1) intro |
| 875 |
; observe_tac "" |
| 876 |
(onNthHypId 1 (fun rec_id -> |
| 877 |
tclTHENLIST |
| 878 |
[ observe_tac "generalize_non_dep in generate_equation_lemma" |
| 879 |
(generalize_non_dep rec_id) |
| 880 |
; observe_tac "h_case" (simplest_case (mkVar rec_id)) |
| 881 |
; intros_reflexivity ])) ] |
| 882 |
in |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
let info = Declare.Info.make () in |
| 887 |
let cinfo = |
| 888 |
Declare.CInfo.make ~name:(mk_equation_id f_id) ~typ:lemma_type () |
| 889 |
in |
| 890 |
let lemma = Declare.Proof.start ~cinfo ~info evd in |
| 891 |
let lemma, _ = Declare.Proof.by (Global.env ()) prove_replacement lemma in |
| 892 |
let (_ : _ list) = |
| 893 |
Declare.Proof.save_regular ~proof:lemma ~opaque:Vernacexpr.Transparent |
| 894 |
~idopt:None |
| 895 |
in |
| 896 |
evd |
| 897 |
|
| 898 |
exception NoLemma |
| 899 |
|
| 900 |
let do_replace (evd : Evd.evar_map ref) params rec_arg_num rev_args_id f fun_num |
| 901 |
all_funs = |
| 902 |
Proofview.Goal.enter (fun g -> |
| 903 |
let env = Proofview.Goal.env g in |
| 904 |
let equation_lemma = |
| 905 |
try |
| 906 |
let finfos = |
| 907 |
match find_Function_infos (fst (destConst !evd f)) with |
| 908 |
| None -> raise Not_found |
| 909 |
| Some finfos -> finfos |
| 910 |
in |
| 911 |
let cst = match finfos.equation_lemma with |
| 912 |
| None -> raise NoLemma |
| 913 |
| Some cst -> cst |
| 914 |
in |
| 915 |
mkConst cst |
| 916 |
with (Not_found | NoLemma) as e -> |
| 917 |
let f_id = Constant.label (fst (destConst !evd f)) in |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
let equation_lemma_id = mk_equation_id f_id in |
| 922 |
evd := |
| 923 |
generate_equation_lemma env !evd all_funs f fun_num (List.length params) |
| 924 |
(List.length rev_args_id) rec_arg_num; |
| 925 |
let _ = |
| 926 |
match e with |
| 927 |
| NoLemma -> |
| 928 |
let finfos = |
| 929 |
match find_Function_infos (fst (destConst !evd f)) with |
| 930 |
| None -> raise Not_found |
| 931 |
| Some finfos -> finfos |
| 932 |
in |
| 933 |
update_Function |
| 934 |
{ finfos with |
| 935 |
equation_lemma = |
| 936 |
Some |
| 937 |
( match |
| 938 |
Nametab.locate (qualid_of_ident equation_lemma_id) |
| 939 |
with |
| 940 |
| GlobRef.ConstRef c -> c |
| 941 |
| _ -> CErrors.anomaly (Pp.str "Not a constant.") ) } |
| 942 |
| _ -> () |
| 943 |
in |
| 944 |
|
| 945 |
let env = Global.env () in |
| 946 |
let evd', res = |
| 947 |
Evd.fresh_global env !evd |
| 948 |
(Option.get (Constrintern.locate_reference |
| 949 |
(qualid_of_ident equation_lemma_id))) |
| 950 |
in |
| 951 |
evd := evd'; |
| 952 |
let sigma, _ = |
| 953 |
Typing.type_of ~refresh:true env !evd res |
| 954 |
in |
| 955 |
evd := sigma; |
| 956 |
res |
| 957 |
in |
| 958 |
let nb_intro_to_do = |
| 959 |
nb_prod (Proofview.Goal.sigma g) (Proofview.Goal.concl g) |
| 960 |
in |
| 961 |
let open Tacticals in |
| 962 |
let open Proofview.Notations in |
| 963 |
tclTHEN |
| 964 |
(tclDO nb_intro_to_do intro) |
| 965 |
(Proofview.Goal.enter (fun g' -> |
| 966 |
let just_introduced = Tacticals.nLastDecls g' nb_intro_to_do in |
| 967 |
let open Context.Named.Declaration in |
| 968 |
let just_introduced_id = List.map get_id just_introduced in |
| 969 |
|
| 970 |
(Proofview.Unsafe.tclSETENV (Global.env ())) <*> |
| 971 |
(Equality.rewriteLR equation_lemma) <*> |
| 972 |
(revert just_introduced_id)))) |
| 973 |
|
| 974 |
let prove_princ_for_struct (evd : Evd.evar_map ref) interactive_proof fun_num |
| 975 |
fnames all_funs _nparams : unit Proofview.tactic = |
| 976 |
let open Tacticals in |
| 977 |
Proofview.Goal.enter (fun g -> |
| 978 |
let princ_type = Proofview.Goal.concl g in |
| 979 |
let env = Proofview.Goal.env g in |
| 980 |
let sigma = Proofview.Goal.sigma g in |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
let princ_info = compute_elim_sig sigma princ_type in |
| 985 |
let fresh_id = |
| 986 |
let avoid = ref (Tacmach.pf_ids_of_hyps g) in |
| 987 |
fun na -> |
| 988 |
let new_id = |
| 989 |
match na with |
| 990 |
| Name id -> fresh_id !avoid (Id.to_string id) |
| 991 |
| Anonymous -> fresh_id !avoid "H" |
| 992 |
in |
| 993 |
avoid := new_id :: !avoid; |
| 994 |
Name new_id |
| 995 |
in |
| 996 |
let fresh_decl = RelDecl.map_name fresh_id in |
| 997 |
let princ_info : elim_scheme = |
| 998 |
{ princ_info with |
| 999 |
params = List.map fresh_decl princ_info.params |
| 1000 |
; predicates = List.map fresh_decl princ_info.predicates |
| 1001 |
; branches = List.map fresh_decl princ_info.branches |
| 1002 |
; args = List.map fresh_decl princ_info.args } |
| 1003 |
in |
| 1004 |
let get_body const = |
| 1005 |
let env = Global.env () in |
| 1006 |
let body = Environ.lookup_constant const env in |
| 1007 |
match body.Declarations.const_body with |
| 1008 |
| Def body -> |
| 1009 |
let sigma = Evd.from_env env in |
| 1010 |
Tacred.cbv_norm_flags ~strong:true |
| 1011 |
(RedFlags.mkflags [RedFlags.fZETA]) |
| 1012 |
env sigma (EConstr.of_constr body) |
| 1013 |
| Undef _ | Primitive _ | Symbol _ | OpaqueDef _ -> user_err Pp.(str "Cannot define a principle over an axiom ") |
| 1014 |
in |
| 1015 |
let fbody = get_body fnames.(fun_num) in |
| 1016 |
let f_ctxt, f_body = decompose_lambda sigma fbody in |
| 1017 |
let f_ctxt_length = List.length f_ctxt in |
| 1018 |
let diff_params = princ_info.nparams - f_ctxt_length in |
| 1019 |
let full_params, princ_params, fbody_with_full_params = |
| 1020 |
if diff_params > 0 then |
| 1021 |
let princ_params, full_params = |
| 1022 |
list_chop diff_params princ_info.params |
| 1023 |
in |
| 1024 |
( full_params |
| 1025 |
, |
| 1026 |
princ_params |
| 1027 |
, |
| 1028 |
substl |
| 1029 |
(List.map var_of_decl full_params) |
| 1030 |
f_body ) |
| 1031 |
else |
| 1032 |
let f_ctxt_other, f_ctxt_params = list_chop (-diff_params) f_ctxt in |
| 1033 |
let f_body = compose_lam f_ctxt_other f_body in |
| 1034 |
( princ_info.params |
| 1035 |
, |
| 1036 |
[] |
| 1037 |
, |
| 1038 |
substl |
| 1039 |
(List.map var_of_decl princ_info.params) |
| 1040 |
f_body ) |
| 1041 |
in |
| 1042 |
observe |
| 1043 |
(fun () -> str "full_params := " |
| 1044 |
++ prlist_with_sep spc |
| 1045 |
(RelDecl.get_name %> Nameops.Name.get_id %> Ppconstr.pr_id) |
| 1046 |
full_params ); |
| 1047 |
observe |
| 1048 |
(fun () -> str "princ_params := " |
| 1049 |
++ prlist_with_sep spc |
| 1050 |
(RelDecl.get_name %> Nameops.Name.get_id %> Ppconstr.pr_id) |
| 1051 |
princ_params ); |
| 1052 |
observe |
| 1053 |
(fun () -> str "fbody_with_full_params := " |
| 1054 |
++ pr_leconstr_env (Global.env ()) !evd fbody_with_full_params ); |
| 1055 |
let all_funs_with_full_params = |
| 1056 |
Array.map |
| 1057 |
(fun f -> applist (f, List.rev_map var_of_decl full_params)) |
| 1058 |
all_funs |
| 1059 |
in |
| 1060 |
let fix_offset = List.length princ_params in |
| 1061 |
let ptes_to_fix, infos = |
| 1062 |
match EConstr.kind sigma fbody_with_full_params with |
| 1063 |
| Fix ((idxs, i), (names, typess, bodies)) -> |
| 1064 |
let bodies_with_all_params = |
| 1065 |
Array.map |
| 1066 |
(fun body -> |
| 1067 |
Reductionops.nf_betaiota env sigma |
| 1068 |
(applist |
| 1069 |
( substl |
| 1070 |
(List.rev (Array.to_list all_funs_with_full_params)) |
| 1071 |
body |
| 1072 |
, List.rev_map var_of_decl princ_params ))) |
| 1073 |
bodies |
| 1074 |
in |
| 1075 |
let info_array = |
| 1076 |
Array.mapi |
| 1077 |
(fun i types -> |
| 1078 |
let types = |
| 1079 |
prod_applist sigma types |
| 1080 |
(List.rev_map var_of_decl princ_params) |
| 1081 |
in |
| 1082 |
{ idx = idxs.(i) - fix_offset |
| 1083 |
; name = Nameops.Name.get_id (fresh_id names.(i).binder_name) |
| 1084 |
; types |
| 1085 |
; offset = fix_offset |
| 1086 |
; nb_realargs = |
| 1087 |
List.length (fst (decompose_lambda sigma bodies.(i))) |
| 1088 |
- fix_offset |
| 1089 |
; body_with_param = bodies_with_all_params.(i) |
| 1090 |
; num_in_block = i }) |
| 1091 |
typess |
| 1092 |
in |
| 1093 |
let pte_to_fix, rev_info = |
| 1094 |
List.fold_left_i |
| 1095 |
(fun i (acc_map, acc_info) decl -> |
| 1096 |
let pte = RelDecl.get_name decl in |
| 1097 |
let infos = info_array.(i) in |
| 1098 |
let type_args, _ = decompose_prod sigma infos.types in |
| 1099 |
let nargs = List.length type_args in |
| 1100 |
let f = |
| 1101 |
applist |
| 1102 |
( mkConst fnames.(i) |
| 1103 |
, List.rev_map var_of_decl princ_info.params ) |
| 1104 |
in |
| 1105 |
let first_args = |
| 1106 |
Array.init nargs (fun i -> mkRel (nargs - i)) |
| 1107 |
in |
| 1108 |
let app_f = mkApp (f, first_args) in |
| 1109 |
let pte_args = Array.to_list first_args @ [app_f] in |
| 1110 |
let app_pte = |
| 1111 |
applist (mkVar (Nameops.Name.get_id pte), pte_args) |
| 1112 |
in |
| 1113 |
let body_with_param, num = |
| 1114 |
let body = get_body fnames.(i) in |
| 1115 |
let body_with_full_params = |
| 1116 |
Reductionops.nf_betaiota env sigma |
| 1117 |
(applist (body, List.rev_map var_of_decl full_params)) |
| 1118 |
in |
| 1119 |
match EConstr.kind sigma body_with_full_params with |
| 1120 |
| Fix ((_, num), (_, _, bs)) -> |
| 1121 |
( Reductionops.nf_betaiota env sigma |
| 1122 |
(applist |
| 1123 |
( substl |
| 1124 |
(List.rev |
| 1125 |
(Array.to_list all_funs_with_full_params)) |
| 1126 |
bs.(num) |
| 1127 |
, List.rev_map var_of_decl princ_params )) |
| 1128 |
, num ) |
| 1129 |
| _ -> user_err Pp.(str "Not a mutual block") |
| 1130 |
in |
| 1131 |
let info = |
| 1132 |
{ infos with |
| 1133 |
types = compose_prod type_args app_pte |
| 1134 |
; body_with_param |
| 1135 |
; num_in_block = num } |
| 1136 |
in |
| 1137 |
|
| 1138 |
|
| 1139 |
( Id.Map.add (Nameops.Name.get_id pte) info acc_map |
| 1140 |
, info :: acc_info )) |
| 1141 |
0 (Id.Map.empty, []) |
| 1142 |
(List.rev princ_info.predicates) |
| 1143 |
in |
| 1144 |
(pte_to_fix, List.rev rev_info) |
| 1145 |
| _ -> (Id.Map.empty, []) |
| 1146 |
in |
| 1147 |
let mk_fixes : unit Proofview.tactic = |
| 1148 |
let pre_info, infos = list_chop fun_num infos in |
| 1149 |
match (pre_info, infos) with |
| 1150 |
| _, [] -> Proofview.tclUNIT () |
| 1151 |
| _, this_fix_info :: others_infos -> |
| 1152 |
let other_fix_infos = |
| 1153 |
List.map |
| 1154 |
(fun fi -> (fi.name, fi.idx + 1, fi.types)) |
| 1155 |
(pre_info @ others_infos) |
| 1156 |
in |
| 1157 |
if List.is_empty other_fix_infos then |
| 1158 |
if this_fix_info.idx + 1 = 0 then Proofview.tclUNIT () |
| 1159 |
|
| 1160 |
else |
| 1161 |
Indfun_common.observe_tac ~header:(str "observation") |
| 1162 |
(fun _ _ -> str "h_fix " ++ int (this_fix_info.idx + 1)) |
| 1163 |
(fix this_fix_info.name (this_fix_info.idx + 1)) |
| 1164 |
else |
| 1165 |
FixTactics.mutual_fix this_fix_info.name (this_fix_info.idx + 1) |
| 1166 |
other_fix_infos |
| 1167 |
in |
| 1168 |
let first_tac : unit Proofview.tactic = |
| 1169 |
|
| 1170 |
|
| 1171 |
tclTHENLIST |
| 1172 |
[ observe_tac "introducing params" |
| 1173 |
(intros_mustbe_force (List.rev_map id_of_decl princ_info.params)) |
| 1174 |
; observe_tac "introducing predicates" |
| 1175 |
(intros_mustbe_force |
| 1176 |
(List.rev_map id_of_decl princ_info.predicates)) |
| 1177 |
; observe_tac "introducing branches" |
| 1178 |
(intros_mustbe_force |
| 1179 |
(List.rev_map id_of_decl princ_info.branches)) |
| 1180 |
; observe_tac "building fixes" mk_fixes ] |
| 1181 |
in |
| 1182 |
let intros_after_fixes : unit Proofview.tactic = |
| 1183 |
Proofview.Goal.enter (fun gl -> |
| 1184 |
let sigma = Proofview.Goal.sigma gl in |
| 1185 |
let ccl = Proofview.Goal.concl gl in |
| 1186 |
let ctxt, pte_app = decompose_prod_decls sigma ccl in |
| 1187 |
let pte, pte_args = decompose_app_list sigma pte_app in |
| 1188 |
try |
| 1189 |
let pte = |
| 1190 |
try destVar sigma pte |
| 1191 |
with DestKO -> anomaly (Pp.str "Property is not a variable.") |
| 1192 |
in |
| 1193 |
let fix_info = Id.Map.find pte ptes_to_fix in |
| 1194 |
let nb_args = fix_info.nb_realargs in |
| 1195 |
tclTHENLIST |
| 1196 |
[ |
| 1197 |
tclDO nb_args intro |
| 1198 |
; Proofview.Goal.enter (fun g -> |
| 1199 |
|
| 1200 |
let args = Tacticals.nLastDecls g nb_args in |
| 1201 |
let fix_body = fix_info.body_with_param in |
| 1202 |
|
| 1203 |
let open Context.Named.Declaration in |
| 1204 |
let args_id = List.map get_id args in |
| 1205 |
let dyn_infos = |
| 1206 |
{ nb_rec_hyps = -100 |
| 1207 |
; rec_hyps = [] |
| 1208 |
; info = |
| 1209 |
Reductionops.nf_betaiota (Proofview.Goal.env g) |
| 1210 |
(Proofview.Goal.sigma g) |
| 1211 |
(applist (fix_body, List.rev_map mkVar args_id)) |
| 1212 |
; eq_hyps = [] } |
| 1213 |
in |
| 1214 |
tclTHENLIST |
| 1215 |
[ observe_tac "do_replace" |
| 1216 |
(do_replace evd full_params |
| 1217 |
(fix_info.idx + List.length princ_params) |
| 1218 |
( args_id |
| 1219 |
@ List.map |
| 1220 |
(RelDecl.get_name %> Nameops.Name.get_id) |
| 1221 |
princ_params ) |
| 1222 |
all_funs.(fix_info.num_in_block) |
| 1223 |
fix_info.num_in_block all_funs) |
| 1224 |
; (let do_prove = |
| 1225 |
build_proof interactive_proof |
| 1226 |
(Array.to_list fnames) |
| 1227 |
(Id.Map.map prove_rec_hyp ptes_to_fix) |
| 1228 |
in |
| 1229 |
let prove_tac branches = |
| 1230 |
let dyn_infos = |
| 1231 |
{ dyn_infos with |
| 1232 |
rec_hyps = branches |
| 1233 |
; nb_rec_hyps = List.length branches } |
| 1234 |
in |
| 1235 |
observe_tac "cleaning" |
| 1236 |
(clean_goal_with_heq |
| 1237 |
(Id.Map.map prove_rec_hyp ptes_to_fix) |
| 1238 |
do_prove dyn_infos) |
| 1239 |
in |
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
instantiate_hyps_with_args prove_tac |
| 1247 |
(List.rev_map id_of_decl princ_info.branches) |
| 1248 |
(List.rev args_id)) ]) ] |
| 1249 |
with Not_found -> |
| 1250 |
let nb_args = min princ_info.nargs (List.length ctxt) in |
| 1251 |
tclTHENLIST |
| 1252 |
[ tclDO nb_args intro |
| 1253 |
; Proofview.Goal.enter (fun g -> |
| 1254 |
let env = Proofview.Goal.env g in |
| 1255 |
let sigma = Proofview.Goal.sigma g in |
| 1256 |
|
| 1257 |
let args = Tacticals.nLastDecls g nb_args in |
| 1258 |
let open Context.Named.Declaration in |
| 1259 |
let args_id = List.map get_id args in |
| 1260 |
let dyn_infos = |
| 1261 |
{ nb_rec_hyps = -100 |
| 1262 |
; rec_hyps = [] |
| 1263 |
; info = |
| 1264 |
Reductionops.nf_betaiota env sigma |
| 1265 |
(applist |
| 1266 |
( fbody_with_full_params |
| 1267 |
, List.rev_map var_of_decl princ_params |
| 1268 |
@ List.rev_map mkVar args_id )) |
| 1269 |
; eq_hyps = [] } |
| 1270 |
in |
| 1271 |
let fname = |
| 1272 |
destConst sigma |
| 1273 |
(fst |
| 1274 |
(decompose_app_list sigma (List.hd (List.rev pte_args)))) |
| 1275 |
in |
| 1276 |
tclTHENLIST |
| 1277 |
[ unfold_in_concl |
| 1278 |
[ ( Locus.AllOccurrences |
| 1279 |
, Evaluable.EvalConstRef (fst fname) ) ] |
| 1280 |
; (let do_prove = |
| 1281 |
build_proof interactive_proof |
| 1282 |
(Array.to_list fnames) |
| 1283 |
(Id.Map.map prove_rec_hyp ptes_to_fix) |
| 1284 |
in |
| 1285 |
let prove_tac branches = |
| 1286 |
let dyn_infos = |
| 1287 |
{ dyn_infos with |
| 1288 |
rec_hyps = branches |
| 1289 |
; nb_rec_hyps = List.length branches } |
| 1290 |
in |
| 1291 |
clean_goal_with_heq |
| 1292 |
(Id.Map.map prove_rec_hyp ptes_to_fix) |
| 1293 |
do_prove dyn_infos |
| 1294 |
in |
| 1295 |
instantiate_hyps_with_args prove_tac |
| 1296 |
(List.rev_map id_of_decl princ_info.branches) |
| 1297 |
(List.rev args_id)) ]) ]) |
| 1298 |
in |
| 1299 |
tclTHEN first_tac intros_after_fixes) |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
let prove_with_tcc tcc_lemma_constr eqs : unit Proofview.tactic = |
| 1311 |
let open Tacticals in |
| 1312 |
match !tcc_lemma_constr with |
| 1313 |
| Undefined -> anomaly (Pp.str "No tcc proof !!") |
| 1314 |
| Value lemma -> |
| 1315 |
|
| 1316 |
|
| 1317 |
tclTHENLIST |
| 1318 |
[ |
| 1319 |
|
| 1320 |
|
| 1321 |
tclTRY (list_rewrite true eqs) |
| 1322 |
; |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
Eauto.gen_eauto ~depth:5 [] (Some []) ] |
| 1328 |
| Not_needed -> Proofview.tclUNIT () |
| 1329 |
|
| 1330 |
let backtrack_eqs_until_hrec hrec eqs : unit Proofview.tactic = |
| 1331 |
let open Tacticals in |
| 1332 |
Proofview.Goal.enter (fun gls -> |
| 1333 |
let sigma = Proofview.Goal.sigma gls in |
| 1334 |
let eqs = List.map mkVar eqs in |
| 1335 |
let rewrite = tclFIRST (List.map Equality.rewriteRL eqs) in |
| 1336 |
let _, hrec_concl = |
| 1337 |
decompose_prod sigma (Tacmach.pf_get_hyp_typ hrec gls) |
| 1338 |
in |
| 1339 |
let f_app = Array.last (snd (destApp sigma hrec_concl)) in |
| 1340 |
let f = fst (destApp sigma f_app) in |
| 1341 |
let rec backtrack () : unit Proofview.tactic = |
| 1342 |
Proofview.Goal.enter (fun g -> |
| 1343 |
let sigma = Proofview.Goal.sigma gls in |
| 1344 |
let f_app = |
| 1345 |
Array.last (snd (destApp sigma (Proofview.Goal.concl g))) |
| 1346 |
in |
| 1347 |
match EConstr.kind sigma f_app with |
| 1348 |
| App (f', _) when eq_constr sigma f' f -> Proofview.tclUNIT () |
| 1349 |
| _ -> tclTHEN rewrite (backtrack ())) |
| 1350 |
in |
| 1351 |
backtrack ()) |
| 1352 |
|
| 1353 |
let rec rewrite_eqs_in_eqs eqs = |
| 1354 |
let open Tacticals in |
| 1355 |
match eqs with |
| 1356 |
| [] -> Proofview.tclUNIT () |
| 1357 |
| eq :: eqs -> |
| 1358 |
tclTHEN |
| 1359 |
(tclMAP |
| 1360 |
(fun id -> |
| 1361 |
observe_tac |
| 1362 |
(Format.sprintf "rewrite %s in %s " (Id.to_string eq) |
| 1363 |
(Id.to_string id)) |
| 1364 |
(tclTRY |
| 1365 |
(Equality.general_rewrite ~where:(Some id) ~l2r:true Locus.AllOccurrences ~freeze:true |
| 1366 |
~dep:true ~with_evars:false (mkVar eq, Tactypes.NoBindings)))) |
| 1367 |
eqs) |
| 1368 |
(rewrite_eqs_in_eqs eqs) |
| 1369 |
|
| 1370 |
let new_prove_with_tcc is_mes acc_inv hrec tcc_hyps eqs : unit Proofview.tactic |
| 1371 |
= |
| 1372 |
let open Tacticals in |
| 1373 |
tclTHENLIST |
| 1374 |
[ backtrack_eqs_until_hrec hrec eqs |
| 1375 |
; |
| 1376 |
tclTHENS |
| 1377 |
(apply (mkVar hrec)) |
| 1378 |
[ tclTHENLIST |
| 1379 |
[ keep (tcc_hyps @ eqs) |
| 1380 |
; apply (Lazy.force acc_inv) |
| 1381 |
; ( if is_mes then |
| 1382 |
unfold_in_concl |
| 1383 |
[ ( Locus.AllOccurrences |
| 1384 |
, evaluable_of_global_reference (delayed_force ltof_ref) ) |
| 1385 |
] |
| 1386 |
else Proofview.tclUNIT () ) |
| 1387 |
; observe_tac "rew_and_finish" |
| 1388 |
(tclTHENLIST |
| 1389 |
[ tclTRY |
| 1390 |
(list_rewrite false |
| 1391 |
(List.map (fun v -> (mkVar v, true)) eqs)) |
| 1392 |
; observe_tac "rewrite_eqs_in_eqs" (rewrite_eqs_in_eqs eqs) |
| 1393 |
; observe_tac "finishing using" |
| 1394 |
(tclCOMPLETE |
| 1395 |
(Eauto.eauto_with_bases ~depth:5 |
| 1396 |
[(fun _ sigma -> (sigma, Lazy.force refl_equal))] |
| 1397 |
[Hints.Hint_db.empty TransparentState.empty false])) |
| 1398 |
]) ] ] ] |
| 1399 |
|
| 1400 |
let is_valid_hypothesis sigma predicates_name = |
| 1401 |
let predicates_name = |
| 1402 |
List.fold_right Id.Set.add predicates_name Id.Set.empty |
| 1403 |
in |
| 1404 |
let is_pte typ = |
| 1405 |
if isApp sigma typ then |
| 1406 |
let pte, _ = destApp sigma typ in |
| 1407 |
if isVar sigma pte then Id.Set.mem (destVar sigma pte) predicates_name |
| 1408 |
else false |
| 1409 |
else false |
| 1410 |
in |
| 1411 |
let rec is_valid_hypothesis typ = |
| 1412 |
is_pte typ |
| 1413 |
|| |
| 1414 |
match EConstr.kind sigma typ with |
| 1415 |
| Prod (_, pte, typ') -> is_pte pte && is_valid_hypothesis typ' |
| 1416 |
| _ -> false |
| 1417 |
in |
| 1418 |
is_valid_hypothesis |
| 1419 |
|
| 1420 |
let prove_principle_for_gen (f_ref, functional_ref, eq_ref) tcc_lemma_ref is_mes |
| 1421 |
rec_arg_num rec_arg_type relation = |
| 1422 |
Proofview.Goal.enter (fun gl -> |
| 1423 |
let sigma = Proofview.Goal.sigma gl in |
| 1424 |
let princ_type = Proofview.Goal.concl gl in |
| 1425 |
let princ_info = compute_elim_sig sigma princ_type in |
| 1426 |
let fresh_id = |
| 1427 |
let avoid = ref (Tacmach.pf_ids_of_hyps gl) in |
| 1428 |
fun na -> |
| 1429 |
let new_id = |
| 1430 |
match na with |
| 1431 |
| Name id -> fresh_id !avoid (Id.to_string id) |
| 1432 |
| Anonymous -> fresh_id !avoid "H" |
| 1433 |
in |
| 1434 |
avoid := new_id :: !avoid; |
| 1435 |
Name new_id |
| 1436 |
in |
| 1437 |
let fresh_decl = map_name fresh_id in |
| 1438 |
let princ_info : elim_scheme = |
| 1439 |
{ princ_info with |
| 1440 |
params = List.map fresh_decl princ_info.params |
| 1441 |
; predicates = List.map fresh_decl princ_info.predicates |
| 1442 |
; branches = List.map fresh_decl princ_info.branches |
| 1443 |
; args = List.map fresh_decl princ_info.args } |
| 1444 |
in |
| 1445 |
let wf_tac = |
| 1446 |
if is_mes then fun b -> |
| 1447 |
Recdef.tclUSER_if_not_mes Tacticals.tclIDTAC b None |
| 1448 |
else fun _ -> prove_with_tcc tcc_lemma_ref [] |
| 1449 |
in |
| 1450 |
let real_rec_arg_num = rec_arg_num - princ_info.nparams in |
| 1451 |
let npost_rec_arg = princ_info.nargs - real_rec_arg_num + 1 in |
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
let post_rec_arg, pre_rec_arg = |
| 1461 |
Util.List.chop npost_rec_arg princ_info.args |
| 1462 |
in |
| 1463 |
let rec_arg_id = |
| 1464 |
match List.rev post_rec_arg with |
| 1465 |
| ( LocalAssum ({binder_name = Name id}, _) |
| 1466 |
| LocalDef ({binder_name = Name id}, _, _) ) |
| 1467 |
:: _ -> |
| 1468 |
id |
| 1469 |
| _ -> assert false |
| 1470 |
in |
| 1471 |
|
| 1472 |
let subst_constrs = |
| 1473 |
List.map |
| 1474 |
(get_name %> Nameops.Name.get_id %> mkVar) |
| 1475 |
(pre_rec_arg @ princ_info.params) |
| 1476 |
in |
| 1477 |
let relation = substl subst_constrs relation in |
| 1478 |
let input_type = substl subst_constrs rec_arg_type in |
| 1479 |
let wf_thm_id = |
| 1480 |
Nameops.Name.get_id (fresh_id (Name (Id.of_string "wf_R"))) |
| 1481 |
in |
| 1482 |
let acc_rec_arg_id = |
| 1483 |
Nameops.Name.get_id |
| 1484 |
(fresh_id (Name (Id.of_string ("Acc_" ^ Id.to_string rec_arg_id)))) |
| 1485 |
in |
| 1486 |
let open Tacticals in |
| 1487 |
let revert l = |
| 1488 |
tclTHEN (Generalize.generalize (List.map mkVar l)) (clear l) |
| 1489 |
in |
| 1490 |
let fix_id = Nameops.Name.get_id (fresh_id (Name hrec_id)) in |
| 1491 |
let prove_rec_arg_acc = |
| 1492 |
|
| 1493 |
tclCOMPLETE |
| 1494 |
(tclTHEN |
| 1495 |
(assert_by (Name wf_thm_id) |
| 1496 |
(mkApp (delayed_force well_founded, [|input_type; relation|])) |
| 1497 |
|
| 1498 |
(tclCOMPLETE (wf_tac is_mes))) |
| 1499 |
( |
| 1500 |
|
| 1501 |
Tactics.Simple.apply |
| 1502 |
(mkApp (mkVar wf_thm_id, [|mkVar rec_arg_id|])))) |
| 1503 |
in |
| 1504 |
let args_ids = |
| 1505 |
List.map (get_name %> Nameops.Name.get_id) princ_info.args |
| 1506 |
in |
| 1507 |
let lemma = |
| 1508 |
match !tcc_lemma_ref with |
| 1509 |
| Undefined -> user_err Pp.(str "No tcc proof !!") |
| 1510 |
| Value lemma -> EConstr.of_constr lemma |
| 1511 |
| Not_needed -> |
| 1512 |
EConstr.of_constr |
| 1513 |
( UnivGen.constr_of_monomorphic_global (Global.env ()) |
| 1514 |
@@ Rocqlib.lib_ref "core.True.I" ) |
| 1515 |
in |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
let tcc_list = ref [] in |
| 1527 |
let start_tac = |
| 1528 |
Proofview.Goal.enter (fun gls -> |
| 1529 |
let hyps = Tacmach.pf_ids_of_hyps gls in |
| 1530 |
let hid = |
| 1531 |
next_ident_away_in_goal (Global.env ()) (Id.of_string "prov") |
| 1532 |
(Id.Set.of_list hyps) |
| 1533 |
in |
| 1534 |
tclTHENLIST |
| 1535 |
[ Generalize.generalize [lemma] |
| 1536 |
; Simple.intro hid |
| 1537 |
; Elim.h_decompose_and (mkVar hid) |
| 1538 |
; Proofview.Goal.enter (fun g -> |
| 1539 |
let new_hyps = Tacmach.pf_ids_of_hyps g in |
| 1540 |
tcc_list := |
| 1541 |
List.rev (List.subtract Id.equal new_hyps (hid :: hyps)); |
| 1542 |
if List.is_empty !tcc_list then begin |
| 1543 |
tcc_list := [hid]; |
| 1544 |
Proofview.tclUNIT () |
| 1545 |
end |
| 1546 |
else clear [hid]) ]) |
| 1547 |
in |
| 1548 |
tclTHENLIST |
| 1549 |
[ observe_tac "start_tac" start_tac |
| 1550 |
; h_intros |
| 1551 |
(List.rev_map |
| 1552 |
(get_name %> Nameops.Name.get_id) |
| 1553 |
( princ_info.args @ princ_info.branches @ princ_info.predicates |
| 1554 |
@ princ_info.params )) |
| 1555 |
; assert_by (Name acc_rec_arg_id) |
| 1556 |
(mkApp |
| 1557 |
( delayed_force acc_rel |
| 1558 |
, [|input_type; relation; mkVar rec_arg_id|] )) |
| 1559 |
prove_rec_arg_acc |
| 1560 |
; revert (List.rev (acc_rec_arg_id :: args_ids)) |
| 1561 |
; fix fix_id (List.length args_ids + 1) |
| 1562 |
; h_intros (List.rev (acc_rec_arg_id :: args_ids)) |
| 1563 |
; Equality.rewriteLR (mkConst eq_ref) |
| 1564 |
; Proofview.Goal.enter (fun gl' -> |
| 1565 |
let body = |
| 1566 |
let _, args = |
| 1567 |
destApp (Proofview.Goal.sigma gl') (Proofview.Goal.concl gl') |
| 1568 |
in |
| 1569 |
Array.last args |
| 1570 |
in |
| 1571 |
let body_info rec_hyps = |
| 1572 |
{ nb_rec_hyps = List.length rec_hyps |
| 1573 |
; rec_hyps |
| 1574 |
; eq_hyps = [] |
| 1575 |
; info = body } |
| 1576 |
in |
| 1577 |
let acc_inv = |
| 1578 |
lazy |
| 1579 |
(mkApp |
| 1580 |
( delayed_force acc_inv_id |
| 1581 |
, [|input_type; relation; mkVar rec_arg_id|] )) |
| 1582 |
in |
| 1583 |
let acc_inv = |
| 1584 |
lazy (mkApp (Lazy.force acc_inv, [|mkVar acc_rec_arg_id|])) |
| 1585 |
in |
| 1586 |
let predicates_names = |
| 1587 |
List.map (get_name %> Nameops.Name.get_id) princ_info.predicates |
| 1588 |
in |
| 1589 |
let pte_info = |
| 1590 |
{ proving_tac = |
| 1591 |
(fun eqs -> |
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
new_prove_with_tcc is_mes acc_inv fix_id |
| 1600 |
( !tcc_list |
| 1601 |
@ List.map |
| 1602 |
(get_name %> Nameops.Name.get_id) |
| 1603 |
(princ_info.args @ princ_info.params) |
| 1604 |
@ [acc_rec_arg_id] ) |
| 1605 |
eqs) |
| 1606 |
; is_valid = |
| 1607 |
is_valid_hypothesis (Proofview.Goal.sigma gl') |
| 1608 |
predicates_names } |
| 1609 |
in |
| 1610 |
let ptes_info : pte_info Id.Map.t = |
| 1611 |
List.fold_left |
| 1612 |
(fun map pte_id -> Id.Map.add pte_id pte_info map) |
| 1613 |
Id.Map.empty predicates_names |
| 1614 |
in |
| 1615 |
let make_proof rec_hyps = |
| 1616 |
build_proof false [f_ref] ptes_info (body_info rec_hyps) |
| 1617 |
in |
| 1618 |
|
| 1619 |
instantiate_hyps_with_args make_proof |
| 1620 |
(List.map (get_name %> Nameops.Name.get_id) princ_info.branches) |
| 1621 |
(List.rev args_ids)) ]) |