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