1 (* Copyright (C) 2019, 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/.
26 module Ast = NotationPt
30 type just = [ `Term of NTacStatus.tactic_term | `Auto of NnAuto.auto_params ]
34 `Auto (l,params) -> distribute_tac (fun status goal -> NnAuto.auto_lowtac
35 ~params:(l,params) status goal)
36 | `Term t -> apply_tac t
39 exception FirstTypeWrong
40 exception NotEquivalentTypes
42 let extract_conclusion_type status goal =
43 let gty = get_goalty status goal in
44 let ctx = ctx_of gty in
45 let status,gty = term_of_cic_term status gty ctx in
49 let same_type_as_conclusion ty t status goal =
50 let gty = get_goalty status goal in
51 let ctx = ctx_of gty in
52 let status,cicterm = disambiguate status ctx ty `XTNone (*(`XTSome (mk_cic_term ctx t))*) in
53 let (_,_,metasenv,subst,_) = status#obj in
54 let status,ty = term_of_cic_term status cicterm ctx in
55 if NCicReduction.alpha_eq status metasenv subst ctx t ty then
61 let are_convertible ty1 ty2 status goal =
62 let gty = get_goalty status goal in
63 let ctx = ctx_of gty in
64 let status,cicterm1 = disambiguate status ctx ty1 `XTNone (*(`XTSome (mk_cic_term ctx t))*) in
65 let status,cicterm2 = disambiguate status ctx ty2 `XTNone (*(`XTSome (mk_cic_term ctx t))*) in
66 NTacStatus.are_convertible status ctx cicterm1 cicterm2
68 (* LCF-like tactic that checks whether the conclusion of the sequent of the given goal is a product, checks that
69 the type of the conclusion's bound variable is the same as t1 and then uses an exact_tac with
70 \lambda id: t1. ?. If a t2 is given it checks that t1 ~_{\beta} t2 and uses and exact_tac with \lambda id: t2. ?
72 let lambda_abstract_tac id t1 t2 status goal =
73 match extract_conclusion_type status goal with
74 | NCic.Prod (_,t,_) ->
75 if same_type_as_conclusion t1 t status goal then
79 exec (exact_tac ("",0,(Ast.Binder (`Lambda,(Ast.Ident (id,None),Some t1),Ast.Implicit
80 `JustOne)))) status goal
83 let status,res = are_convertible t1 t2 status goal in
86 exec (exact_tac ("",0,(Ast.Binder (`Lambda,(Ast.Ident (id,None),Some t2),Ast.Implicit
87 `JustOne)))) status goal
89 raise NotEquivalentTypes
92 | _ -> raise NotAProduct
94 let assume name ty eqty =
95 distribute_tac (fun status goal ->
96 try lambda_abstract_tac name ty eqty status goal
98 | NotAProduct -> fail (lazy "You can't assume without an universal quantification")
99 | FirstTypeWrong -> fail (lazy "The assumed type is wrong")
100 | NotEquivalentTypes -> fail (lazy "The two given types are not equivalent")
104 let suppose t1 id t2 =
105 distribute_tac (fun status goal ->
106 try lambda_abstract_tac id t1 t2 status goal
108 | NotAProduct -> fail (lazy "You can't suppose without a logical implication")
109 | FirstTypeWrong -> fail (lazy "The supposed proposition is different from the premise")
110 | NotEquivalentTypes -> fail (lazy "The two given propositions are not equivalent")
114 let assert_tac t1 t2 status goal continuation =
115 let t = extract_conclusion_type status goal in
116 if same_type_as_conclusion t1 t status goal then
118 | None -> exec continuation status goal
120 let status,res = are_convertible t1 t2 status goal in
122 exec continuation status goal
124 raise NotEquivalentTypes
128 let we_need_to_prove t id t1 =
129 distribute_tac (fun status goal ->
134 | None -> (* We need to prove t *)
137 assert_tac t None status goal id_tac
139 | FirstTypeWrong -> fail (lazy "The given proposition is not the same as the conclusion")
141 | Some t1 -> (* We need to prove t or equivalently t1 *)
144 assert_tac t (Some t1) status goal (change_tac ~where:("",0,(None,[],Some Ast.UserInput)) ~with_what:t1)
146 | FirstTypeWrong -> fail (lazy "The given proposition is not the same as the conclusion")
147 | NotEquivalentTypes -> fail (lazy "The given propositions are not equivalent")
153 | None -> (* We need to prove t (id) *)
154 exec (block_tac [cut_tac t; branch_tac ~force:false; shift_tac; intro_tac id;
155 (*merge_tac*)]) status goal
156 | Some t1 -> (* We need to prove t (id) or equivalently t1 *)
157 exec (block_tac [cut_tac t; branch_tac ~force:false; change_tac ~where:("",0,(None,[],Some Ast.UserInput))
158 ~with_what:t1; shift_tac; intro_tac id; merge_tac]) status goal
163 let by_just_we_proved just ty id ty' =
164 distribute_tac (fun status goal ->
165 let just = mk_just just in
169 | None -> (* just we proved P done *)
172 assert_tac ty None status goal just
174 | FirstTypeWrong -> fail (lazy "The given proposition is not the same as the conclusion")
175 | NotEquivalentTypes -> fail (lazy "The given propositions are not equivalent")
177 | Some ty' -> (* just we proved P that is equivalent to P' done *)
180 assert_tac ty' (Some ty) status goal (block_tac [change_tac
181 ~where:("",0,(None,[],Some Ast.UserInput)) ~with_what:ty; just])
183 | FirstTypeWrong -> fail (lazy "The second proposition is not the same as the conclusion")
184 | NotEquivalentTypes -> fail (lazy "The given propositions are not equivalent")
190 | None -> exec (block_tac [cut_tac ty; branch_tac; just; shift_tac; intro_tac
191 id; merge_tac ]) status goal
192 | Some ty' -> exec (block_tac [cut_tac ty; branch_tac; just; shift_tac; intro_tac
193 id; change_tac ~where:("",0,(None,[id,Ast.UserInput],None))
194 ~with_what:ty'; merge_tac]) status goal
199 let thesisbecomes t1 t2 = we_need_to_prove t1 None t2 ;;
205 let existselim just id1 t1 t2 id2 =
208 let just = mk_just just in
210 cut_tac ("",0,(Ast.Appl [Ast.Ident ("ex",None); t1; Ast.Binder (`Lambda,(Ast.Ident
211 (id1,None), Some t1),t2)]));
212 branch_tac ~force:false;
216 intros_tac ~names_ref:(ref []) [id1;id2];
220 let andelim just t1 id1 t2 id2 =
223 let just = mk_just just in
225 cut_tac ("",0,(Ast.Appl [Ast.Ident ("And",None); t1 ; t2]));
226 branch_tac ~force:false;
230 intros_tac ~names_ref:(ref []) [id1;id2];
237 let rewritingstep lhs rhs just last_step = fail (lazy "Not implemented");
239 let aux ((proof,goal) as status) =
240 let (curi,metasenv,_subst,proofbo,proofty, attrs) = proof in
241 let _,context,gty = CicUtil.lookup_meta goal metasenv in
243 match LibraryObjects.eq_URI () with
244 None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default equality first. Please use the \"default\" command"))
246 Cic.MutInd (uri,0,[]), Cic.Const (LibraryObjects.trans_eq_URI ~eq:uri,[])
249 CicTypeChecker.type_of_aux' metasenv context rhs CicUniv.oblivion_ugraph in
252 `Auto (univ, params) ->
254 if not (List.exists (fun (k,_) -> k = "timeout") params) then
255 ("timeout","3")::params
259 if not (List.exists (fun (k,_) -> k = "paramodulation") params) then
260 ("paramodulation","1")::params
263 if params = params' then
264 Tactics.auto ~dbd ~params:(univ, params) ~automation_cache
267 [Tactics.auto ~dbd ~params:(univ, params) ~automation_cache ;
268 Tactics.auto ~dbd ~params:(univ, params') ~automation_cache]
269 | `Term just -> Tactics.apply just
271 Tactics.demodulate ~automation_cache ~dbd
272 ~params:(Some [term],
273 ["all","1";"steps","1"; "use_context","false"])
277 let plhs,prhs,prepare =
282 Cic.Appl [_;_;plhs;prhs] -> plhs,prhs
287 ProofEngineTypes.apply_tactic continuation status)
291 Cic.Appl [_;_;plhs;prhs] -> plhs,prhs
294 (*CSC: manca check plhs convertibile con lhs *)
297 ProofEngineTypes.apply_tactic continuation status)
298 | Some (Some name,lhs) ->
299 let newmeta = CicMkImplicit.new_meta metasenv [] in
301 CicMkImplicit.identity_relocation_list_for_metavariable context in
303 let prhs = Cic.Meta(newmeta,irl) in
306 let metasenv = (newmeta, context, ty)::metasenv in
307 let mk_fresh_name_callback =
308 fun metasenv context _ ~typ ->
309 FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context
312 let proof = curi,metasenv,_subst,proofbo,proofty, attrs in
314 ProofEngineTypes.apply_tactic
316 ~start:(Tactics.cut ~mk_fresh_name_callback
317 (Cic.Appl [eq ; ty ; lhs ; prhs]))
318 ~continuations:[Tacticals.id_tac ; continuation]) (proof,goal)
321 match just,goals with
322 `Proof, [g1;g2;g3] -> [g2;g3;newmeta;g1]
323 | _, [g1;g2] -> [g2;newmeta;g1]
325 prerr_endline (String.concat "," (List.map string_of_int l));
326 prerr_endline (CicMetaSubst.ppmetasenv [] metasenv);
333 (*CSC:manca controllo sul fatto che rhs sia convertibile con prhs*)
337 ~start:(Tactics.apply ~term:(Cic.Appl [trans;ty;plhs;rhs;prhs]))
338 ~continuations:[just' ; Tacticals.id_tac]
342 ProofEngineTypes.mk_tactic aux