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