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