]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/discriminationTactics.ml
Bug fixed in injection: lifting was not performed correctly, but it worked
[helm.git] / components / tactics / discriminationTactics.ml
1 (* Copyright (C) 2002, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (* $Id$ *)
27
28 let debug_print = fun _ -> ()
29
30 let rec injection_tac ~term =
31  let injection_tac ~term status = 
32   let (proof, goal) = status in
33   let module C = Cic in
34   let module U = UriManager in
35   let module P = PrimitiveTactics in
36   let module T = Tacticals in
37   let _,metasenv,_,_ = proof in
38   let _,context,_ = CicUtil.lookup_meta goal metasenv in
39   let termty,_ = (* TASSI: FIXME *)
40     CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
41   in
42     ProofEngineTypes.apply_tactic
43       (match termty with
44           (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
45              when LibraryObjects.is_eq_URI equri -> (
46            match tty with
47               (C.MutInd (turi,typeno,exp_named_subst))
48             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) -> (
49                    match t1,t2 with
50                       ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
51                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
52                          when (uri1 = uri2) && (typeno1 = typeno2) && 
53                               (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
54                        (* raise (ProofEngineTypes.Fail "Injection: nothing to do") ; *) T.id_tac
55                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
56                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
57                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
58                        let rec traverse_list i liftno l1 l2 =
59                          match l1,l2 with
60                             [],[] -> T.id_tac
61                           | hd1::tl1,hd2::tl2 -> 
62                              if
63                               fst
64                                (CicReduction.are_convertible ~metasenv
65                                  context hd1 hd2 CicUniv.empty_ugraph)
66                              then
67                               traverse_list (i+1) liftno tl1 tl2
68                              else
69                               T.then_ 
70                                ~start:
71                                  (injection1_tac ~i ~liftno
72                                    ~term:(CicSubstitution.lift liftno term))
73                                ~continuation:
74                                  (traverse_list (i+1) (liftno+1) tl1 tl2)
75                           | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???"))
76                        in traverse_list 1 0 applist1 applist2
77                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
78                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
79                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
80                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
81                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
82                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
83                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
84                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
85                          when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
86                        (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality but a discriminable one") ; *) T.id_tac
87                     | _ -> (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality") ; *) T.id_tac
88                    )
89             | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
90            )
91         | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equation"))
92       ) status
93  in 
94   ProofEngineTypes.mk_tactic (injection_tac ~term)
95
96 and injection1_tac ~term ~i ~liftno = 
97  let injection1_tac ~term ~i status =
98   let (proof, goal) = status in
99   (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma differiscono (o potrebbero differire?) nell'i-esimo parametro del costruttore *)
100    let module C = Cic in
101    let module S = CicSubstitution in
102    let module U = UriManager in
103    let module P = PrimitiveTactics in
104    let module T = Tacticals in
105    let _,metasenv,_,_ = proof in
106    let _,context,_ = CicUtil.lookup_meta goal metasenv in
107    let termty,_ = (* TASSI: FIXME *)
108      CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
109      match termty with (* an equality *)
110          (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
111              when LibraryObjects.is_eq_URI equri -> (
112            match tty with (* some inductive type *)
113               (C.MutInd (turi,typeno,exp_named_subst))
114             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
115                let t1',t2',consno = (* sono i due sottotermini che differiscono *)
116                 match t1,t2 with
117                    ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
118                     (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
119                       when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) -> (* controllo ridondante *)
120                     (List.nth applist1 (i-1)),(List.nth applist2 (i-1)),consno2
121                  | _ -> assert false
122                in
123                 let tty',_ = 
124                   CicTypeChecker.type_of_aux' metasenv context t1' 
125                     CicUniv.empty_ugraph  in
126                 let patterns,outtype =
127                  match
128                   fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi)
129                  with
130                     C.InductiveDefinition (ind_type_list,_,paramsno,_)->
131                      let _,_,_,constructor_list =
132                       List.nth ind_type_list typeno in
133                      let i_constr_id,_ =
134                       List.nth constructor_list (consno - 1) in
135                      let 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 b -> C.Name b
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 rec to_lambdas te head =
168                        match CicReduction.whd context te with
169                         | C.Prod (name,so,ta) ->
170                            C.Lambda (name,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 ~term:(C.Rel 1))
208                      ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *) 
209                     ;
210                     T.then_ 
211                      ~start:(ProofEngineTypes.mk_tactic 
212                        (fun status ->    
213                          let (proof, goal) = status in
214                          let _,metasenv,_,_ = proof in
215                           let _,context,gty =
216                            CicUtil.lookup_meta goal metasenv
217                           in
218                            let new_t1' = 
219                             match gty with 
220                                (C.Appl (C.MutInd (_,_,_)::arglist)) -> 
221                                 List.nth arglist 1
222                              | _ ->
223                                raise
224                                 (ProofEngineTypes.Fail
225                                   (lazy
226                                     "Injection: goal after cut is not correct"))
227                            in
228 let aaa =
229                             ProofEngineTypes.apply_tactic 
230                             (ReductionTactics.change_tac
231                                ~pattern:(ProofEngineTypes.conclusion_pattern
232                                 (Some new_t1'))
233                                (fun _ m u ->
234 let xxx =
235                                  C.Appl [
236                                   C.Lambda
237                                    (C.Name "x",
238                                      tty,
239                                      C.MutCase
240                                       (turi,typeno,outtype,C.Rel 1,patterns)) ;
241                                   t1]
242 in
243 prerr_endline ("i=" ^ string_of_int i ^ "; liftno=" ^ string_of_int liftno);
244 prerr_endline ("XXX: " ^ CicPp.ppterm xxx);
245 prerr_endline ("WITH: " ^ CicPp.ppterm new_t1');
246 xxx,
247                                  m, u))
248                         status
249 in
250 prerr_endline "OK";
251 aaa
252                        ))
253                      ~continuation:
254                        (T.then_
255                          ~start:
256                            (EqualityTactics.rewrite_simpl_tac
257                              ~direction:`LeftToRight
258                              ~pattern:(ProofEngineTypes.conclusion_pattern None)
259                              term)
260                          ~continuation:EqualityTactics.reflexivity_tac
261                        )
262                    ])     
263                   status
264             | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality over elements of an inductive type"))
265            )
266         | _ -> ProofEngineTypes.apply_tactic T.id_tac status (*XXXraise (ProofEngineTypes.Fail (lazy "Injection: not an equality"))*)
267  in
268   ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
269 ;;
270
271 exception TwoDifferentSubtermsFound of int 
272
273 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
274 diversi *)
275
276 let discriminate'_tac ~term =
277  let module C = Cic in
278  let module U = UriManager in
279  let module P = PrimitiveTactics in
280  let module T = Tacticals in
281  let true_URI =
282   match LibraryObjects.true_URI () with
283      Some uri -> uri
284    | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
285  let false_URI =
286   match LibraryObjects.false_URI () with
287      Some uri -> uri
288    | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
289  let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
290  let find_discriminating_consno t1 t2 =
291    let rec aux t1 t2 =
292      match t1, t2 with
293      | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
294      | C.Appl ((C.MutConstruct _ as constr1) :: args1),
295        C.Appl ((C.MutConstruct _ as constr2) :: args2)
296        when constr1 = constr2 ->
297          let rec aux_list l1 l2 =
298            match l1, l2 with
299            | [], [] -> None
300            | hd1 :: tl1, hd2 :: tl2 ->
301                (match aux hd1 hd2 with
302                | None -> aux_list tl1 tl2
303                | Some _ as res -> res)
304            | _ -> (* same constructor applied to a different number of args *)
305                assert false
306          in
307          aux_list args1 args2
308      | ((C.MutConstruct (_,_,consno1,subst1)),
309        (C.MutConstruct (_,_,consno2,subst2)))
310      | ((C.MutConstruct (_,_,consno1,subst1)),
311        (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
312      | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
313        (C.MutConstruct (_,_,consno2,subst2)))
314      | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
315        (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
316        when (consno1 <> consno2) || (subst1 <> subst2) ->
317          Some consno2
318      | _ -> fail "not a discriminable equality"
319    in
320    aux t1 t2
321  in
322  let mk_pattern turi typeno consno context left_args =
323     (* a list of "True" except for the element in position consno which
324      * is "False" *)
325     match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
326     | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_)  ->
327         let _,_,_,constructor_list = List.nth ind_type_list typeno in 
328         let false_constr_id,_ = List.nth constructor_list (consno - 1) in
329         List.map 
330           (fun (id,cty) ->
331             (* dubbio: e' corretto ridurre in questo context ??? *)
332             let red_ty = CicReduction.whd context cty in
333             let rec aux t k =
334               match t with
335               | C.Prod (_,_,target) when (k <= nr_ind_params) ->
336                   CicSubstitution.subst (List.nth left_args (k-1))
337                     (aux target (k+1))
338               | C.Prod (binder,source,target) when (k > nr_ind_params) ->
339                   C.Lambda (binder, source, (aux target (k+1)))
340               | _ -> 
341                   if (id = false_constr_id)
342                   then (C.MutInd(false_URI,0,[]))
343                   else (C.MutInd(true_URI,0,[]))
344             in
345             (CicSubstitution.lift 1 (aux red_ty 1)))
346           constructor_list
347     | _ -> (* object is not an inductive definition *)
348         assert false
349  in
350  let discriminate'_tac ~term status = 
351   let (proof, goal) = status in
352   let _,metasenv,_,_ = proof in
353   let _,context,_ = CicUtil.lookup_meta goal metasenv in
354   let termty,_ = 
355     CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
356   in
357   match termty with
358   | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
359     when LibraryObjects.is_eq_URI equri ->
360       let turi,typeno,exp_named_subst,left_args = 
361         match tty with
362         | (C.MutInd (turi,typeno,exp_named_subst)) ->
363             turi,typeno,exp_named_subst,[]
364         | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
365             turi,typeno,exp_named_subst,left_args
366         | _ -> fail "not a discriminable equality"
367       in
368       let consno =
369         match find_discriminating_consno t1 t2 with
370         | Some consno -> consno
371         | None -> fail "discriminating terms are structurally equal"
372       in
373       let pattern = mk_pattern turi typeno consno context left_args in
374       let (proof',goals') = 
375         ProofEngineTypes.apply_tactic 
376           (EliminationTactics.elim_type_tac
377             (C.MutInd (false_URI, 0, [])))
378           status 
379       in
380       (match goals' with
381       | [goal'] -> 
382           let _,metasenv',_,_ = proof' in
383           let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
384           ProofEngineTypes.apply_tactic
385             (T.then_
386               ~start:
387                 (ReductionTactics.change_tac 
388                   ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
389                   (fun _ m u ->
390                     C.Appl [
391                       C.Lambda ( C.Name "x", tty,
392                         C.MutCase (turi, typeno,
393                           (C.Lambda ((C.Name "x"),
394                            (CicSubstitution.lift 1 tty),
395                            (C.Sort C.Prop))),
396                           (C.Rel 1), pattern));
397                       t2 ], m, u))
398               ~continuation:
399                 (T.then_
400                   ~start:
401                     (EqualityTactics.rewrite_simpl_tac
402                       ~direction:`RightToLeft
403                       ~pattern:(ProofEngineTypes.conclusion_pattern None)
404                       term)
405                   ~continuation:
406                     (IntroductionTactics.constructor_tac ~n:1)))
407             (proof',goal')
408       | [] -> fail "ElimType False left no goals"
409       | _ -> fail "ElimType False left more than one goal")
410     | _ -> fail "not an equality"
411   in
412   ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
413
414 let discriminate_tac ~term = 
415  let discriminate_tac ~term status =
416   ProofEngineTypes.apply_tactic 
417   (Tacticals.then_
418     ~start:(* (injection_tac ~term) *) Tacticals.id_tac
419     ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
420    status
421  in
422   ProofEngineTypes.mk_tactic (discriminate_tac ~term)
423
424 (* DISCRIMINTATE SENZA INJECTION 
425
426 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int) 
427
428 let discriminate_tac ~term status =
429   let module C = Cic in
430   let module U = UriManager in
431   let module P = PrimitiveTactics in
432   let module T = Tacticals in
433   let (proof, goal) = status in
434    let _,metasenv,_,_ = proof in
435     let _,context,_ = CicUtil.lookup_meta goal metasenv in
436      let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
437       match termty with
438          (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2]) 
439           when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind")) 
440             or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
441            match tty with
442               (C.MutInd (turi,typeno,exp_named_subst))
443             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
444
445                 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
446                  try
447                   let rec traverse t1 t2 =
448 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
449 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
450                    match t1,t2 with
451                       ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
452                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
453                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
454                        t1,t2,0
455                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
456                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2))) 
457                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
458                        let rec traverse_list l1 l2 =
459                          match l1,l2 with
460                             [],[] -> t1,t2,0
461                           | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
462                           | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
463                        in traverse_list applist1 applist2
464
465                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
466                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
467                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
468                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
469                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
470                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
471                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
472                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
473                          when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
474                        raise (TwoDifferentSubtermsFound (t1,t2,consno2))
475                     | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
476                   in traverse t1 t2
477                  with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
478                 in
479 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
480                  if consno2' = 0 
481                   then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
482                   else
483
484                    let pattern = 
485                      (* a list of "True" except for the element in position consno2' which is "False" *)
486                      match fst(CicEnvironment.get_obj turi 
487                                  CicUniv.empty_ugraph) with
488                         C.InductiveDefinition (ind_type_list,_,nr_ind_params)  ->
489 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
490                          let _,_,_,constructor_list = (List.nth ind_type_list typeno) in 
491 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
492                           let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
493 debug_print (lazy "XXXX nth funzionano ") ;
494                            List.map 
495                             (function (id,cty) ->
496                               let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
497                                let rec aux t k =
498                                 match t with
499                                    C.Prod (_,_,target) when (k <= nr_ind_params) ->
500                                     aux target (k+1)
501                                  | C.Prod (binder,source,target) when (k > nr_ind_params) -> 
502                                     C.Lambda (binder,source,(aux target (k+1)))
503                                  | _ -> 
504                                     if (id = false_constr_id)
505                                      then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
506                                      else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
507                                in aux red_ty 1
508                             ) 
509                             constructor_list
510                       | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
511                    in
512
513                     let (proof',goals') = 
514                      EliminationTactics.elim_type_tac 
515                       ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] ) 
516                       status 
517                     in
518                      (match goals' with
519                          [goal'] -> 
520                           let _,metasenv',_,_ = proof' in
521                            let _,context',gty' =
522                              CicUtil.lookup_meta goal' metasenv'
523                            in
524                             T.then_
525                              ~start:
526                               (P.change_tac 
527                                ~what:gty' 
528                                ~with_what:
529                                 (C.Appl [
530                                   C.Lambda (
531                                    C.Name "x", tty, 
532                                    C.MutCase (
533                                     turi, typeno,
534                                     (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
535                                     (C.Rel 1), pattern
536                                    )
537                                   ); 
538                                   t2']
539                                 )
540                               )
541                              ~continuation:
542                               (
543 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
544 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
545 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
546 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
547 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
548 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
549 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
550 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
551 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2') 
552  then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
553  metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
554
555                                let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
556                                 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
557
558 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
559                                  T.then_
560                                    ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
561                                    ~continuation:(IntroductionTactics.constructor_tac ~n:1) 
562                               )
563                              (proof',goal')
564                        | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
565                      )    
566             | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
567            )
568        | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")
569 ;;
570
571 *)
572
573
574