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