]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/discriminationTactics.ml
cic_acic should be compiled before cic_exportation
[helm.git] / components / tactics / discriminationTactics.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$ *)
27
28 module C = Cic
29 module U = UriManager
30 module P = PrimitiveTactics
31 module T = Tacticals
32 module CR = CicReduction 
33 module PST = ProofEngineStructuralRules
34 module PET = ProofEngineTypes
35 module CTC = CicTypeChecker
36 module CU = CicUniv
37 module S = CicSubstitution
38 module RT = ReductionTactics
39 module PEH = ProofEngineHelpers
40 module ET = EqualityTactics
41
42 let debug = false
43 let debug_print = 
44   if debug then (fun x -> prerr_endline (Lazy.force x)) else (fun _ -> ())
45
46 (* funzione generale di rilocazione dei riferimenti locali *)
47
48 let relocate_term map t =
49    let rec map_xnss k xnss =
50       let imap (uri, t) = uri, map_term k t in
51       List.map imap xnss
52    and map_mss k mss =
53       let imap = function
54          | None   -> None
55          | Some t -> Some (map_term k t)
56       in
57       List.map imap mss
58    and map_fs len k fs = 
59       let imap (name, i, ty, bo) = name, i, map_term k ty, map_term (k + len) bo in
60       List.map imap fs
61    and map_cfs len k cfs = 
62       let imap (name, ty, bo) = name, map_term k ty, map_term (k + len) bo in
63       List.map imap cfs
64    and map_term k = function
65       | C.Rel m -> if m < k then C.Rel m else C.Rel (map (m - k))
66       | C.Sort _ as t -> t
67       | C.Implicit _ as t -> t
68       | C.Var (uri, xnss) -> C.Var (uri, map_xnss k xnss)
69       | C.Const (uri, xnss) -> C.Const (uri, map_xnss k xnss)
70       | C.MutInd (uri, tyno, xnss) -> C.MutInd (uri, tyno, map_xnss k xnss)
71       | C.MutConstruct (uri, tyno, consno, xnss) ->
72          C.MutConstruct (uri, tyno, consno, map_xnss k xnss)
73       | C.Meta (i, mss) -> C.Meta(i, map_mss k mss)
74       | C.Cast (te, ty) -> C.Cast (map_term k te, map_term k ty)
75       | C.Appl ts -> C.Appl (List.map (map_term k) ts)
76       | C.MutCase (sp, i, outty, t, pl) ->
77          C.MutCase (sp, i, map_term k outty, map_term k t, List.map (map_term k) pl)    
78       | C.Prod (n, s, t) -> C.Prod (n, map_term k s, map_term (succ k) t)
79       | C.Lambda (n, s, t) -> C.Lambda (n, map_term k s, map_term (succ k) t)
80       | C.LetIn (n, s, t) -> C.LetIn (n, map_term k s, map_term (succ k) t)
81       | C.Fix (i, fs) -> C.Fix (i, map_fs (List.length fs) k fs)
82       | C.CoFix (i, cfs) -> C.CoFix (i, map_cfs (List.length cfs) k cfs)
83    in
84    map_term 0 t
85
86 let id n = n
87
88 let after continuation aftermap beforemap = 
89    continuation ~map:(fun n -> aftermap (beforemap n))
90
91 let after2 continuation aftermap beforemap ~map = 
92    continuation ~map:(fun n -> map (aftermap (beforemap n)))
93
94 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
95 diversi *)
96
97 let discriminate_tac ~term =
98  let true_URI =
99   match LibraryObjects.true_URI () with
100      Some uri -> uri
101    | None -> raise (PET.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
102  let false_URI =
103   match LibraryObjects.false_URI () with
104      Some uri -> uri
105    | None -> raise (PET.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
106  let fail msg = raise (PET.Fail (lazy ("Discriminate: " ^ msg))) in
107  let find_discriminating_consno t1 t2 =
108    let rec aux t1 t2 =
109      match t1, t2 with
110      | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
111      | C.Appl ((C.MutConstruct _ as constr1) :: args1),
112        C.Appl ((C.MutConstruct _ as constr2) :: args2)
113        when constr1 = constr2 ->
114          let rec aux_list l1 l2 =
115            match l1, l2 with
116            | [], [] -> None
117            | hd1 :: tl1, hd2 :: tl2 ->
118                (match aux hd1 hd2 with
119                | None -> aux_list tl1 tl2
120                | Some _ as res -> res)
121            | _ -> (* same constructor applied to a different number of args *)
122                assert false
123          in
124          aux_list args1 args2
125      | ((C.MutConstruct (_,_,consno1,subst1)),
126        (C.MutConstruct (_,_,consno2,subst2)))
127      | ((C.MutConstruct (_,_,consno1,subst1)),
128        (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
129      | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
130        (C.MutConstruct (_,_,consno2,subst2)))
131      | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
132        (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
133        when (consno1 <> consno2) || (subst1 <> subst2) ->
134          Some consno2
135      | _ -> fail "not a discriminable equality"
136    in
137    aux t1 t2
138  in
139  let mk_branches_and_outtype turi typeno consno context args =
140     (* a list of "True" except for the element in position consno which
141      * is "False" *)
142     match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
143     | C.InductiveDefinition (ind_type_list,_,paramsno,_)  ->
144         let _,_,rty,constructor_list = List.nth ind_type_list typeno in 
145         let false_constr_id,_ = List.nth constructor_list (consno - 1) in
146         let branches =
147          List.map 
148            (fun (id,cty) ->
149              (* dubbio: e' corretto ridurre in questo context ??? *)
150              let red_ty = CR.whd context cty in
151              let rec aux t k =
152                match t with
153                | C.Prod (_,_,target) when (k <= paramsno) ->
154                    S.subst (List.nth args (k-1))
155                      (aux target (k+1))
156                | C.Prod (binder,source,target) when (k > paramsno) ->
157                    C.Lambda (binder, source, (aux target (k+1)))
158                | _ -> 
159                    if (id = false_constr_id)
160                    then (C.MutInd(false_URI,0,[]))
161                    else (C.MutInd(true_URI,0,[]))
162              in
163              (S.lift 1 (aux red_ty 1)))
164            constructor_list in
165         let outtype =
166          let seed = ref 0 in
167          let rec mk_lambdas rev_left_args =
168           function
169              0, args, C.Prod (_,so,ta) ->
170               C.Lambda
171                (C.Name (incr seed; "x" ^ string_of_int !seed),
172                so,
173                mk_lambdas rev_left_args (0,args,ta))
174            | 0, args, C.Sort _ ->
175               let rec mk_rels =
176                function
177                   0 -> []
178                 | n -> C.Rel n :: mk_rels (n - 1) in
179               let argsno = List.length args in
180                C.Lambda
181                 (C.Name "x",
182                  (if argsno + List.length rev_left_args > 0 then
183                    C.Appl
184                     (C.MutInd (turi, typeno, []) ::
185                      (List.map
186                       (S.lift (argsno + 1))
187                       (List.rev rev_left_args)) @
188                      mk_rels argsno)
189                   else
190                    C.MutInd (turi,typeno,[])),
191                  C.Sort C.Prop)
192            | 0, _, _ -> assert false (* seriously screwed up *)
193            | n, he::tl, C.Prod (_,_,ta) ->
194               mk_lambdas (he::rev_left_args)(n-1,tl,S.subst he ta)
195            | n,_,_ ->
196               assert false (* we should probably reduce in some context *)
197          in
198           mk_lambdas [] (paramsno, args, rty)
199         in
200          branches, outtype 
201     | _ -> assert false
202  in
203  let discriminate'_tac ~term status = 
204   let (proof, goal) = status in
205   let _,metasenv,_subst,_,_, _ = proof in
206   let _,context,_ = CicUtil.lookup_meta goal metasenv in
207   let termty,_ = 
208     CTC.type_of_aux' metasenv context term CicUniv.empty_ugraph
209   in
210   match termty with
211    | C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2]
212      when LibraryObjects.is_eq_URI equri ->
213       let turi,typeno,exp_named_subst,args = 
214         match tty with
215         | (C.MutInd (turi,typeno,exp_named_subst)) ->
216             turi,typeno,exp_named_subst,[]
217         | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::args)) ->
218             turi,typeno,exp_named_subst,args
219         | _ -> fail "not a discriminable equality"
220       in
221       let consno =
222         match find_discriminating_consno t1 t2 with
223         | Some consno -> consno
224         | None -> fail "discriminating terms are structurally equal"
225       in
226       let branches,outtype =
227        mk_branches_and_outtype turi typeno consno context args
228       in
229       PET.apply_tactic
230        (T.then_
231          ~start:(EliminationTactics.elim_type_tac (C.MutInd (false_URI, 0, [])))
232          ~continuation:
233            (T.then_
234              ~start:
235                (RT.change_tac 
236                  ~pattern:(PET.conclusion_pattern None)
237                  (fun _ m u ->
238                    C.Appl [
239                      C.Lambda ( C.Name "x", tty,
240                        C.MutCase (turi, typeno, outtype, (C.Rel 1), branches));
241                      t2 ],
242                    m, u))
243              ~continuation:
244                (T.then_
245                  ~start:
246                    (ET.rewrite_simpl_tac
247                      ~direction:`RightToLeft
248                      ~pattern:(PET.conclusion_pattern None)
249                      term [])
250                  ~continuation:
251                    (IntroductionTactics.constructor_tac ~n:1)))) status
252     | _ -> fail "not an equality"
253   in
254   PET.mk_tactic (discriminate'_tac ~term)
255
256 let exn_nonproj = 
257   PET.Fail (lazy "Injection: not a projectable equality")
258 let exn_noneq = 
259   PET.Fail (lazy "Injection: not an equality")
260 let exn_nothingtodo = 
261   PET.Fail (lazy "Nothing to do")
262 let exn_discrnonind =
263   PET.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible")
264 let exn_injwronggoal = 
265   PET.Fail (lazy "Injection: goal after cut is not correct")
266 let exn_noneqind =
267   PET.Fail (lazy "Injection: not an equality over elements of an inductive type")
268
269 let pp ctx t = 
270   let names = List.map (function Some (n,_) -> Some n | None -> None) ctx in
271   CicPp.pp t names
272
273 let clear_term first_time context term =
274    let g () = if first_time then raise exn_nothingtodo else T.id_tac in
275    match term with
276       | C.Rel n -> 
277          begin match List.nth context (pred n) with
278             | Some (C.Name id, _) -> PST.clear ~hyps:[id]
279             | _                   -> assert false
280          end
281        | _      -> g ()
282
283 let simpl_in_term context = function
284    | Cic.Rel i ->
285       let name = match List.nth context (pred i) with
286          | Some (Cic.Name s, Cic.Def _) -> s
287          | Some (Cic.Name s, Cic.Decl _) -> s
288          | _ -> assert false
289       in
290       RT.simpl_tac ~pattern:(None,[name,Cic.Implicit (Some `Hole)],None)
291    | _ -> raise exn_nonproj
292
293 (* ~term vive nel contesto della tattica una volta ~mappato
294  * ~continuation riceve la mappa assoluta
295  *)
296 let rec injection_tac ~map ~term ~i ~continuation =
297  let give_name seed = function
298    | C.Name _ as name -> name
299    | C.Anonymous -> C.Name (incr seed; "y" ^ string_of_int !seed)
300  in
301  let rec mk_rels = function | 0 -> [] | n -> C.Rel n :: (mk_rels (n - 1)) in
302  let injection_tac status =
303   let (proof, goal) = status in
304   (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma 
305    * differiscono (o potrebbero differire?) nell'i-esimo parametro 
306    * del costruttore *)
307   let _,metasenv,_subst,_,_, _ = proof in
308   let _,context,_ = CicUtil.lookup_meta goal metasenv in
309   let term = relocate_term map term in
310   let termty,_ = 
311     CTC.type_of_aux' metasenv context term CicUniv.empty_ugraph
312   in
313   debug_print (lazy ("\ninjection su : " ^ pp context termty)); 
314   match termty with (* an equality *)
315    | C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2]
316     when LibraryObjects.is_eq_URI equri -> 
317       let turi,typeno,ens,params =
318         match tty with (* some inductive type *)
319         | C.MutInd (turi,typeno,ens) -> turi,typeno,ens,[]
320         | C.Appl (C.MutInd (turi,typeno,ens)::params) -> turi,typeno,ens,params
321         | _ -> raise exn_noneqind
322       in
323       let t1',t2',consno = (* sono i due sottotermini che differiscono *)
324         match t1,t2 with
325         | C.Appl ((C.MutConstruct (uri1,typeno1,consno1,ens1))::applist1),
326           C.Appl ((C.MutConstruct (uri2,typeno2,consno2,ens2))::applist2)
327           when (uri1 = uri2) && (typeno1 = typeno2) && 
328                (consno1 = consno2) && (ens1 = ens2) -> 
329                (* controllo ridondante *)
330             List.nth applist1 (pred i),List.nth applist2 (pred i),consno2
331         | _ -> assert false
332       in
333       let tty',_ = CTC.type_of_aux' metasenv context t1' CU.empty_ugraph in
334       let patterns,outtype =
335         match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
336         | C.InductiveDefinition (ind_type_list,_,paramsno,_)->
337            let left_params, right_params = HExtlib.split_nth paramsno params in
338            let _,_,_,constructor_list = List.nth ind_type_list typeno in
339            let i_constr_id,_ = List.nth constructor_list (consno - 1) in
340            let patterns =
341              let seed = ref 0 in
342              List.map
343                (function (id,cty) ->
344                  let reduced_cty = CR.whd context cty in
345                  let rec aux k = function
346                    | C.Prod (_,_,tgt) when k <= paramsno -> 
347                        let left = List.nth left_params (k-1) in
348                        aux (k+1) (S.subst left tgt)
349                    | C.Prod (binder,source,target) when k > paramsno ->
350                       let binder' = give_name seed binder in
351                       C.Lambda (binder',source,(aux (k+1) target))
352                    | _ ->
353                      let nr_param_constr = k - paramsno - 1 in
354                      if id = i_constr_id then C.Rel (k - i)
355                      else S.lift nr_param_constr t1' 
356                      (* + 1 per liftare anche il lambda aggiunto
357                       * esternamente al case *)
358                  in S.lift 1 (aux 1 reduced_cty))
359                constructor_list 
360            in
361            (* this code should be taken from cases_tac *)
362            let outtype =
363              let seed = ref 0 in
364              let rec to_lambdas te head =
365                match CR.whd context te with
366                | C.Prod (binder,so,ta) ->
367                    let binder' = give_name seed binder in
368                    C.Lambda (binder',so,to_lambdas ta head)
369                | _ -> head 
370              in
371              let rec skip_prods params te =
372                match params, CR.whd context te with
373                | [], _ -> te
374                | left::tl, C.Prod (_,_,ta) -> 
375                    skip_prods tl (S.subst left ta)
376                | _, _ -> assert false
377              in
378              let abstracted_tty =
379                let tty =
380                  List.fold_left (fun x y -> S.subst y x) tty left_params
381                in
382                (* non lift, ma subst coi left! *)
383                match S.lift 1 tty with
384                | C.MutInd _ as tty' -> tty'
385                | C.Appl l ->
386                    let keep,abstract = HExtlib.split_nth (paramsno +1) l in
387                    let keep = List.map (S.lift paramsno) keep in
388                    C.Appl (keep@mk_rels (List.length abstract))
389                | _ -> assert false
390              in
391              match ind_type_list with
392              | [] -> assert false
393              | (_,_,ty,_)::_ ->
394                (* this is in general wrong, do as in cases_tac *)
395                to_lambdas (skip_prods left_params ty)
396                  (C.Lambda 
397                    (C.Name "cased", abstracted_tty,
398                      (* here we should capture right parameters *)
399                      (* 1 for his Lambda, one for the Lambda outside the match
400                       * and then one for each to_lambda *)
401                      S.lift (2+List.length right_params) tty'))
402           in
403             patterns,outtype
404         | _ -> raise exn_discrnonind
405       in
406       let cutted = C.Appl [C.MutInd (equri,0,[]) ; tty' ; t1' ; t2'] in
407       let changed = 
408         C.Appl [ C.Lambda (C.Name "x", tty, 
409                   C.MutCase (turi,typeno,outtype,C.Rel 1,patterns)) ; t1]
410       in
411       (* check if cutted and changed are well typed and if t1' ~ changed *)
412       let go_on =
413         try
414           let _,g = CTC.type_of_aux' metasenv context  cutted
415             CicUniv.empty_ugraph
416           in
417           let _,g = CTC.type_of_aux' metasenv context changed g in
418           fst (CR.are_convertible ~metasenv context  t1' changed g)
419         with
420         | CTC.TypeCheckerFailure _ -> false
421       in
422       if not go_on then 
423         PET.apply_tactic (continuation ~map) status
424       else
425         let tac term = 
426            let tac status =
427                debug_print (lazy "riempio il cut"); 
428                let (proof, goal) = status in
429                let _,metasenv,_subst,_,_, _ = proof in
430                let _,context,gty = CicUtil.lookup_meta goal metasenv in
431                let gty = Unshare.unshare gty in
432                let new_t1' = match gty with 
433                   | (C.Appl (C.MutInd (_,_,_)::_::t::_)) -> t
434                   | _ -> raise exn_injwronggoal
435                in
436                debug_print (lazy ("metto: " ^ pp context changed));
437                debug_print (lazy ("al posto di: " ^ pp context new_t1'));
438                debug_print (lazy ("nel goal: " ^ pp context gty));
439                debug_print (lazy ("nel contesto:\n" ^ CicPp.ppcontext context));
440                debug_print (lazy ("e poi rewrite con: "^pp context term));
441                let tac = T.seq ~tactics:[
442                   RT.change_tac
443                      ~pattern:(None, [], Some (PEH.pattern_of ~term:gty [new_t1']))
444                      (fun _ m u -> changed,m,u);
445                   ET.rewrite_simpl_tac
446                      ~direction:`LeftToRight
447                      ~pattern:(PET.conclusion_pattern None)
448                      term [];
449                   ET.reflexivity_tac   
450                ] in
451                PET.apply_tactic tac status
452            in
453            PET.mk_tactic tac
454         in
455         debug_print (lazy ("CUT: " ^ pp context cutted));  
456         PET.apply_tactic   
457           (T.thens ~start: (P.cut_tac cutted)
458                    ~continuations:[
459                       (destruct ~first_time:false ~term:(C.Rel 1) ~map:id 
460                                  ~continuation:(after2 continuation succ map) 
461                       );  
462                       tac term] 
463           ) status
464    | _ -> raise exn_noneq
465  in
466   PET.mk_tactic injection_tac
467
468 (* ~term vive nel contesto della tattica una volta ~mappato
469  * ~continuation riceve la mappa assoluta
470  *)
471 and subst_tac ~map ~term ~direction ~where ~continuation =
472    let fail_tactic = continuation ~map in
473    let subst_tac status =
474       let term = relocate_term map term in
475       let tactic = match where with
476          | None      -> 
477             let pattern = PET.conclusion_pattern None in
478             let tactic = ET.rewrite_tac ~direction ~pattern term [] in
479             T.then_ ~start:(T.try_tactic ~tactic)
480                     ~continuation:fail_tactic
481          | Some name ->   
482             let pattern = None, [name, PET.hole], None in
483             let start = ET.rewrite_tac ~direction ~pattern term [] in
484             let continuation =
485                destruct ~first_time:false ~term:(C.Rel 1) ~map:id 
486                         ~continuation:(after2 continuation succ map)
487             in
488             T.if_ ~start ~continuation ~fail:fail_tactic
489       in 
490       PET.apply_tactic tactic status
491    in
492    PET.mk_tactic subst_tac
493
494 (* ~term vive nel contesto della tattica una volta ~mappato
495  * ~continuation riceve la mappa assoluta
496  *)
497 and destruct ~first_time ~map ~term ~continuation =
498  let are_convertible hd1 hd2 metasenv context = 
499    fst (CR.are_convertible ~metasenv context hd1 hd2 CicUniv.empty_ugraph)
500  in
501  let destruct status = 
502   let (proof, goal) = status in
503   let _,metasenv,_subst, _,_, _ = proof in
504   let _,context,_ = CicUtil.lookup_meta goal metasenv in
505   let term = relocate_term map term in
506   debug_print (lazy ("\nqnify di: " ^ pp context term)); 
507   debug_print (lazy ("nel contesto:\n" ^ CicPp.ppcontext context));
508   let termty,_ = 
509     CTC.type_of_aux' metasenv context term CicUniv.empty_ugraph
510   in
511   debug_print (lazy ("\nqnify su: " ^ pp context termty)); 
512   let tac = match termty with
513     | C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2] 
514       when LibraryObjects.is_eq_URI equri -> begin
515         match (CR.whd ~delta:true context tty) with
516         | C.MutInd _
517         | C.Appl (C.MutInd _ :: _) -> 
518            begin match t1,t2 with
519             | C.MutConstruct _,
520               C.MutConstruct _
521               when t1 = t2 ->
522                 T.then_ ~start:(clear_term first_time context term)
523                         ~continuation:(continuation ~map)
524             | C.Appl (C.MutConstruct _ as mc1 :: applist1),
525               C.Appl (C.MutConstruct _ as mc2 :: applist2)
526               when mc1 = mc2 ->
527                 let rec traverse_list first_time i l1 l2 = 
528                    match l1, l2 with
529                       | [], [] ->
530                          fun ~map:aftermap ->
531                             T.then_ ~start:(clear_term first_time context term)
532                                     ~continuation:(after continuation aftermap map)
533                       | hd1 :: tl1, hd2 :: tl2 -> 
534                         if are_convertible hd1 hd2 metasenv context then
535                            traverse_list first_time (succ i) tl1 tl2
536                         else
537                            injection_tac ~i ~term ~continuation:
538                               (traverse_list false (succ i) tl1 tl2)
539                       | _ -> assert false 
540                       (* i 2 termini hanno in testa lo stesso costruttore, 
541                        * ma applicato a un numero diverso di termini *)
542                 in
543                   traverse_list first_time 1 applist1 applist2 ~map:id
544             | C.MutConstruct (_,_,consno1,ens1),
545               C.MutConstruct (_,_,consno2,ens2)
546             | C.MutConstruct (_,_,consno1,ens1),
547               C.Appl ((C.MutConstruct (_,_,consno2,ens2))::_)
548             | C.Appl ((C.MutConstruct (_,_,consno1,ens1))::_),
549               C.MutConstruct (_,_,consno2,ens2)
550             | C.Appl ((C.MutConstruct (_,_,consno1,ens1))::_),
551               C.Appl ((C.MutConstruct (_,_,consno2,ens2))::_)
552               when (consno1 <> consno2) || (ens1 <> ens2) -> 
553                 discriminate_tac ~term
554             | _ when not first_time -> continuation ~map
555             | _ (* when first_time *) -> 
556                T.then_ ~start:(simpl_in_term context term)
557                        ~continuation:(destruct ~first_time:false ~term ~map ~continuation)
558            end
559         | _ when not first_time -> continuation ~map
560         | _ (* when first_time *) -> raise exn_nonproj
561         end 
562     | _ -> raise exn_nonproj
563   in  
564     PET.apply_tactic tac status
565  in 
566    PET.mk_tactic destruct
567
568 (* destruct performs either injection or discriminate *)
569 (* equivalent to Coq's "analyze equality"             *)
570 let destruct_tac =
571  destruct
572   ~first_time:true ~map:id ~continuation:(fun ~map -> T.id_tac)