| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
module CVars = Vars |
| 12 |
open Constr |
| 13 |
open Context |
| 14 |
open EConstr |
| 15 |
open Vars |
| 16 |
open Namegen |
| 17 |
open Environ |
| 18 |
open Pp |
| 19 |
open Names |
| 20 |
open Libnames |
| 21 |
open Nameops |
| 22 |
open CErrors |
| 23 |
open Util |
| 24 |
open UnivGen |
| 25 |
open Tacticals |
| 26 |
open Tactics |
| 27 |
open Nametab |
| 28 |
open Tacred |
| 29 |
open Termops |
| 30 |
open Constrintern |
| 31 |
open Tactypes |
| 32 |
open Genredexpr |
| 33 |
open Equality |
| 34 |
open Eauto |
| 35 |
open Indfun_common |
| 36 |
open Context.Rel.Declaration |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
let rocq_constant s = |
| 41 |
EConstr.of_constr @@ UnivGen.constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref s |
| 42 |
|
| 43 |
let rocq_init_constant s = |
| 44 |
EConstr.of_constr (UnivGen.constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref s) |
| 45 |
|
| 46 |
let find_reference sl s = |
| 47 |
let dp = Names.DirPath.make (List.rev_map Id.of_string sl) in |
| 48 |
locate (make_qualid dp (Id.of_string s)) |
| 49 |
|
| 50 |
let declare_fun name kind ?univs value = |
| 51 |
let ce = Declare.definition_entry ?univs value in |
| 52 |
GlobRef.ConstRef |
| 53 |
(Declare.declare_constant ~name ~kind (Declare.DefinitionEntry ce)) |
| 54 |
|
| 55 |
let defined lemma = |
| 56 |
let (_ : _ list) = |
| 57 |
Declare.Proof.save_regular ~proof:lemma ~opaque:Vernacexpr.Transparent |
| 58 |
~idopt:None |
| 59 |
in |
| 60 |
() |
| 61 |
|
| 62 |
let def_of_const t = |
| 63 |
match Constr.kind t with |
| 64 |
| Const sp -> ( |
| 65 |
try |
| 66 |
match constant_opt_value_in (Global.env ()) sp with |
| 67 |
| Some c -> c |
| 68 |
| _ -> raise Not_found |
| 69 |
with Not_found -> |
| 70 |
anomaly |
| 71 |
( str "Cannot find definition of constant " |
| 72 |
++ Id.print (Constant.label (fst sp)) |
| 73 |
++ str "." ) ) |
| 74 |
| _ -> assert false |
| 75 |
|
| 76 |
let type_of_const env sigma t = |
| 77 |
match EConstr.kind sigma t with |
| 78 |
| Const (sp, u) -> |
| 79 |
let u = EInstance.kind sigma u in |
| 80 |
|
| 81 |
Typeops.type_of_constant_in env (sp, u) |
| 82 |
| _ -> assert false |
| 83 |
|
| 84 |
let constant sl s = UnivGen.constr_of_monomorphic_global (Global.env ()) (find_reference sl s) |
| 85 |
|
| 86 |
let const_of_ref = function |
| 87 |
| GlobRef.ConstRef kn -> kn |
| 88 |
| _ -> anomaly (Pp.str "ConstRef expected.") |
| 89 |
|
| 90 |
|
| 91 |
let pf_get_new_ids idl g = |
| 92 |
let ids = Tacmach.pf_ids_of_hyps g in |
| 93 |
let ids = Id.Set.of_list ids in |
| 94 |
List.fold_right |
| 95 |
(fun id acc -> |
| 96 |
next_global_ident_away (Global.safe_env ()) id (Id.Set.union (Id.Set.of_list acc) ids) :: acc) |
| 97 |
idl [] |
| 98 |
|
| 99 |
let next_ident_away_in_goal ids avoid = |
| 100 |
next_ident_away_in_goal (Global.env ()) ids (Id.Set.of_list avoid) |
| 101 |
|
| 102 |
let h'_id = Id.of_string "h'" |
| 103 |
let teq_id = Id.of_string "teq" |
| 104 |
let ano_id = Id.of_string "anonymous" |
| 105 |
let x_id = Id.of_string "x" |
| 106 |
let k_id = Id.of_string "k" |
| 107 |
let v_id = Id.of_string "v" |
| 108 |
let def_id = Id.of_string "def" |
| 109 |
let p_id = Id.of_string "p" |
| 110 |
let rec_res_id = Id.of_string "rec_res" |
| 111 |
let lt = function () -> rocq_init_constant "num.nat.lt" |
| 112 |
let le = function () -> Rocqlib.lib_ref "num.nat.le" |
| 113 |
let ex = function () -> rocq_init_constant "core.ex.type" |
| 114 |
let nat = function () -> rocq_init_constant "num.nat.type" |
| 115 |
|
| 116 |
let iter_ref () = |
| 117 |
try find_reference ["Recdef"] "iter" |
| 118 |
with Not_found -> user_err Pp.(str "Module Recdef not loaded.") |
| 119 |
|
| 120 |
let iter_rd = function |
| 121 |
| () -> constr_of_monomorphic_global (Global.env ()) (delayed_force iter_ref) |
| 122 |
|
| 123 |
let eq = function () -> rocq_init_constant "core.eq.type" |
| 124 |
let le_lt_SS = function () -> constant ["Recdef"] "le_lt_SS" |
| 125 |
let le_lt_n_Sm = function () -> rocq_constant "num.nat.le_lt_n_Sm" |
| 126 |
let le_trans = function () -> rocq_constant "num.nat.le_trans" |
| 127 |
let le_lt_trans = function () -> rocq_constant "num.nat.le_lt_trans" |
| 128 |
let lt_S_n = function () -> rocq_constant "num.nat.lt_S_n" |
| 129 |
let le_n = function () -> rocq_init_constant "num.nat.le_n" |
| 130 |
|
| 131 |
let rocq_sig_ref = function |
| 132 |
| () -> find_reference ["Corelib"; "Init"; "Specif"] "sig" |
| 133 |
|
| 134 |
let rocq_proj1_sig = lazy (Rocqlib.build_sigma ()).proj1 |
| 135 |
|
| 136 |
let rocq_O = function () -> rocq_init_constant "num.nat.O" |
| 137 |
let rocq_S = function () -> rocq_init_constant "num.nat.S" |
| 138 |
let lt_n_O = function () -> rocq_constant "num.nat.nlt_0_r" |
| 139 |
let max_ref = function () -> find_reference ["Recdef"] "max" |
| 140 |
|
| 141 |
let max_constr = function |
| 142 |
| () -> |
| 143 |
EConstr.of_constr (constr_of_monomorphic_global (Global.env ()) (delayed_force max_ref)) |
| 144 |
|
| 145 |
let f_S t = mkApp (delayed_force rocq_S, [|t|]) |
| 146 |
|
| 147 |
let rec n_x_id ids n = |
| 148 |
if Int.equal n 0 then [] |
| 149 |
else |
| 150 |
let x = next_ident_away_in_goal x_id ids in |
| 151 |
x :: n_x_id (x :: ids) (n - 1) |
| 152 |
|
| 153 |
let simpl_iter clause = |
| 154 |
reduce |
| 155 |
(Lazy |
| 156 |
{ rBeta = true |
| 157 |
; rMatch = true |
| 158 |
; rFix = true |
| 159 |
; rCofix = true |
| 160 |
; rZeta = true |
| 161 |
; rDelta = false |
| 162 |
; rConst = [EvalConstRef (const_of_ref (delayed_force iter_ref))] |
| 163 |
; rStrength = Norm }) |
| 164 |
clause |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
let (value_f : Constr.rel_context -> GlobRef.t -> Constr.t) = |
| 169 |
fun context fterm -> |
| 170 |
let env = Global.env () in |
| 171 |
let sigma = Evd.from_env env in |
| 172 |
let env = Environ.push_rel_context context env in |
| 173 |
let proj = Globnames.destConstRef (Lazy.force rocq_proj1_sig) in |
| 174 |
let proj_body = constant_value_in env (proj, UVars.Instance.empty) in |
| 175 |
let arg = mkApp (mkRef (fterm, EInstance.empty), Context.Rel.instance mkRel 0 context) in |
| 176 |
let t, p = Hipattern.match_sigma env sigma (Retyping.get_type_of env sigma arg) in |
| 177 |
let body = Reduction.beta_applist proj_body (List.map (EConstr.to_constr sigma) [t; p; arg]) in |
| 178 |
Term.it_mkLambda_or_LetIn body context |
| 179 |
|
| 180 |
let (declare_f : |
| 181 |
Id.t -> Decls.logical_kind -> Constr.rel_context -> GlobRef.t -> GlobRef.t) = |
| 182 |
fun f_id kind input_type fterm_ref -> |
| 183 |
declare_fun f_id kind (value_f input_type fterm_ref) |
| 184 |
|
| 185 |
let observe_tac = observe_tac ~header:(Pp.mt ()) |
| 186 |
|
| 187 |
let observe_tclTHENLIST s tacl = |
| 188 |
if do_observe () then |
| 189 |
let rec aux n = function |
| 190 |
| [] -> tclIDTAC |
| 191 |
| [tac] -> |
| 192 |
observe_tac (fun env sigma -> s env sigma ++ spc () ++ int n) tac |
| 193 |
| tac :: tacl -> |
| 194 |
observe_tac |
| 195 |
(fun env sigma -> s env sigma ++ spc () ++ int n) |
| 196 |
(tclTHEN tac (aux (succ n) tacl)) |
| 197 |
in |
| 198 |
aux 0 tacl |
| 199 |
else tclTHENLIST tacl |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
let tclUSER tac is_mes l = |
| 206 |
let open Tacticals in |
| 207 |
let clear_tac = |
| 208 |
match l with |
| 209 |
| None -> tclIDTAC |
| 210 |
| Some l -> tclMAP (fun id -> tclTRY (clear [id])) (List.rev l) |
| 211 |
in |
| 212 |
observe_tclTHENLIST |
| 213 |
(fun _ _ -> str "tclUSER1") |
| 214 |
[ clear_tac |
| 215 |
; ( if is_mes then |
| 216 |
observe_tclTHENLIST |
| 217 |
(fun _ _ -> str "tclUSER2") |
| 218 |
[ unfold_in_concl |
| 219 |
[ ( Locus.AllOccurrences |
| 220 |
, evaluable_of_global_reference |
| 221 |
(delayed_force Indfun_common.ltof_ref) ) ] |
| 222 |
; tac ] |
| 223 |
else tac ) ] |
| 224 |
|
| 225 |
let tclUSER_if_not_mes concl_tac is_mes names_to_suppress = |
| 226 |
if is_mes then |
| 227 |
Tacticals.tclCOMPLETE (Simple.apply (delayed_force well_founded_ltof)) |
| 228 |
else |
| 229 |
|
| 230 |
tclUSER concl_tac is_mes names_to_suppress |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
let check_not_nested env sigma forbidden e = |
| 241 |
let rec check_not_nested e = |
| 242 |
match EConstr.kind sigma e with |
| 243 |
| Rel _ -> () |
| 244 |
| Int _ | Float _ | String _ -> () |
| 245 |
| Var x -> |
| 246 |
if Id.List.mem x forbidden then |
| 247 |
user_err |
| 248 |
(str "check_not_nested: failure " ++ Id.print x ++ str ".") |
| 249 |
| Meta _ | Evar _ | Sort _ -> () |
| 250 |
| Cast (e, _, t) -> check_not_nested e; check_not_nested t |
| 251 |
| Prod (_, t, b) -> check_not_nested t; check_not_nested b |
| 252 |
| Lambda (_, t, b) -> check_not_nested t; check_not_nested b |
| 253 |
| LetIn (_, v, t, b) -> |
| 254 |
check_not_nested t; check_not_nested b; check_not_nested v |
| 255 |
| App (f, l) -> check_not_nested f |
| 256 |
| Array (_u, t, def, ty) -> |
| 257 |
Array.iter check_not_nested t; |
| 258 |
check_not_nested def; |
| 259 |
check_not_nested ty |
| 260 |
| Proj (p, _, c) -> check_not_nested c |
| 261 |
| Const _ -> () |
| 262 |
| Ind _ -> () |
| 263 |
| Construct _ -> () |
| 264 |
| Case (_, _, pms, ((_, t), _), _, e, a) -> |
| 265 |
Array.iter check_not_nested pms; |
| 266 |
check_not_nested t; |
| 267 |
check_not_nested e; |
| 268 |
Array.iter (fun (_, c) -> check_not_nested c) a |
| 269 |
| Fix _ -> user_err Pp.(str "check_not_nested : Fix") |
| 270 |
| CoFix _ -> user_err Pp.(str "check_not_nested : Fix") |
| 271 |
in |
| 272 |
try check_not_nested e |
| 273 |
with UserError p -> |
| 274 |
user_err |
| 275 |
(str "on expr : " ++ Printer.pr_leconstr_env env sigma e ++ str " " ++ p ++ str ".") |
| 276 |
|
| 277 |
|
| 278 |
type 'a infos = |
| 279 |
{ nb_arg : int |
| 280 |
; |
| 281 |
concl_tac : unit Proofview.tactic |
| 282 |
; |
| 283 |
rec_arg_id : Id.t |
| 284 |
; |
| 285 |
is_mes : bool |
| 286 |
; |
| 287 |
ih : Id.t |
| 288 |
; |
| 289 |
f_id : Id.t |
| 290 |
; |
| 291 |
f_constr : constr |
| 292 |
; |
| 293 |
f_terminate : constr |
| 294 |
; |
| 295 |
func : GlobRef.t |
| 296 |
; |
| 297 |
info : 'a |
| 298 |
; is_main_branch : bool |
| 299 |
; |
| 300 |
is_final : bool |
| 301 |
; |
| 302 |
values_and_bounds : (Id.t * Id.t) list |
| 303 |
; eqs : Id.t list |
| 304 |
; forbidden_ids : Id.t list |
| 305 |
; acc_inv : constr lazy_t |
| 306 |
; acc_id : Id.t |
| 307 |
; args_assoc : (constr list * constr) list } |
| 308 |
|
| 309 |
type ('a, 'b) journey_info_tac = |
| 310 |
'a |
| 311 |
-> |
| 312 |
'b infos |
| 313 |
-> |
| 314 |
('b infos -> unit Proofview.tactic) |
| 315 |
-> |
| 316 |
'b infos |
| 317 |
-> |
| 318 |
unit Proofview.tactic |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
type journey_info = |
| 323 |
{ letiN : (Name.t * constr * types * constr, constr) journey_info_tac |
| 324 |
; lambdA : (Name.t * types * constr, constr) journey_info_tac |
| 325 |
; casE : |
| 326 |
( (constr infos -> unit Proofview.tactic) |
| 327 |
-> constr infos |
| 328 |
-> unit Proofview.tactic) |
| 329 |
-> ( case_info |
| 330 |
* (constr * ERelevance.t) |
| 331 |
* case_invert |
| 332 |
* constr |
| 333 |
* constr array |
| 334 |
, constr ) |
| 335 |
journey_info_tac |
| 336 |
; otherS : (unit, constr) journey_info_tac |
| 337 |
; apP : (constr * constr list, constr) journey_info_tac |
| 338 |
; app_reC : (constr * constr list, constr) journey_info_tac |
| 339 |
; message : string } |
| 340 |
|
| 341 |
let add_vars sigma forbidden e = |
| 342 |
let rec aux forbidden e = |
| 343 |
match EConstr.kind sigma e with |
| 344 |
| Var x -> x :: forbidden |
| 345 |
| _ -> EConstr.fold sigma aux forbidden e |
| 346 |
in |
| 347 |
aux forbidden e |
| 348 |
|
| 349 |
let treat_case forbid_new_ids to_intros finalize_tac nb_lam e infos : |
| 350 |
unit Proofview.tactic = |
| 351 |
Proofview.Goal.enter (fun g -> |
| 352 |
let rev_context, b = decompose_lambda_n (Proofview.Goal.sigma g) nb_lam e in |
| 353 |
let ids = |
| 354 |
List.fold_left |
| 355 |
(fun acc (na, _) -> |
| 356 |
let pre_id = |
| 357 |
match na.binder_name with Name x -> x | Anonymous -> ano_id |
| 358 |
in |
| 359 |
pre_id :: acc) |
| 360 |
[] rev_context |
| 361 |
in |
| 362 |
let rev_ids = pf_get_new_ids (List.rev ids) g in |
| 363 |
let new_b = substl (List.map mkVar rev_ids) b in |
| 364 |
observe_tclTHENLIST |
| 365 |
(fun _ _ -> str "treat_case1") |
| 366 |
[ h_intros (List.rev rev_ids) |
| 367 |
; intro_using_then teq_id (fun _ -> Proofview.tclUNIT ()) |
| 368 |
; Tacticals.onLastHypId (fun heq -> |
| 369 |
observe_tclTHENLIST |
| 370 |
(fun _ _ -> str "treat_case2") |
| 371 |
[ clear to_intros |
| 372 |
; h_intros to_intros |
| 373 |
; Proofview.Goal.enter (fun g' -> |
| 374 |
let sigma = Proofview.Goal.sigma g' in |
| 375 |
let ty_teq = Tacmach.pf_get_hyp_typ heq g' in |
| 376 |
let teq_lhs, teq_rhs = |
| 377 |
let _, args = |
| 378 |
try destApp sigma ty_teq with DestKO -> assert false |
| 379 |
in |
| 380 |
(args.(1), args.(2)) |
| 381 |
in |
| 382 |
let new_b' = |
| 383 |
Termops.replace_term sigma teq_lhs teq_rhs new_b |
| 384 |
in |
| 385 |
let new_infos = |
| 386 |
{ infos with |
| 387 |
info = new_b' |
| 388 |
; eqs = heq :: infos.eqs |
| 389 |
; forbidden_ids = |
| 390 |
( if forbid_new_ids then |
| 391 |
add_vars sigma infos.forbidden_ids new_b' |
| 392 |
else infos.forbidden_ids ) } |
| 393 |
in |
| 394 |
finalize_tac new_infos) ]) ]) |
| 395 |
|
| 396 |
let rec travel_aux jinfo continuation_tac (expr_info : constr infos) = |
| 397 |
Proofview.Goal.enter (fun g -> |
| 398 |
let sigma = Proofview.Goal.sigma g in |
| 399 |
let env = Proofview.Goal.env g in |
| 400 |
match EConstr.kind sigma expr_info.info with |
| 401 |
| CoFix _ | Fix _ -> |
| 402 |
user_err Pp.(str "Function cannot treat local fixpoint or cofixpoint") |
| 403 |
| Array _ -> user_err Pp.(str "Function cannot treat arrays") |
| 404 |
| Proj _ -> user_err Pp.(str "Function cannot treat projections") |
| 405 |
| LetIn (na, b, t, e) -> |
| 406 |
let new_continuation_tac = |
| 407 |
jinfo.letiN (na.binder_name, b, t, e) expr_info continuation_tac |
| 408 |
in |
| 409 |
travel jinfo new_continuation_tac |
| 410 |
{expr_info with info = b; is_final = false} |
| 411 |
| Rel _ -> anomaly (Pp.str "Free var in goal conclusion!") |
| 412 |
| Prod _ -> ( |
| 413 |
try |
| 414 |
check_not_nested env sigma |
| 415 |
(expr_info.f_id :: expr_info.forbidden_ids) |
| 416 |
expr_info.info; |
| 417 |
jinfo.otherS () expr_info continuation_tac expr_info |
| 418 |
with e when CErrors.noncritical e -> |
| 419 |
user_err |
| 420 |
( str "the term " |
| 421 |
++ Printer.pr_leconstr_env env sigma expr_info.info |
| 422 |
++ str " can not contain a recursive call to " |
| 423 |
++ Id.print expr_info.f_id ) ) |
| 424 |
| Lambda (n, t, b) -> ( |
| 425 |
try |
| 426 |
check_not_nested env sigma |
| 427 |
(expr_info.f_id :: expr_info.forbidden_ids) |
| 428 |
expr_info.info; |
| 429 |
jinfo.otherS () expr_info continuation_tac expr_info |
| 430 |
with e when CErrors.noncritical e -> |
| 431 |
user_err |
| 432 |
( str "the term " |
| 433 |
++ Printer.pr_leconstr_env env sigma expr_info.info |
| 434 |
++ str " can not contain a recursive call to " |
| 435 |
++ Id.print expr_info.f_id ++ str ".") ) |
| 436 |
| Case (ci, u, pms, t, iv, a, l) -> |
| 437 |
let (ci, t, iv, a, l) = EConstr.expand_case env sigma (ci, u, pms, t, iv, a, l) in |
| 438 |
let continuation_tac_a = |
| 439 |
jinfo.casE (travel jinfo) (ci, t, iv, a, l) expr_info continuation_tac |
| 440 |
in |
| 441 |
travel jinfo continuation_tac_a |
| 442 |
{expr_info with info = a; is_main_branch = false; is_final = false} |
| 443 |
| App _ -> ( |
| 444 |
let f, args = decompose_app_list sigma expr_info.info in |
| 445 |
if EConstr.eq_constr sigma f expr_info.f_constr then |
| 446 |
jinfo.app_reC (f, args) expr_info continuation_tac expr_info |
| 447 |
else |
| 448 |
match EConstr.kind sigma f with |
| 449 |
| App _ -> assert false |
| 450 |
| Const _ | Construct _ | Rel _ | Evar _ | Meta _ | Ind _ | Sort _ |
| 451 |
| Prod _ | Var _ -> |
| 452 |
let new_infos = {expr_info with info = (f, args)} in |
| 453 |
let new_continuation_tac = |
| 454 |
jinfo.apP (f, args) expr_info continuation_tac |
| 455 |
in |
| 456 |
travel_args jinfo expr_info.is_main_branch new_continuation_tac |
| 457 |
new_infos |
| 458 |
| Case _ -> |
| 459 |
user_err |
| 460 |
( str "the term " |
| 461 |
++ Printer.pr_leconstr_env env sigma expr_info.info |
| 462 |
++ str |
| 463 |
" can not contain an applied match (See Limitation in \ |
| 464 |
Section 2.3 of refman)." ) |
| 465 |
| Fix _ -> user_err Pp.(str "Function cannot treat local fixpoint or cofixpoint") |
| 466 |
| Proj _ -> user_err Pp.(str "Function cannot treat projections") |
| 467 |
| Lambda _ | Cast _ | LetIn _ |
| 468 |
| CoFix _ | Array _ | Int _ | Float _ | String _ -> |
| 469 |
anomaly |
| 470 |
( Pp.str "travel_aux : unexpected " |
| 471 |
++ Printer.pr_leconstr_env env sigma expr_info.info |
| 472 |
++ Pp.str "." ) ) |
| 473 |
| Cast (t, _, _) -> travel jinfo continuation_tac {expr_info with info = t} |
| 474 |
| Const _ | Var _ | Meta _ | Evar _ | Sort _ | Construct _ | Ind _ |
| 475 |
|Int _ | Float _ | String _ -> |
| 476 |
let new_continuation_tac = jinfo.otherS () expr_info continuation_tac in |
| 477 |
new_continuation_tac expr_info) |
| 478 |
|
| 479 |
and travel_args jinfo is_final continuation_tac infos = |
| 480 |
let f_args', args = infos.info in |
| 481 |
match args with |
| 482 |
| [] -> continuation_tac {infos with info = f_args'; is_final} |
| 483 |
| arg :: args' -> |
| 484 |
let new_continuation_tac new_infos = |
| 485 |
let new_arg = new_infos.info in |
| 486 |
travel_args jinfo is_final continuation_tac |
| 487 |
{new_infos with info = (mkApp (f_args', [|new_arg|]), args')} |
| 488 |
in |
| 489 |
travel jinfo new_continuation_tac {infos with info = arg; is_final = false} |
| 490 |
|
| 491 |
and travel jinfo continuation_tac expr_info = |
| 492 |
observe_tac |
| 493 |
(fun env sigma -> |
| 494 |
str jinfo.message ++ Printer.pr_leconstr_env env sigma expr_info.info) |
| 495 |
(travel_aux jinfo continuation_tac expr_info) |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
let rec prove_lt hyple = |
| 500 |
Proofview.Goal.enter (fun g -> |
| 501 |
let sigma = Proofview.Goal.sigma g in |
| 502 |
try |
| 503 |
let varx, varz = |
| 504 |
match decompose_app_list sigma (Proofview.Goal.concl g) with |
| 505 |
| _, x :: z :: _ when isVar sigma x && isVar sigma z -> (x, z) |
| 506 |
| _ -> assert false |
| 507 |
in |
| 508 |
let h = |
| 509 |
List.find |
| 510 |
(fun id -> |
| 511 |
match decompose_app_list sigma (Tacmach.pf_get_hyp_typ id g) with |
| 512 |
| _, t :: _ -> EConstr.eq_constr sigma t varx |
| 513 |
| _ -> false) |
| 514 |
hyple |
| 515 |
in |
| 516 |
let y = |
| 517 |
List.hd |
| 518 |
(List.tl |
| 519 |
(snd (decompose_app_list sigma (Tacmach.pf_get_hyp_typ h g)))) |
| 520 |
in |
| 521 |
observe_tclTHENLIST |
| 522 |
(fun _ _ -> str "prove_lt1") |
| 523 |
[ apply (mkApp (le_lt_trans (), [|varx; y; varz; mkVar h|])) |
| 524 |
; observe_tac (fun _ _ -> str "prove_lt") (prove_lt hyple) ] |
| 525 |
with Not_found -> |
| 526 |
observe_tclTHENLIST |
| 527 |
(fun _ _ -> str "prove_lt2") |
| 528 |
[ apply (delayed_force lt_S_n) |
| 529 |
; observe_tac |
| 530 |
(fun _ _ -> |
| 531 |
str "assumption: " |
| 532 |
++ Printer.Debug.pr_goal g) |
| 533 |
assumption ]) |
| 534 |
|
| 535 |
let default_full_auto = |
| 536 |
Auto.gen_auto (Some Auto.default_search_depth) [] None |
| 537 |
|
| 538 |
let rec destruct_bounds_aux infos (bound, hyple, rechyps) lbounds = |
| 539 |
let open Tacticals in |
| 540 |
Proofview.Goal.enter (fun g -> |
| 541 |
match lbounds with |
| 542 |
| [] -> |
| 543 |
let ids = Tacmach.pf_ids_of_hyps g in |
| 544 |
let s_max = mkApp (delayed_force rocq_S, [|bound|]) in |
| 545 |
let k = next_ident_away_in_goal k_id ids in |
| 546 |
let ids = k :: ids in |
| 547 |
let h' = next_ident_away_in_goal h'_id ids in |
| 548 |
let ids = h' :: ids in |
| 549 |
let def = next_ident_away_in_goal def_id ids in |
| 550 |
observe_tclTHENLIST |
| 551 |
(fun _ _ -> str "destruct_bounds_aux1") |
| 552 |
[ split (ImplicitBindings [s_max]) |
| 553 |
; intro_then (fun id -> |
| 554 |
observe_tac |
| 555 |
(fun _ _ -> str "destruct_bounds_aux") |
| 556 |
(tclTHENS |
| 557 |
(simplest_case (mkVar id)) |
| 558 |
[ observe_tclTHENLIST |
| 559 |
(fun _ _ -> str "") |
| 560 |
[ intro_using_then h_id |
| 561 |
|
| 562 |
|
| 563 |
(fun _ -> Proofview.tclUNIT ()) |
| 564 |
; simplest_elim |
| 565 |
(mkApp (delayed_force lt_n_O, [|s_max|])) |
| 566 |
; default_full_auto ] |
| 567 |
; observe_tclTHENLIST |
| 568 |
(fun _ _ -> str "destruct_bounds_aux2") |
| 569 |
[ observe_tac |
| 570 |
(fun _ _ -> str "clearing k ") |
| 571 |
(clear [id]) |
| 572 |
; h_intros [k; h'; def] |
| 573 |
; observe_tac |
| 574 |
(fun _ _ -> str "simple_iter") |
| 575 |
(simpl_iter Locusops.onConcl) |
| 576 |
; observe_tac |
| 577 |
(fun _ _ -> str "unfold functional") |
| 578 |
(unfold_in_concl |
| 579 |
[ ( Locus.OnlyOccurrences [1] |
| 580 |
, evaluable_of_global_reference infos.func ) |
| 581 |
]) |
| 582 |
; observe_tclTHENLIST |
| 583 |
(fun _ _ -> str "test") |
| 584 |
[ list_rewrite true |
| 585 |
(List.fold_right |
| 586 |
(fun e acc -> (mkVar e, true) :: acc) |
| 587 |
infos.eqs |
| 588 |
(List.map (fun e -> (e, true)) rechyps)) |
| 589 |
; |
| 590 |
|
| 591 |
|
| 592 |
observe_tac |
| 593 |
(fun _ _ -> str "finishing") |
| 594 |
(tclORELSE intros_reflexivity |
| 595 |
(observe_tac |
| 596 |
(fun _ _ -> str "calling prove_lt") |
| 597 |
(prove_lt hyple))) ] ] ])) ] |
| 598 |
| (_, v_bound) :: l -> |
| 599 |
observe_tclTHENLIST |
| 600 |
(fun _ _ -> str "destruct_bounds_aux3") |
| 601 |
[ simplest_elim (mkVar v_bound) |
| 602 |
; clear [v_bound] |
| 603 |
; tclDO 2 intro |
| 604 |
; onNthHypId 1 (fun p_hyp -> |
| 605 |
onNthHypId 2 (fun p -> |
| 606 |
observe_tclTHENLIST |
| 607 |
(fun _ _ -> str "destruct_bounds_aux4") |
| 608 |
[ simplest_elim |
| 609 |
(mkApp (delayed_force max_constr, [|bound; mkVar p|])) |
| 610 |
; tclDO 3 intro |
| 611 |
; onNLastHypsId 3 (fun lids -> |
| 612 |
match lids with |
| 613 |
| [hle2; hle1; pmax] -> |
| 614 |
destruct_bounds_aux infos |
| 615 |
( mkVar pmax |
| 616 |
, hle1 :: hle2 :: hyple |
| 617 |
, mkVar p_hyp :: rechyps ) |
| 618 |
l |
| 619 |
| _ -> assert false) ])) ]) |
| 620 |
|
| 621 |
let destruct_bounds infos = |
| 622 |
destruct_bounds_aux infos |
| 623 |
(delayed_force rocq_O, [], []) |
| 624 |
infos.values_and_bounds |
| 625 |
|
| 626 |
let terminate_app f_and_args expr_info continuation_tac infos = |
| 627 |
if expr_info.is_final && expr_info.is_main_branch then |
| 628 |
observe_tclTHENLIST |
| 629 |
(fun _ _ -> str "terminate_app1") |
| 630 |
[ continuation_tac infos |
| 631 |
; observe_tac |
| 632 |
(fun _ _ -> str "first split") |
| 633 |
(split (ImplicitBindings [infos.info])) |
| 634 |
; observe_tac |
| 635 |
(fun _ _ -> str "destruct_bounds (1)") |
| 636 |
(destruct_bounds infos) ] |
| 637 |
else continuation_tac infos |
| 638 |
|
| 639 |
let terminate_others _ expr_info continuation_tac infos = |
| 640 |
if expr_info.is_final && expr_info.is_main_branch then |
| 641 |
observe_tclTHENLIST |
| 642 |
(fun _ _ -> str "terminate_others") |
| 643 |
[ continuation_tac infos |
| 644 |
; observe_tac |
| 645 |
(fun _ _ -> str "first split") |
| 646 |
(split (ImplicitBindings [infos.info])) |
| 647 |
; observe_tac |
| 648 |
(fun _ _ -> str "destruct_bounds") |
| 649 |
(destruct_bounds infos) ] |
| 650 |
else continuation_tac infos |
| 651 |
|
| 652 |
let terminate_letin (na, b, t, e) expr_info continuation_tac info = |
| 653 |
Proofview.Goal.enter (fun g -> |
| 654 |
let sigma = Proofview.Goal.sigma g in |
| 655 |
let env = Proofview.Goal.env g in |
| 656 |
let new_e = subst1 info.info e in |
| 657 |
let new_forbidden = |
| 658 |
let forbid = |
| 659 |
try |
| 660 |
check_not_nested env sigma |
| 661 |
(expr_info.f_id :: expr_info.forbidden_ids) |
| 662 |
b; |
| 663 |
true |
| 664 |
with e when CErrors.noncritical e -> false |
| 665 |
in |
| 666 |
if forbid then |
| 667 |
match na with |
| 668 |
| Anonymous -> info.forbidden_ids |
| 669 |
| Name id -> id :: info.forbidden_ids |
| 670 |
else info.forbidden_ids |
| 671 |
in |
| 672 |
continuation_tac {info with info = new_e; forbidden_ids = new_forbidden}) |
| 673 |
|
| 674 |
let pf_type c tac = |
| 675 |
let open Tacticals in |
| 676 |
Proofview.Goal.enter (fun gl -> |
| 677 |
let env = Proofview.Goal.env gl in |
| 678 |
let sigma = Proofview.Goal.sigma gl in |
| 679 |
let evars, ty = Typing.type_of env sigma c in |
| 680 |
tclTHEN (Proofview.Unsafe.tclEVARS evars) (tac ty)) |
| 681 |
|
| 682 |
let pf_typel l tac = |
| 683 |
let rec aux tys l = |
| 684 |
match l with |
| 685 |
| [] -> tac (List.rev tys) |
| 686 |
| hd :: tl -> pf_type hd (fun ty -> aux (ty :: tys) tl) |
| 687 |
in |
| 688 |
aux [] l |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
let mkDestructEq not_on_hyp env sigma expr = |
| 696 |
let hyps = EConstr.named_context env in |
| 697 |
let to_revert = |
| 698 |
Util.List.map_filter |
| 699 |
(fun decl -> |
| 700 |
let open Context.Named.Declaration in |
| 701 |
let id = get_id decl in |
| 702 |
if |
| 703 |
Id.List.mem id not_on_hyp |
| 704 |
|| not (Termops.dependent sigma expr (get_type decl)) |
| 705 |
then None |
| 706 |
else Some id) |
| 707 |
hyps |
| 708 |
in |
| 709 |
let to_revert_constr = List.rev_map mkVar to_revert in |
| 710 |
let sigma, type_of_expr = Typing.type_of env sigma expr in |
| 711 |
let new_hyps = |
| 712 |
mkApp (Lazy.force refl_equal, [|type_of_expr; expr|]) :: to_revert_constr |
| 713 |
in |
| 714 |
let tac = |
| 715 |
pf_typel new_hyps (fun _ -> |
| 716 |
observe_tclTHENLIST |
| 717 |
(fun _ _ -> str "mkDestructEq") |
| 718 |
[ Generalize.generalize new_hyps |
| 719 |
; Proofview.Goal.enter (fun g2 -> |
| 720 |
let changefun patvars env sigma = |
| 721 |
pattern_occs |
| 722 |
[(Locus.AllOccurrencesBut [1], expr)] |
| 723 |
(Proofview.Goal.env g2) sigma (Proofview.Goal.concl g2) |
| 724 |
in |
| 725 |
change_in_concl ~check:true None changefun) |
| 726 |
; simplest_case expr ]) |
| 727 |
in |
| 728 |
(sigma, tac, to_revert) |
| 729 |
|
| 730 |
let terminate_case next_step (ci, a, iv, t, l) expr_info continuation_tac infos |
| 731 |
= |
| 732 |
let open Tacticals in |
| 733 |
Proofview.Goal.enter (fun g -> |
| 734 |
let sigma = Proofview.Goal.sigma g in |
| 735 |
let env = Proofview.Goal.env g in |
| 736 |
let f_is_present = |
| 737 |
try |
| 738 |
check_not_nested env sigma |
| 739 |
(expr_info.f_id :: expr_info.forbidden_ids) |
| 740 |
(fst a); |
| 741 |
false |
| 742 |
with e when CErrors.noncritical e -> true |
| 743 |
in |
| 744 |
let a' = infos.info in |
| 745 |
let new_info = |
| 746 |
{ infos with |
| 747 |
info = mkCase (EConstr.contract_case env sigma (ci, a, iv, a', l)) |
| 748 |
; is_main_branch = expr_info.is_main_branch |
| 749 |
; is_final = expr_info.is_final } |
| 750 |
in |
| 751 |
let sigma, destruct_tac, rev_to_thin_intro = |
| 752 |
mkDestructEq [expr_info.rec_arg_id] env sigma a' |
| 753 |
in |
| 754 |
let to_thin_intro = List.rev rev_to_thin_intro in |
| 755 |
observe_tac |
| 756 |
(fun _ _ -> |
| 757 |
str "treating cases (" |
| 758 |
++ int (Array.length l) |
| 759 |
++ str ")" ++ spc () |
| 760 |
++ Printer.pr_leconstr_env env sigma a') |
| 761 |
( |
| 762 |
|
| 763 |
tclTHENS destruct_tac |
| 764 |
(List.map_i |
| 765 |
(fun i e -> |
| 766 |
observe_tac |
| 767 |
(fun _ _ -> str "do treat case") |
| 768 |
(treat_case f_is_present to_thin_intro |
| 769 |
(next_step continuation_tac) |
| 770 |
ci.ci_cstr_ndecls.(i) e new_info)) |
| 771 |
0 (Array.to_list l)))) |
| 772 |
|
| 773 |
let terminate_app_rec (f, args) expr_info continuation_tac _ = |
| 774 |
let open Tacticals in |
| 775 |
Proofview.Goal.enter (fun g -> |
| 776 |
let sigma = Proofview.Goal.sigma g in |
| 777 |
let env = Proofview.Goal.env g in |
| 778 |
List.iter |
| 779 |
(check_not_nested env sigma (expr_info.f_id :: expr_info.forbidden_ids)) |
| 780 |
args; |
| 781 |
try |
| 782 |
let v = |
| 783 |
List.assoc_f |
| 784 |
(List.equal (EConstr.eq_constr sigma)) |
| 785 |
args expr_info.args_assoc |
| 786 |
in |
| 787 |
let new_infos = {expr_info with info = v} in |
| 788 |
observe_tclTHENLIST |
| 789 |
(fun _ _ -> str "terminate_app_rec") |
| 790 |
[ continuation_tac new_infos |
| 791 |
; ( if expr_info.is_final && expr_info.is_main_branch then |
| 792 |
observe_tclTHENLIST |
| 793 |
(fun _ _ -> str "terminate_app_rec1") |
| 794 |
[ observe_tac |
| 795 |
(fun _ _ -> str "first split") |
| 796 |
(split (ImplicitBindings [new_infos.info])) |
| 797 |
; observe_tac |
| 798 |
(fun _ _ -> str "destruct_bounds (3)") |
| 799 |
(destruct_bounds new_infos) ] |
| 800 |
else Proofview.tclUNIT () ) ] |
| 801 |
with Not_found -> |
| 802 |
observe_tac |
| 803 |
(fun _ _ -> str "terminate_app_rec not found") |
| 804 |
(tclTHENS |
| 805 |
(simplest_elim (mkApp (mkVar expr_info.ih, Array.of_list args))) |
| 806 |
[ observe_tclTHENLIST |
| 807 |
(fun _ _ -> str "terminate_app_rec2") |
| 808 |
[ intro_using_then rec_res_id |
| 809 |
|
| 810 |
(fun _ -> Proofview.tclUNIT ()) |
| 811 |
; intro |
| 812 |
; onNthHypId 1 (fun v_bound -> |
| 813 |
onNthHypId 2 (fun v -> |
| 814 |
let new_infos = |
| 815 |
{ expr_info with |
| 816 |
info = mkVar v |
| 817 |
; values_and_bounds = |
| 818 |
(v, v_bound) :: expr_info.values_and_bounds |
| 819 |
; args_assoc = |
| 820 |
(args, mkVar v) :: expr_info.args_assoc } |
| 821 |
in |
| 822 |
observe_tclTHENLIST |
| 823 |
(fun _ _ -> str "terminate_app_rec3") |
| 824 |
[ continuation_tac new_infos |
| 825 |
; ( if |
| 826 |
expr_info.is_final && expr_info.is_main_branch |
| 827 |
then |
| 828 |
observe_tclTHENLIST |
| 829 |
(fun _ _ -> str "terminate_app_rec4") |
| 830 |
[ observe_tac |
| 831 |
(fun _ _ -> str "first split") |
| 832 |
(split |
| 833 |
(ImplicitBindings [new_infos.info])) |
| 834 |
; observe_tac |
| 835 |
(fun _ _ -> str "destruct_bounds (2)") |
| 836 |
(destruct_bounds new_infos) ] |
| 837 |
else Proofview.tclUNIT () ) ])) ] |
| 838 |
; observe_tac |
| 839 |
(fun _ _ -> str "proving decreasing") |
| 840 |
(tclTHENS |
| 841 |
(apply (Lazy.force expr_info.acc_inv)) |
| 842 |
[ observe_tac (fun _ _ -> str "assumption") assumption |
| 843 |
; observe_tclTHENLIST |
| 844 |
(fun _ _ -> str "terminate_app_rec5") |
| 845 |
[ tclTRY |
| 846 |
(list_rewrite true |
| 847 |
(List.map |
| 848 |
(fun e -> (mkVar e, true)) |
| 849 |
expr_info.eqs)) |
| 850 |
; tclUSER expr_info.concl_tac true |
| 851 |
(Some |
| 852 |
( expr_info.ih :: expr_info.acc_id |
| 853 |
:: (fun (x, y) -> y) |
| 854 |
(List.split expr_info.values_and_bounds) )) |
| 855 |
] ]) ])) |
| 856 |
|
| 857 |
let terminate_info = |
| 858 |
{ message = "prove_terminate with term " |
| 859 |
; letiN = terminate_letin |
| 860 |
; lambdA = (fun _ _ _ _ -> assert false) |
| 861 |
; casE = terminate_case |
| 862 |
; otherS = terminate_others |
| 863 |
; apP = terminate_app |
| 864 |
; app_reC = terminate_app_rec } |
| 865 |
|
| 866 |
let prove_terminate = travel terminate_info |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
let equation_case next_step case expr_info continuation_tac infos = |
| 871 |
observe_tac |
| 872 |
(fun _ _ -> str "equation case") |
| 873 |
(terminate_case next_step case expr_info continuation_tac infos) |
| 874 |
|
| 875 |
let rec prove_le () = |
| 876 |
let open Tacticals in |
| 877 |
Proofview.Goal.enter (fun g -> |
| 878 |
let env = Proofview.Goal.env g in |
| 879 |
let sigma = Proofview.Goal.sigma g in |
| 880 |
let x, z = |
| 881 |
let _, args = decompose_app_list sigma (Proofview.Goal.concl g) in |
| 882 |
(List.hd args, List.hd (List.tl args)) |
| 883 |
in |
| 884 |
tclFIRST |
| 885 |
[ assumption |
| 886 |
; apply (delayed_force le_n) |
| 887 |
; begin |
| 888 |
try |
| 889 |
let matching_fun decl = |
| 890 |
let t = Context.Named.Declaration.get_type decl in |
| 891 |
match EConstr.kind sigma t with |
| 892 |
| App (c, [|x0; _|]) -> |
| 893 |
if EConstr.isVar sigma x0 |
| 894 |
&& Id.equal (destVar sigma x0) (destVar sigma x) |
| 895 |
&& EConstr.isRefX env sigma (le ()) c |
| 896 |
then Some (Context.Named.Declaration.get_id decl, t) |
| 897 |
else None |
| 898 |
| _ -> None |
| 899 |
in |
| 900 |
let h, t = |
| 901 |
List.find_map_exn matching_fun (EConstr.named_context env) |
| 902 |
in |
| 903 |
let y = |
| 904 |
let _, args = decompose_app_list sigma t in |
| 905 |
List.hd (List.tl args) |
| 906 |
in |
| 907 |
observe_tclTHENLIST |
| 908 |
(fun _ _ -> str "prove_le") |
| 909 |
[ apply (mkApp (le_trans (), [|x; y; z; mkVar h|])) |
| 910 |
; observe_tac |
| 911 |
(fun _ _ -> str "prove_le (rec)") |
| 912 |
(prove_le ()) ] |
| 913 |
with Not_found -> Tacticals.tclFAIL (mt ()) |
| 914 |
end ]) |
| 915 |
|
| 916 |
let rec make_rewrite_list expr_info max = function |
| 917 |
| [] -> Proofview.tclUNIT () |
| 918 |
| (_, p, hp) :: l -> |
| 919 |
let open Tacticals in |
| 920 |
observe_tac |
| 921 |
(fun _ _ -> str "make_rewrite_list") |
| 922 |
(tclTHENS |
| 923 |
(observe_tac |
| 924 |
(fun _ _ -> str "rewrite heq on " ++ Id.print p) |
| 925 |
(Proofview.Goal.enter (fun g -> |
| 926 |
let sigma = Proofview.Goal.sigma g in |
| 927 |
let t_eq = Tacmach.pf_get_hyp_typ hp g in |
| 928 |
let k, def = |
| 929 |
let k_na, _, t = destProd sigma t_eq in |
| 930 |
let _, _, t = destProd sigma t in |
| 931 |
let def_na, _, _ = destProd sigma t in |
| 932 |
( Nameops.Name.get_id k_na.binder_name |
| 933 |
, Nameops.Name.get_id def_na.binder_name ) |
| 934 |
in |
| 935 |
general_rewrite ~where:None ~l2r:false Locus.AllOccurrences ~freeze:true |
| 936 |
~dep:true ~with_evars:false |
| 937 |
( mkVar hp |
| 938 |
, ExplicitBindings |
| 939 |
[ CAst.make @@ (NamedHyp (CAst.make def), expr_info.f_constr) |
| 940 |
; CAst.make @@ (NamedHyp (CAst.make k), f_S max) ] ) |
| 941 |
))) |
| 942 |
[ make_rewrite_list expr_info max l |
| 943 |
; observe_tclTHENLIST |
| 944 |
(fun _ _ -> str "make_rewrite_list") |
| 945 |
[ |
| 946 |
apply (delayed_force le_lt_n_Sm) |
| 947 |
; observe_tac (fun _ _ -> str "prove_le(2)") (prove_le ()) ] ]) |
| 948 |
|
| 949 |
let make_rewrite expr_info l hp max = |
| 950 |
let open Tacticals in |
| 951 |
tclTHENFIRST |
| 952 |
(observe_tac |
| 953 |
(fun _ _ -> str "make_rewrite") |
| 954 |
(make_rewrite_list expr_info max l)) |
| 955 |
(observe_tac |
| 956 |
(fun _ _ -> str "make_rewrite") |
| 957 |
(tclTHENS |
| 958 |
(Proofview.Goal.enter (fun g -> |
| 959 |
let sigma = Proofview.Goal.sigma g in |
| 960 |
let t_eq = Tacmach.pf_get_hyp_typ hp g in |
| 961 |
let k, def = |
| 962 |
let k_na, _, t = destProd sigma t_eq in |
| 963 |
let _, _, t = destProd sigma t in |
| 964 |
let def_na, _, _ = destProd sigma t in |
| 965 |
( Nameops.Name.get_id k_na.binder_name |
| 966 |
, Nameops.Name.get_id def_na.binder_name ) |
| 967 |
in |
| 968 |
observe_tac |
| 969 |
(fun _ _ -> str "general_rewrite_bindings") |
| 970 |
(general_rewrite ~where:None ~l2r:false Locus.AllOccurrences ~freeze:true |
| 971 |
~dep:true ~with_evars:false |
| 972 |
( mkVar hp |
| 973 |
, ExplicitBindings |
| 974 |
[ CAst.make @@ (NamedHyp (CAst.make def), expr_info.f_constr) |
| 975 |
; CAst.make @@ (NamedHyp (CAst.make k), f_S (f_S max)) ] ) |
| 976 |
))) |
| 977 |
[ observe_tac |
| 978 |
(fun _ _ -> str "make_rewrite finalize") |
| 979 |
( |
| 980 |
observe_tclTHENLIST |
| 981 |
(fun _ _ -> str "make_rewrite") |
| 982 |
[ simpl_iter Locusops.onConcl |
| 983 |
; observe_tac |
| 984 |
(fun _ _ -> str "unfold functional") |
| 985 |
(unfold_in_concl |
| 986 |
[ ( Locus.OnlyOccurrences [1] |
| 987 |
, evaluable_of_global_reference expr_info.func ) ]) |
| 988 |
; list_rewrite true |
| 989 |
(List.map (fun e -> (mkVar e, true)) expr_info.eqs) |
| 990 |
; observe_tac |
| 991 |
(fun _ _ -> str "h_reflexivity") |
| 992 |
intros_reflexivity ]) |
| 993 |
; observe_tclTHENLIST |
| 994 |
(fun _ _ -> str "make_rewrite1") |
| 995 |
[ |
| 996 |
apply (EConstr.of_constr (delayed_force le_lt_SS)) |
| 997 |
; observe_tac (fun _ _ -> str "prove_le (3)") (prove_le ()) ] |
| 998 |
])) |
| 999 |
|
| 1000 |
let rec compute_max rew_tac max l = |
| 1001 |
match l with |
| 1002 |
| [] -> rew_tac max |
| 1003 |
| (_, p, _) :: l -> |
| 1004 |
let open Tacticals in |
| 1005 |
observe_tclTHENLIST |
| 1006 |
(fun _ _ -> str "compute_max") |
| 1007 |
[ simplest_elim (mkApp (delayed_force max_constr, [|max; mkVar p|])) |
| 1008 |
; tclDO 3 intro |
| 1009 |
; onNLastHypsId 3 (fun lids -> |
| 1010 |
match lids with |
| 1011 |
| [hle2; hle1; pmax] -> compute_max rew_tac (mkVar pmax) l |
| 1012 |
| _ -> assert false) ] |
| 1013 |
|
| 1014 |
let rec destruct_hex expr_info acc l = |
| 1015 |
let open Tacticals in |
| 1016 |
match l with |
| 1017 |
| [] -> ( |
| 1018 |
match List.rev acc with |
| 1019 |
| [] -> Proofview.tclUNIT () |
| 1020 |
| (_, p, hp) :: tl -> |
| 1021 |
observe_tac |
| 1022 |
(fun _ _ -> str "compute max ") |
| 1023 |
(compute_max (make_rewrite expr_info tl hp) (mkVar p) tl) ) |
| 1024 |
| (v, hex) :: l -> |
| 1025 |
observe_tclTHENLIST |
| 1026 |
(fun _ _ -> str "destruct_hex") |
| 1027 |
[ simplest_case (mkVar hex) |
| 1028 |
; clear [hex] |
| 1029 |
; tclDO 2 intro |
| 1030 |
; onNthHypId 1 (fun hp -> |
| 1031 |
onNthHypId 2 (fun p -> |
| 1032 |
observe_tac |
| 1033 |
(fun _ _ -> |
| 1034 |
str "destruct_hex after " ++ Id.print hp ++ spc () |
| 1035 |
++ Id.print p) |
| 1036 |
(destruct_hex expr_info ((v, p, hp) :: acc) l))) ] |
| 1037 |
|
| 1038 |
let rec intros_values_eq expr_info acc = |
| 1039 |
let open Tacticals in |
| 1040 |
tclORELSE |
| 1041 |
(observe_tclTHENLIST |
| 1042 |
(fun _ _ -> str "intros_values_eq") |
| 1043 |
[ tclDO 2 intro |
| 1044 |
; onNthHypId 1 (fun hex -> |
| 1045 |
onNthHypId 2 (fun v -> |
| 1046 |
intros_values_eq expr_info ((v, hex) :: acc))) ]) |
| 1047 |
(tclCOMPLETE (destruct_hex expr_info [] acc)) |
| 1048 |
|
| 1049 |
let equation_others _ expr_info continuation_tac infos = |
| 1050 |
let open Tacticals in |
| 1051 |
if expr_info.is_final && expr_info.is_main_branch then |
| 1052 |
observe_tac |
| 1053 |
(fun env sigma -> |
| 1054 |
str "equation_others (cont_tac +intros) " |
| 1055 |
++ Printer.pr_leconstr_env env sigma expr_info.info) |
| 1056 |
(tclTHEN (continuation_tac infos) |
| 1057 |
(observe_tac |
| 1058 |
(fun env sigma -> |
| 1059 |
str "intros_values_eq equation_others " |
| 1060 |
++ Printer.pr_leconstr_env env sigma expr_info.info) |
| 1061 |
(intros_values_eq expr_info []))) |
| 1062 |
else |
| 1063 |
observe_tac |
| 1064 |
(fun env sigma -> |
| 1065 |
str "equation_others (cont_tac) " |
| 1066 |
++ Printer.pr_leconstr_env env sigma expr_info.info) |
| 1067 |
(continuation_tac infos) |
| 1068 |
|
| 1069 |
let equation_app f_and_args expr_info continuation_tac infos = |
| 1070 |
if expr_info.is_final && expr_info.is_main_branch then |
| 1071 |
observe_tac |
| 1072 |
(fun _ _ -> str "intros_values_eq equation_app") |
| 1073 |
(intros_values_eq expr_info []) |
| 1074 |
else continuation_tac infos |
| 1075 |
|
| 1076 |
let equation_app_rec (f, args) expr_info continuation_tac info = |
| 1077 |
Proofview.Goal.enter (fun g -> |
| 1078 |
let sigma = Proofview.Goal.sigma g in |
| 1079 |
try |
| 1080 |
let v = |
| 1081 |
List.assoc_f |
| 1082 |
(List.equal (EConstr.eq_constr sigma)) |
| 1083 |
args expr_info.args_assoc |
| 1084 |
in |
| 1085 |
let new_infos = {expr_info with info = v} in |
| 1086 |
observe_tac |
| 1087 |
(fun _ _ -> str "app_rec found") |
| 1088 |
(continuation_tac new_infos) |
| 1089 |
with Not_found -> |
| 1090 |
if expr_info.is_final && expr_info.is_main_branch then |
| 1091 |
observe_tclTHENLIST |
| 1092 |
(fun _ _ -> str "equation_app_rec") |
| 1093 |
[ simplest_case (mkApp (expr_info.f_terminate, Array.of_list args)) |
| 1094 |
; continuation_tac |
| 1095 |
{ expr_info with |
| 1096 |
args_assoc = |
| 1097 |
(args, delayed_force rocq_O) :: expr_info.args_assoc } |
| 1098 |
; observe_tac |
| 1099 |
(fun _ _ -> str "app_rec intros_values_eq") |
| 1100 |
(intros_values_eq expr_info []) ] |
| 1101 |
else |
| 1102 |
observe_tclTHENLIST |
| 1103 |
(fun _ _ -> str "equation_app_rec1") |
| 1104 |
[ simplest_case (mkApp (expr_info.f_terminate, Array.of_list args)) |
| 1105 |
; observe_tac |
| 1106 |
(fun _ _ -> str "app_rec not_found") |
| 1107 |
(continuation_tac |
| 1108 |
{ expr_info with |
| 1109 |
args_assoc = |
| 1110 |
(args, delayed_force rocq_O) :: expr_info.args_assoc }) ]) |
| 1111 |
|
| 1112 |
let equation_info = |
| 1113 |
{ message = "prove_equation with term " |
| 1114 |
; letiN = (fun _ -> assert false) |
| 1115 |
; lambdA = (fun _ _ _ _ -> assert false) |
| 1116 |
; casE = equation_case |
| 1117 |
; otherS = equation_others |
| 1118 |
; apP = equation_app |
| 1119 |
; app_reC = equation_app_rec } |
| 1120 |
|
| 1121 |
let prove_eq = travel equation_info |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
let compute_terminate_type nb_args func = |
| 1127 |
let open Term in |
| 1128 |
let open Constr in |
| 1129 |
let open CVars in |
| 1130 |
let _, a_arrow_b, _ = |
| 1131 |
destLambda (def_of_const (constr_of_monomorphic_global (Global.env ()) func)) |
| 1132 |
in |
| 1133 |
let rev_args, b = decompose_prod_n nb_args a_arrow_b in |
| 1134 |
let left = |
| 1135 |
mkApp |
| 1136 |
( delayed_force iter_rd |
| 1137 |
, Array.of_list |
| 1138 |
( lift 5 a_arrow_b :: mkRel 3 |
| 1139 |
:: constr_of_monomorphic_global (Global.env ()) func |
| 1140 |
:: mkRel 1 |
| 1141 |
:: List.rev (List.map_i (fun i _ -> mkRel (6 + i)) 0 rev_args) ) ) |
| 1142 |
in |
| 1143 |
let right = mkRel 5 in |
| 1144 |
let delayed_force c = EConstr.Unsafe.to_constr (delayed_force c) in |
| 1145 |
let equality = mkApp (delayed_force eq, [|lift 5 b; left; right|]) in |
| 1146 |
let result = |
| 1147 |
mkProd (make_annot (Name def_id) Sorts.Relevant, lift 4 a_arrow_b, equality) |
| 1148 |
in |
| 1149 |
let cond = mkApp (delayed_force lt, [|mkRel 2; mkRel 1|]) in |
| 1150 |
let nb_iter = |
| 1151 |
mkApp |
| 1152 |
( delayed_force ex |
| 1153 |
, [| delayed_force nat |
| 1154 |
; mkLambda |
| 1155 |
( make_annot (Name p_id) Sorts.Relevant |
| 1156 |
, delayed_force nat |
| 1157 |
, mkProd |
| 1158 |
( make_annot (Name k_id) Sorts.Relevant |
| 1159 |
, delayed_force nat |
| 1160 |
, mkArrow cond Sorts.Relevant result ) ) |] ) |
| 1161 |
in |
| 1162 |
let value = |
| 1163 |
mkApp |
| 1164 |
( constr_of_monomorphic_global (Global.env ()) (Util.delayed_force rocq_sig_ref) |
| 1165 |
, [|b; mkLambda (make_annot (Name v_id) Sorts.Relevant, b, nb_iter)|] ) |
| 1166 |
in |
| 1167 |
compose_prod rev_args value |
| 1168 |
|
| 1169 |
let termination_proof_header is_mes input_type ids args_id relation rec_arg_num |
| 1170 |
rec_arg_id tac wf_tac : unit Proofview.tactic = |
| 1171 |
let open Tacticals in |
| 1172 |
Proofview.Goal.enter (fun g -> |
| 1173 |
let nargs = List.length args_id in |
| 1174 |
let pre_rec_args = |
| 1175 |
List.rev_map mkVar (fst (List.chop (rec_arg_num - 1) args_id)) |
| 1176 |
in |
| 1177 |
let relation = substl pre_rec_args relation in |
| 1178 |
let input_type = substl pre_rec_args input_type in |
| 1179 |
let wf_thm = next_ident_away_in_goal (Id.of_string "wf_R") ids in |
| 1180 |
let wf_rec_arg = |
| 1181 |
next_ident_away_in_goal |
| 1182 |
(Id.of_string ("Acc_" ^ Id.to_string rec_arg_id)) |
| 1183 |
(wf_thm :: ids) |
| 1184 |
in |
| 1185 |
let hrec = |
| 1186 |
next_ident_away_in_goal hrec_id (wf_rec_arg :: wf_thm :: ids) |
| 1187 |
in |
| 1188 |
let acc_inv = |
| 1189 |
lazy |
| 1190 |
(mkApp |
| 1191 |
( delayed_force acc_inv_id |
| 1192 |
, [|input_type; relation; mkVar rec_arg_id|] )) |
| 1193 |
in |
| 1194 |
tclTHEN (h_intros args_id) |
| 1195 |
(tclTHENS |
| 1196 |
(observe_tac |
| 1197 |
(fun _ _ -> str "first assert") |
| 1198 |
(assert_before (Name wf_rec_arg) |
| 1199 |
(mkApp |
| 1200 |
( delayed_force acc_rel |
| 1201 |
, [|input_type; relation; mkVar rec_arg_id|] )))) |
| 1202 |
[ |
| 1203 |
tclTHENS |
| 1204 |
(observe_tac |
| 1205 |
(fun _ _ -> str "second assert") |
| 1206 |
(assert_before (Name wf_thm) |
| 1207 |
(mkApp |
| 1208 |
(delayed_force well_founded, [|input_type; relation|])))) |
| 1209 |
[ |
| 1210 |
observe_tac |
| 1211 |
(fun _ _ -> str "wf_tac") |
| 1212 |
(wf_tac is_mes (Some args_id)) |
| 1213 |
; |
| 1214 |
observe_tac |
| 1215 |
(fun _ _ -> str "apply wf_thm") |
| 1216 |
(Simple.apply (mkApp (mkVar wf_thm, [|mkVar rec_arg_id|]))) |
| 1217 |
] |
| 1218 |
; |
| 1219 |
observe_tclTHENLIST |
| 1220 |
(fun _ _ -> str "rest of proof") |
| 1221 |
[ observe_tac |
| 1222 |
(fun _ _ -> str "generalize") |
| 1223 |
(onNLastHypsId (nargs + 1) |
| 1224 |
(tclMAP (fun id -> |
| 1225 |
tclTHEN (Generalize.generalize [mkVar id]) (clear [id])))) |
| 1226 |
; observe_tac (fun _ _ -> str "fix") (FixTactics.fix hrec (nargs + 1)) |
| 1227 |
; h_intros args_id |
| 1228 |
; Simple.intro wf_rec_arg |
| 1229 |
; observe_tac |
| 1230 |
(fun _ _ -> str "tac") |
| 1231 |
(tac wf_rec_arg hrec wf_rec_arg acc_inv) ] ])) |
| 1232 |
|
| 1233 |
let rec instantiate_lambda sigma t l = |
| 1234 |
match l with |
| 1235 |
| [] -> t |
| 1236 |
| a :: l -> |
| 1237 |
let _, _, body = destLambda sigma t in |
| 1238 |
instantiate_lambda sigma (subst1 a body) l |
| 1239 |
|
| 1240 |
let whole_start concl_tac nb_args is_mes func input_type relation rec_arg_num : |
| 1241 |
unit Proofview.tactic = |
| 1242 |
Proofview.Goal.enter (fun g -> |
| 1243 |
let sigma = Proofview.Goal.sigma g in |
| 1244 |
let hyps = Proofview.Goal.hyps g in |
| 1245 |
let ids = Termops.ids_of_named_context hyps in |
| 1246 |
let func_body = def_of_const (constr_of_monomorphic_global (Global.env ()) func) in |
| 1247 |
let func_body = EConstr.of_constr func_body in |
| 1248 |
let f_name, _, body1 = destLambda sigma func_body in |
| 1249 |
let f_id = |
| 1250 |
match f_name.binder_name with |
| 1251 |
| Name f_id -> next_ident_away_in_goal f_id ids |
| 1252 |
| Anonymous -> anomaly (Pp.str "Anonymous function.") |
| 1253 |
in |
| 1254 |
let n_names_types, _ = decompose_lambda_n sigma nb_args body1 in |
| 1255 |
let n_ids, ids = |
| 1256 |
List.fold_left |
| 1257 |
(fun (n_ids, ids) (n_name, _) -> |
| 1258 |
match n_name.binder_name with |
| 1259 |
| Name id -> |
| 1260 |
let n_id = next_ident_away_in_goal id ids in |
| 1261 |
(n_id :: n_ids, n_id :: ids) |
| 1262 |
| _ -> anomaly (Pp.str "anonymous argument.")) |
| 1263 |
([], f_id :: ids) |
| 1264 |
n_names_types |
| 1265 |
in |
| 1266 |
let rec_arg_id = List.nth n_ids (rec_arg_num - 1) in |
| 1267 |
let expr = |
| 1268 |
instantiate_lambda sigma func_body (mkVar f_id :: List.map mkVar n_ids) |
| 1269 |
in |
| 1270 |
termination_proof_header is_mes input_type ids n_ids relation rec_arg_num |
| 1271 |
rec_arg_id |
| 1272 |
(fun rec_arg_id hrec acc_id acc_inv -> |
| 1273 |
prove_terminate |
| 1274 |
(fun infos -> Proofview.tclUNIT ()) |
| 1275 |
{ is_main_branch = true |
| 1276 |
; |
| 1277 |
is_final = true |
| 1278 |
; |
| 1279 |
f_terminate = delayed_force rocq_O |
| 1280 |
; nb_arg = nb_args |
| 1281 |
; concl_tac |
| 1282 |
; rec_arg_id |
| 1283 |
; is_mes |
| 1284 |
; ih = hrec |
| 1285 |
; f_id |
| 1286 |
; f_constr = mkVar f_id |
| 1287 |
; func |
| 1288 |
; info = expr |
| 1289 |
; acc_inv |
| 1290 |
; acc_id |
| 1291 |
; values_and_bounds = [] |
| 1292 |
; eqs = [] |
| 1293 |
; forbidden_ids = [] |
| 1294 |
; args_assoc = [] }) |
| 1295 |
(fun b ids -> tclUSER_if_not_mes concl_tac b ids)) |
| 1296 |
|
| 1297 |
|
| 1298 |
let abstract_type sigma gl = |
| 1299 |
let open EConstr in |
| 1300 |
let genv = Global.env () in |
| 1301 |
let evi = Evd.find_undefined sigma gl in |
| 1302 |
let env = Evd.evar_filtered_env genv evi in |
| 1303 |
let is_proof_var decl = |
| 1304 |
try ignore (Environ.lookup_named (Context.Named.Declaration.get_id decl) genv); false |
| 1305 |
with Not_found -> true in |
| 1306 |
Environ.fold_named_context_reverse (fun t decl -> |
| 1307 |
if is_proof_var decl then |
| 1308 |
let decl = EConstr.of_named_decl decl in |
| 1309 |
mkNamedProd_or_LetIn sigma decl t |
| 1310 |
else |
| 1311 |
t |
| 1312 |
) ~init:(Evd.evar_concl evi) env |
| 1313 |
|
| 1314 |
let get_current_subgoals_types pstate = |
| 1315 |
let p = Declare.Proof.get pstate in |
| 1316 |
let Proof.{goals = sgs; sigma; _} = Proof.data p in |
| 1317 |
(sigma, List.map (abstract_type sigma) sgs) |
| 1318 |
|
| 1319 |
exception EmptySubgoals |
| 1320 |
|
| 1321 |
let build_and_l sigma l = |
| 1322 |
let and_constr = |
| 1323 |
UnivGen.constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref "core.and.type" |
| 1324 |
in |
| 1325 |
let conj_constr = Rocqlib.lib_ref "core.and.conj" in |
| 1326 |
let mk_and p1 p2 = mkApp (EConstr.of_constr and_constr, [|p1; p2|]) in |
| 1327 |
let rec is_well_founded t = |
| 1328 |
match EConstr.kind sigma t with |
| 1329 |
| Prod (_, _, t') -> is_well_founded t' |
| 1330 |
| App (_, _) -> |
| 1331 |
let f, _ = decompose_app sigma t in |
| 1332 |
EConstr.eq_constr sigma f (well_founded ()) |
| 1333 |
| _ -> false |
| 1334 |
in |
| 1335 |
let compare t1 t2 = |
| 1336 |
let b1, b2 = (is_well_founded t1, is_well_founded t2) in |
| 1337 |
if (b1 && b2) || not (b1 || b2) then 0 else if b1 && not b2 then 1 else -1 |
| 1338 |
in |
| 1339 |
let l = List.sort compare l in |
| 1340 |
let rec f = function |
| 1341 |
| [] -> raise EmptySubgoals |
| 1342 |
| [p] -> (p, tclIDTAC, 1) |
| 1343 |
| p1 :: pl -> |
| 1344 |
let c, tac, nb = f pl in |
| 1345 |
( mk_and p1 c |
| 1346 |
, tclTHENS |
| 1347 |
(apply (EConstr.of_constr (constr_of_monomorphic_global (Global.env ()) conj_constr))) |
| 1348 |
[tclIDTAC; tac] |
| 1349 |
, nb + 1 ) |
| 1350 |
in |
| 1351 |
f l |
| 1352 |
|
| 1353 |
let is_rec_res id = |
| 1354 |
let rec_res_name = Id.to_string rec_res_id in |
| 1355 |
let id_name = Id.to_string id in |
| 1356 |
try |
| 1357 |
String.equal |
| 1358 |
(String.sub id_name 0 (String.length rec_res_name)) |
| 1359 |
rec_res_name |
| 1360 |
with Invalid_argument _ -> false |
| 1361 |
|
| 1362 |
let clear_goals sigma = |
| 1363 |
let rec clear_goal t = |
| 1364 |
match EConstr.kind sigma t with |
| 1365 |
| Prod (({binder_name = Name id} as na), t', b) -> |
| 1366 |
let b' = clear_goal b in |
| 1367 |
if noccurn sigma 1 b' && is_rec_res id then Vars.lift (-1) b' |
| 1368 |
else if b' == b then t |
| 1369 |
else mkProd (na, t', b') |
| 1370 |
| _ -> EConstr.map sigma clear_goal t |
| 1371 |
in |
| 1372 |
List.map clear_goal |
| 1373 |
|
| 1374 |
let build_new_goal_type lemma = |
| 1375 |
let sigma, sub_gls_types = get_current_subgoals_types lemma in |
| 1376 |
|
| 1377 |
let sub_gls_types = clear_goals sigma sub_gls_types in |
| 1378 |
|
| 1379 |
let res = build_and_l sigma sub_gls_types in |
| 1380 |
(sigma, res) |
| 1381 |
|
| 1382 |
let is_opaque_constant c = |
| 1383 |
let cb = Global.lookup_constant c in |
| 1384 |
let open Vernacexpr in |
| 1385 |
match cb.Declarations.const_body with |
| 1386 |
| Declarations.OpaqueDef _ -> Opaque |
| 1387 |
| Declarations.Undef _ -> Opaque |
| 1388 |
| Declarations.Def _ -> Transparent |
| 1389 |
| Declarations.Primitive _ -> Opaque |
| 1390 |
| Declarations.Symbol _ -> Opaque |
| 1391 |
|
| 1392 |
let open_new_goal ~lemma build_proof sigma using_lemmas ref_ goal_name |
| 1393 |
(gls_type, decompose_and_tac, nb_goal) = |
| 1394 |
|
| 1395 |
let current_proof_name = Declare.Proof.get_name lemma in |
| 1396 |
let name = |
| 1397 |
match goal_name with |
| 1398 |
| Some s -> s |
| 1399 |
| None -> ( |
| 1400 |
try add_suffix current_proof_name "_subproof" |
| 1401 |
with e when CErrors.noncritical e -> |
| 1402 |
anomaly (Pp.str "open_new_goal with an unnamed theorem.") ) |
| 1403 |
in |
| 1404 |
let na = next_global_ident_away (Global.safe_env ()) name Id.Set.empty in |
| 1405 |
if Termops.occur_existential sigma gls_type then |
| 1406 |
CErrors.user_err Pp.(str "\"abstract\" cannot handle existentials"); |
| 1407 |
let hook _ = |
| 1408 |
let opacity = |
| 1409 |
let na_ref = qualid_of_ident na in |
| 1410 |
let na_global = Smartlocate.global_with_alias na_ref in |
| 1411 |
match na_global with |
| 1412 |
| GlobRef.ConstRef c -> is_opaque_constant c |
| 1413 |
| _ -> anomaly ~label:"equation_lemma" (Pp.str "not a constant.") |
| 1414 |
in |
| 1415 |
|
| 1416 |
let lemma = UnsafeMonomorphic.mkConst (Names.Constant.make1 (Lib.make_kn na)) in |
| 1417 |
ref_ := Value (EConstr.Unsafe.to_constr lemma); |
| 1418 |
let lid = ref [] in |
| 1419 |
let h_num = ref (-1) in |
| 1420 |
let env = Global.env () in |
| 1421 |
let start_tac = |
| 1422 |
let open Tacticals in |
| 1423 |
Proofview.Goal.enter (fun gl -> |
| 1424 |
let hid = next_ident_away_in_goal h_id (Tacmach.pf_ids_of_hyps gl) in |
| 1425 |
observe_tclTHENLIST |
| 1426 |
(fun _ _ -> mt ()) |
| 1427 |
[ Generalize.generalize [lemma] |
| 1428 |
; Simple.intro hid |
| 1429 |
; Proofview.Goal.enter (fun gl -> |
| 1430 |
let ids = Tacmach.pf_ids_of_hyps gl in |
| 1431 |
tclTHEN |
| 1432 |
(Elim.h_decompose_and (mkVar hid)) |
| 1433 |
(Proofview.Goal.enter (fun gl -> |
| 1434 |
let ids' = Tacmach.pf_ids_of_hyps gl in |
| 1435 |
lid := List.rev (List.subtract Id.equal ids' ids); |
| 1436 |
if List.is_empty !lid then lid := [hid]; |
| 1437 |
tclIDTAC))) ]) |
| 1438 |
in |
| 1439 |
let end_tac = |
| 1440 |
let open Tacticals in |
| 1441 |
Proofview.Goal.enter (fun gl -> |
| 1442 |
let sigma = Proofview.Goal.sigma gl in |
| 1443 |
let concl = Proofview.Goal.concl gl in |
| 1444 |
match EConstr.kind sigma concl with |
| 1445 |
| App (f, _) when EConstr.eq_constr sigma f (well_founded ()) -> |
| 1446 |
Auto.gen_auto None [] (Some []) |
| 1447 |
| _ -> |
| 1448 |
incr h_num; |
| 1449 |
tclCOMPLETE |
| 1450 |
(tclFIRST |
| 1451 |
[ tclTHEN |
| 1452 |
(eapply_with_bindings |
| 1453 |
(mkVar (List.nth !lid !h_num), NoBindings)) |
| 1454 |
e_assumption |
| 1455 |
; Eauto.eauto_with_bases ~depth:5 |
| 1456 |
[(fun _ sigma -> (sigma, Lazy.force refl_equal))] |
| 1457 |
[Hints.Hint_db.empty TransparentState.empty false] ])) |
| 1458 |
in |
| 1459 |
let lemma = build_proof env (Evd.from_env env) start_tac end_tac in |
| 1460 |
let (_ : _ list) = |
| 1461 |
Declare.Proof.save_regular ~proof:lemma ~opaque:opacity ~idopt:None |
| 1462 |
in |
| 1463 |
() |
| 1464 |
in |
| 1465 |
let info = Declare.Info.make ~hook:(Declare.Hook.make hook) () in |
| 1466 |
let cinfo = Declare.CInfo.make ~name:na ~typ:gls_type () in |
| 1467 |
let lemma = Declare.Proof.start ~cinfo ~info sigma in |
| 1468 |
let lemma = |
| 1469 |
if Indfun_common.is_strict_tcc () then |
| 1470 |
fst @@ Declare.Proof.by (Global.env ()) tclIDTAC lemma |
| 1471 |
else |
| 1472 |
fst |
| 1473 |
@@ Declare.Proof.by (Global.env ()) |
| 1474 |
(tclTHEN decompose_and_tac |
| 1475 |
(tclORELSE |
| 1476 |
(tclFIRST |
| 1477 |
(List.map |
| 1478 |
(fun c -> |
| 1479 |
Tacticals.tclTHENLIST |
| 1480 |
[ intros |
| 1481 |
; Simple.apply |
| 1482 |
(fst (interp_constr (Global.env ()) Evd.empty c)) |
| 1483 |
|
| 1484 |
; Tacticals.tclCOMPLETE Auto.default_auto ]) |
| 1485 |
using_lemmas)) |
| 1486 |
tclIDTAC)) |
| 1487 |
lemma |
| 1488 |
in |
| 1489 |
if Declare.Proof.get_open_goals lemma = 0 then (defined lemma; None) |
| 1490 |
else Some lemma |
| 1491 |
|
| 1492 |
let com_terminate interactive_proof tcc_lemma_name tcc_lemma_ref is_mes |
| 1493 |
fonctional_ref input_type relation rec_arg_num thm_name using_lemmas nb_args |
| 1494 |
ctx hook = |
| 1495 |
let start_proof env ctx tac_start tac_end = |
| 1496 |
let cinfo = |
| 1497 |
Declare.CInfo.make ~name:thm_name |
| 1498 |
~typ:(EConstr.of_constr (compute_terminate_type nb_args fonctional_ref)) |
| 1499 |
() |
| 1500 |
in |
| 1501 |
let info = Declare.Info.make ~hook () in |
| 1502 |
let lemma = Declare.Proof.start ~cinfo ~info ctx in |
| 1503 |
let lemma = |
| 1504 |
fst |
| 1505 |
@@ Declare.Proof.by (Global.env ()) |
| 1506 |
(observe_tac (fun _ _ -> str "starting_tac") tac_start) |
| 1507 |
lemma |
| 1508 |
in |
| 1509 |
fst |
| 1510 |
@@ Declare.Proof.by (Global.env ()) |
| 1511 |
(observe_tac |
| 1512 |
(fun _ _ -> str "whole_start") |
| 1513 |
(whole_start tac_end nb_args is_mes fonctional_ref input_type |
| 1514 |
relation rec_arg_num)) |
| 1515 |
lemma |
| 1516 |
in |
| 1517 |
let lemma = |
| 1518 |
start_proof |
| 1519 |
Global.(env ()) |
| 1520 |
ctx Tacticals.tclIDTAC Tacticals.tclIDTAC |
| 1521 |
in |
| 1522 |
try |
| 1523 |
let sigma, new_goal_type = build_new_goal_type lemma in |
| 1524 |
let sigma = Evd.from_ctx (Evd.ustate sigma) in |
| 1525 |
open_new_goal ~lemma start_proof sigma using_lemmas tcc_lemma_ref |
| 1526 |
(Some tcc_lemma_name) new_goal_type |
| 1527 |
with EmptySubgoals -> |
| 1528 |
|
| 1529 |
tcc_lemma_ref := Not_needed; |
| 1530 |
if interactive_proof then Some lemma else (defined lemma; None) |
| 1531 |
|
| 1532 |
let start_equation (f : GlobRef.t) (term_f : GlobRef.t) |
| 1533 |
(cont_tactic : Id.t list -> unit Proofview.tactic) = |
| 1534 |
Proofview.Goal.enter (fun g -> |
| 1535 |
let sigma = Proofview.Goal.sigma g in |
| 1536 |
let ids = Tacmach.pf_ids_of_hyps g in |
| 1537 |
let terminate_constr = constr_of_monomorphic_global (Global.env ()) term_f in |
| 1538 |
let terminate_constr = EConstr.of_constr terminate_constr in |
| 1539 |
let nargs = |
| 1540 |
nb_prod sigma (EConstr.of_constr (type_of_const (Global.env ()) sigma terminate_constr)) |
| 1541 |
in |
| 1542 |
let x = n_x_id ids nargs in |
| 1543 |
observe_tac |
| 1544 |
(fun _ _ -> str "start_equation") |
| 1545 |
(observe_tclTHENLIST |
| 1546 |
(fun _ _ -> str "start_equation") |
| 1547 |
[ h_intros x |
| 1548 |
; unfold_in_concl |
| 1549 |
[(Locus.AllOccurrences, evaluable_of_global_reference f)] |
| 1550 |
; observe_tac |
| 1551 |
(fun _ _ -> str "simplest_case") |
| 1552 |
(simplest_case |
| 1553 |
(mkApp (terminate_constr, Array.of_list (List.map mkVar x)))) |
| 1554 |
; observe_tac (fun _ _ -> str "prove_eq") (cont_tactic x) ])) |
| 1555 |
|
| 1556 |
let com_eqn uctx nb_arg eq_name functional_ref f_ref terminate_ref |
| 1557 |
equation_lemma_type = |
| 1558 |
let open CVars in |
| 1559 |
let opacity = |
| 1560 |
match terminate_ref with |
| 1561 |
| GlobRef.ConstRef c -> is_opaque_constant c |
| 1562 |
| _ -> anomaly ~label:"terminate_lemma" (Pp.str "not a constant.") |
| 1563 |
in |
| 1564 |
let evd = Evd.from_ctx uctx in |
| 1565 |
let f_constr = constr_of_monomorphic_global (Global.env ()) f_ref in |
| 1566 |
let equation_lemma_type = subst1 f_constr equation_lemma_type in |
| 1567 |
let info = Declare.Info.make () in |
| 1568 |
let cinfo = |
| 1569 |
Declare.CInfo.make ~name:eq_name |
| 1570 |
~typ:(EConstr.of_constr equation_lemma_type) |
| 1571 |
() |
| 1572 |
in |
| 1573 |
let lemma = Declare.Proof.start ~cinfo evd ~info in |
| 1574 |
let lemma = |
| 1575 |
fst |
| 1576 |
@@ Declare.Proof.by (Global.env ()) |
| 1577 |
(start_equation f_ref terminate_ref (fun x -> |
| 1578 |
prove_eq |
| 1579 |
(fun _ -> Proofview.tclUNIT ()) |
| 1580 |
{ nb_arg |
| 1581 |
; f_terminate = |
| 1582 |
EConstr.of_constr |
| 1583 |
(constr_of_monomorphic_global (Global.env ()) terminate_ref) |
| 1584 |
; f_constr = EConstr.of_constr f_constr |
| 1585 |
; concl_tac = Tacticals.tclIDTAC |
| 1586 |
; func = functional_ref |
| 1587 |
; info = |
| 1588 |
instantiate_lambda Evd.empty |
| 1589 |
(EConstr.of_constr |
| 1590 |
(def_of_const |
| 1591 |
(constr_of_monomorphic_global (Global.env ()) functional_ref))) |
| 1592 |
(EConstr.of_constr f_constr :: List.map mkVar x) |
| 1593 |
; is_main_branch = true |
| 1594 |
; is_final = true |
| 1595 |
; values_and_bounds = [] |
| 1596 |
; eqs = [] |
| 1597 |
; forbidden_ids = [] |
| 1598 |
; acc_inv = lazy (assert false) |
| 1599 |
; acc_id = Id.of_string "____" |
| 1600 |
; args_assoc = [] |
| 1601 |
; f_id = Id.of_string "______" |
| 1602 |
; rec_arg_id = Id.of_string "______" |
| 1603 |
; is_mes = false |
| 1604 |
; ih = Id.of_string "______" })) |
| 1605 |
lemma |
| 1606 |
in |
| 1607 |
let _ = |
| 1608 |
Flags.silently |
| 1609 |
(fun () -> |
| 1610 |
let (_ : _ list) = |
| 1611 |
Declare.Proof.save_regular ~proof:lemma ~opaque:opacity ~idopt:None |
| 1612 |
in |
| 1613 |
()) |
| 1614 |
() |
| 1615 |
in |
| 1616 |
() |
| 1617 |
|
| 1618 |
let check_relation_type env evd relation = |
| 1619 |
let err () = CErrors.user_err (strbrk "a well-founded relation is expected to be of type A->A->Prop for some type A.") in |
| 1620 |
let evd, t = Typing.type_of env evd relation in |
| 1621 |
let open Reductionops in |
| 1622 |
match EConstr.kind evd (whd_all env evd t) with |
| 1623 |
| Prod (_, typ, _) -> |
| 1624 |
let rel = mkArrowR typ (mkArrowR (Vars.lift 1 typ) mkProp) in |
| 1625 |
if not (is_conv env evd rel t) then err () |
| 1626 |
| _ -> err () |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
let recursive_definition ~interactive_proof ~is_mes function_name rec_impls |
| 1631 |
type_of_f r rec_arg_num eq generate_induction_principle using_lemmas : |
| 1632 |
Declare.Proof.t option = |
| 1633 |
let open Term in |
| 1634 |
let open Constr in |
| 1635 |
let open CVars in |
| 1636 |
let env = Global.env () in |
| 1637 |
let evd = Evd.from_env env in |
| 1638 |
let evd, function_type = |
| 1639 |
interp_type_evars ~program_mode:false env evd type_of_f |
| 1640 |
in |
| 1641 |
let function_r = ERelevance.relevant in |
| 1642 |
|
| 1643 |
let env = |
| 1644 |
EConstr.push_named |
| 1645 |
(Context.Named.Declaration.LocalAssum |
| 1646 |
(make_annot function_name function_r, function_type)) |
| 1647 |
env |
| 1648 |
in |
| 1649 |
|
| 1650 |
let evd, ty = |
| 1651 |
interp_type_evars ~program_mode:false env evd ~impls:rec_impls eq |
| 1652 |
in |
| 1653 |
let evd = Evd.minimize_universes evd in |
| 1654 |
let equation_lemma_type = |
| 1655 |
Reductionops.nf_betaiotazeta env evd (Evarutil.nf_evar evd ty) |
| 1656 |
in |
| 1657 |
let function_type = |
| 1658 |
EConstr.to_constr ~abort_on_undefined_evars:false evd function_type |
| 1659 |
in |
| 1660 |
let equation_lemma_type = EConstr.Unsafe.to_constr equation_lemma_type in |
| 1661 |
|
| 1662 |
let res_vars, eq' = decompose_prod equation_lemma_type in |
| 1663 |
let env_eq' = |
| 1664 |
Environ.push_rel_context |
| 1665 |
(List.map (fun (x, y) -> LocalAssum (x, y)) res_vars) |
| 1666 |
env |
| 1667 |
in |
| 1668 |
let eq' = Reductionops.nf_zeta env_eq' evd (EConstr.of_constr eq') in |
| 1669 |
let eq' = EConstr.Unsafe.to_constr eq' in |
| 1670 |
let res = |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
match Constr.kind eq' with |
| 1675 |
| App (e, [|_; _; eq_fix|]) -> |
| 1676 |
mkLambda |
| 1677 |
( make_annot (Name function_name) Sorts.Relevant |
| 1678 |
, function_type |
| 1679 |
, subst_var function_name (compose_lam res_vars eq_fix) ) |
| 1680 |
| _ -> failwith "Recursive Definition (res not eq)" |
| 1681 |
in |
| 1682 |
let pre_rec_args, function_type_before_rec_arg = |
| 1683 |
decompose_prod_n (rec_arg_num - 1) function_type |
| 1684 |
in |
| 1685 |
let _, rec_arg_type, _ = destProd function_type_before_rec_arg in |
| 1686 |
let arg_ctx, _ = decompose_prod_n_decls (List.length res_vars) function_type in |
| 1687 |
let equation_id = add_suffix function_name "_equation" in |
| 1688 |
let functional_id = add_suffix function_name "_F" in |
| 1689 |
let term_id = add_suffix function_name "_terminate" in |
| 1690 |
let functional_ref = |
| 1691 |
let univs = Evd.univ_entry ~poly:PolyFlags.default evd in |
| 1692 |
declare_fun functional_id Decls.(IsDefinition Definition) ~univs res |
| 1693 |
in |
| 1694 |
|
| 1695 |
let evd = Evd.from_env (Global.env ()) in |
| 1696 |
let env_with_pre_rec_args = |
| 1697 |
push_rel_context |
| 1698 |
(List.map (function x, t -> LocalAssum (x, t)) pre_rec_args) |
| 1699 |
env |
| 1700 |
in |
| 1701 |
let relation, evuctx = interp_constr env_with_pre_rec_args evd r in |
| 1702 |
let () = check_relation_type env_with_pre_rec_args evd relation in |
| 1703 |
let evd = Evd.from_ctx evuctx in |
| 1704 |
let tcc_lemma_name = add_suffix function_name "_tcc" in |
| 1705 |
let tcc_lemma_constr = ref Undefined in |
| 1706 |
|
| 1707 |
let hook {Declare.Hook.S.uctx; dref; _ } = |
| 1708 |
assert (match dref with GlobRef.ConstRef cst -> Id.equal (Constant.label cst) term_id | _ -> assert false); |
| 1709 |
let f_ref = |
| 1710 |
declare_f function_name Decls.(IsProof Lemma) arg_ctx dref |
| 1711 |
in |
| 1712 |
let _ = |
| 1713 |
Extraction_plugin.Table.extraction_inline true [qualid_of_ident term_id] |
| 1714 |
in |
| 1715 |
|
| 1716 |
let stop = |
| 1717 |
|
| 1718 |
try |
| 1719 |
com_eqn uctx (List.length res_vars) equation_id functional_ref f_ref |
| 1720 |
dref |
| 1721 |
(subst_var function_name equation_lemma_type); |
| 1722 |
false |
| 1723 |
with e when CErrors.noncritical e -> |
| 1724 |
if do_observe () then |
| 1725 |
Feedback.msg_debug |
| 1726 |
(str "Cannot create equation Lemma " ++ CErrors.print e ++ str ".") |
| 1727 |
else |
| 1728 |
CErrors.user_err |
| 1729 |
( str "Cannot create equation lemma." |
| 1730 |
++ spc () |
| 1731 |
++ str "This may be because the function is nested-recursive." ); |
| 1732 |
true |
| 1733 |
in |
| 1734 |
if not stop then |
| 1735 |
let eq_ref = Nametab.locate (qualid_of_ident equation_id) in |
| 1736 |
let f_ref = destConst (constr_of_monomorphic_global (Global.env ()) f_ref) |
| 1737 |
and functional_ref = |
| 1738 |
destConst (constr_of_monomorphic_global (Global.env ()) functional_ref) |
| 1739 |
and eq_ref = destConst (constr_of_monomorphic_global (Global.env ()) eq_ref) in |
| 1740 |
generate_induction_principle f_ref tcc_lemma_constr functional_ref eq_ref |
| 1741 |
rec_arg_num |
| 1742 |
(EConstr.of_constr rec_arg_type) |
| 1743 |
(nb_prod evd (EConstr.of_constr res)) |
| 1744 |
relation |
| 1745 |
in |
| 1746 |
|
| 1747 |
funind_purify |
| 1748 |
(fun () -> |
| 1749 |
com_terminate interactive_proof tcc_lemma_name tcc_lemma_constr is_mes |
| 1750 |
functional_ref |
| 1751 |
(EConstr.of_constr rec_arg_type) |
| 1752 |
relation rec_arg_num term_id using_lemmas (List.length res_vars) evd |
| 1753 |
(Declare.Hook.make hook)) |
| 1754 |
() |