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