1 (* Copyright (C) 2002, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (* $Id: destructTactic.ml 9774 2009-05-15 19:37:08Z sacerdot $ *)
29 open Continuationals.Stack
33 if debug then (fun x -> prerr_endline (Lazy.force x)) else (fun _ -> ())
39 "z" ^ string_of_int !i
43 let id = if id = "_" then fresh_name () else id in
44 NotationPt.Ident (id,None)
47 let mk_sym s = NotationPt.Symbol (s,0);;
49 let rec mk_prods l t =
52 | hd::tl -> NotationPt.Binder (`Forall, (mk_id hd, None), mk_prods tl t)
59 | l -> NotationPt.Appl l
62 let rec iter f n acc =
64 else iter f (n-1) (f n acc)
67 let subst_metasenv_and_fix_names status =
68 let u,h,metasenv, subst,o = status#obj in
70 NCicUntrusted.map_obj_kind ~skip_body:true
71 (NCicUntrusted.apply_subst status subst []) o
73 status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv status subst metasenv,subst,o)
76 (* needed to workaround a weakness of the refiner? *)
77 let rec generalize0_tac tl s =
80 | t0::tl0 -> NTactics.generalize0_tac [t0] (generalize0_tac tl0 s)
84 (* input: nome della variabile riscritta
85 * output: lista dei nomi delle variabili il cui tipo dipende dall'input *)
86 let cascade_select_in_ctx status ~subst ctx skip iname =
87 let lctx, rctx = HExtlib.split_nth (iname - 1) ctx in
88 let lctx = List.rev lctx in
89 let rec rm_last = function
91 | hd::tl -> hd::(rm_last tl)
94 let indices,_ = List.fold_left
95 (fun (acc,context) item ->
97 | n,(NCic.Decl s | NCic.Def (s,_))
98 when (not (List.for_all (fun x -> NCicTypeChecker.does_not_occur status ~subst context (x-1) x s) acc)
99 && not (List.mem n skip)) ->
100 List.iter (fun m -> pp (lazy ("acc has " ^ (string_of_int m)))) acc;
101 pp (lazy ("acc occurs in the type of " ^ n));
102 (1::List.map ((+) 1) acc, item::context)
103 | _ -> (List.map ((+) 1) acc, item::context))
105 let indices = rm_last indices in
106 let res = List.map (fun n -> let s,_ = List.nth ctx (n-1) in s) indices in
107 List.iter (fun n -> pp (lazy n)) res;
108 pp (lazy (status#ppcontext ~metasenv:[] ~subst ctx));
112 let rec mk_fresh_name ctx firstch n =
113 let candidate = (String.make 1 firstch) ^ (string_of_int n) in
114 if (List.for_all (fun (s,_) -> s <> candidate) ctx) then candidate
115 else mk_fresh_name ctx firstch (n+1)
118 let arg_list nleft t =
119 let rec drop_prods n t =
122 | NCic.Prod (_,_,ta) -> drop_prods (n-1) ta
123 | _ -> raise (Failure "drop_prods")
125 let rec aux = function
126 | NCic.Prod (_,so,ta) -> so::aux ta
128 in aux (drop_prods nleft t)
131 let nargs it nleft consno =
132 pp (lazy (Printf.sprintf "nargs %d %d" nleft consno));
133 let _,indname,_,cl = it in
134 let _,_,t_k = List.nth cl consno in
135 List.length (arg_list nleft t_k) ;;
137 let default_pattern = "",0,(None,[],Some NotationPt.UserInput);;
139 "",0,(None,[],Some NotationPt.Binder
140 (`Pi, (mk_id "_",Some (NotationPt.Appl
141 [ NotationPt.Implicit `JustOne
142 ; NotationPt.Implicit `JustOne
143 ; NotationPt.UserInput
144 ; NotationPt.Implicit `JustOne ])),
145 NotationPt.Implicit `JustOne));;
147 let prod_pattern_jm =
148 "",0,(None,[],Some NotationPt.Binder
149 (`Pi, (mk_id "_",Some (NotationPt.Appl
150 [ NotationPt.Implicit `JustOne
151 ; NotationPt.Implicit `JustOne
152 ; NotationPt.UserInput
153 ; NotationPt.Implicit `JustOne
154 ; NotationPt.Implicit `JustOne ])),
155 NotationPt.Implicit `JustOne));;
158 "",0,(None,[n, NotationPt.Appl
159 [ NotationPt.Implicit `JustOne
160 ; NotationPt.Implicit `JustOne
161 ; NotationPt.UserInput
162 ; NotationPt.Implicit `JustOne ] ],
165 let hp_pattern_jm n =
166 "",0,(None,[n, NotationPt.Appl
167 [ NotationPt.Implicit `JustOne
168 ; NotationPt.Implicit `JustOne
169 ; NotationPt.UserInput
170 ; NotationPt.Implicit `JustOne
171 ; NotationPt.Implicit `JustOne ] ],
174 (* creates the discrimination = injection+contradiction principle *)
175 exception ConstructorTooBig of string;;
177 let mk_discriminator ~use_jmeq name it leftno status baseuri =
179 let _,_,arity,_ = it in
180 List.length (arg_list 0 arity) in
181 let _,itname,_,_ = it in
182 let params = List.map (fun x -> "a" ^ string_of_int x) (HExtlib.list_seq 1 (itnargs+1)) in
183 let xyty = mk_appl (List.map mk_id (itname::params)) in
185 (* PHASE 1: derive the type of the discriminator (we'll name it "principle") *)
187 let mk_eq tys ts us es n =
189 mk_appl [mk_id "jmeq";
190 NotationPt.Implicit `JustOne; List.nth ts n;
191 NotationPt.Implicit `JustOne; List.nth us n]
193 (* eqty = Tn u0 e0...un-1 en-1 *)
195 (List.nth tys n :: iter (fun i acc ->
200 (* params = [T0;t0;...;Tn;tn;u0;e0;un-1;en-1] *)
201 let params = iter (fun i acc ->
203 List.nth ts i :: acc) n
206 List.nth es i:: acc) (n-1) []) in
207 mk_appl [mk_id "eq"; eqty;
208 mk_appl (mk_id ("R" ^ string_of_int n) :: params);
216 let _,name,_ = List.nth cl j in
220 let branch i j ts us =
221 let nargs = nargs it leftno i in
222 let es = List.map (fun x -> mk_id ("e" ^ string_of_int x)) (HExtlib.list_seq 0 nargs) in
226 NotationPt.Binder (`Lambda, (mk_id ("x" ^ string_of_int i), None),
227 NotationPt.Binder (`Lambda, (mk_id ("p" ^ string_of_int i), None),
229 (NotationPt.Implicit (`Tagged ("T" ^ (string_of_int x)))))
230 (HExtlib.list_seq 0 nargs) in
233 NotationPt.Binder (`Lambda, (mk_id ("x" ^ string_of_int i), None),
234 NotationPt.Binder (`Lambda, (mk_id ("p" ^ string_of_int i), None),
236 (mk_appl [mk_id "eq"; NotationPt.Implicit `JustOne;
237 mk_appl (mk_id (kname i)::
238 List.map (fun x -> mk_id ("x" ^string_of_int x))
239 (HExtlib.list_seq 0 (List.length ts)));
240 mk_appl (mk_id (kname j)::us)])]
242 (** NotationPt.Binder (`Lambda, (mk_id "e",
245 NotationPt.Implicit `JustOne;
246 mk_appl (mk_id (kname it i)::ts);
247 mk_appl (mk_id (kname it j)::us)])),
248 let ts = ts @ [mk_id "e"] in
251 NotationPt.Implicit `JustOne;
252 mk_appl (mk_id (kname it j)::us)] in
253 let us = us @ [refl2] in *)
254 NotationPt.Binder (`Forall, (mk_id "P", Some (NotationPt.Sort (`NType "1") )),
256 NotationPt.Binder (`Forall, (mk_id "_",
257 Some (iter (fun i acc ->
258 NotationPt.Binder (`Forall, (List.nth es i, Some (mk_eq tys ts us es i)), acc))
260 (** (NotationPt.Binder (`Forall, (mk_id "_",
261 Some (mk_eq tys ts us es nargs)),*)
262 (mk_id "P"))), mk_id "P")
266 let inner i ts = NotationPt.Case
268 (*Some (NotationPt.Binder (`Lambda, (mk_id "y",None),
269 NotationPt.Binder (`Forall, (mk_id "_", Some
270 (mk_appl [mk_id "eq";NotationPt.Implicit
271 `JustOne;(*NotationPt.Implicit `JustOne*)
272 mk_appl (mk_id (kname it i)::ts);mk_id "y"])),
273 NotationPt.Implicit `JustOne )))*)
277 let nargs_kty = nargs it leftno j in
278 let us = iter (fun m acc -> mk_id ("u" ^ (string_of_int m))::acc)
279 (nargs_kty - 1) [] in
281 iter (fun _ acc -> None::acc) (nargs_kty - 1) [] in
282 NotationPt.Pattern (kname j,
284 List.combine us nones),
286 (HExtlib.list_seq 0 (List.length cl)))
288 let outer = NotationPt.Case
293 let nargs_kty = nargs it leftno i in
294 if (nargs_kty > 5 && not use_jmeq) then raise (ConstructorTooBig (kname i));
295 let ts = iter (fun m acc -> mk_id ("t" ^ (string_of_int m))::acc)
296 (nargs_kty - 1) [] in
298 iter (fun _ acc -> None::acc) (nargs_kty - 1) [] in
299 NotationPt.Pattern (kname i,
301 List.combine ts nones),
303 (HExtlib.list_seq 0 (List.length cl))) in
305 mk_prods params (NotationPt.Binder (`Forall, (mk_id "x",
307 NotationPt.Binder (`Forall, (mk_id "y", Some xyty),
309 NotationPt.Binder (`Forall, (mk_id "e",
311 [mk_sym "jmsimeq"; NotationPt.Implicit `JustOne; mk_id "x";
312 NotationPt.Implicit `JustOne; mk_id "y"])),
315 NotationPt.Binder (`Forall, (mk_id "e",
316 Some (mk_appl [mk_sym "eq";NotationPt.Implicit `JustOne; mk_id "x"; mk_id "y"])),
319 pp (lazy ("discriminator = " ^ (NotationPp.pp_term status principle)));
321 (* PHASE 2: create the object for the proof of the principle: we'll name it
323 let status, theorem =
324 GrafiteDisambiguate.disambiguate_nobj status ~baseuri
325 (baseuri ^ name ^ ".def",0,
327 (`Theorem,name,principle,
328 Some (NotationPt.Implicit (`Tagged "inv")),`DiscriminationPrinciple))
330 let uri,height,nmenv,nsubst,nobj = theorem in
331 let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
332 let status = status#set_obj theorem in
333 let status = status#set_stack ninitial_stack in
334 let status = subst_metasenv_and_fix_names status in
336 (* PHASE 3: we finally prove the discrimination principle *)
337 let dbranch it ~use_jmeq leftno consno =
338 let refl_id = mk_sym "refl" in
339 pp (lazy (Printf.sprintf "dbranch %d %d" leftno consno));
340 let nlist = HExtlib.list_seq 0 (nargs it leftno consno) in
341 (* (\forall ...\forall P.\forall DH : ( ... = ... -> P). P) *)
342 let params = List.map (fun x -> NTactics.intro_tac ("a" ^ string_of_int x)) nlist in
343 NTactics.reduce_tac ~reduction:(`Normalize true) ~where:default_pattern::
345 NTactics.intro_tac "P";
346 NTactics.intro_tac "DH";
347 NTactics.apply_tac ("",0,mk_id "DH");
348 NTactics.apply_tac ("",0,refl_id); (* well, it works even if no goal is selected after applying DH... *)
350 let dbranches it ~use_jmeq leftno =
351 pp (lazy (Printf.sprintf "dbranches %d" leftno));
352 let nbranches = List.length cl in
353 let branches = iter (fun n acc ->
354 let m = nbranches - n - 1 in
355 if m = 0 then acc @ (dbranch it ~use_jmeq leftno m)
356 else acc @ NTactics.shift_tac :: (dbranch it ~use_jmeq
359 if nbranches > 1 then
360 NTactics.branch_tac ~force:false:: branches @ [NTactics.merge_tac]
363 let print_tac s status = pp s ; status in
367 [print_tac (lazy "ci sono");
368 NTactics.reduce_tac ~reduction:(`Normalize true) ~where:default_pattern
370 @ List.map (fun x -> NTactics.intro_tac x) params @
371 [NTactics.intro_tac "x";
372 NTactics.intro_tac "y";
373 NTactics.intro_tac "Deq";
374 print_tac (lazy "ci sono 2");
375 NTactics.rewrite_tac ~dir:`RightToLeft ~what:("",0,mk_id "Deq") ~where:default_pattern;
376 NTactics.cases_tac ~what:("",0,mk_id "x") ~where:default_pattern]
377 @ dbranches it ~use_jmeq leftno) status
378 in status, status#obj
381 let hd_of_term = function
382 | NCic.Appl (hd::_) -> hd
386 let name_of_rel ~context rel =
387 let s, _ = List.nth context (rel-1) in s
390 (* let lookup_in_ctx ~context n =
391 List.nth context ((List.length context) - n - 1)
394 let discriminate_tac ~context cur_eq status =
395 pp (lazy (Printf.sprintf "discriminate: equation %s" (name_of_rel ~context cur_eq)));
398 let eq_name,(NCic.Decl s | NCic.Def (s,_)) = List.nth context (cur_eq-1) in
399 let _,ctx' = HExtlib.split_nth cur_eq context in
400 let status, s = NTacStatus.whd status ctx' (mk_cic_term ctx' s) in
401 let status, s = term_of_cic_term status s ctx' in
402 let status,it,use_jmeq =
403 let it,use_jmeq = match s with
404 | NCic.Appl [_;it;_;_] -> it,false
405 | NCic.Appl [_;it;_;_;_] -> it,true
406 | _ -> assert false in
407 (* XXX: serve? ho già fatto whd *)
408 let status, it = whd status ctx' (mk_cic_term ctx' it) in
409 let status, it = term_of_cic_term status it ctx' in
410 let _uri,indtyno,its = match it with
411 | NCic.Const (NReference.Ref (uri, NReference.Ind (_,indtyno,_)) as r)
412 | NCic.Appl (NCic.Const
413 (NReference.Ref (uri, NReference.Ind (_,indtyno,_)) as r)::_) ->
414 uri, indtyno, NCicEnvironment.get_checked_indtys status r
415 | _ -> pp (lazy ("discriminate: indty =" ^ status#ppterm
416 ~metasenv:[] ~subst:[] ~context:[] it)) ; assert false in
417 let _,_,its,_,_ = its in
418 status,List.nth its indtyno, use_jmeq
422 let _,_,arity,_ = it in
423 List.length (arg_list 0 arity) in
424 let _,itname,_,_ = it in
425 let params = List.map (fun x -> "a" ^ string_of_int x) (HExtlib.list_seq 1 (itnargs+1)) in
427 if use_jmeq then itname ^ "_jmdiscr"
428 else itname ^ "_discr"
430 pp (lazy ("apply (" ^ principle_name ^ " " ^
431 (String.concat "" (HExtlib.mk_list "?" (List.length params + 2))) ^
432 " " ^ eq_name ^ ")"));
433 NTactics.apply_tac ("",0,mk_appl ([mk_id principle_name]@
434 HExtlib.mk_list (NotationPt.Implicit `JustOne) (List.length params + 2) @
435 [mk_id eq_name ])) status
438 let saturate_skip status context skip =
442 let ix = HExtlib.list_index ((=) x) (List.map fst context)
446 fst (cascade_select_in_ctx status ~subst:(get_subst status) context [] (i+1)) @ acc) skip skip)
449 let subst_tac ~context ~dir skip cur_eq =
450 fun status as oldstatus ->
451 let eq_name,(NCic.Decl s | NCic.Def (s,_)) = List.nth context (cur_eq-1) in
452 let _,ctx' = HExtlib.split_nth cur_eq context in
453 let status, s = NTacStatus.whd status ctx' (mk_cic_term ctx' s) in
454 let status, s = term_of_cic_term status s ctx' in
455 let skip = saturate_skip status context skip in
456 pp (lazy (Printf.sprintf "subst: equation %s" eq_name));
457 let l, r = match s with
458 | NCic.Appl [_;_;t1;t2] | NCic.Appl [_;_;t1;_;t2] -> t1,t2
459 | _ -> assert false in
460 let var = match dir with
462 | `RightToLeft -> r in
463 (* let var = match var with
465 | _ -> assert false in *)
466 let names_to_gen, _ =
469 cascade_select_in_ctx status ~subst:(get_subst status) context skip (var+cur_eq)
470 | _ -> cascade_select_in_ctx status ~subst:(get_subst status) context skip cur_eq in
471 let names_to_gen = List.filter (fun n -> n <> eq_name) names_to_gen in
472 if (List.exists (fun x -> List.mem x skip) names_to_gen)
477 let x' = String.concat " " x in
478 let x = List.map mk_id x in
479 (* let s = NTactics.print_tac false ("@generalize " ^ x') s in *)
480 generalize0_tac x s) in
482 (* (List.map gen_tac names_to_gen)@ *)
483 [gen_tac (List.rev names_to_gen);
484 NTactics.clear_tac names_to_gen;
485 NTactics.rewrite_tac ~dir
486 ~what:("",0,mk_id eq_name) ~where:default_pattern;
487 (* NTactics.reduce_tac ~reduction:(`Normalize true)
488 ~where:default_pattern;*)
489 NTactics.try_tac (NTactics.clear_tac [eq_name]);
491 (List.map NTactics.intro_tac (List.rev names_to_gen))) status
494 let clearid_tac ~context skip cur_eq =
496 let eq_name,(NCic.Decl s | NCic.Def (s,_)) = List.nth context (cur_eq-1) in
497 let _,ctx' = HExtlib.split_nth cur_eq context in
498 let status, s = NTacStatus.whd status ctx' (mk_cic_term ctx' s) in
499 let status, s = term_of_cic_term status s ctx' in
500 let skip = saturate_skip status context skip in
502 pp (lazy (Printf.sprintf "clearid: equation %s" eq_name));
503 let streicher_id = mk_id "streicherK" in
504 let names_to_gen, _ =
505 cascade_select_in_ctx status ~subst:(get_subst status) context skip cur_eq in
506 let gen_tac x = generalize0_tac (List.map mk_id x) in
510 | NCic.Appl [_;_;_;_;_] ->
511 let names_to_gen = List.rev names_to_gen in
512 (*let gen_eq = NTactics.generalize_tac
513 ~where:("",0,(Some (mk_appl [mk_id "jmeq_to_eq";
514 NotationPt.Implicit `JustOne;
515 NotationPt.Implicit `JustOne;
516 NotationPt.Implicit `JustOne;
517 mk_id eq_name]),[], Some
518 NotationPt.UserInput)) in*)
519 let gen_eq = generalize0_tac
520 [mk_appl [mk_id "jmeq_to_eq";
521 NotationPt.Implicit `JustOne;
522 NotationPt.Implicit `JustOne;
523 NotationPt.Implicit `JustOne;
525 NTactics.block_tac ((gen_tac (List.rev names_to_gen))::gen_eq::
526 [NTactics.clear_tac names_to_gen;
527 NTactics.try_tac (NTactics.clear_tac [eq_name]);
528 NTactics.apply_tac ("",0, mk_appl [streicher_id;
529 NotationPt.Implicit `JustOne;
530 NotationPt.Implicit `JustOne;
531 NotationPt.Implicit `JustOne;
532 NotationPt.Implicit `JustOne]);
534 (List.map NTactics.intro_tac names_to_gen)) status
537 | NCic.Appl [_;_;_;_] ->
539 let names_to_gen, _ =
540 cascade_select_in_ctx status ~subst:(get_subst status) context skip cur_eq in
541 let names_to_gen = eq_name :: (List.rev names_to_gen) in
542 NTactics.block_tac ((gen_tac names_to_gen)::
543 [NTactics.clear_tac names_to_gen;
544 NTactics.apply_tac ("",0, mk_appl [streicher_id;
545 NotationPt.Implicit `JustOne;
546 NotationPt.Implicit `JustOne;
547 NotationPt.Implicit `JustOne;
548 NotationPt.Implicit `JustOne])
549 (* NTactics.reduce_tac ~reduction:(`Normalize true)
550 ~where:default_pattern *)
552 let names_to_intro = List.tl names_to_gen in
553 (List.map NTactics.intro_tac names_to_intro)) status
559 let get_ctx st goal =
560 ctx_of (get_goalty st goal)
563 (* = select + classify *)
564 let select_eq ctx acc domain status goal =
565 let classify ~use_jmeq ~subst ctx' l r =
566 (* FIXME: metasenv *)
567 if NCicReduction.are_convertible status ~metasenv:[] ~subst ctx' l r
568 then status, `Identity
569 else status, (match hd_of_term l, hd_of_term r with
570 | NCic.Const (NReference.Ref (_,NReference.Con (_,ki,nleft)) as kref),
571 NCic.Const (NReference.Ref (_,NReference.Con (_,kj,_))) ->
572 if ki != kj then `Discriminate (0,true, use_jmeq)
574 let rit = NReference.mk_indty true kref in
575 let _,_,its,_,itno = NCicEnvironment.get_checked_indtys status rit in
576 let it = List.nth its itno in
577 let newprods = nargs it nleft (ki-1) in
578 `Discriminate (newprods, false, use_jmeq)
580 when NCicTypeChecker.does_not_occur status ~subst ctx' (j-1) j r
581 && l = NCic.Rel j -> `Subst `LeftToRight
583 when NCicTypeChecker.does_not_occur status ~subst ctx' (j-1) j l
584 && r = NCic.Rel j -> `Subst `RightToLeft
585 | (NCic.Rel _, _ | _, NCic.Rel _ ) -> `Cycle (* could be a blob too... *)
589 let index = List.length ctx - i in
590 pp (lazy ("provo classify di index = " ^string_of_int index));
591 match (List.nth ctx (index - 1)) with
592 | n, (NCic.Decl ty | NCic.Def (ty,_)) ->
593 (let _,ctx_ty = HExtlib.split_nth index ctx in
594 let status, ty = NTacStatus.whd status ctx_ty (mk_cic_term ctx_ty ty) in
595 let status, ty = term_of_cic_term status ty ctx_ty in
596 pp (lazy (Printf.sprintf "select_eq tries %s" (status#ppterm ~context:ctx_ty ~subst:[] ~metasenv:[] ty)));
597 let status, kind = match ty with
598 | NCic.Appl [NCic.Const (NReference.Ref (u,_)) ;_;l;r]
599 when NUri.name_of_uri u = "eq" ->
600 classify ~use_jmeq:false ~subst:(get_subst status) ctx_ty l r
601 | NCic.Appl [NCic.Const (NReference.Ref (u,_)) ;lty;l;rty;r]
602 when NUri.name_of_uri u = "jmeq" &&
603 NCicReduction.are_convertible status ~metasenv:[]
604 ~subst:(get_subst status) ctx_ty lty rty
605 -> classify ~use_jmeq:true ~subst:(get_subst status) ctx_ty l r
606 | _ -> status, `NonEq
609 let status, goalty = term_of_cic_term status (get_goalty status goal) ctx in
610 status, Some (List.length ctx - i), kind
611 | `Cycle | `Blob | `NonEq -> aux (i+1) (* XXX: skip cyclic/blob equations for now *)
613 if (List.for_all (fun x -> x <> n) acc) &&
614 (List.exists (fun x -> x = n) domain)
615 then status, Some (List.length ctx - i), kind
617 with Failure _ | Invalid_argument _ -> status, None, `Blob
621 let tagged_intro_tac curtag name =
623 | `Notag -> NTactics.intro_tac name
626 [ NTactics.intro_tac name
627 ; NTactics.reduce_tac
628 ~reduction:(`Whd true) ~where:((if use_jmeq then hp_pattern_jm else hp_pattern) name) ]
631 distribute_tac (fun s g ->
632 let eq_name,(NCic.Decl s | NCic.Def (s,_)) = List.nth context (cur_eq-1) in
633 let _,ctx' = HExtlib.split_nth cur_eq context in
634 let status, s = NTacStatus.whd status ctx' (mk_cic_term ctx' s) in
635 let status, s = term_of_cic_term status s ctx' in
638 | NCic.Appl [_;it;t1;t2] -> false
639 | NCic.Appl [_;it;t1;_;t2] -> true
640 | _ -> assert false in
642 let it, t1, t2, use_jmeq = match s with
643 | NCic.Appl [_;it;t1;t2] -> it,t1,t2,false
644 | NCic.Appl [_;it;t1;_;t2] -> it,t1,t2,true
645 | _ -> assert false in
646 [ NTactics.intro_tac name
647 ; NTactics.reduce_tac ~reduction:(`Whd true) ~where:prod_pattern ]*)
650 let rec destruct_tac0 tags acc domain skip status goal =
656 let pptags tags = String.concat ", " (List.map pptag tags) in
657 let ctx = get_ctx status goal in
658 let subst = get_subst status in
659 let get_newgoal os ns ogoal =
660 let go, gc = NTactics.compare_statuses ~past:os ~present:ns in
661 let go' = ([ogoal] @- gc) @+ go in
662 match go' with [] -> assert false | g::_ -> g
664 let status, selection, kind = select_eq ctx acc domain status goal in
665 pp (lazy ("destruct: acc is " ^ String.concat "," acc ));
666 match selection, kind with
668 pp (lazy (Printf.sprintf
669 "destruct: no selection, context is %s, stack is %s"
670 (status#ppcontext ~metasenv:[] ~subst ctx) (pptags tags)));
674 let fresh = mk_fresh_name ctx 'e' 0 in
675 let status' = NTactics.exec (tagged_intro_tac curtag fresh) status goal in
676 destruct_tac0 tags' acc (fresh::domain) skip status'
677 (get_newgoal status status' goal))
678 | Some cur_eq, `Discriminate (newprods,conflict,use_jmeq) ->
679 pp (lazy (Printf.sprintf
680 "destruct: discriminate - nselection is %d, context is %s,stack is %s"
681 cur_eq (status#ppcontext ~metasenv:[] ~subst ctx) (pptags tags)));
682 let status' = NTactics.exec (discriminate_tac ~context:ctx cur_eq) status goal in
683 if conflict then status'
685 let newtags = HExtlib.mk_list (`Eq use_jmeq) newprods in
686 destruct_tac0 (newtags@tags)
687 (name_of_rel ~context:ctx cur_eq::acc)
688 (List.filter (fun x -> x <> name_of_rel ~context:ctx cur_eq) domain)
690 status' (get_newgoal status status' goal)
691 | Some cur_eq, `Subst dir ->
692 pp (lazy (Printf.sprintf
693 "destruct: subst - selection is %d, context is %s, stack is %s"
694 cur_eq (status#ppcontext ~metasenv:[] ~subst ctx) (pptags tags)));
695 let status' = NTactics.exec (subst_tac ~context:ctx ~dir skip cur_eq) status goal in
696 pp (lazy (Printf.sprintf " ctx after subst = %s" (status#ppcontext ~metasenv:[] ~subst (get_ctx status' (get_newgoal status status' goal)))));
697 let eq_name,_ = List.nth ctx (cur_eq-1) in
698 let newgoal = get_newgoal status status' goal in
701 let _ = NTactics.find_in_context eq_name (get_ctx status' newgoal) in false
703 let rm_eq b l = if b then List.filter (fun x -> x <> eq_name) l else l in
704 let acc = rm_eq has_cleared acc in
705 let skip = rm_eq has_cleared skip in
706 let domain = rm_eq has_cleared domain in
707 destruct_tac0 tags acc domain skip status' newgoal
708 | Some cur_eq, `Identity ->
709 pp (lazy (Printf.sprintf
710 "destruct: identity - selection is %d, context is %s, stack is %s"
711 cur_eq (status#ppcontext ~metasenv:[] ~subst ctx) (pptags tags)));
712 let eq_name,_ = List.nth ctx (cur_eq-1) in
713 let status' = NTactics.exec (clearid_tac ~context:ctx skip cur_eq) status goal in
714 let newgoal = get_newgoal status status' goal in
717 let _ = NTactics.find_in_context eq_name (get_ctx status' newgoal) in false
719 let rm_eq b l = if b then List.filter (fun x -> x <> eq_name) l else l in
720 let acc = rm_eq has_cleared acc in
721 let skip = rm_eq has_cleared skip in
722 let domain = rm_eq has_cleared domain in
723 destruct_tac0 tags acc domain skip status' newgoal
724 | Some cur_eq, `Cycle -> (* TODO, should never happen *)
725 pp (lazy (Printf.sprintf
726 "destruct: cycle - selection is %d, context is %s, stack is %s"
727 cur_eq (status#ppcontext ~metasenv:[] ~subst ctx) (pptags tags)));
729 | Some cur_eq, `Blob ->
730 pp (lazy (Printf.sprintf
731 "destruct: blob - selection is %d, context is %s, stack is %s"
732 cur_eq (status#ppcontext ~metasenv:[] ~subst ctx) (pptags tags)));
737 let destruct_tac dom skip s =
738 NTactics.distribute_tac
740 let ctx = get_ctx s' g in
741 let domain = match dom with
742 | None -> List.map (fun (n,_) -> n) ctx
745 destruct_tac0 [] [] domain skip s' g) s;;