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