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
119 match fst(CicEnvironment.get_obj
120 CicUniv.empty_ugraph turi ) with
121 C.InductiveDefinition (ind_type_list,_,nr_ind_params_dx,_) ->
122 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
123 let i_constr_id,_ = List.nth constructor_list (consno - 1) in
125 (function (id,cty) ->
126 let reduced_cty = CicReduction.whd context cty in
129 C.Prod (_,_,target) when (k <= nr_ind_params_dx) ->
131 | C.Prod (binder,source,target) when (k > nr_ind_params_dx) ->
135 | C.Anonymous -> C.Name "y"
137 C.Lambda (binder',source,(aux target (k+1)))
139 let nr_param_constr = k - 1 - nr_ind_params_dx in
140 if (id = i_constr_id)
141 then C.Rel (nr_param_constr - i + 1)
142 else S.lift (nr_param_constr + 1) t1' (* + 1 per liftare anche il lambda agguinto esternamente al case *)
146 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: object is not an Inductive Definition: it's imposible"))
148 ProofEngineTypes.apply_tactic
150 ~start:(P.cut_tac (C.Appl [(C.MutInd (equri,0,[])) ; tty' ; t1' ; t2']))
153 ~start:(injection_tac ~term:(C.Rel 1))
154 ~continuation:T.id_tac (* !!! qui devo anche fare clear di term tranne al primo passaggio *)
157 ~start:(ProofEngineTypes.mk_tactic
159 let (proof, goal) = status in
160 let _,metasenv,_,_ = proof in
161 let _,context,gty = CicUtil.lookup_meta goal metasenv in
164 (C.Appl (C.MutInd (_,_,_)::arglist)) ->
166 | _ -> raise (ProofEngineTypes.Fail (lazy "Injection: goal after cut is not correct"))
168 ProofEngineTypes.apply_tactic
169 (ReductionTactics.change_tac
170 ~pattern:(ProofEngineTypes.conclusion_pattern
173 C.Appl [ C.Lambda (C.Name "x", tty,
174 C.MutCase (turi, typeno,
175 (C.Lambda ((C.Name "x"),
187 (EqualityTactics.rewrite_simpl_tac
188 ~direction:`LeftToRight
189 ~pattern:(ProofEngineTypes.conclusion_pattern None)
191 ~continuation:EqualityTactics.reflexivity_tac
195 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: not a discriminable equality"))
197 | _ -> raise (ProofEngineTypes.Fail (lazy "Discriminate: not an equality"))
199 ProofEngineTypes.mk_tactic (injection1_tac ~term ~i)
202 exception TwoDifferentSubtermsFound of int
204 (* term ha tipo t1=t2; funziona solo se t1 e t2 hanno in testa costruttori
207 let discriminate'_tac ~term =
208 let module C = Cic in
209 let module U = UriManager in
210 let module P = PrimitiveTactics in
211 let module T = Tacticals in
213 match LibraryObjects.true_URI () with
215 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"true\" definition first. Please use the \"default\" command")) in
217 match LibraryObjects.false_URI () with
219 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default \"false\" definition first. Please use the \"default\" command")) in
220 let fail msg = raise (ProofEngineTypes.Fail (lazy ("Discriminate: " ^ msg))) in
221 let find_discriminating_consno t1 t2 =
224 | C.MutConstruct _, C.MutConstruct _ when t1 = t2 -> None
225 | C.Appl ((C.MutConstruct _ as constr1) :: args1),
226 C.Appl ((C.MutConstruct _ as constr2) :: args2)
227 when constr1 = constr2 ->
228 let rec aux_list l1 l2 =
231 | hd1 :: tl1, hd2 :: tl2 ->
232 (match aux hd1 hd2 with
233 | None -> aux_list tl1 tl2
234 | Some _ as res -> res)
235 | _ -> (* same constructor applied to a different number of args *)
239 | ((C.MutConstruct (_,_,consno1,subst1)),
240 (C.MutConstruct (_,_,consno2,subst2)))
241 | ((C.MutConstruct (_,_,consno1,subst1)),
242 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
243 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
244 (C.MutConstruct (_,_,consno2,subst2)))
245 | ((C.Appl ((C.MutConstruct (_,_,consno1,subst1)) :: _)),
246 (C.Appl ((C.MutConstruct (_,_,consno2,subst2)) :: _)))
247 when (consno1 <> consno2) || (subst1 <> subst2) ->
249 | _ -> fail "not a discriminable equality"
253 let mk_pattern turi typeno consno context left_args =
254 (* a list of "True" except for the element in position consno which
256 match fst (CicEnvironment.get_obj CicUniv.empty_ugraph turi) with
257 | C.InductiveDefinition (ind_type_list,_,nr_ind_params,_) ->
258 let _,_,_,constructor_list = List.nth ind_type_list typeno in
259 let false_constr_id,_ = List.nth constructor_list (consno - 1) in
262 (* dubbio: e' corretto ridurre in questo context ??? *)
263 let red_ty = CicReduction.whd context cty in
266 | C.Prod (_,_,target) when (k <= nr_ind_params) ->
267 CicSubstitution.subst (List.nth left_args (k-1))
269 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
270 C.Lambda (binder, source, (aux target (k+1)))
272 if (id = false_constr_id)
273 then (C.MutInd(false_URI,0,[]))
274 else (C.MutInd(true_URI,0,[]))
276 (CicSubstitution.lift 1 (aux red_ty 1)))
278 | _ -> (* object is not an inductive definition *)
281 let discriminate'_tac ~term status =
282 let (proof, goal) = status in
283 let _,metasenv,_,_ = proof in
284 let _,context,_ = CicUtil.lookup_meta goal metasenv in
286 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph
289 | (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
290 when LibraryObjects.is_eq_URI equri ->
291 let turi,typeno,exp_named_subst,left_args =
293 | (C.MutInd (turi,typeno,exp_named_subst)) ->
294 turi,typeno,exp_named_subst,[]
295 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::left_args)) ->
296 turi,typeno,exp_named_subst,left_args
297 | _ -> fail "not a discriminable equality"
300 match find_discriminating_consno t1 t2 with
301 | Some consno -> consno
302 | None -> fail "discriminating terms are structurally equal"
304 let pattern = mk_pattern turi typeno consno context left_args in
305 let (proof',goals') =
306 ProofEngineTypes.apply_tactic
307 (EliminationTactics.elim_type_tac
308 (C.MutInd (false_URI, 0, [])))
313 let _,metasenv',_,_ = proof' in
314 let _,context',gty' = CicUtil.lookup_meta goal' metasenv' in
315 ProofEngineTypes.apply_tactic
318 (ReductionTactics.change_tac
319 ~pattern:(ProofEngineTypes.conclusion_pattern (Some gty'))
322 C.Lambda ( C.Name "x", tty,
323 C.MutCase (turi, typeno,
324 (C.Lambda ((C.Name "x"),
325 (CicSubstitution.lift 1 tty),
327 (C.Rel 1), pattern));
332 (EqualityTactics.rewrite_simpl_tac
333 ~direction:`RightToLeft
334 ~pattern:(ProofEngineTypes.conclusion_pattern None)
337 (IntroductionTactics.constructor_tac ~n:1)))
339 | [] -> fail "ElimType False left no goals"
340 | _ -> fail "ElimType False left more than one goal")
341 | _ -> fail "not an equality"
343 ProofEngineTypes.mk_tactic (discriminate'_tac ~term)
345 let discriminate_tac ~term =
346 let discriminate_tac ~term status =
347 ProofEngineTypes.apply_tactic
349 ~start:(* (injection_tac ~term) *) Tacticals.id_tac
350 ~continuation:(discriminate'_tac ~term)) (* NOOO!!! non term ma una (qualunque) delle nuove hyp introdotte da inject *)
353 ProofEngineTypes.mk_tactic (discriminate_tac ~term)
355 (* DISCRIMINTATE SENZA INJECTION
357 exception TwoDifferentSubtermsFound of (Cic.term * Cic.term * int)
359 let discriminate_tac ~term status =
360 let module C = Cic in
361 let module U = UriManager in
362 let module P = PrimitiveTactics in
363 let module T = Tacticals in
364 let (proof, goal) = status in
365 let _,metasenv,_,_ = proof in
366 let _,context,_ = CicUtil.lookup_meta goal metasenv in
367 let termty = (CicTypeChecker.type_of_aux' metasenv context term) in
369 (C.Appl [(C.MutInd (equri, 0, [])) ; tty ; t1 ; t2])
370 when (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind"))
371 or (U.eq equri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) -> (
373 (C.MutInd (turi,typeno,exp_named_subst))
374 | (C.Appl (C.MutInd (turi,typeno,exp_named_subst)::_)) ->
376 let (t1',t2',consno2') = (* bruuutto: uso un eccezione per terminare con successo! buuu!! :-/ *)
378 let rec traverse t1 t2 =
379 debug_print (lazy ("XXXX t1 " ^ CicPp.ppterm t1)) ;
380 debug_print (lazy ("XXXX t2 " ^ CicPp.ppterm t2)) ;
382 ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
383 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
384 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
386 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::applist1)),
387 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::applist2)))
388 when (uri1 = uri2) && (typeno1 = typeno2) && (consno1 = consno2) && (exp_named_subst1 = exp_named_subst2) ->
389 let rec traverse_list l1 l2 =
392 | hd1::tl1,hd2::tl2 -> traverse hd1 hd2; traverse_list tl1 tl2
393 | _ -> raise (ProofEngineTypes.Fail "Discriminate: i 2 termini hanno in testa lo stesso costruttore, ma applicato a un numero diverso di termini. possibile???")
394 in traverse_list applist1 applist2
396 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
397 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
398 | ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1)),
399 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
400 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
401 (C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2)))
402 | ((C.Appl ((C.MutConstruct (uri1,typeno1,consno1,exp_named_subst1))::_)),
403 (C.Appl ((C.MutConstruct (uri2,typeno2,consno2,exp_named_subst2))::_)))
404 when (consno1 <> consno2) || (exp_named_subst1 <> exp_named_subst2) ->
405 raise (TwoDifferentSubtermsFound (t1,t2,consno2))
406 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
408 with (TwoDifferentSubtermsFound (t1,t2,consno2)) -> (t1,t2,consno2)
410 debug_print (lazy ("XXXX consno2' " ^ (string_of_int consno2'))) ;
412 then raise (ProofEngineTypes.Fail "Discriminate: Discriminating terms are structurally equal")
416 (* a list of "True" except for the element in position consno2' which is "False" *)
417 match fst(CicEnvironment.get_obj turi
418 CicUniv.empty_ugraph) with
419 C.InductiveDefinition (ind_type_list,_,nr_ind_params) ->
420 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length ind_type_list)) ^ " " ^ (string_of_int typeno))) ;
421 let _,_,_,constructor_list = (List.nth ind_type_list typeno) in
422 debug_print (lazy ("XXXX nth " ^ (string_of_int (List.length constructor_list)) ^ " " ^ (string_of_int consno2'))) ;
423 let false_constr_id,_ = List.nth constructor_list (consno2' - 1) in
424 debug_print (lazy "XXXX nth funzionano ") ;
426 (function (id,cty) ->
427 let red_ty = CicReduction.whd context cty in (* dubbio: e' corretto ridurre in questo context ??? *)
430 C.Prod (_,_,target) when (k <= nr_ind_params) ->
432 | C.Prod (binder,source,target) when (k > nr_ind_params) ->
433 C.Lambda (binder,source,(aux target (k+1)))
435 if (id = false_constr_id)
436 then (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [])
437 else (C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/True.ind") 0 [])
441 | _ -> raise (ProofEngineTypes.Fail "Discriminate: object is not an Inductive Definition: it's imposible")
444 let (proof',goals') =
445 EliminationTactics.elim_type_tac
446 ~term:(C.MutInd (U.uri_of_string "cic:/Coq/Init/Logic/False.ind") 0 [] )
451 let _,metasenv',_,_ = proof' in
452 let _,context',gty' =
453 CicUtil.lookup_meta goal' metasenv'
465 (C.Lambda ((C.Name "x"),tty,(C.Sort C.Prop))),
474 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))));
475 debug_print (lazy ("XXXX rewrite<-: " ^ CicPp.ppterm (C.Appl [(C.MutInd (equri,0,[])) ; tty ; t1' ; t2']))) ;
476 debug_print (lazy ("XXXX equri: " ^ U.string_of_uri equri)) ;
477 debug_print (lazy ("XXXX tty : " ^ CicPp.ppterm tty)) ;
478 debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
479 debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
480 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> tty then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t1'))) ;
481 if (CicTypeChecker.type_of_aux' metasenv' context' t2') <> tty then debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
482 if (CicTypeChecker.type_of_aux' metasenv' context' t1') <> (CicTypeChecker.type_of_aux' metasenv' context' t2')
483 then debug_print (lazy ("XXXX tt1': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux'
484 metasenv' context' t1'))) ; debug_print (lazy ("XXXX tt2': " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' t2'))) ;
486 let termty' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t1 ~with_what:t1' ~where:termty in
487 let termty'' = ProofEngineReduction.replace_lifting ~equality:(==) ~what:t2 ~with_what:t2' ~where:termty' in
489 debug_print (lazy ("XXXX rewrite<- " ^ CicPp.ppterm term ^ " : " ^ CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv' context' term)));
491 ~start:(EqualityTactics.rewrite_back_simpl_tac ~term:term)
492 ~continuation:(IntroductionTactics.constructor_tac ~n:1)
495 | _ -> raise (ProofEngineTypes.Fail "Discriminate: ElimType False left more (or less) than one goal")
497 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not a discriminable equality")
499 | _ -> raise (ProofEngineTypes.Fail "Discriminate: not an equality")