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 l1 l2 =
61 | hd1::tl1,hd2::tl2 ->
65 ~term:(CicSubstitution.lift (i-1) term))
66 ~continuation:(traverse_list (i+1) tl1 tl2)
67 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???"))
68 in traverse_list 1 applist1 applist2
69 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
70 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
71 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
72 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
73 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
74 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
75 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
76 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
77 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
78 (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality but a discriminable one") ; *) T.id_tac
79 | _ -> (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality") ; *) T.id_tac
81 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
83 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equation"))
86 ProofEngineTypes.mk_tactic (injection_tac ~term)
88 and injection1_tac ~term ~i =
89 let injection1_tac ~term ~i status =
90 let (proof, goal) = status in
91 (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma differiscono (o potrebbero differire?) nell'i-esimo parametro del costruttore *)
93 let module S = CicSubstitution in
94 let module U = UriManager in
95 let module P = PrimitiveTactics in
96 let module T = Tacticals in
97 let _,metasenv,_,_ = proof in
98 let _,context,_ = CicUtil.lookup_meta goal metasenv in
99 let termty,_ = (* TASSI: FIXME *)
100 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
101 match termty with (* an equality *)
102 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
103 when LibraryObjects.is_eq_URI equri -> (
104 match tty with (* some inductive type *)
105 (C.MutInd (turi,typeno,exp_named_subst))
106 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
107 let t1',t2',consno = (* sono i due sottotermini che differiscono *)
109 ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
110 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
111 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) -> (* controllo ridondante *)
112 (List.nth applist1 (i-1)),(List.nth applist2 (i-1)),consno2
116 CicTypeChecker.type_of_aux' metasenv context t1'
117 CicUniv.empty_ugraph in
118 let patterns,outtype =
120 fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi)
122 C.InductiveDefinition (ind_type_list,_,paramsno,_)->
123 let _,_,_,constructor_list =
124 List.nth ind_type_list typeno in
126 List.nth constructor_list (consno - 1) in
129 (function (id,cty) ->
130 let reduced_cty = CicReduction.whd context cty in
133 C.Prod (_,_,target) when k <= paramsno ->
135 | C.Prod (binder,source,target) when k > paramsno ->
139 | C.Anonymous -> C.Name "y"
141 C.Lambda (binder',source,(aux target (k+1)))
143 let nr_param_constr = k - 1 - paramsno in
145 then C.Rel (nr_param_constr - i + 1)
146 else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda aggiunto esternamente al case *)
148 ) constructor_list in
150 let rec to_lambdas te head =
151 match CicReduction.whd context te with
152 | C.Prod (name,so,ta) ->
153 C.Lambda (name,so,to_lambdas ta head)
155 let rec skip_prods n te =
156 match n, CicReduction.whd context te with
158 | n, C.Prod (_,_,ta) -> skip_prods (n - 1) ta
159 | _, _ -> assert false
162 match CicSubstitution.lift (paramsno + 1) tty with
163 C.MutInd _ as tty' -> tty'
166 HExtlib.split_nth (paramsno +1) l in
170 | n -> C.Rel n :: (mk_rels (n - 1))
172 C.Appl (keep@mk_rels (List.length abstract))
175 match ind_type_list with
178 to_lambdas (skip_prods paramsno ty)
179 (C.Lambda (C.Name "x", abstracted_tty,
180 S.lift (2+paramsno) tty'))
183 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
185 ProofEngineTypes.apply_tactic
187 ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
190 ~start:(injection_tac ~term:(C.Rel 1))
191 ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *)
194 ~start:(ProofEngineTypes.mk_tactic
196 let (proof, goal) = status in
197 let _,metasenv,_,_ = proof in
198 let _,context,gty = CicUtil.lookup_meta goal metasenv in
201 (C.Appl (C.MutInd (_,_,_)::arglist)) ->
203 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: goal after cut is not correct"))
205 ProofEngineTypes.apply_tactic
206 (ReductionTactics.change_tac
207 ~pattern:(ProofEngineTypes.conclusion_pattern
216 (turi,typeno,outtype,C.Rel 1,patterns)) ;
219 (*prerr_endline ("XXX: " ^ CicPp.ppterm xxx);*)
227 (EqualityTactics.rewrite_simpl_tac
228 ~direction:`LeftToRight
229 ~pattern:(ProofEngineTypes.conclusion_pattern None)
231 ~continuation:EqualityTactics.reflexivity_tac
235 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: not a discriminable equality"))
237 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: not an equality"))
239 ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
242 exception TwoDifferentSubtermsFound of int
244 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
247 let discriminate'_tac ~term =
248 let module C = Cic in
249 let module U = UriManager in
250 let module P = PrimitiveTactics in
251 let module T = Tacticals in
253 match LibraryObjects.true_URI () with
255 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
257 match LibraryObjects.false_URI () with
259 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
260 let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
261 let find_discriminating_consno t1 t2 =
264 | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
265 | C.Appl ((C.MutConstruct _ as constr1) :: args1),
266 C.Appl ((C.MutConstruct _ as constr2) :: args2)
267 when constr1 = constr2 ->
268 let rec aux_list l1 l2 =
271 | hd1 :: tl1, hd2 :: tl2 ->
272 (match aux hd1 hd2 with
273 | None -> aux_list tl1 tl2
274 | Some _ as res -> res)
275 | _ -> (* same constructor applied to a different number of args *)
279 | ((C.MutConstruct (_,_,consno1,subst1)),
280 (C.MutConstruct (_,_,consno2,subst2)))
281 | ((C.MutConstruct (_,_,consno1,subst1)),
282 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
283 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
284 (C.MutConstruct (_,_,consno2,subst2)))
285 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
286 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
287 when (consno1 <> consno2) || (subst1 <> subst2) ->
289 | _ -> fail "not a discriminable equality"
293 let mk_pattern turi typeno consno context left_args =
294 (* a list of "True" except for the element in position consno which
296 match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
297 | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_) ->
298 let _,_,_,constructor_list = List.nth ind_type_list typeno in
299 let false_constr_id,_ = List.nth constructor_list (consno - 1) in
302 (* dubbio: e' corretto ridurre in questo context ??? *)
303 let red_ty = CicReduction.whd context cty in
306 | C.Prod (_,_,target) when (k <= nr_ind_params) ->
307 CicSubstitution.subst (List.nth left_args (k-1))
309 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
310 C.Lambda (binder, source, (aux target (k+1)))
312 if (id = false_constr_id)
313 then (C.MutInd(false_URI,0,[]))
314 else (C.MutInd(true_URI,0,[]))
316 (CicSubstitution.lift 1 (aux red_ty 1)))
318 | _ -> (* object is not an inductive definition *)
321 let discriminate'_tac ~term status =
322 let (proof, goal) = status in
323 let _,metasenv,_,_ = proof in
324 let _,context,_ = CicUtil.lookup_meta goal metasenv in
326 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
329 | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
330 when LibraryObjects.is_eq_URI equri ->
331 let turi,typeno,exp_named_subst,left_args =
333 | (C.MutInd (turi,typeno,exp_named_subst)) ->
334 turi,typeno,exp_named_subst,[]
335 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
336 turi,typeno,exp_named_subst,left_args
337 | _ -> fail "not a discriminable equality"
340 match find_discriminating_consno t1 t2 with
341 | Some consno -> consno
342 | None -> fail "discriminating terms are structurally equal"
344 let pattern = mk_pattern turi typeno consno context left_args in
345 let (proof',goals') =
346 ProofEngineTypes.apply_tactic
347 (EliminationTactics.elim_type_tac
348 (C.MutInd (false_URI, 0, [])))
353 let _,metasenv',_,_ = proof' in
354 let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
355 ProofEngineTypes.apply_tactic
358 (ReductionTactics.change_tac
359 ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
362 C.Lambda ( C.Name "x", tty,
363 C.MutCase (turi, typeno,
364 (C.Lambda ((C.Name "x"),
365 (CicSubstitution.lift 1 tty),
367 (C.Rel 1), pattern));
372 (EqualityTactics.rewrite_simpl_tac
373 ~direction:`RightToLeft
374 ~pattern:(ProofEngineTypes.conclusion_pattern None)
377 (IntroductionTactics.constructor_tac ~n:1)))
379 | [] -> fail "ElimType False left no goals"
380 | _ -> fail "ElimType False left more than one goal")
381 | _ -> fail "not an equality"
383 ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
385 let discriminate_tac ~term =
386 let discriminate_tac ~term status =
387 ProofEngineTypes.apply_tactic
389 ~start:(* (injection_tac ~term) *) Tacticals.id_tac
390 ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
393 ProofEngineTypes.mk_tactic (discriminate_tac ~term)
395 (* DISCRIMINTATE SENZA INJECTION
397 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int)
399 let discriminate_tac ~term status =
400 let module C = Cic in
401 let module U = UriManager in
402 let module P = PrimitiveTactics in
403 let module T = Tacticals in
404 let (proof, goal) = status in
405 let _,metasenv,_,_ = proof in
406 let _,context,_ = CicUtil.lookup_meta goal metasenv in
407 let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
409 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
410 when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind"))
411 or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
413 (C.MutInd (turi,typeno,exp_named_subst))
414 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
416 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
418 let rec traverse t1 t2 =
419 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
420 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
422 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
423 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
424 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
426 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
427 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
428 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
429 let rec traverse_list l1 l2 =
432 | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
433 | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
434 in traverse_list applist1 applist2
436 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
437 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
438 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
439 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
440 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
441 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
442 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
443 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
444 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
445 raise (TwoDifferentSubtermsFound (t1,t2,consno2))
446 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
448 with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
450 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
452 then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
456 (* a list of "True" except for the element in position consno2' which is "False" *)
457 match fst(CicEnvironment.get_obj turi
458 CicUniv.empty_ugraph) with
459 C.InductiveDefinition (ind_type_list,_,nr_ind_params) ->
460 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
461 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
462 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
463 let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
464 debug_print (lazy "XXXX nth funzionano ") ;
466 (function (id,cty) ->
467 let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
470 C.Prod (_,_,target) when (k <= nr_ind_params) ->
472 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
473 C.Lambda (binder,source,(aux target (k+1)))
475 if (id = false_constr_id)
476 then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
477 else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
481 | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
484 let (proof',goals') =
485 EliminationTactics.elim_type_tac
486 ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] )
491 let _,metasenv',_,_ = proof' in
492 let _,context',gty' =
493 CicUtil.lookup_meta goal' metasenv'
505 (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
514 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
515 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
516 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
517 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
518 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
519 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
520 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
521 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
522 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2')
523 then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
524 metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
526 let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
527 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
529 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
531 ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
532 ~continuation:(IntroductionTactics.constructor_tac ~n:1)
535 | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
537 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
539 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")