]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/discriminationTactics.ml
1. rewrite_* and rewrite_back_* merged into one function
[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 debug_print = fun _ -> ()
29
30 let rec injection_tac ~term =
31  let injection_tac ~term status = 
32   let (proof, goal) = status in
33   let module C = Cic in
34   let module U = UriManager in
35   let module P = PrimitiveTactics in
36   let module T = Tacticals in
37   let _,metasenv,_,_ = proof in
38   let _,context,_ = CicUtil.lookup_meta goal metasenv in
39   let termty,_ = (* TASSI: FIXME *)
40     CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in  
41     ProofEngineTypes.apply_tactic
42       (match termty with
43           (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
44              when (U.eq equri Logic.eq_URI) -> (
45            match tty with
46               (C.MutInd (turi,typeno,exp_named_subst))
47             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) -> (
48                    match t1,t2 with
49                       ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
50                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
51                          when (uri1 = uri2) && (typeno1 = typeno2) && 
52                               (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
53                        (* raise (ProofEngineTypes.Fail "Injection: nothing to do") ; *) T.id_tac
54                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
55                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
56                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
57                        let rec traverse_list i l1 l2 =
58                          match l1,l2 with
59                             [],[] -> T.id_tac
60                           | hd1::tl1,hd2::tl2 -> 
61                              T.then_ 
62                               ~start:(injection1_tac ~i ~term)
63                               ~continuation:(traverse_list (i+1) tl1 tl2)
64                           | _ -> 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
65                        in traverse_list 1 applist1 applist2
66                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
67                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
68                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
69                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
70                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
71                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
72                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
73                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
74                          when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
75                        (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality but a discriminable one") ; *) T.id_tac
76                     | _ -> (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality") ; *) T.id_tac
77                    )
78             | _ -> raise (ProofEngineTypes.Fail "Injection: not a projectable equality")
79            )
80         | _ -> raise (ProofEngineTypes.Fail "Injection: not an equation")
81       ) status
82  in 
83   ProofEngineTypes.mk_tactic (injection_tac ~term)
84
85 and injection1_tac ~term ~i = 
86  let injection1_tac ~term ~i status =
87   let (proof, goal) = status in
88   (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma differiscono (o potrebbero differire?) nell'i-esimo parametro del costruttore *)
89    let module C = Cic in
90    let module S = CicSubstitution in
91    let module U = UriManager in
92    let module P = PrimitiveTactics in
93    let module T = Tacticals in
94    let _,metasenv,_,_ = proof in
95    let _,context,_ = CicUtil.lookup_meta goal metasenv in
96    let termty,_ = (* TASSI: FIXME *)
97      CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
98      match termty with (* an equality *)
99          (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
100              when (U.eq equri Logic.eq_URI) -> (
101            match tty with (* some inductive type *)
102               (C.MutInd (turi,typeno,exp_named_subst))
103             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
104                let t1',t2',consno = (* sono i due sottotermini che differiscono *)
105                 match t1,t2 with
106                    ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
107                     (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
108                       when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) -> (* controllo ridondante *)
109                     (List.nth applist1 (i-1)),(List.nth applist2 (i-1)),consno2
110                  | _ -> assert false
111                in
112                 let tty',_ = 
113                   CicTypeChecker.type_of_aux' metasenv context t1' 
114                     CicUniv.empty_ugraph  in
115                 let pattern =
116                       match fst(CicEnvironment.get_obj 
117                                   CicUniv.empty_ugraph turi ) with
118                          C.InductiveDefinition (ind_type_list,_,nr_ind_params_dx,_)  ->
119                           let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
120                            let i_constr_id,_ = List.nth constructor_list (consno - 1) in
121                             List.map
122                              (function (id,cty) ->
123                                let reduced_cty = CicReduction.whd context cty in
124                                 let rec aux t k =
125                                  match t with
126                                     C.Prod (_,_,target) when (k <= nr_ind_params_dx) ->
127                                      aux target (k+1)
128                                   | C.Prod (binder,source,target) when (k > nr_ind_params_dx) ->
129                                     let binder' =
130                                       match binder with
131                                          C.Name b -> C.Name b
132                                        | C.Anonymous -> C.Name "y"
133                                      in
134                                       C.Lambda (binder',source,(aux target (k+1)))
135                                   | _ ->
136                                      let nr_param_constr = k - 1 - nr_ind_params_dx in
137                                       if (id = i_constr_id)
138                                        then C.Rel (nr_param_constr - i + 1)
139                                        else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda agguinto esternamente al case *)
140                                 in aux reduced_cty 1
141                              )
142                              constructor_list
143                        | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
144                 in
145                 ProofEngineTypes.apply_tactic   
146                  (T.thens 
147                   ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
148                   ~continuations:[
149                     T.then_ 
150                      ~start:(injection_tac ~term:(C.Rel 1))
151                      ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *) 
152                     ;
153                     T.then_ 
154                      ~start:(ProofEngineTypes.mk_tactic 
155                        (fun status ->    
156                          let (proof, goal) = status in
157                          let _,metasenv,_,_ = proof in
158                           let _,context,gty = CicUtil.lookup_meta goal metasenv in
159                            let new_t1' = 
160                             match gty with 
161                                (C.Appl (C.MutInd (_,_,_)::arglist)) -> 
162                                 List.nth arglist 1
163                              | _ -> raise (ProofEngineTypes.Fail "Injection: goal after cut is not correct")
164                            in
165                             ProofEngineTypes.apply_tactic 
166                             (P.change_tac
167                                ~pattern:(ProofEngineTypes.conclusion_pattern (Some new_t1'))
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:
187                            (EqualityTactics.rewrite_simpl_tac
188                              ~direction:`LeftToRight
189                              ~pattern:(ProofEngineTypes.conclusion_pattern None)
190                              term)
191                          ~continuation:EqualityTactics.reflexivity_tac
192                        )
193                    ])     
194                   status
195             | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
196            )
197         | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")
198  in
199   ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
200 ;;
201
202
203
204 exception TwoDifferentSubtermsFound of int 
205
206 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
207 diversi *)
208
209 let discriminate'_tac ~term =
210  let discriminate'_tac ~term status = 
211   let (proof, goal) = status in
212   let module C = Cic in
213   let module U = UriManager in
214   let module P = PrimitiveTactics in
215   let module T = Tacticals in
216   let _,metasenv,_,_ = proof in
217   let _,context,_ = CicUtil.lookup_meta goal metasenv in
218   let termty,_ = 
219     CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
220       match termty with
221          (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2]) 
222           when (U.eq equri Logic.eq_URI) -> (
223            match tty with
224               (C.MutInd (turi,typeno,exp_named_subst))
225             | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
226
227                 let consno2 = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
228                  try
229                   let rec traverse t1 t2 =
230                    match t1,t2 with
231                       ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
232                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
233                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
234                        0
235                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
236                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2))) 
237                          when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
238                        let rec traverse_list l1 l2 =
239                          match l1,l2 with
240                             [],[] -> 0
241                           | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
242                           | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
243                        in traverse_list applist1 applist2
244
245                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
246                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
247                     | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
248                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
249                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
250                        (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
251                     | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
252                        (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
253                          when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
254                        raise (TwoDifferentSubtermsFound consno2)
255                     | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
256                   in traverse t1 t2
257                  with (TwoDifferentSubtermsFound consno2) -> consno2
258                 in
259                  if consno2 = 0 
260                   then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
261                   else
262
263                    let pattern = 
264                      (* a list of "True" except for the element in position consno2 which is "False" *)
265                      match fst(CicEnvironment.get_obj 
266                                  CicUniv.empty_ugraph turi) with
267                         C.InductiveDefinition (ind_type_list,_,nr_ind_params,_)  ->
268                          let _,_,_,constructor_list = (List.nth ind_type_list typeno) in 
269                           let false_constr_id,_ = List.nth constructor_list (consno2 - 1) in
270                            List.map 
271                             (function (id,cty) ->
272                               let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
273                                let rec aux t k =
274                                 match t with
275                                    C.Prod (_,_,target) when (k <= nr_ind_params) ->
276                                     aux target (k+1)
277                                  | C.Prod (binder,source,target) when (k > nr_ind_params) -> 
278                                     C.Lambda (binder,source,(aux target (k+1)))
279                                  | _ -> 
280                                     if (id = false_constr_id)
281                                      then (C.MutInd(Logic.false_URI,0,[]))
282                                      else (C.MutInd(Logic.true_URI,0,[]))
283                                in aux red_ty 1
284                             ) 
285                             constructor_list
286                       | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
287                    in
288
289                     let (proof',goals') = 
290                      ProofEngineTypes.apply_tactic 
291                       (EliminationTactics.elim_type_tac 
292                        ~term:(C.MutInd(Logic.false_URI,0,[])))
293                       status 
294                     in
295                      (match goals' with
296                          [goal'] -> 
297                           let _,metasenv',_,_ = proof' in
298                            let _,context',gty' =
299                              CicUtil.lookup_meta goal' metasenv'
300                            in
301                             ProofEngineTypes.apply_tactic
302                             (T.then_
303                              ~start:
304                               (P.change_tac 
305                                ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
306                                (C.Appl [
307                                  C.Lambda (
308                                   C.Name "x", tty, 
309                                   C.MutCase (
310                                    turi, typeno,
311                                    (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
312                                    (C.Rel 1), pattern
313                                   )
314                                  ); 
315                                  t2]
316                                )
317                               )
318                              ~continuation:
319                               (
320                                  T.then_
321                                    ~start:
322                                      (EqualityTactics.rewrite_simpl_tac
323                                        ~direction:`RightToLeft
324                                        ~pattern:(ProofEngineTypes.conclusion_pattern None)
325                                        term)
326                                    ~continuation:(IntroductionTactics.constructor_tac ~n:1) 
327                               ))
328                              (proof',goal')
329                        | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
330                      )    
331             | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
332            )
333        | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")
334  in
335   ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
336 ;;
337
338 let discriminate_tac ~term = 
339  let discriminate_tac ~term status =
340   ProofEngineTypes.apply_tactic 
341   (Tacticals.then_
342     ~start:(* (injection_tac ~term) *) Tacticals.id_tac
343     ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
344    status
345  in
346   ProofEngineTypes.mk_tactic (discriminate_tac ~term)
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 = Tacticals.id_tac
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 debug_print ("XXXX t1 " ^ CicPp.ppterm t1) ;
439 debug_print ("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 debug_print ("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 fst(CicEnvironment.get_obj turi 
477                                  CicUniv.empty_ugraph) with
478                         C.InductiveDefinition (ind_type_list,_,nr_ind_params)  ->
479 debug_print ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno)) ;
480                          let _,_,_,constructor_list = (List.nth ind_type_list typeno) in 
481 debug_print ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2')) ;
482                           let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
483 debug_print ("XXXX nth funzionano ") ;
484                            List.map 
485                             (function (id,cty) ->
486                               let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
487                                let rec aux t k =
488                                 match t with
489                                    C.Prod (_,_,target) when (k <= nr_ind_params) ->
490                                     aux target (k+1)
491                                  | C.Prod (binder,source,target) when (k > nr_ind_params) -> 
492                                     C.Lambda (binder,source,(aux target (k+1)))
493                                  | _ -> 
494                                     if (id = false_constr_id)
495                                      then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
496                                      else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
497                                in aux red_ty 1
498                             ) 
499                             constructor_list
500                       | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
501                    in
502
503                     let (proof',goals') = 
504                      EliminationTactics.elim_type_tac 
505                       ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] ) 
506                       status 
507                     in
508                      (match goals' with
509                          [goal'] -> 
510                           let _,metasenv',_,_ = proof' in
511                            let _,context',gty' =
512                              CicUtil.lookup_meta goal' metasenv'
513                            in
514                             T.then_
515                              ~start:
516                               (P.change_tac 
517                                ~what:gty' 
518                                ~with_what:
519                                 (C.Appl [
520                                   C.Lambda (
521                                    C.Name "x", tty, 
522                                    C.MutCase (
523                                     turi, typeno,
524                                     (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
525                                     (C.Rel 1), pattern
526                                    )
527                                   ); 
528                                   t2']
529                                 )
530                               )
531                              ~continuation:
532                               (
533 debug_print ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2'])));
534 debug_print ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2'])) ;
535 debug_print ("XXXX equri: " ^ U.string_of_uri equri) ;
536 debug_print ("XXXX tty : " ^ CicPp.ppterm tty) ;
537 debug_print ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1')) ;
538 debug_print ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2')) ;
539 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1')) ;
540 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2')) ;
541 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2') 
542  then debug_print ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
543  metasenv' context' t1')) ; debug_print ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2')) ;
544
545                                let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
546                                 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
547
548 debug_print ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term));
549                                  T.then_
550                                    ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
551                                    ~continuation:(IntroductionTactics.constructor_tac ~n:1) 
552                               )
553                              (proof',goal')
554                        | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
555                      )    
556             | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
557            )
558        | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")
559 ;;
560
561 *)
562
563
564