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 ~first_time ~term ~liftno ~continuation =
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 term = CicSubstitution.lift liftno term in
40 let termty,_ = (* TASSI: FIXME *)
41 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
43 ProofEngineTypes.apply_tactic
45 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
46 when LibraryObjects.is_eq_URI equri ->
49 (C.MutInd (turi,typeno,exp_named_subst))
50 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
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)
61 (ProofEngineTypes.Fail (lazy "Injection: nothing to do"))
66 (uri1,typeno1,consno1,exp_named_subst1))::applist1),
69 (uri2,typeno2,consno2,exp_named_subst2))::applist2)
70 when (uri1 = uri2) && (typeno1 = typeno2) &&
71 (consno1 = consno2) &&
72 (exp_named_subst1 = exp_named_subst2)
74 let rec traverse_list i l1 l2 =
82 (match List.nth context (n-1) with
87 (ProofEngineStructuralRules.clear
89 ~continuation:(continuation ~liftno)
92 | hd1::tl1,hd2::tl2 ->
95 (CicReduction.are_convertible ~metasenv
96 context hd1 hd2 CicUniv.empty_ugraph)
98 traverse_list (i+1) tl1 tl2
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???"))
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"))
117 raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
123 raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
127 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equation"))
130 ProofEngineTypes.mk_tactic (injection_tac ~term)
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
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 *)
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
162 CicTypeChecker.type_of_aux' metasenv context t1'
163 CicUniv.empty_ugraph in
164 let patterns,outtype =
166 fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi)
168 C.InductiveDefinition (ind_type_list,_,paramsno,_)->
169 let _,_,_,constructor_list =
170 List.nth ind_type_list typeno in
172 List.nth constructor_list (consno - 1) in
176 (function (id,cty) ->
177 let reduced_cty = CicReduction.whd context cty in
180 C.Prod (_,_,target) when k <= paramsno ->
182 | C.Prod (binder,source,target) when k > paramsno ->
188 (incr seed; "y" ^ string_of_int !seed)
190 C.Lambda (binder',source,(aux target (k+1)))
192 let nr_param_constr = k - 1 - paramsno in
195 else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda aggiunto esternamente al case *)
197 ) constructor_list in
200 let rec to_lambdas te head =
201 match CicReduction.whd context te with
202 | C.Prod (binder,so,ta) ->
207 C.Name (incr seed; "d" ^ string_of_int !seed)
209 C.Lambda (binder',so,to_lambdas ta head)
211 let rec skip_prods n te =
212 match n, CicReduction.whd context te with
214 | n, C.Prod (_,_,ta) -> skip_prods (n - 1) ta
215 | _, _ -> assert false
218 match CicSubstitution.lift (paramsno + 1) tty with
219 C.MutInd _ as tty' -> tty'
222 HExtlib.split_nth (paramsno +1) l in
226 | n -> C.Rel n :: (mk_rels (n - 1))
228 C.Appl (keep@mk_rels (List.length abstract))
231 match ind_type_list with
234 to_lambdas (skip_prods paramsno ty)
235 (C.Lambda (C.Name "x", abstracted_tty,
236 S.lift (2+paramsno) tty'))
239 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
241 ProofEngineTypes.apply_tactic
245 (C.Appl [C.MutInd (equri,0,[]) ; tty' ; t1' ; t2']))
247 [ injection_tac ~first_time:false ~liftno:0
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 *)
254 continuation ~liftno:(liftno + 1 + x)) ;
256 ~start:(ProofEngineTypes.mk_tactic
258 let (proof, goal) = status in
259 let _,metasenv,_,_ = proof in
261 CicUtil.lookup_meta goal metasenv
265 (C.Appl (C.MutInd (_,_,_)::arglist)) ->
269 (ProofEngineTypes.Fail
271 "Injection: goal after cut is not correct"))
273 ProofEngineTypes.apply_tactic
274 (ReductionTactics.change_tac
275 ~pattern:(ProofEngineTypes.conclusion_pattern
283 (turi,typeno,outtype,C.Rel 1,patterns)) ;
291 (EqualityTactics.rewrite_simpl_tac
292 ~direction:`LeftToRight
293 ~pattern:(ProofEngineTypes.conclusion_pattern None)
295 ~continuation:EqualityTactics.reflexivity_tac)
298 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality over elements of an inductive type"))
300 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality"))
302 ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
306 injection_tac ~first_time:true ~liftno:0 ~continuation:(fun ~liftno -> Tacticals.id_tac)
309 exception TwoDifferentSubtermsFound of int
311 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
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
320 match LibraryObjects.true_URI () with
322 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
324 match LibraryObjects.false_URI () with
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 =
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 =
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 *)
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) ->
356 | _ -> fail "not a discriminable equality"
360 let mk_pattern turi typeno consno context left_args =
361 (* a list of "True" except for the element in position consno which
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
369 (* dubbio: e' corretto ridurre in questo context ??? *)
370 let red_ty = CicReduction.whd context cty in
373 | C.Prod (_,_,target) when (k <= nr_ind_params) ->
374 CicSubstitution.subst (List.nth left_args (k-1))
376 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
377 C.Lambda (binder, source, (aux target (k+1)))
379 if (id = false_constr_id)
380 then (C.MutInd(false_URI,0,[]))
381 else (C.MutInd(true_URI,0,[]))
383 (CicSubstitution.lift 1 (aux red_ty 1)))
385 | _ -> (* object is not an inductive definition *)
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
393 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
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 =
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"
407 match find_discriminating_consno t1 t2 with
408 | Some consno -> consno
409 | None -> fail "discriminating terms are structurally equal"
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, [])))
420 let _,metasenv',_,_ = proof' in
421 let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
422 ProofEngineTypes.apply_tactic
425 (ReductionTactics.change_tac
426 ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
429 C.Lambda ( C.Name "x", tty,
430 C.MutCase (turi, typeno,
431 (C.Lambda ((C.Name "x"),
432 (CicSubstitution.lift 1 tty),
434 (C.Rel 1), pattern));
439 (EqualityTactics.rewrite_simpl_tac
440 ~direction:`RightToLeft
441 ~pattern:(ProofEngineTypes.conclusion_pattern None)
444 (IntroductionTactics.constructor_tac ~n:1)))
446 | [] -> fail "ElimType False left no goals"
447 | _ -> fail "ElimType False left more than one goal")
448 | _ -> fail "not an equality"
450 ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
452 let discriminate_tac ~term =
453 let discriminate_tac ~term status =
454 ProofEngineTypes.apply_tactic
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 *)
460 ProofEngineTypes.mk_tactic (discriminate_tac ~term)
462 (* DISCRIMINTATE SENZA INJECTION
464 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int)
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
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")) -> (
480 (C.MutInd (turi,typeno,exp_named_subst))
481 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
483 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
485 let rec traverse t1 t2 =
486 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
487 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
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) ->
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 =
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
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")
515 with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
517 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
519 then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
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 ") ;
533 (function (id,cty) ->
534 let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
537 C.Prod (_,_,target) when (k <= nr_ind_params) ->
539 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
540 C.Lambda (binder,source,(aux target (k+1)))
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 [])
548 | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
551 let (proof',goals') =
552 EliminationTactics.elim_type_tac
553 ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] )
558 let _,metasenv',_,_ = proof' in
559 let _,context',gty' =
560 CicUtil.lookup_meta goal' metasenv'
572 (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
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'))) ;
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
596 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
598 ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
599 ~continuation:(IntroductionTactics.constructor_tac ~n:1)
602 | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
604 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
606 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")