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