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 in
41 ProofEngineTypes.apply_tactic
43 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
44 when LibraryObjects.is_eq_URI equri -> (
46 (C.MutInd (turi,typeno,exp_named_subst))
47 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) -> (
49 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
50 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
51 when (uri1 = uri2) && (typeno1 = typeno2) &&
52 (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
53 (* raise (ProofEngineTypes.Fail "Injection: nothing to do") ; *) T.id_tac
54 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
55 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
56 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
57 let rec traverse_list i l1 l2 =
60 | hd1::tl1,hd2::tl2 ->
62 ~start:(injection1_tac ~i ~term)
63 ~continuation:(traverse_list (i+1) tl1 tl2)
64 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???"))
65 in traverse_list 1 applist1 applist2
66 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
67 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
68 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
69 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
70 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
71 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
72 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
73 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
74 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
75 (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality but a discriminable one") ; *) T.id_tac
76 | _ -> (* raise (ProofEngineTypes.Fail "Injection: not a projectable equality") ; *) T.id_tac
78 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not a projectable equality"))
80 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: not an equation"))
83 ProofEngineTypes.mk_tactic (injection_tac ~term)
85 and injection1_tac ~term ~i =
86 let injection1_tac ~term ~i status =
87 let (proof, goal) = status in
88 (* precondizione: t1 e t2 hanno in testa lo stesso costruttore ma differiscono (o potrebbero differire?) nell'i-esimo parametro del costruttore *)
90 let module S = CicSubstitution in
91 let module U = UriManager in
92 let module P = PrimitiveTactics in
93 let module T = Tacticals in
94 let _,metasenv,_,_ = proof in
95 let _,context,_ = CicUtil.lookup_meta goal metasenv in
96 let termty,_ = (* TASSI: FIXME *)
97 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
98 match termty with (* an equality *)
99 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
100 when LibraryObjects.is_eq_URI equri -> (
101 match tty with (* some inductive type *)
102 (C.MutInd (turi,typeno,exp_named_subst))
103 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
104 let t1',t2',consno = (* sono i due sottotermini che differiscono *)
106 ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
107 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
108 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) -> (* controllo ridondante *)
109 (List.nth applist1 (i-1)),(List.nth applist2 (i-1)),consno2
113 CicTypeChecker.type_of_aux' metasenv context t1'
114 CicUniv.empty_ugraph in
116 match fst(CicEnvironment.get_obj
117 CicUniv.empty_ugraph turi ) with
118 C.InductiveDefinition (ind_type_list,_,nr_ind_params_dx,_) ->
119 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
120 let i_constr_id,_ = List.nth constructor_list (consno - 1) in
122 (function (id,cty) ->
123 let reduced_cty = CicReduction.whd context cty in
126 C.Prod (_,_,target) when (k <= nr_ind_params_dx) ->
128 | C.Prod (binder,source,target) when (k > nr_ind_params_dx) ->
132 | C.Anonymous -> C.Name "y"
134 C.Lambda (binder',source,(aux target (k+1)))
136 let nr_param_constr = k - 1 - nr_ind_params_dx in
137 if (id = i_constr_id)
138 then C.Rel (nr_param_constr - i + 1)
139 else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda agguinto esternamente al case *)
143 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
145 ProofEngineTypes.apply_tactic
147 ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
150 ~start:(injection_tac ~term:(C.Rel 1))
151 ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *)
154 ~start:(ProofEngineTypes.mk_tactic
156 let (proof, goal) = status in
157 let _,metasenv,_,_ = proof in
158 let _,context,gty = CicUtil.lookup_meta goal metasenv in
161 (C.Appl (C.MutInd (_,_,_)::arglist)) ->
163 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: goal after cut is not correct"))
165 ProofEngineTypes.apply_tactic
166 (ReductionTactics.change_tac
167 ~pattern:(ProofEngineTypes.conclusion_pattern
170 C.Appl [ C.Lambda (C.Name "x", tty,
171 C.MutCase (turi, typeno,
172 (C.Lambda ((C.Name "x"),
184 (EqualityTactics.rewrite_simpl_tac
185 ~direction:`LeftToRight
186 ~pattern:(ProofEngineTypes.conclusion_pattern None)
188 ~continuation:EqualityTactics.reflexivity_tac
192 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: not a discriminable equality"))
194 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: not an equality"))
196 ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
199 exception TwoDifferentSubtermsFound of int
201 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
204 let discriminate'_tac ~term =
205 let module C = Cic in
206 let module U = UriManager in
207 let module P = PrimitiveTactics in
208 let module T = Tacticals in
209 let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
210 let find_discriminating_consno t1 t2 =
213 | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
214 | C.Appl ((C.MutConstruct _ as constr1) :: args1),
215 C.Appl ((C.MutConstruct _ as constr2) :: args2)
216 when constr1 = constr2 ->
217 let rec aux_list l1 l2 =
220 | hd1 :: tl1, hd2 :: tl2 ->
221 (match aux hd1 hd2 with
222 | None -> aux_list tl1 tl2
223 | Some _ as res -> res)
224 | _ -> (* same constructor applied to a different number of args *)
228 | ((C.MutConstruct (_,_,consno1,subst1)),
229 (C.MutConstruct (_,_,consno2,subst2)))
230 | ((C.MutConstruct (_,_,consno1,subst1)),
231 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
232 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
233 (C.MutConstruct (_,_,consno2,subst2)))
234 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
235 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
236 when (consno1 <> consno2) || (subst1 <> subst2) ->
238 | _ -> fail "not a discriminable equality"
242 let mk_pattern turi typeno consno context left_args =
243 (* a list of "True" except for the element in position consno which
245 match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
246 | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_) ->
247 let _,_,_,constructor_list = List.nth ind_type_list typeno in
248 let false_constr_id,_ = List.nth constructor_list (consno - 1) in
251 (* dubbio: e' corretto ridurre in questo context ??? *)
252 let red_ty = CicReduction.whd context cty in
255 | C.Prod (_,_,target) when (k <= nr_ind_params) ->
256 CicSubstitution.subst (List.nth left_args (k-1))
258 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
259 C.Lambda (binder, source, (aux target (k+1)))
261 if (id = false_constr_id)
262 then (C.MutInd(LibraryObjects.false_URI (),0,[]))
263 else (C.MutInd(LibraryObjects.true_URI (),0,[]))
265 (CicSubstitution.lift 1 (aux red_ty 1)))
267 | _ -> (* object is not an inductive definition *)
270 let discriminate'_tac ~term status =
271 let (proof, goal) = status in
272 let _,metasenv,_,_ = proof in
273 let _,context,_ = CicUtil.lookup_meta goal metasenv in
275 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
278 | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
279 when LibraryObjects.is_eq_URI equri ->
280 let turi,typeno,exp_named_subst,left_args =
282 | (C.MutInd (turi,typeno,exp_named_subst)) ->
283 turi,typeno,exp_named_subst,[]
284 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
285 turi,typeno,exp_named_subst,left_args
286 | _ -> fail "not a discriminable equality"
289 match find_discriminating_consno t1 t2 with
290 | Some consno -> consno
291 | None -> fail "discriminating terms are structurally equal"
293 let pattern = mk_pattern turi typeno consno context left_args in
294 let (proof',goals') =
295 ProofEngineTypes.apply_tactic
296 (EliminationTactics.elim_type_tac
297 (C.MutInd (LibraryObjects.false_URI (), 0, [])))
302 let _,metasenv',_,_ = proof' in
303 let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
304 ProofEngineTypes.apply_tactic
307 (ReductionTactics.change_tac
308 ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
311 C.Lambda ( C.Name "x", tty,
312 C.MutCase (turi, typeno,
313 (C.Lambda ((C.Name "x"),
314 (CicSubstitution.lift 1 tty),
316 (C.Rel 1), pattern));
321 (EqualityTactics.rewrite_simpl_tac
322 ~direction:`RightToLeft
323 ~pattern:(ProofEngineTypes.conclusion_pattern None)
326 (IntroductionTactics.constructor_tac ~n:1)))
328 | [] -> fail "ElimType False left no goals"
329 | _ -> fail "ElimType False left more than one goal")
330 | _ -> fail "not an equality"
332 ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
334 let discriminate_tac ~term =
335 let discriminate_tac ~term status =
336 ProofEngineTypes.apply_tactic
338 ~start:(* (injection_tac ~term) *) Tacticals.id_tac
339 ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
342 ProofEngineTypes.mk_tactic (discriminate_tac ~term)
344 (* DISCRIMINTATE SENZA INJECTION
346 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int)
348 let discriminate_tac ~term status =
349 let module C = Cic in
350 let module U = UriManager in
351 let module P = PrimitiveTactics in
352 let module T = Tacticals in
353 let (proof, goal) = status in
354 let _,metasenv,_,_ = proof in
355 let _,context,_ = CicUtil.lookup_meta goal metasenv in
356 let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
358 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
359 when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind"))
360 or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
362 (C.MutInd (turi,typeno,exp_named_subst))
363 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
365 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
367 let rec traverse t1 t2 =
368 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
369 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
371 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
372 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
373 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
375 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
376 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
377 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
378 let rec traverse_list l1 l2 =
381 | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
382 | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
383 in traverse_list applist1 applist2
385 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
386 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
387 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
388 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
389 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
390 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
391 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
392 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
393 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
394 raise (TwoDifferentSubtermsFound (t1,t2,consno2))
395 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
397 with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
399 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
401 then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
405 (* a list of "True" except for the element in position consno2' which is "False" *)
406 match fst(CicEnvironment.get_obj turi
407 CicUniv.empty_ugraph) with
408 C.InductiveDefinition (ind_type_list,_,nr_ind_params) ->
409 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
410 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
411 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
412 let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
413 debug_print (lazy "XXXX nth funzionano ") ;
415 (function (id,cty) ->
416 let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
419 C.Prod (_,_,target) when (k <= nr_ind_params) ->
421 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
422 C.Lambda (binder,source,(aux target (k+1)))
424 if (id = false_constr_id)
425 then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
426 else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
430 | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
433 let (proof',goals') =
434 EliminationTactics.elim_type_tac
435 ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] )
440 let _,metasenv',_,_ = proof' in
441 let _,context',gty' =
442 CicUtil.lookup_meta goal' metasenv'
454 (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
463 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
464 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
465 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
466 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
467 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
468 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
469 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
470 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
471 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2')
472 then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
473 metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
475 let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
476 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
478 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
480 ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
481 ~continuation:(IntroductionTactics.constructor_tac ~n:1)
484 | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
486 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
488 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")