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