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