]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/discriminationTactics.ml
1. added a test for injection
[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 let debug_print = fun _ -> ()
29
30 let rec injection_tac ~term =
31  let injection_tac ~term status = 
32   let (proof, goal) = status in
33   let module C = Cic in
34   let module U = UriManager in
35   let module P = PrimitiveTactics in
36   let module T = Tacticals in
37   let _,metasenv,_,_ = proof in
38   let _,context,_ = CicUtil.lookup_meta goal metasenv in
39   let termty,_ = (* TASSI: FIXME *)
40     CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
41   in
42     ProofEngineTypes.apply_tactic
43       (match termty with
44           (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
45              when LibraryObjects.is_eq_URI equri -> (
46            match tty with
47               (C.MutInd (turi,typeno,exp_named_subst))
48             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) -> (
49                    match t1,t2 with
50                       ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
51                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
52                          when (uri1 = uri2) && (typeno1 = typeno2) && 
53                               (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
54                        (* raise (ProofEngineTypes.Fail "Injection: nothing to do") ; *) T.id_tac
55                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
56                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
57                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
58                        let rec traverse_list i liftno l1 l2 =
59                          match l1,l2 with
60                             [],[] -> T.id_tac
61                           | hd1::tl1,hd2::tl2 -> 
62                              if
63                               fst
64                                (CicReduction.are_convertible ~metasenv
65                                  context hd1 hd2 CicUniv.empty_ugraph)
66                              then
67                               traverse_list (i+1) liftno tl1 tl2
68                              else
69                               T.then_ 
70                                ~start:
71                                  (injection1_tac ~i ~liftno
72                                    ~term:(CicSubstitution.lift liftno term))
73                                ~continuation:
74                                  (traverse_list (i+1) (liftno+1) tl1 tl2)
75                           | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???"))
76                        in traverse_list 1 0 applist1 applist2
77                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
78                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
79                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
80                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
81                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
82                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
83                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
84                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
85                          when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
86                        (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality but a discriminable one") ; *) T.id_tac
87                     | _ -> (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality") ; *) T.id_tac
88                    )
89             | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
90            )
91         | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equation"))
92       ) status
93  in 
94   ProofEngineTypes.mk_tactic (injection_tac ~term)
95
96 and injection1_tac ~term ~i ~liftno = 
97  let injection1_tac ~term ~i status =
98   let (proof, goal) = status in
99   (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma differiscono (o potrebbero differire?) nell'i-esimo parametro del costruttore *)
100    let module C = Cic in
101    let module S = CicSubstitution in
102    let module U = UriManager in
103    let module P = PrimitiveTactics in
104    let module T = Tacticals in
105    let _,metasenv,_,_ = proof in
106    let _,context,_ = CicUtil.lookup_meta goal metasenv in
107    let termty,_ = (* TASSI: FIXME *)
108      CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
109      match termty with (* an equality *)
110          (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
111              when LibraryObjects.is_eq_URI equri -> (
112            match tty with (* some inductive type *)
113               (C.MutInd (turi,typeno,exp_named_subst))
114             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
115                let t1',t2',consno = (* sono i due sottotermini che differiscono *)
116                 match t1,t2 with
117                    ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
118                     (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
119                       when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) -> (* controllo ridondante *)
120                     (List.nth applist1 (i-1)),(List.nth applist2 (i-1)),consno2
121                  | _ -> assert false
122                in
123                 let tty',_ = 
124                   CicTypeChecker.type_of_aux' metasenv context t1' 
125                     CicUniv.empty_ugraph  in
126                 let patterns,outtype =
127                  match
128                   fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi)
129                  with
130                     C.InductiveDefinition (ind_type_list,_,paramsno,_)->
131                      let _,_,_,constructor_list =
132                       List.nth ind_type_list typeno in
133                      let i_constr_id,_ =
134                       List.nth constructor_list (consno - 1) in
135                      let patterns =
136                       List.map
137                        (function (id,cty) ->
138                          let reduced_cty = CicReduction.whd context cty in
139                           let rec aux t k =
140                            match t with
141                               C.Prod (_,_,target) when k <= paramsno ->
142                                aux target (k+1)
143                             | C.Prod (binder,source,target) when k > paramsno ->
144                                let binder' =
145                                 match binder with
146                                    C.Name b -> C.Name b
147                                  | C.Anonymous -> C.Name "y"
148                                in
149                                 C.Lambda (binder',source,(aux target (k+1)))
150                             | _ ->
151                                let nr_param_constr = k - 1 - paramsno in
152                                 if id = i_constr_id
153                                  then C.Rel (nr_param_constr - liftno)
154                                  else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda aggiunto esternamente al case *)
155                           in aux reduced_cty 1
156                        ) constructor_list in
157                      let outtype =
158                       let rec to_lambdas te head =
159                        match CicReduction.whd context te with
160                         | C.Prod (name,so,ta) ->
161                            C.Lambda (name,so,to_lambdas ta head)
162                         | _ -> head in
163                       let rec skip_prods n te =
164                        match n, CicReduction.whd context te with
165                           0, _ -> te
166                         | n, C.Prod (_,_,ta) -> skip_prods (n - 1) ta
167                         | _, _ -> assert false
168                       in
169                        let abstracted_tty =
170                         match CicSubstitution.lift (paramsno + 1) tty with
171                            C.MutInd _ as tty' -> tty'
172                          | C.Appl l ->
173                              let keep,abstract =
174                               HExtlib.split_nth (paramsno +1) l in
175                              let rec mk_rels =
176                               function
177                                  0 -> []
178                                | n -> C.Rel n :: (mk_rels (n - 1))
179                              in
180                               C.Appl (keep@mk_rels (List.length abstract))
181                          | _ -> assert false
182                        in
183                         match ind_type_list with
184                            [] -> assert false
185                          | (_,_,ty,_)::_ ->
186                             to_lambdas (skip_prods paramsno ty)
187                              (C.Lambda (C.Name "x", abstracted_tty,
188                                S.lift (2+paramsno) tty'))
189                       in
190                        patterns,outtype
191                   | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
192                 in
193                 ProofEngineTypes.apply_tactic   
194                  (T.thens 
195                   ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
196                   ~continuations:[
197                     T.then_ 
198                      ~start:(injection_tac ~term:(C.Rel 1))
199                      ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *) 
200                     ;
201                     T.then_ 
202                      ~start:(ProofEngineTypes.mk_tactic 
203                        (fun status ->    
204                          let (proof, goal) = status in
205                          let _,metasenv,_,_ = proof in
206                           let _,context,gty =
207                            CicUtil.lookup_meta goal metasenv
208                           in
209                            let new_t1' = 
210                             match gty with 
211                                (C.Appl (C.MutInd (_,_,_)::arglist)) -> 
212                                 List.nth arglist 1
213                              | _ ->
214                                raise
215                                 (ProofEngineTypes.Fail
216                                   (lazy
217                                     "Injection: goal after cut is not correct"))
218                            in
219 let aaa =
220                             ProofEngineTypes.apply_tactic 
221                             (ReductionTactics.change_tac
222                                ~pattern:(ProofEngineTypes.conclusion_pattern
223                                 (Some new_t1'))
224                                (fun _ m u ->
225 let xxx =
226                                  C.Appl [
227                                   C.Lambda
228                                    (C.Name "x",
229                                      tty,
230                                      C.MutCase
231                                       (turi,typeno,outtype,C.Rel 1,patterns)) ;
232                                   t1]
233 in
234 prerr_endline ("XXX: " ^ CicPp.ppterm xxx);
235 prerr_endline ("WITH: " ^ CicPp.ppterm new_t1');
236 xxx,
237                                  m, u))
238                         status
239 in
240 prerr_endline "OK";
241 aaa
242                        ))
243                      ~continuation:
244                        (T.then_
245                          ~start:
246                            (EqualityTactics.rewrite_simpl_tac
247                              ~direction:`LeftToRight
248                              ~pattern:(ProofEngineTypes.conclusion_pattern None)
249                              term)
250                          ~continuation:EqualityTactics.reflexivity_tac
251                        )
252                    ])     
253                   status
254             | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality over elements of an inductive type"))
255            )
256         | _ -> ProofEngineTypes.apply_tactic T.id_tac status (*XXXraise (ProofEngineTypes.Fail (lazy "Injection: not an equality"))*)
257  in
258   ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
259 ;;
260
261 exception TwoDifferentSubtermsFound of int 
262
263 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
264 diversi *)
265
266 let discriminate'_tac ~term =
267  let module C = Cic in
268  let module U = UriManager in
269  let module P = PrimitiveTactics in
270  let module T = Tacticals in
271  let true_URI =
272   match LibraryObjects.true_URI () with
273      Some uri -> uri
274    | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
275  let false_URI =
276   match LibraryObjects.false_URI () with
277      Some uri -> uri
278    | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
279  let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
280  let find_discriminating_consno t1 t2 =
281    let rec aux t1 t2 =
282      match t1, t2 with
283      | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
284      | C.Appl ((C.MutConstruct _ as constr1) :: args1),
285        C.Appl ((C.MutConstruct _ as constr2) :: args2)
286        when constr1 = constr2 ->
287          let rec aux_list l1 l2 =
288            match l1, l2 with
289            | [], [] -> None
290            | hd1 :: tl1, hd2 :: tl2 ->
291                (match aux hd1 hd2 with
292                | None -> aux_list tl1 tl2
293                | Some _ as res -> res)
294            | _ -> (* same constructor applied to a different number of args *)
295                assert false
296          in
297          aux_list args1 args2
298      | ((C.MutConstruct (_,_,consno1,subst1)),
299        (C.MutConstruct (_,_,consno2,subst2)))
300      | ((C.MutConstruct (_,_,consno1,subst1)),
301        (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
302      | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
303        (C.MutConstruct (_,_,consno2,subst2)))
304      | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
305        (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
306        when (consno1 <> consno2) || (subst1 <> subst2) ->
307          Some consno2
308      | _ -> fail "not a discriminable equality"
309    in
310    aux t1 t2
311  in
312  let mk_pattern turi typeno consno context left_args =
313     (* a list of "True" except for the element in position consno which
314      * is "False" *)
315     match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
316     | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_)  ->
317         let _,_,_,constructor_list = List.nth ind_type_list typeno in 
318         let false_constr_id,_ = List.nth constructor_list (consno - 1) in
319         List.map 
320           (fun (id,cty) ->
321             (* dubbio: e' corretto ridurre in questo context ??? *)
322             let red_ty = CicReduction.whd context cty in
323             let rec aux t k =
324               match t with
325               | C.Prod (_,_,target) when (k <= nr_ind_params) ->
326                   CicSubstitution.subst (List.nth left_args (k-1))
327                     (aux target (k+1))
328               | C.Prod (binder,source,target) when (k > nr_ind_params) ->
329                   C.Lambda (binder, source, (aux target (k+1)))
330               | _ -> 
331                   if (id = false_constr_id)
332                   then (C.MutInd(false_URI,0,[]))
333                   else (C.MutInd(true_URI,0,[]))
334             in
335             (CicSubstitution.lift 1 (aux red_ty 1)))
336           constructor_list
337     | _ -> (* object is not an inductive definition *)
338         assert false
339  in
340  let discriminate'_tac ~term status = 
341   let (proof, goal) = status in
342   let _,metasenv,_,_ = proof in
343   let _,context,_ = CicUtil.lookup_meta goal metasenv in
344   let termty,_ = 
345     CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
346   in
347   match termty with
348   | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
349     when LibraryObjects.is_eq_URI equri ->
350       let turi,typeno,exp_named_subst,left_args = 
351         match tty with
352         | (C.MutInd (turi,typeno,exp_named_subst)) ->
353             turi,typeno,exp_named_subst,[]
354         | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
355             turi,typeno,exp_named_subst,left_args
356         | _ -> fail "not a discriminable equality"
357       in
358       let consno =
359         match find_discriminating_consno t1 t2 with
360         | Some consno -> consno
361         | None -> fail "discriminating terms are structurally equal"
362       in
363       let pattern = mk_pattern turi typeno consno context left_args in
364       let (proof',goals') = 
365         ProofEngineTypes.apply_tactic 
366           (EliminationTactics.elim_type_tac
367             (C.MutInd (false_URI, 0, [])))
368           status 
369       in
370       (match goals' with
371       | [goal'] -> 
372           let _,metasenv',_,_ = proof' in
373           let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
374           ProofEngineTypes.apply_tactic
375             (T.then_
376               ~start:
377                 (ReductionTactics.change_tac 
378                   ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
379                   (fun _ m u ->
380                     C.Appl [
381                       C.Lambda ( C.Name "x", tty,
382                         C.MutCase (turi, typeno,
383                           (C.Lambda ((C.Name "x"),
384                            (CicSubstitution.lift 1 tty),
385                            (C.Sort C.Prop))),
386                           (C.Rel 1), pattern));
387                       t2 ], m, u))
388               ~continuation:
389                 (T.then_
390                   ~start:
391                     (EqualityTactics.rewrite_simpl_tac
392                       ~direction:`RightToLeft
393                       ~pattern:(ProofEngineTypes.conclusion_pattern None)
394                       term)
395                   ~continuation:
396                     (IntroductionTactics.constructor_tac ~n:1)))
397             (proof',goal')
398       | [] -> fail "ElimType False left no goals"
399       | _ -> fail "ElimType False left more than one goal")
400     | _ -> fail "not an equality"
401   in
402   ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
403
404 let discriminate_tac ~term = 
405  let discriminate_tac ~term status =
406   ProofEngineTypes.apply_tactic 
407   (Tacticals.then_
408     ~start:(* (injection_tac ~term) *) Tacticals.id_tac
409     ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
410    status
411  in
412   ProofEngineTypes.mk_tactic (discriminate_tac ~term)
413
414 (* DISCRIMINTATE SENZA INJECTION 
415
416 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int) 
417
418 let discriminate_tac ~term status =
419   let module C = Cic in
420   let module U = UriManager in
421   let module P = PrimitiveTactics in
422   let module T = Tacticals in
423   let (proof, goal) = status in
424    let _,metasenv,_,_ = proof in
425     let _,context,_ = CicUtil.lookup_meta goal metasenv in
426      let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
427       match termty with
428          (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2]) 
429           when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind")) 
430             or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
431            match tty with
432               (C.MutInd (turi,typeno,exp_named_subst))
433             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
434
435                 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
436                  try
437                   let rec traverse t1 t2 =
438 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
439 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
440                    match t1,t2 with
441                       ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
442                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
443                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
444                        t1,t2,0
445                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
446                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2))) 
447                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
448                        let rec traverse_list l1 l2 =
449                          match l1,l2 with
450                             [],[] -> t1,t2,0
451                           | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
452                           | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
453                        in traverse_list applist1 applist2
454
455                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
456                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
457                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
458                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
459                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
460                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
461                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
462                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
463                          when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
464                        raise (TwoDifferentSubtermsFound (t1,t2,consno2))
465                     | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
466                   in traverse t1 t2
467                  with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
468                 in
469 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
470                  if consno2' = 0 
471                   then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
472                   else
473
474                    let pattern = 
475                      (* a list of "True" except for the element in position consno2' which is "False" *)
476                      match fst(CicEnvironment.get_obj turi 
477                                  CicUniv.empty_ugraph) with
478                         C.InductiveDefinition (ind_type_list,_,nr_ind_params)  ->
479 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
480                          let _,_,_,constructor_list = (List.nth ind_type_list typeno) in 
481 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
482                           let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
483 debug_print (lazy "XXXX nth funzionano ") ;
484                            List.map 
485                             (function (id,cty) ->
486                               let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
487                                let rec aux t k =
488                                 match t with
489                                    C.Prod (_,_,target) when (k <= nr_ind_params) ->
490                                     aux target (k+1)
491                                  | C.Prod (binder,source,target) when (k > nr_ind_params) -> 
492                                     C.Lambda (binder,source,(aux target (k+1)))
493                                  | _ -> 
494                                     if (id = false_constr_id)
495                                      then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
496                                      else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
497                                in aux red_ty 1
498                             ) 
499                             constructor_list
500                       | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
501                    in
502
503                     let (proof',goals') = 
504                      EliminationTactics.elim_type_tac 
505                       ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] ) 
506                       status 
507                     in
508                      (match goals' with
509                          [goal'] -> 
510                           let _,metasenv',_,_ = proof' in
511                            let _,context',gty' =
512                              CicUtil.lookup_meta goal' metasenv'
513                            in
514                             T.then_
515                              ~start:
516                               (P.change_tac 
517                                ~what:gty' 
518                                ~with_what:
519                                 (C.Appl [
520                                   C.Lambda (
521                                    C.Name "x", tty, 
522                                    C.MutCase (
523                                     turi, typeno,
524                                     (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
525                                     (C.Rel 1), pattern
526                                    )
527                                   ); 
528                                   t2']
529                                 )
530                               )
531                              ~continuation:
532                               (
533 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
534 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
535 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
536 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
537 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
538 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
539 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
540 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
541 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2') 
542  then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
543  metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
544
545                                let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
546                                 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
547
548 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
549                                  T.then_
550                                    ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
551                                    ~continuation:(IntroductionTactics.constructor_tac ~n:1) 
552                               )
553                              (proof',goal')
554                        | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
555                      )    
556             | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
557            )
558        | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")
559 ;;
560
561 *)
562
563
564