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 "Injection: not a projectable equality but a discriminable one") ; *) T.id_tac
87 | _ -> (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality") ; *) T.id_tac
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
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 ->
147 | C.Anonymous -> C.Name "y"
149 C.Lambda (binder',source,(aux target (k+1)))
151 let nr_param_constr = k - 1 - paramsno in
153 then C.Rel (nr_param_constr - liftno)
154 else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda aggiunto esternamente al case *)
156 ) constructor_list in
158 let rec to_lambdas te head =
159 match CicReduction.whd context te with
160 | C.Prod (name,so,ta) ->
161 C.Lambda (name,so,to_lambdas ta head)
163 let rec skip_prods n te =
164 match n, CicReduction.whd context te with
166 | n, C.Prod (_,_,ta) -> skip_prods (n - 1) ta
167 | _, _ -> assert false
170 match CicSubstitution.lift (paramsno + 1) tty with
171 C.MutInd _ as tty' -> tty'
174 HExtlib.split_nth (paramsno +1) l in
178 | n -> C.Rel n :: (mk_rels (n - 1))
180 C.Appl (keep@mk_rels (List.length abstract))
183 match ind_type_list with
186 to_lambdas (skip_prods paramsno ty)
187 (C.Lambda (C.Name "x", abstracted_tty,
188 S.lift (2+paramsno) tty'))
191 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
193 ProofEngineTypes.apply_tactic
195 ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
198 ~start:(injection_tac ~term:(C.Rel 1))
199 ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *)
202 ~start:(ProofEngineTypes.mk_tactic
204 let (proof, goal) = status in
205 let _,metasenv,_,_ = proof in
207 CicUtil.lookup_meta goal metasenv
211 (C.Appl (C.MutInd (_,_,_)::arglist)) ->
215 (ProofEngineTypes.Fail
217 "Injection: goal after cut is not correct"))
220 ProofEngineTypes.apply_tactic
221 (ReductionTactics.change_tac
222 ~pattern:(ProofEngineTypes.conclusion_pattern
231 (turi,typeno,outtype,C.Rel 1,patterns)) ;
234 prerr_endline ("XXX: " ^ CicPp.ppterm xxx);
235 prerr_endline ("WITH: " ^ CicPp.ppterm new_t1');
246 (EqualityTactics.rewrite_simpl_tac
247 ~direction:`LeftToRight
248 ~pattern:(ProofEngineTypes.conclusion_pattern None)
250 ~continuation:EqualityTactics.reflexivity_tac
254 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equality over elements of an inductive type"))
256 | _ -> ProofEngineTypes.apply_tactic T.id_tac status (*XXXraise (ProofEngineTypes.Fail (lazy "Injection: not an equality"))*)
258 ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
261 exception TwoDifferentSubtermsFound of int
263 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
266 let discriminate'_tac ~term =
267 let module C = Cic in
268 let module U = UriManager in
269 let module P = PrimitiveTactics in
270 let module T = Tacticals in
272 match LibraryObjects.true_URI () with
274 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
276 match LibraryObjects.false_URI () with
278 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
279 let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
280 let find_discriminating_consno t1 t2 =
283 | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
284 | C.Appl ((C.MutConstruct _ as constr1) :: args1),
285 C.Appl ((C.MutConstruct _ as constr2) :: args2)
286 when constr1 = constr2 ->
287 let rec aux_list l1 l2 =
290 | hd1 :: tl1, hd2 :: tl2 ->
291 (match aux hd1 hd2 with
292 | None -> aux_list tl1 tl2
293 | Some _ as res -> res)
294 | _ -> (* same constructor applied to a different number of args *)
298 | ((C.MutConstruct (_,_,consno1,subst1)),
299 (C.MutConstruct (_,_,consno2,subst2)))
300 | ((C.MutConstruct (_,_,consno1,subst1)),
301 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
302 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
303 (C.MutConstruct (_,_,consno2,subst2)))
304 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
305 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
306 when (consno1 <> consno2) || (subst1 <> subst2) ->
308 | _ -> fail "not a discriminable equality"
312 let mk_pattern turi typeno consno context left_args =
313 (* a list of "True" except for the element in position consno which
315 match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
316 | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_) ->
317 let _,_,_,constructor_list = List.nth ind_type_list typeno in
318 let false_constr_id,_ = List.nth constructor_list (consno - 1) in
321 (* dubbio: e' corretto ridurre in questo context ??? *)
322 let red_ty = CicReduction.whd context cty in
325 | C.Prod (_,_,target) when (k <= nr_ind_params) ->
326 CicSubstitution.subst (List.nth left_args (k-1))
328 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
329 C.Lambda (binder, source, (aux target (k+1)))
331 if (id = false_constr_id)
332 then (C.MutInd(false_URI,0,[]))
333 else (C.MutInd(true_URI,0,[]))
335 (CicSubstitution.lift 1 (aux red_ty 1)))
337 | _ -> (* object is not an inductive definition *)
340 let discriminate'_tac ~term status =
341 let (proof, goal) = status in
342 let _,metasenv,_,_ = proof in
343 let _,context,_ = CicUtil.lookup_meta goal metasenv in
345 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
348 | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
349 when LibraryObjects.is_eq_URI equri ->
350 let turi,typeno,exp_named_subst,left_args =
352 | (C.MutInd (turi,typeno,exp_named_subst)) ->
353 turi,typeno,exp_named_subst,[]
354 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
355 turi,typeno,exp_named_subst,left_args
356 | _ -> fail "not a discriminable equality"
359 match find_discriminating_consno t1 t2 with
360 | Some consno -> consno
361 | None -> fail "discriminating terms are structurally equal"
363 let pattern = mk_pattern turi typeno consno context left_args in
364 let (proof',goals') =
365 ProofEngineTypes.apply_tactic
366 (EliminationTactics.elim_type_tac
367 (C.MutInd (false_URI, 0, [])))
372 let _,metasenv',_,_ = proof' in
373 let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
374 ProofEngineTypes.apply_tactic
377 (ReductionTactics.change_tac
378 ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
381 C.Lambda ( C.Name "x", tty,
382 C.MutCase (turi, typeno,
383 (C.Lambda ((C.Name "x"),
384 (CicSubstitution.lift 1 tty),
386 (C.Rel 1), pattern));
391 (EqualityTactics.rewrite_simpl_tac
392 ~direction:`RightToLeft
393 ~pattern:(ProofEngineTypes.conclusion_pattern None)
396 (IntroductionTactics.constructor_tac ~n:1)))
398 | [] -> fail "ElimType False left no goals"
399 | _ -> fail "ElimType False left more than one goal")
400 | _ -> fail "not an equality"
402 ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
404 let discriminate_tac ~term =
405 let discriminate_tac ~term status =
406 ProofEngineTypes.apply_tactic
408 ~start:(* (injection_tac ~term) *) Tacticals.id_tac
409 ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
412 ProofEngineTypes.mk_tactic (discriminate_tac ~term)
414 (* DISCRIMINTATE SENZA INJECTION
416 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int)
418 let discriminate_tac ~term status =
419 let module C = Cic in
420 let module U = UriManager in
421 let module P = PrimitiveTactics in
422 let module T = Tacticals in
423 let (proof, goal) = status in
424 let _,metasenv,_,_ = proof in
425 let _,context,_ = CicUtil.lookup_meta goal metasenv in
426 let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
428 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
429 when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind"))
430 or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
432 (C.MutInd (turi,typeno,exp_named_subst))
433 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
435 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
437 let rec traverse t1 t2 =
438 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
439 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
441 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
442 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
443 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
445 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
446 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
447 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
448 let rec traverse_list l1 l2 =
451 | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
452 | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
453 in traverse_list applist1 applist2
455 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
456 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
457 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
458 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
459 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
460 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
461 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
462 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
463 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
464 raise (TwoDifferentSubtermsFound (t1,t2,consno2))
465 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
467 with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
469 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
471 then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
475 (* a list of "True" except for the element in position consno2' which is "False" *)
476 match fst(CicEnvironment.get_obj turi
477 CicUniv.empty_ugraph) with
478 C.InductiveDefinition (ind_type_list,_,nr_ind_params) ->
479 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
480 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
481 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
482 let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
483 debug_print (lazy "XXXX nth funzionano ") ;
485 (function (id,cty) ->
486 let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
489 C.Prod (_,_,target) when (k <= nr_ind_params) ->
491 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
492 C.Lambda (binder,source,(aux target (k+1)))
494 if (id = false_constr_id)
495 then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
496 else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
500 | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
503 let (proof',goals') =
504 EliminationTactics.elim_type_tac
505 ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] )
510 let _,metasenv',_,_ = proof' in
511 let _,context',gty' =
512 CicUtil.lookup_meta goal' metasenv'
524 (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
533 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
534 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
535 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
536 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
537 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
538 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
539 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
540 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
541 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2')
542 then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
543 metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
545 let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
546 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
548 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
550 ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
551 ~continuation:(IntroductionTactics.constructor_tac ~n:1)
554 | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
556 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
558 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")