1 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
28 let debug_print = fun _ -> ()
30 let rec injection_tac ~term =
31 let injection_tac ~term status =
32 let (proof, goal) = status 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
42 ProofEngineTypes.apply_tactic
44 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
45 when LibraryObjects.is_eq_URI equri -> (
47 (C.MutInd (turi,typeno,exp_named_subst))
48 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) -> (
50 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
51 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
52 when (uri1 = uri2) && (typeno1 = typeno2) &&
53 (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
54 (* raise (ProofEngineTypes.Fail "Injection: nothing to do") ; *) T.id_tac
55 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
56 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
57 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
58 let rec traverse_list i liftno l1 l2 =
61 | hd1::tl1,hd2::tl2 ->
64 (CicReduction.are_convertible ~metasenv
65 context hd1 hd2 CicUniv.empty_ugraph)
67 traverse_list (i+1) liftno tl1 tl2
71 (injection1_tac ~i ~liftno
72 ~term:(CicSubstitution.lift liftno term))
74 (traverse_list (i+1) (liftno+1) tl1 tl2)
75 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???"))
76 in traverse_list 1 0 applist1 applist2
77 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
78 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
79 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
80 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
81 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
82 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
83 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
84 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
85 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
86 raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality but a discriminable one"))
87 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
89 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
91 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equation"))
94 ProofEngineTypes.mk_tactic (injection_tac ~term)
96 and injection1_tac ~term ~i ~liftno =
97 let injection1_tac ~term ~i status =
98 let (proof, goal) = status in
99 (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma differiscono (o potrebbero differire?) nell'i-esimo parametro del costruttore *)
100 let module C = Cic in
101 let module S = CicSubstitution in
102 let module U = UriManager in
103 let module P = PrimitiveTactics in
104 let module T = Tacticals in
105 let _,metasenv,_,_ = proof in
106 let _,context,_ = CicUtil.lookup_meta goal metasenv in
107 let termty,_ = (* TASSI: FIXME *)
108 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
109 match termty with (* an equality *)
110 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
111 when LibraryObjects.is_eq_URI equri -> (
112 match tty with (* some inductive type *)
113 (C.MutInd (turi,typeno,exp_named_subst))
114 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
115 let t1',t2',consno = (* sono i due sottotermini che differiscono *)
117 ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
118 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
119 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) -> (* controllo ridondante *)
120 (List.nth applist1 (i-1)),(List.nth applist2 (i-1)),consno2
124 CicTypeChecker.type_of_aux' metasenv context t1'
125 CicUniv.empty_ugraph in
126 let patterns,outtype =
128 fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi)
130 C.InductiveDefinition (ind_type_list,_,paramsno,_)->
131 let _,_,_,constructor_list =
132 List.nth ind_type_list typeno in
134 List.nth constructor_list (consno - 1) in
138 (function (id,cty) ->
139 let reduced_cty = CicReduction.whd context cty in
142 C.Prod (_,_,target) when k <= paramsno ->
144 | C.Prod (binder,source,target) when k > paramsno ->
150 (incr seed; "y" ^ string_of_int !seed)
152 C.Lambda (binder',source,(aux target (k+1)))
154 if id = i_constr_id then (
155 prerr_endline ("k= " ^ string_of_int k);
156 prerr_endline ("paramsno= " ^ string_of_int paramsno);
157 prerr_endline ("nr_param_constr " ^ string_of_int (k - 1 - paramsno));
158 prerr_endline ("rel= " ^ string_of_int (k - i));
160 let nr_param_constr = k - 1 - paramsno in
163 else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda aggiunto esternamente al case *)
165 ) constructor_list in
168 let rec to_lambdas te head =
169 match CicReduction.whd context te with
170 | C.Prod (binder,so,ta) ->
175 C.Name (incr seed; "d" ^ string_of_int !seed)
177 C.Lambda (binder',so,to_lambdas ta head)
179 let rec skip_prods n te =
180 match n, CicReduction.whd context te with
182 | n, C.Prod (_,_,ta) -> skip_prods (n - 1) ta
183 | _, _ -> assert false
186 match CicSubstitution.lift (paramsno + 1) tty with
187 C.MutInd _ as tty' -> tty'
190 HExtlib.split_nth (paramsno +1) l in
194 | n -> C.Rel n :: (mk_rels (n - 1))
196 C.Appl (keep@mk_rels (List.length abstract))
199 match ind_type_list with
202 to_lambdas (skip_prods paramsno ty)
203 (C.Lambda (C.Name "x", abstracted_tty,
204 S.lift (2+paramsno) tty'))
207 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
209 ProofEngineTypes.apply_tactic
211 ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
214 ~start:T.id_tac (*(injection_tac ~term:(C.Rel 1))*)
215 ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *)
218 ~start:(ProofEngineTypes.mk_tactic
220 let (proof, goal) = status in
221 let _,metasenv,_,_ = proof in
223 CicUtil.lookup_meta goal metasenv
227 (C.Appl (C.MutInd (_,_,_)::arglist)) ->
231 (ProofEngineTypes.Fail
233 "Injection: goal after cut is not correct"))
236 ProofEngineTypes.apply_tactic
237 (ReductionTactics.change_tac
238 ~pattern:(ProofEngineTypes.conclusion_pattern
247 (turi,typeno,outtype,C.Rel 1,patterns)) ;
250 prerr_endline ("i=" ^ string_of_int i ^ "; liftno=" ^ string_of_int liftno);
251 prerr_endline ("XXX: " ^ CicPp.ppterm xxx);
252 prerr_endline ("WITH: " ^ CicPp.ppterm new_t1');
263 (EqualityTactics.rewrite_simpl_tac
264 ~direction:`LeftToRight
265 ~pattern:(ProofEngineTypes.conclusion_pattern None)
267 ~continuation:EqualityTactics.reflexivity_tac
271 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality over elements of an inductive type"))
273 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality"))
275 ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
278 exception TwoDifferentSubtermsFound of int
280 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
283 let discriminate'_tac ~term =
284 let module C = Cic in
285 let module U = UriManager in
286 let module P = PrimitiveTactics in
287 let module T = Tacticals in
289 match LibraryObjects.true_URI () with
291 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
293 match LibraryObjects.false_URI () with
295 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
296 let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
297 let find_discriminating_consno t1 t2 =
300 | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
301 | C.Appl ((C.MutConstruct _ as constr1) :: args1),
302 C.Appl ((C.MutConstruct _ as constr2) :: args2)
303 when constr1 = constr2 ->
304 let rec aux_list l1 l2 =
307 | hd1 :: tl1, hd2 :: tl2 ->
308 (match aux hd1 hd2 with
309 | None -> aux_list tl1 tl2
310 | Some _ as res -> res)
311 | _ -> (* same constructor applied to a different number of args *)
315 | ((C.MutConstruct (_,_,consno1,subst1)),
316 (C.MutConstruct (_,_,consno2,subst2)))
317 | ((C.MutConstruct (_,_,consno1,subst1)),
318 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
319 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
320 (C.MutConstruct (_,_,consno2,subst2)))
321 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
322 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
323 when (consno1 <> consno2) || (subst1 <> subst2) ->
325 | _ -> fail "not a discriminable equality"
329 let mk_pattern turi typeno consno context left_args =
330 (* a list of "True" except for the element in position consno which
332 match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
333 | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_) ->
334 let _,_,_,constructor_list = List.nth ind_type_list typeno in
335 let false_constr_id,_ = List.nth constructor_list (consno - 1) in
338 (* dubbio: e' corretto ridurre in questo context ??? *)
339 let red_ty = CicReduction.whd context cty in
342 | C.Prod (_,_,target) when (k <= nr_ind_params) ->
343 CicSubstitution.subst (List.nth left_args (k-1))
345 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
346 C.Lambda (binder, source, (aux target (k+1)))
348 if (id = false_constr_id)
349 then (C.MutInd(false_URI,0,[]))
350 else (C.MutInd(true_URI,0,[]))
352 (CicSubstitution.lift 1 (aux red_ty 1)))
354 | _ -> (* object is not an inductive definition *)
357 let discriminate'_tac ~term status =
358 let (proof, goal) = status in
359 let _,metasenv,_,_ = proof in
360 let _,context,_ = CicUtil.lookup_meta goal metasenv in
362 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
365 | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
366 when LibraryObjects.is_eq_URI equri ->
367 let turi,typeno,exp_named_subst,left_args =
369 | (C.MutInd (turi,typeno,exp_named_subst)) ->
370 turi,typeno,exp_named_subst,[]
371 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
372 turi,typeno,exp_named_subst,left_args
373 | _ -> fail "not a discriminable equality"
376 match find_discriminating_consno t1 t2 with
377 | Some consno -> consno
378 | None -> fail "discriminating terms are structurally equal"
380 let pattern = mk_pattern turi typeno consno context left_args in
381 let (proof',goals') =
382 ProofEngineTypes.apply_tactic
383 (EliminationTactics.elim_type_tac
384 (C.MutInd (false_URI, 0, [])))
389 let _,metasenv',_,_ = proof' in
390 let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
391 ProofEngineTypes.apply_tactic
394 (ReductionTactics.change_tac
395 ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
398 C.Lambda ( C.Name "x", tty,
399 C.MutCase (turi, typeno,
400 (C.Lambda ((C.Name "x"),
401 (CicSubstitution.lift 1 tty),
403 (C.Rel 1), pattern));
408 (EqualityTactics.rewrite_simpl_tac
409 ~direction:`RightToLeft
410 ~pattern:(ProofEngineTypes.conclusion_pattern None)
413 (IntroductionTactics.constructor_tac ~n:1)))
415 | [] -> fail "ElimType False left no goals"
416 | _ -> fail "ElimType False left more than one goal")
417 | _ -> fail "not an equality"
419 ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
421 let discriminate_tac ~term =
422 let discriminate_tac ~term status =
423 ProofEngineTypes.apply_tactic
425 ~start:(* (injection_tac ~term) *) Tacticals.id_tac
426 ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
429 ProofEngineTypes.mk_tactic (discriminate_tac ~term)
431 (* DISCRIMINTATE SENZA INJECTION
433 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int)
435 let discriminate_tac ~term status =
436 let module C = Cic in
437 let module U = UriManager in
438 let module P = PrimitiveTactics in
439 let module T = Tacticals in
440 let (proof, goal) = status in
441 let _,metasenv,_,_ = proof in
442 let _,context,_ = CicUtil.lookup_meta goal metasenv in
443 let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
445 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
446 when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind"))
447 or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
449 (C.MutInd (turi,typeno,exp_named_subst))
450 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
452 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
454 let rec traverse t1 t2 =
455 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
456 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
458 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
459 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
460 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
462 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
463 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
464 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
465 let rec traverse_list l1 l2 =
468 | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
469 | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
470 in traverse_list applist1 applist2
472 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
473 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
474 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
475 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
476 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
477 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
478 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
479 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
480 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
481 raise (TwoDifferentSubtermsFound (t1,t2,consno2))
482 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
484 with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
486 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
488 then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
492 (* a list of "True" except for the element in position consno2' which is "False" *)
493 match fst(CicEnvironment.get_obj turi
494 CicUniv.empty_ugraph) with
495 C.InductiveDefinition (ind_type_list,_,nr_ind_params) ->
496 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
497 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
498 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
499 let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
500 debug_print (lazy "XXXX nth funzionano ") ;
502 (function (id,cty) ->
503 let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
506 C.Prod (_,_,target) when (k <= nr_ind_params) ->
508 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
509 C.Lambda (binder,source,(aux target (k+1)))
511 if (id = false_constr_id)
512 then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
513 else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
517 | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
520 let (proof',goals') =
521 EliminationTactics.elim_type_tac
522 ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] )
527 let _,metasenv',_,_ = proof' in
528 let _,context',gty' =
529 CicUtil.lookup_meta goal' metasenv'
541 (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
550 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
551 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
552 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
553 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
554 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
555 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
556 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
557 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
558 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2')
559 then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
560 metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
562 let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
563 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
565 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
567 ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
568 ~continuation:(IntroductionTactics.constructor_tac ~n:1)
571 | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
573 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
575 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")