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 ~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 -> (
48 (C.MutInd (turi,typeno,exp_named_subst))
49 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) -> (
51 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
52 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
53 when (uri1 = uri2) && (typeno1 = typeno2) &&
54 (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
55 (* raise (ProofEngineTypes.Fail "Injection: nothing to do") ; *) continuation ~liftno
56 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
57 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
58 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
59 let rec traverse_list i l1 l2 =
62 | hd1::tl1,hd2::tl2 ->
65 (CicReduction.are_convertible ~metasenv
66 context hd1 hd2 CicUniv.empty_ugraph)
68 traverse_list (i+1) tl1 tl2
70 injection1_tac ~i ~term
71 ~continuation:(traverse_list (i+1) tl1 tl2)
72 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???"))
73 in traverse_list 1 applist1 applist2 ~liftno
74 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
75 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
76 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
77 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
78 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
79 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
80 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
81 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
82 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
83 raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality but a discriminable one"))
84 | _ -> continuation ~liftno (*raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))*)
86 | _ -> continuation ~liftno (*raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))*)
88 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equation"))
91 ProofEngineTypes.mk_tactic (injection_tac ~term)
93 and injection1_tac ~term ~i ~liftno ~continuation =
94 let injection1_tac ~term ~i status =
95 let (proof, goal) = status in
96 (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma differiscono (o potrebbero differire?) nell'i-esimo parametro del costruttore *)
98 let module S = CicSubstitution in
99 let module U = UriManager in
100 let module P = PrimitiveTactics in
101 let module T = Tacticals in
102 let term = CicSubstitution.lift liftno term in
103 let _,metasenv,_,_ = proof in
104 let _,context,_ = CicUtil.lookup_meta goal metasenv in
105 let termty,_ = (* TASSI: FIXME *)
106 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
108 match termty with (* an equality *)
109 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
110 when LibraryObjects.is_eq_URI equri -> (
111 match tty with (* some inductive type *)
112 (C.MutInd (turi,typeno,exp_named_subst))
113 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
114 let t1',t2',consno = (* sono i due sottotermini che differiscono *)
116 ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
117 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
118 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) -> (* controllo ridondante *)
119 (List.nth applist1 (i-1)),(List.nth applist2 (i-1)),consno2
123 CicTypeChecker.type_of_aux' metasenv context t1'
124 CicUniv.empty_ugraph in
125 let patterns,outtype =
127 fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi)
129 C.InductiveDefinition (ind_type_list,_,paramsno,_)->
130 let _,_,_,constructor_list =
131 List.nth ind_type_list typeno in
133 List.nth constructor_list (consno - 1) in
137 (function (id,cty) ->
138 let reduced_cty = CicReduction.whd context cty in
141 C.Prod (_,_,target) when k <= paramsno ->
143 | C.Prod (binder,source,target) when k > paramsno ->
149 (incr seed; "y" ^ string_of_int !seed)
151 C.Lambda (binder',source,(aux target (k+1)))
153 let nr_param_constr = k - 1 - paramsno in
156 else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda aggiunto esternamente al case *)
158 ) constructor_list in
161 let rec to_lambdas te head =
162 match CicReduction.whd context te with
163 | C.Prod (binder,so,ta) ->
168 C.Name (incr seed; "d" ^ string_of_int !seed)
170 C.Lambda (binder',so,to_lambdas ta head)
172 let rec skip_prods n te =
173 match n, CicReduction.whd context te with
175 | n, C.Prod (_,_,ta) -> skip_prods (n - 1) ta
176 | _, _ -> assert false
179 match CicSubstitution.lift (paramsno + 1) tty with
180 C.MutInd _ as tty' -> tty'
183 HExtlib.split_nth (paramsno +1) l in
187 | n -> C.Rel n :: (mk_rels (n - 1))
189 C.Appl (keep@mk_rels (List.length abstract))
192 match ind_type_list with
195 to_lambdas (skip_prods paramsno ty)
196 (C.Lambda (C.Name "x", abstracted_tty,
197 S.lift (2+paramsno) tty'))
200 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
202 ProofEngineTypes.apply_tactic
204 ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
207 ~start:(injection_tac ~liftno:0 ~term:(C.Rel 1)
208 ~continuation:(fun ~liftno:x -> continuation ~liftno:(liftno + 1 + x)))
209 ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *)
212 ~start:(ProofEngineTypes.mk_tactic
214 let (proof, goal) = status in
215 let _,metasenv,_,_ = proof in
217 CicUtil.lookup_meta goal metasenv
221 (C.Appl (C.MutInd (_,_,_)::arglist)) ->
225 (ProofEngineTypes.Fail
227 "Injection: goal after cut is not correct"))
229 ProofEngineTypes.apply_tactic
230 (ReductionTactics.change_tac
231 ~pattern:(ProofEngineTypes.conclusion_pattern
239 (turi,typeno,outtype,C.Rel 1,patterns)) ;
247 (EqualityTactics.rewrite_simpl_tac
248 ~direction:`LeftToRight
249 ~pattern:(ProofEngineTypes.conclusion_pattern None)
251 ~continuation:EqualityTactics.reflexivity_tac
255 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality over elements of an inductive type"))
257 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality"))
259 ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
263 injection_tac ~liftno:0 ~continuation:(fun ~liftno -> Tacticals.id_tac)
266 exception TwoDifferentSubtermsFound of int
268 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
271 let discriminate'_tac ~term =
272 let module C = Cic in
273 let module U = UriManager in
274 let module P = PrimitiveTactics in
275 let module T = Tacticals in
277 match LibraryObjects.true_URI () with
279 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
281 match LibraryObjects.false_URI () with
283 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
284 let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
285 let find_discriminating_consno t1 t2 =
288 | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
289 | C.Appl ((C.MutConstruct _ as constr1) :: args1),
290 C.Appl ((C.MutConstruct _ as constr2) :: args2)
291 when constr1 = constr2 ->
292 let rec aux_list l1 l2 =
295 | hd1 :: tl1, hd2 :: tl2 ->
296 (match aux hd1 hd2 with
297 | None -> aux_list tl1 tl2
298 | Some _ as res -> res)
299 | _ -> (* same constructor applied to a different number of args *)
303 | ((C.MutConstruct (_,_,consno1,subst1)),
304 (C.MutConstruct (_,_,consno2,subst2)))
305 | ((C.MutConstruct (_,_,consno1,subst1)),
306 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
307 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
308 (C.MutConstruct (_,_,consno2,subst2)))
309 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
310 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
311 when (consno1 <> consno2) || (subst1 <> subst2) ->
313 | _ -> fail "not a discriminable equality"
317 let mk_pattern turi typeno consno context left_args =
318 (* a list of "True" except for the element in position consno which
320 match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
321 | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_) ->
322 let _,_,_,constructor_list = List.nth ind_type_list typeno in
323 let false_constr_id,_ = List.nth constructor_list (consno - 1) in
326 (* dubbio: e' corretto ridurre in questo context ??? *)
327 let red_ty = CicReduction.whd context cty in
330 | C.Prod (_,_,target) when (k <= nr_ind_params) ->
331 CicSubstitution.subst (List.nth left_args (k-1))
333 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
334 C.Lambda (binder, source, (aux target (k+1)))
336 if (id = false_constr_id)
337 then (C.MutInd(false_URI,0,[]))
338 else (C.MutInd(true_URI,0,[]))
340 (CicSubstitution.lift 1 (aux red_ty 1)))
342 | _ -> (* object is not an inductive definition *)
345 let discriminate'_tac ~term status =
346 let (proof, goal) = status in
347 let _,metasenv,_,_ = proof in
348 let _,context,_ = CicUtil.lookup_meta goal metasenv in
350 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
353 | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
354 when LibraryObjects.is_eq_URI equri ->
355 let turi,typeno,exp_named_subst,left_args =
357 | (C.MutInd (turi,typeno,exp_named_subst)) ->
358 turi,typeno,exp_named_subst,[]
359 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
360 turi,typeno,exp_named_subst,left_args
361 | _ -> fail "not a discriminable equality"
364 match find_discriminating_consno t1 t2 with
365 | Some consno -> consno
366 | None -> fail "discriminating terms are structurally equal"
368 let pattern = mk_pattern turi typeno consno context left_args in
369 let (proof',goals') =
370 ProofEngineTypes.apply_tactic
371 (EliminationTactics.elim_type_tac
372 (C.MutInd (false_URI, 0, [])))
377 let _,metasenv',_,_ = proof' in
378 let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
379 ProofEngineTypes.apply_tactic
382 (ReductionTactics.change_tac
383 ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
386 C.Lambda ( C.Name "x", tty,
387 C.MutCase (turi, typeno,
388 (C.Lambda ((C.Name "x"),
389 (CicSubstitution.lift 1 tty),
391 (C.Rel 1), pattern));
396 (EqualityTactics.rewrite_simpl_tac
397 ~direction:`RightToLeft
398 ~pattern:(ProofEngineTypes.conclusion_pattern None)
401 (IntroductionTactics.constructor_tac ~n:1)))
403 | [] -> fail "ElimType False left no goals"
404 | _ -> fail "ElimType False left more than one goal")
405 | _ -> fail "not an equality"
407 ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
409 let discriminate_tac ~term =
410 let discriminate_tac ~term status =
411 ProofEngineTypes.apply_tactic
413 ~start:(* (injection_tac ~term) *) Tacticals.id_tac
414 ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
417 ProofEngineTypes.mk_tactic (discriminate_tac ~term)
419 (* DISCRIMINTATE SENZA INJECTION
421 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int)
423 let discriminate_tac ~term status =
424 let module C = Cic in
425 let module U = UriManager in
426 let module P = PrimitiveTactics in
427 let module T = Tacticals in
428 let (proof, goal) = status in
429 let _,metasenv,_,_ = proof in
430 let _,context,_ = CicUtil.lookup_meta goal metasenv in
431 let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
433 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
434 when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind"))
435 or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
437 (C.MutInd (turi,typeno,exp_named_subst))
438 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
440 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
442 let rec traverse t1 t2 =
443 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
444 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
446 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
447 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
448 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
450 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
451 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
452 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
453 let rec traverse_list l1 l2 =
456 | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
457 | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
458 in traverse_list applist1 applist2
460 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
461 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
462 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
463 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
464 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
465 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
466 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
467 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
468 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
469 raise (TwoDifferentSubtermsFound (t1,t2,consno2))
470 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
472 with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
474 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
476 then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
480 (* a list of "True" except for the element in position consno2' which is "False" *)
481 match fst(CicEnvironment.get_obj turi
482 CicUniv.empty_ugraph) with
483 C.InductiveDefinition (ind_type_list,_,nr_ind_params) ->
484 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
485 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
486 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
487 let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
488 debug_print (lazy "XXXX nth funzionano ") ;
490 (function (id,cty) ->
491 let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
494 C.Prod (_,_,target) when (k <= nr_ind_params) ->
496 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
497 C.Lambda (binder,source,(aux target (k+1)))
499 if (id = false_constr_id)
500 then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
501 else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
505 | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
508 let (proof',goals') =
509 EliminationTactics.elim_type_tac
510 ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] )
515 let _,metasenv',_,_ = proof' in
516 let _,context',gty' =
517 CicUtil.lookup_meta goal' metasenv'
529 (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
538 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
539 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
540 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
541 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
542 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
543 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
544 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
545 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
546 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2')
547 then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
548 metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
550 let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
551 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
553 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
555 ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
556 ~continuation:(IntroductionTactics.constructor_tac ~n:1)
559 | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
561 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
563 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")