]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nDestructTac.ml
01ca13ee1f931a91e2f0be628fdab39f35b550c7
[helm.git] / helm / software / components / ng_tactics / nDestructTac.ml
1 (* Copyright (C) 2002, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (* $Id: destructTactic.ml 9774 2009-05-15 19:37:08Z sacerdot $ *)
27
28 open NTacStatus
29 open Continuationals.Stack
30
31 let debug = true 
32 let pp = 
33   if debug then (fun x -> prerr_endline (Lazy.force x)) else (fun _ -> ())
34
35 let fresh_name =
36  let i = ref 0 in
37  function () ->
38   incr i;
39   "z" ^ string_of_int !i
40 ;;
41
42 let mk_id id =
43  let id = if id = "_" then fresh_name () else id in
44   CicNotationPt.Ident (id,None)
45 ;;
46
47 let rec mk_prods l t =
48   match l with
49     [] -> t
50   | hd::tl -> CicNotationPt.Binder (`Forall, (mk_id hd, None), mk_prods tl t)
51 ;;
52
53 let mk_appl =
54  function
55     [] -> assert false
56   | [x] -> x
57   | l -> CicNotationPt.Appl l
58 ;;
59
60 let rec iter f n acc =
61   if n < 0 then acc
62   else iter f (n-1) (f n acc)
63 ;;
64
65 let subst_metasenv_and_fix_names status =
66   let u,h,metasenv, subst,o = status#obj in
67   let o = 
68     NCicUntrusted.map_obj_kind ~skip_body:true 
69      (NCicUntrusted.apply_subst subst []) o
70   in
71    status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv subst metasenv,subst,o)
72 ;;
73
74 (* input: nome della variabile riscritta
75  * output: lista dei nomi delle variabili il cui tipo dipende dall'input *)
76 let cascade_select_in_ctx ~subst ctx iname =
77   prerr_endline "C";
78   let lctx, rctx = HExtlib.split_nth (iname - 1) ctx in
79   let lctx = List.rev lctx in
80   let rec rm_last = function
81       [] | [_] -> []
82     | hd::tl -> hd::(rm_last tl)
83   in
84
85   let indices,_ = List.fold_left
86        (fun (acc,context) item -> 
87          prerr_endline "C2";
88           match item with
89             | n,(NCic.Decl s | NCic.Def (s,_)) 
90                   when not (List.for_all (fun x -> NCicTypeChecker.does_not_occur ~subst context (x-1) x s) acc) ->
91                 List.iter (fun m -> prerr_endline ("acc has " ^ (string_of_int m))) acc;
92                 prerr_endline ("acc occurs in the type of " ^ n);
93                 (1::List.map ((+) 1) acc, item::context)
94             | _ -> (List.map ((+) 1) acc, item::context))
95        ([1], rctx) lctx in
96     prerr_endline "C3:";
97     List.iter (fun n -> prerr_endline (string_of_int n)) indices;
98     let indices = rm_last indices in
99     let res = List.map (fun n -> let s,_ = List.nth ctx (n-1) in s) indices in
100     prerr_endline "C4:";
101     List.iter (fun n -> prerr_endline n) res;
102     prerr_endline (NCicPp.ppcontext ~metasenv:[] ~subst ctx);
103     res, indices
104 ;;
105
106 let rec mk_fresh_name ctx firstch n =
107   let candidate = (String.make 1 firstch) ^ (string_of_int n) in
108   if (List.for_all (fun (s,_) -> s <> candidate) ctx) then candidate
109   else mk_fresh_name ctx firstch (n+1)
110 ;;
111
112 let arg_list nleft t =
113   let rec drop_prods n t =
114     if n <= 0 then t
115     else match t with
116       | NCic.Prod (_,_,ta) -> drop_prods (n-1) ta
117       | _ -> raise (Failure "drop_prods")
118   in
119   let rec aux = function
120     | NCic.Prod (_,so,ta) -> so::aux ta
121     | _ -> []
122   in aux (drop_prods nleft t)
123 ;;
124
125 let nargs it nleft consno =
126   prerr_endline (Printf.sprintf "nargs %d %d" nleft consno);
127   let _,indname,_,cl = it in
128   let _,_,t_k = List.nth cl consno in
129   List.length (arg_list nleft t_k) ;;
130
131 let default_pattern = "",0,(None,[],Some CicNotationPt.UserInput);;
132
133 (* returns the discrimination = injection+contradiction principle *)
134 (* FIXME: mi riservo di considerare tipi con parametri sx alla fine *)
135
136 let mk_discriminator it nleft status =
137   let _,indname,_,cl = it in
138
139
140   let mk_eq tys ts us es n =
141     (* eqty = Tn u0 e0...un-1 en-1 *)
142     let eqty = mk_appl 
143                  (List.nth tys n :: iter (fun i acc -> 
144                                            List.nth us i::
145                                            List.nth es i:: acc) 
146                                      (n-1) []) in
147
148     (* params = [T0;t0;...;Tn;tn;u0;e0;un-1;en-1] *)
149     let params = iter (fun i acc -> 
150                          List.nth tys i ::
151                          List.nth ts i :: acc) n
152                      (iter (fun i acc ->
153                             List.nth us i::
154                             List.nth es i:: acc) (n-1) []) in
155     mk_appl [mk_id "eq"; eqty;
156                         mk_appl (mk_id ("R" ^ string_of_int n) :: params);
157                         List.nth us n] 
158   in
159
160   let kname it j =
161     let _,_,_,cl = it in
162     let _,name,_ = List.nth cl j in
163     name
164   in
165
166   let branch i j ts us = 
167     let nargs = nargs it nleft i in
168     let es = List.map (fun x -> mk_id ("e" ^ string_of_int x)) (HExtlib.list_seq 0 nargs) in
169     let tys = List.map 
170                 (fun x -> CicNotationPt.Implicit (`Tagged ("T" ^ (string_of_int x)))) 
171                 (HExtlib.list_seq 0 nargs) in
172     let tys = tys @ 
173       [iter (fun i acc -> 
174         CicNotationPt.Binder (`Lambda, (mk_id ("x" ^ string_of_int i), None),
175         CicNotationPt.Binder (`Lambda, (mk_id ("p" ^ string_of_int i), None),
176         acc))) (nargs-1)
177         (mk_appl [mk_id "eq"; CicNotationPt.Implicit `JustOne;
178           mk_appl (mk_id (kname it i)::
179            List.map (fun x -> mk_id ("x" ^string_of_int x))
180               (HExtlib.list_seq 0 (List.length ts)));
181               mk_appl (mk_id (kname it j)::us)])]
182     in
183     (** CicNotationPt.Binder (`Lambda, (mk_id "e", 
184       Some (mk_appl 
185         [mk_id "eq";
186          CicNotationPt.Implicit `JustOne;
187          mk_appl (mk_id (kname it i)::ts);
188          mk_appl (mk_id (kname it j)::us)])),
189     let ts = ts @ [mk_id "e"] in 
190     let refl2 = mk_appl
191                   [mk_id "refl";
192                    CicNotationPt.Implicit `JustOne;
193                    mk_appl (mk_id (kname it j)::us)] in
194     let us = us @ [refl2] in *)
195     CicNotationPt.Binder (`Forall, (mk_id "P", Some (CicNotationPt.Sort (`NType "1") )),
196       if i = j then 
197        CicNotationPt.Binder (`Forall, (mk_id "_",
198         Some (iter (fun i acc -> 
199               CicNotationPt.Binder (`Forall, (List.nth es i, Some (mk_eq tys ts us es i)), acc))
200               (nargs-1) 
201               (** (CicNotationPt.Binder (`Forall, (mk_id "_", 
202                 Some (mk_eq tys ts us es nargs)),*)
203                 (mk_id "P"))), mk_id "P")
204       else mk_id "P")
205   in
206
207   let inner i ts = CicNotationPt.Case 
208               (mk_id "y",None,
209                (*Some (CicNotationPt.Binder (`Lambda, (mk_id "y",None), 
210                  CicNotationPt.Binder (`Forall, (mk_id "_", Some
211                  (mk_appl [mk_id "eq";CicNotationPt.Implicit
212                  `JustOne;(*CicNotationPt.Implicit `JustOne*)
213                   mk_appl (mk_id (kname it i)::ts);mk_id "y"])),
214                  CicNotationPt.Implicit `JustOne )))*)
215                   None,
216                   List.map
217                   (fun j -> 
218                      let nargs_kty = nargs it nleft j in
219                      let us = iter (fun m acc -> mk_id ("u" ^ (string_of_int m))::acc) 
220                                 (nargs_kty - 1) [] in
221                      let nones = 
222                        iter (fun _ acc -> None::acc) (nargs_kty - 1) [] in
223                      CicNotationPt.Pattern (kname it j,
224                                             None,
225                                             List.combine us nones), 
226                                 branch i j ts us)
227                   (HExtlib.list_seq 0 (List.length cl)))
228   in
229   let outer = CicNotationPt.Case
230                 (mk_id "x",None,
231                  None ,
232                  List.map
233                    (fun i -> 
234                       let nargs_kty = nargs it nleft i in
235                       let ts = iter (fun m acc -> mk_id ("t" ^ (string_of_int m))::acc)
236                                  (nargs_kty - 1) [] in
237                      let nones = 
238                        iter (fun _ acc -> None::acc) (nargs_kty - 1) [] in
239                       CicNotationPt.Pattern (kname it i,
240                                              None,
241                                              List.combine ts nones),
242                                 inner i ts)
243                    (HExtlib.list_seq 0 (List.length cl))) in
244   let principle = CicNotationPt.Binder (`Lambda, (mk_id "x", (*Some (mk_id indname)*) None),
245                         CicNotationPt.Binder (`Lambda, (mk_id "y", (*Some (mk_id indname)*) None), outer))
246   in
247   pp (lazy ("discriminator = " ^ (CicNotationPp.pp_term principle)));
248   
249   status, principle 
250 ;;
251
252 let hd_of_term = function
253   | NCic.Appl (hd::_) -> hd
254   | t -> t
255 ;;
256
257 let name_of_rel ~context rel =
258   let s, _ = List.nth context (rel-1) in s
259 ;;
260
261 (* let lookup_in_ctx ~context n =
262   List.nth context ((List.length context) - n - 1)
263 ;;*)
264
265 let discriminate_tac ~context cur_eq status =
266   pp (lazy (Printf.sprintf "discriminate: equation %s" (name_of_rel ~context cur_eq)));
267
268   let dbranch it leftno consno =
269     prerr_endline (Printf.sprintf "dbranch %d %d" leftno consno);
270     let nlist = HExtlib.list_seq 0 (nargs it leftno consno) in
271     (* (\forall ...\forall P.\forall DH : ( ... = ... -> P). P) *)
272     let params = List.map (fun x -> prerr_endline (Printf.sprintf "dbranch param a%d" x); NTactics.intro_tac ("a" ^ string_of_int x)) nlist in
273         NTactics.reduce_tac ~reduction:(`Normalize true) ~where:default_pattern::
274         params @ [
275         NTactics.intro_tac "P";
276         NTactics.intro_tac "DH";
277         NTactics.apply_tac ("",0,mk_id "DH");
278         NTactics.apply_tac ("",0,mk_id "refl");
279     ] in
280   let dbranches it leftno =
281     prerr_endline (Printf.sprintf "dbranches %d" leftno);
282     let _,_,_,cl = it in
283     let nbranches = List.length cl in 
284     let branches = iter (fun n acc -> 
285       let m = nbranches - n - 1 in
286       if m = 0 then (prerr_endline "no shift"; acc @ (dbranch it leftno m))
287       else (prerr_endline "sì shift"; acc @ NTactics.shift_tac :: (dbranch it
288       leftno m)))
289       (nbranches-1) [] in
290     if nbranches > 1 then
291       (prerr_endline "sì branch";
292          NTactics.branch_tac:: branches @ [NTactics.merge_tac])
293     else
294       (prerr_endline "no branch";
295       branches)
296   in
297   
298   let eq_name,(NCic.Decl s | NCic.Def (s,_)) = List.nth context (cur_eq-1) in
299   let _,ctx' = HExtlib.split_nth cur_eq context in
300   let status, s = NTacStatus.whd status ctx' (mk_cic_term ctx' s) in
301   let status, s = term_of_cic_term status s ctx' in
302   let status, leftno, it =
303     let it, t1, t2 = match s with
304       | NCic.Appl [_;it;t1;t2] -> it,t1,t2
305       | _ -> assert false in
306     (* XXX: serve? ho già fatto whd *)
307     let status, it = whd status ctx' (mk_cic_term ctx' it) in
308     let status, it = term_of_cic_term status it ctx' in
309     let _uri,indtyno,its = match it with
310       | NCic.Const (NReference.Ref (uri, NReference.Ind (_,indtyno,_)) as r) 
311       | NCic.Appl (NCic.Const 
312           (NReference.Ref (uri, NReference.Ind (_,indtyno,_)) as r)::_) -> 
313         uri, indtyno, NCicEnvironment.get_checked_indtys r
314       | _ -> prerr_endline ("discriminate: indty ="  ^ NCicPp.ppterm
315                   ~metasenv:[] ~subst:[] ~context:[] it) ; assert false in
316     let _,leftno,its,_,_ = its in
317     status, leftno, List.nth its indtyno
318   in
319   
320   let itnargs = 
321     let _,_,arity,_ = it in 
322     List.length (arg_list 0 arity) in
323   let _,itname,_,_ = it in
324   let params = List.map (fun x -> "a" ^ string_of_int x) (HExtlib.list_seq 1 (itnargs+1)) in
325   let print_tac s status = pp s ; status in 
326   NTactics.block_tac (
327     [(fun status ->
328      let status, discr = mk_discriminator it leftno status in
329       NTactics.cut_tac ("",0, mk_prods params (CicNotationPt.Binder (`Forall, (mk_id "x", 
330                              Some (mk_appl (List.map mk_id (itname::params)))),
331                          CicNotationPt.Binder (`Forall, (mk_id "y", None),
332                           CicNotationPt.Binder (`Forall, (mk_id "e",
333                            Some (mk_appl [mk_id "eq";CicNotationPt.Implicit `JustOne; mk_id "x"; mk_id "y"])),
334                            mk_appl [discr; mk_id "x"; mk_id "y"(*;mk_id "e"*)])))))
335       status);
336     NTactics.branch_tac;
337     print_tac (lazy "ci sono");
338      NTactics.reduce_tac ~reduction:(`Normalize true) ~where:default_pattern]
339   @ List.map (fun x -> NTactics.intro_tac x) params @
340     [NTactics.intro_tac "x";
341      NTactics.intro_tac "y";
342      NTactics.intro_tac "Deq";
343     print_tac (lazy "ci sono 2");
344      NTactics.rewrite_tac ~dir:`RightToLeft ~what:("",0,mk_id "Deq") ~where:default_pattern;
345      NTactics.cases_tac ~what:("",0,mk_id "x") ~where:default_pattern]
346   @ dbranches it leftno @ 
347    [NTactics.shift_tac;
348     print_tac (lazy "ci sono 3");
349     NTactics.intro_tac "#discriminate";
350     NTactics.apply_tac ("",0,mk_appl ([mk_id "#discriminate"]@
351                                 HExtlib.mk_list (CicNotationPt.Implicit `JustOne) (List.length params + 2) @
352                                 [mk_id eq_name ]));
353     NTactics.reduce_tac ~reduction:(`Normalize true) ~where:default_pattern;
354     NTactics.clear_tac ["#discriminate"];
355     NTactics.merge_tac] 
356   ) status
357 ;;
358       
359 let subst_tac ~context ~dir cur_eq =
360   fun status ->
361   let eq_name,(NCic.Decl s | NCic.Def (s,_)) = List.nth context (cur_eq-1) in
362   let _,ctx' = HExtlib.split_nth cur_eq context in
363   let status, s = NTacStatus.whd status ctx' (mk_cic_term ctx' s) in
364   let status, s = term_of_cic_term status s ctx' in
365   pp (lazy (Printf.sprintf "subst: equation %s" eq_name));
366     let l, r = match s with
367       | NCic.Appl [_;_;t1;t2] -> t1,t2
368       | _ -> assert false in
369     let var = match dir with
370       | `LeftToRight -> l
371       | `RightToLeft -> r in
372     let var = match var with
373       | NCic.Rel i -> i
374       | _ -> assert false in
375     let names_to_gen, _ = 
376       cascade_select_in_ctx ~subst:(get_subst status) context (var+cur_eq) in
377     let names_to_gen = match names_to_gen with [] -> [] | _::tl -> tl in
378     let gen_tac x = 
379       NTactics.generalize_tac 
380       ~where:("",0,(Some (mk_id x),[], Some CicNotationPt.UserInput)) in
381     NTactics.block_tac ((List.map gen_tac names_to_gen)@
382                 [NTactics.clear_tac names_to_gen;
383                  NTactics.rewrite_tac ~dir 
384                    ~what:("",0,mk_id eq_name) ~where:default_pattern;
385                  NTactics.reduce_tac ~reduction:(`Normalize true)
386                    ~where:default_pattern;
387                  NTactics.clear_tac [eq_name]]@
388                  (List.map NTactics.intro_tac (List.rev names_to_gen))) status
389 ;;
390
391 let clearid_tac ~context cur_eq =
392   fun status ->
393   let eq_name,(NCic.Decl s | NCic.Def (s,_)) = List.nth context (cur_eq-1) in
394   let _,ctx' = HExtlib.split_nth cur_eq context in
395   let status, s = NTacStatus.whd status ctx' (mk_cic_term ctx' s) in
396   let status, s = term_of_cic_term status s ctx' in
397   pp (lazy (Printf.sprintf "clearid: equation %s" eq_name));
398     let names_to_gen, _ = 
399       cascade_select_in_ctx ~subst:(get_subst status) context cur_eq in
400     let names_to_gen = names_to_gen @ [eq_name] in
401     let gen_tac x = 
402       NTactics.generalize_tac 
403       ~where:("",0,(Some (mk_id x),[], Some CicNotationPt.UserInput)) in
404     NTactics.block_tac ((List.map gen_tac names_to_gen)@
405                 [NTactics.clear_tac names_to_gen;
406                  NTactics.apply_tac ("",0, mk_appl [mk_id "streicherK";
407                                                     CicNotationPt.Implicit `JustOne;
408                                                     CicNotationPt.Implicit `JustOne;
409                                                     CicNotationPt.Implicit `JustOne;
410                                                     CicNotationPt.Implicit `JustOne]);
411                  NTactics.reduce_tac ~reduction:(`Normalize true)
412                    ~where:default_pattern] @
413                  (let names_to_intro = 
414                     match List.rev names_to_gen with
415                     | [] -> []
416                     | _::tl -> tl in
417                   List.map NTactics.intro_tac names_to_intro)) status
418 ;;
419
420 let get_ctx st goal =
421     ctx_of (get_goalty st goal)
422 ;;
423
424 (* = select + classify *)
425 let select_eq ctx acc status goal =
426   let classify ~subst ctx' l r =
427     (* FIXME: metasenv *)
428     if NCicReduction.are_convertible ~metasenv:[] ~subst ctx' l r 
429       then status, `Identity
430       else status, (match hd_of_term l, hd_of_term r with
431         | NCic.Const (NReference.Ref (_,NReference.Con (_,ki,nleft)) as kref),
432           NCic.Const (NReference.Ref (_,NReference.Con (_,kj,_))) -> 
433             if ki != kj then `Discriminate (0,true)
434             else
435               let rit = NReference.mk_indty true kref in
436               let _,_,its,_,itno = NCicEnvironment.get_checked_indtys rit in 
437               let it = List.nth its itno in
438               let newprods = nargs it nleft (ki-1) in
439               `Discriminate (newprods, false) 
440         | NCic.Rel j, _  
441             when NCicTypeChecker.does_not_occur ~subst ctx' (j-1) j r -> 
442               `Subst `LeftToRight
443         | _, NCic.Rel j 
444             when NCicTypeChecker.does_not_occur ~subst ctx' (j-1) j l -> 
445               `Subst `RightToLeft
446         | (NCic.Rel _, _ | _, NCic.Rel _ ) -> `Cycle
447         | _ -> `Blob) in
448   let rec aux i =
449     try
450       let index = List.length ctx - i in
451       pp (lazy ("provo classify di index = " ^string_of_int index));
452       match (List.nth ctx (index - 1)) with
453       | n, (NCic.Decl ty | NCic.Def (ty,_)) ->
454           (let _,ctx_ty = HExtlib.split_nth index ctx in 
455            let status, ty = NTacStatus.whd status ctx_ty (mk_cic_term ctx_ty ty) in
456            let status, ty = term_of_cic_term status ty ctx_ty in
457            pp (lazy (Printf.sprintf "select_eq tries %s" (NCicPp.ppterm ~context:ctx_ty ~subst:[] ~metasenv:[] ty)));
458            match ty with
459            | NCic.Appl [NCic.Const (NReference.Ref (u,_)) ;_;l;r] when NUri.name_of_uri u = "eq" ->
460               (let status, kind = classify ~subst:(get_subst status) ctx_ty l r in
461                match kind with
462                  | `Identity ->
463                      let status, goalty = term_of_cic_term status (get_goalty status goal) ctx in
464                         status, Some (List.length ctx - i), kind
465                  | `Cycle | `Blob -> aux (i+1) (* XXX: skip cyclic/blob equations for now *)
466                  | _ -> 
467                     if (List.for_all (fun x -> x <> n) acc) then 
468                       status, Some (List.length ctx - i), kind
469                     else aux (i+1))
470            | _ -> aux (i+1))
471     with Failure _ | Invalid_argument _ -> status, None, `Blob
472   in aux 0
473 ;;
474
475 let rec destruct_tac0 nprods acc status goal =
476   let ctx = get_ctx status goal in
477   let subst = get_subst status in
478   let get_newgoal os ns ogoal =
479     let go, gc = NTactics.compare_statuses ~past:os ~present:ns in
480     let go' = ([ogoal] @- gc) @+ go in
481       match go' with [] -> assert false | g::_ -> g
482   in
483   let status, selection, kind  = select_eq ctx acc status goal in
484   pp (lazy ("destruct: acc is " ^ String.concat "," acc ));
485   match selection, kind with
486   | None, _ -> 
487     pp (lazy (Printf.sprintf "destruct: nprods is %d, no selection, context is %s" nprods (NCicPp.ppcontext ~metasenv:[] ~subst ctx)));
488       if nprods > 0  then 
489         let status' = NTactics.exec (NTactics.intro_tac (mk_fresh_name ctx 'e' 0)) status goal in
490         destruct_tac0 (nprods-1) acc status' (get_newgoal status status' goal)
491       else
492         status
493   | Some cur_eq, `Discriminate (newprods,conflict) -> 
494     pp (lazy (Printf.sprintf "destruct: discriminate - nprods is %d, selection is %d, context is %s" nprods cur_eq (NCicPp.ppcontext ~metasenv:[] ~subst ctx)));
495       let status' = NTactics.exec (discriminate_tac ~context:ctx cur_eq) status goal in
496       if conflict then status'
497       else destruct_tac0 (nprods+newprods) 
498              (name_of_rel ~context:ctx cur_eq::acc) status' (get_newgoal status status' goal)
499   | Some cur_eq, `Subst dir ->
500     pp (lazy (Printf.sprintf "destruct: subst - nprods is %d, selection is %d, context is %s" nprods cur_eq (NCicPp.ppcontext ~metasenv:[] ~subst ctx)));
501     let status' = NTactics.exec (subst_tac ~context:ctx ~dir cur_eq) status goal in
502       pp (lazy (Printf.sprintf " ctx after subst = %s" (NCicPp.ppcontext ~metasenv:[] ~subst (get_ctx status' (get_newgoal status status' goal)))));
503     let eq_name,_ = List.nth ctx (cur_eq-1) in
504       destruct_tac0 nprods (List.filter (fun x -> x <> eq_name) acc) status' (get_newgoal status status' goal)
505   | Some cur_eq, `Identity ->
506     pp (lazy (Printf.sprintf "destruct: identity - nprods is %d, selection is %d, context is %s" nprods cur_eq (NCicPp.ppcontext ~metasenv:[] ~subst ctx)));
507       let eq_name,_ = List.nth ctx (cur_eq-1) in
508       let status' = NTactics.exec (clearid_tac ~context:ctx cur_eq) status goal in
509         destruct_tac0 nprods (List.filter (fun x -> x <> eq_name) acc) status' (get_newgoal status status' goal)
510   | Some cur_eq, `Cycle -> (* TODO, should never happen *)
511     pp (lazy (Printf.sprintf "destruct: cycle - nprods is %d, selection is %d, context is %s" nprods cur_eq (NCicPp.ppcontext ~metasenv:[] ~subst ctx)));
512       assert false
513   | Some cur_eq, `Blob ->
514     pp (lazy (Printf.sprintf "destruct: blob - nprods is %d, selection is %d, context is %s" nprods cur_eq (NCicPp.ppcontext ~metasenv:[] ~subst ctx)));
515       assert false
516 ;;
517
518 let destruct_tac s = NTactics.distribute_tac (destruct_tac0 0 []) s;;