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