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