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