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 open Continuationals.Stack
27 module Ast = NotationPt
31 type just = [ `Term of NTacStatus.tactic_term | `Auto of NnAuto.auto_params ]
33 let mk_just status goal =
35 `Auto (l,params) -> NnAuto.auto_lowtac ~params:(l,params) status goal
36 | `Term t -> apply_tac t
39 exception FirstTypeWrong
40 exception NotEquivalentTypes
42 let extract_first_goal_from_status status =
43 let s = status#stack in
45 | [] -> fail (lazy "There's nothing to prove")
46 | (g1, _, _k, _tag1, _) :: _tl ->
47 let goals = filter_open g1 in
49 [] -> fail (lazy "No goals under focus")
51 let goal = goal_of_loc (loc) in
54 let extract_conclusion_type status goal =
55 let gty = get_goalty status goal in
56 let ctx = ctx_of gty in
57 term_of_cic_term status gty ctx
60 let alpha_eq_tacterm_kerterm ty t status goal =
61 let gty = get_goalty status goal in
62 let ctx = ctx_of gty in
63 let status,cicterm = disambiguate status ctx ty `XTNone (*(`XTSome (mk_cic_term ctx t))*) in
64 let (_,_,metasenv,subst,_) = status#obj in
65 let status,ty = term_of_cic_term status cicterm ctx in
66 if NCicReduction.alpha_eq status metasenv subst ctx t ty then
72 let are_convertible ty1 ty2 status goal =
73 let gty = get_goalty status goal in
74 let ctx = ctx_of gty in
75 let status,cicterm1 = disambiguate status ctx ty1 `XTNone in
76 let status,cicterm2 = disambiguate status ctx ty2 `XTNone in
77 NTacStatus.are_convertible status ctx cicterm1 cicterm2
79 let clear_volatile_params_tac status =
80 match status#stack with
81 [] -> fail (lazy "Empty stack")
82 | (g,t,k,tag,p)::tl ->
83 let rec remove_volatile = function
85 | (k,_v as hd')::tl' ->
86 let re = Str.regexp "volatile_.*" in
87 if Str.string_match re k 0 then
90 hd'::(remove_volatile tl')
92 let newp = remove_volatile p in
93 status#set_stack ((g,t,k,tag,newp)::tl)
96 let add_parameter_tac key value status =
97 match status#stack with
99 | (g,t,k,tag,p) :: tl -> status#set_stack ((g,t,k,tag,(key,value)::p)::tl)
103 (* LCF-like tactic that checks whether the conclusion of the sequent of the given goal is a product, checks that
104 the type of the conclusion's bound variable is the same as t1 and then uses an exact_tac with
105 \lambda id: t1. ?. If a t2 is given it checks that t1 ~_{\beta} t2 and uses and exact_tac with \lambda id: t2. ?
107 let lambda_abstract_tac id t1 status goal =
108 match extract_conclusion_type status goal with
109 | status,NCic.Prod (_,t,_) ->
110 if alpha_eq_tacterm_kerterm t1 t status goal then
112 block_tac [exact_tac ("",0,(Ast.Binder (`Lambda,(Ast.Ident (id,None),Some t1),Ast.Implicit
113 `JustOne))); clear_volatile_params_tac;
114 add_parameter_tac "volatile_newhypo" id] status
117 | _ -> raise NotAProduct
119 let assume name ty status =
120 let goal = extract_first_goal_from_status status in
121 try lambda_abstract_tac name ty status goal
123 | NotAProduct -> fail (lazy "You can't assume without an universal quantification")
124 | FirstTypeWrong -> fail (lazy "The assumed type is wrong")
125 | NotEquivalentTypes -> fail (lazy "The two given types are not equivalent")
128 let suppose t1 id status =
129 let goal = extract_first_goal_from_status status in
130 try lambda_abstract_tac id t1 status goal
132 | NotAProduct -> fail (lazy "You can't suppose without a logical implication")
133 | FirstTypeWrong -> fail (lazy "The supposed proposition is different from the premise")
134 | NotEquivalentTypes -> fail (lazy "The two given propositions are not equivalent")
137 let assert_tac t1 t2 status goal continuation =
138 let status,t = extract_conclusion_type status goal in
139 if alpha_eq_tacterm_kerterm t1 t status goal then
141 | None -> continuation
143 let _status,res = are_convertible t1 t2 status goal in
144 if res then continuation
146 raise NotEquivalentTypes
150 let branch_dot_tac status =
151 match status#stack with
152 ([],t,k,tag,p) :: tl ->
153 if List.length t > 0 then
154 status#set_stack (([List.hd t],List.tl t,k,tag,p)::tl)
160 let status_parameter key status =
161 match status#stack with
163 | (_g,_t,_k,_tag,p)::_ -> try List.assoc key p with _ -> ""
166 let beta_rewriting_step t status =
167 let ctx = status_parameter "volatile_context" status in
168 if ctx <> "beta_rewrite" then
170 let newhypo = status_parameter "volatile_newhypo" status in
172 fail (lazy "Invalid use of 'that is equivalent to'")
174 change_tac ~where:("",0,(None,[newhypo,Ast.UserInput],None)) ~with_what:t status
177 change_tac ~where:("",0,(None,[],Some
178 Ast.UserInput)) ~with_what:t status
181 let done_continuation status =
182 let rec continuation l =
185 | (_,t,_,tag,p)::tl ->
186 if tag = `BranchTag then
187 if List.length t > 0 then
190 try List.assoc "context" p
193 ctx <> "induction" && ctx <> "cases"
195 if continue then [clear_volatile_params_tac;branch_dot_tac] else
196 [clear_volatile_params_tac]
198 [merge_tac] @ (continuation tl)
202 continuation status#stack
205 let bydone just status =
206 let goal = extract_first_goal_from_status status in
207 let continuation = done_continuation status in
208 let l = [mk_just status goal just] @ continuation in
212 let push_goals_tac status =
213 match status#stack with
214 [] -> fail (lazy "Error pushing goals")
215 | (g1,t1,k1,tag1,p1) :: (g2,t2,k2,tag2,p2) :: tl ->
216 if List.length g2 > 0 then
217 status#set_stack ((g1,t1 @+ g2,k1,tag1,p1) :: ([],t2,k2,tag2,p2) :: tl)
218 else status (* Nothing to push *)
221 let we_need_to_prove t id status =
222 let goal = extract_first_goal_from_status status in
226 try assert_tac t None status goal (add_parameter_tac "volatile_context" "beta_rewrite" status)
228 | FirstTypeWrong -> fail (lazy "The given proposition is not the same as the conclusion")
232 block_tac [clear_volatile_params_tac; cut_tac t; branch_tac; shift_tac; intro_tac id; merge_tac; branch_tac;
233 push_goals_tac; add_parameter_tac "volatile_context" "beta_rewrite"
238 let by_just_we_proved just ty id status =
239 let goal = extract_first_goal_from_status status in
240 let just = mk_just status goal just in
243 assert_tac ty None status goal (block_tac [clear_volatile_params_tac; add_parameter_tac
244 "volatile_context" "beta_rewrite"] status)
247 block_tac [cut_tac ty; branch_tac; just; shift_tac; intro_tac id; merge_tac;
248 clear_volatile_params_tac; add_parameter_tac "volatile_newhypo" id] status
252 let existselim just id1 t1 t2 id2 status =
253 let goal = extract_first_goal_from_status status in
256 let just = mk_just status goal just in
258 cut_tac ("",0,(Ast.Appl [Ast.Ident ("ex",None); t1; Ast.Binder (`Lambda,(Ast.Ident
259 (id1,None), Some t1),t2)]));
260 branch_tac ~force:false;
264 intros_tac ~names_ref:(ref []) [id1;id2];
266 clear_volatile_params_tac
270 let andelim just t1 id1 t2 id2 status =
271 let goal = extract_first_goal_from_status status in
274 let just = mk_just status goal just in
276 cut_tac ("",0,(Ast.Appl [Ast.Ident ("And",None); t1 ; t2]));
277 branch_tac ~force:false;
281 intros_tac ~names_ref:(ref []) [id1;id2];
283 clear_volatile_params_tac
287 let type_of_tactic_term status ctx t =
288 let status,cicterm = disambiguate status ctx t `XTNone in
289 let (_,cicty) = typeof status ctx cicterm in
292 let swap_first_two_goals_tac status =
294 match status#stack with
296 | (g,t,k,tag,p) :: s ->
298 | (loc1) :: (loc2) :: tl ->
299 ([loc2;loc1] @+ tl,t,k,tag,p) :: s
302 status#set_stack gstatus
304 let thesisbecomes t1 = we_need_to_prove t1 None
307 let obtain id t1 status =
308 let goal = extract_first_goal_from_status status in
309 let cicgty = get_goalty status goal in
310 let ctx = ctx_of cicgty in
311 let cicty = type_of_tactic_term status ctx t1 in
312 let _,ty = term_of_cic_term status cicty ctx in
314 block_tac [ cut_tac ("",0,(Ast.Appl [Ast.Ident ("eq",None); Ast.NCic ty; t1; Ast.Implicit
316 swap_first_two_goals_tac;
317 branch_tac; shift_tac; shift_tac; intro_tac id; merge_tac; branch_tac; push_goals_tac;
318 add_parameter_tac "volatile_context" "rewrite"
323 let conclude t1 status =
324 let goal = extract_first_goal_from_status status in
325 let cicgty = get_goalty status goal in
326 let ctx = ctx_of cicgty in
327 let _,gty = term_of_cic_term status cicgty ctx in
329 (* The first term of this Appl should probably be "eq" *)
330 NCic.Appl [_;_;plhs;_] ->
331 if alpha_eq_tacterm_kerterm t1 plhs status goal then
332 add_parameter_tac "volatile_context" "rewrite" status
334 fail (lazy "The given conclusion is different from the left-hand side of the current conclusion")
335 | _ -> fail (lazy "Your conclusion needs to be an equality")
338 let rewritingstep rhs just last_step status =
339 let ctx = status_parameter "volatile_context" status in
340 if ctx = "rewrite" then
342 let goal = extract_first_goal_from_status status in
343 let cicgty = get_goalty status goal in
344 let ctx = ctx_of cicgty in
345 let _,gty = term_of_cic_term status cicgty ctx in
346 let cicty = type_of_tactic_term status ctx rhs in
347 let _,ty = term_of_cic_term status cicty ctx in
348 let just' = (* Extraction of the ""justification"" from the ad hoc justification *)
350 `Auto (univ, params) ->
352 if not (List.mem_assoc "timeout" params) then
353 ("timeout","3")::params
357 if not (List.mem_assoc "paramodulation" params) then
358 ("paramodulation","1")::params
361 if params = params' then NnAuto.auto_lowtac ~params:(univ, params) status goal
363 first_tac [NnAuto.auto_lowtac ~params:(univ, params) status goal; NnAuto.auto_lowtac
364 ~params:(univ, params') status goal]
365 | `Term just -> apply_tac just
366 | `SolveWith term -> NnAuto.demod_tac ~params:(Some [term], ["all","1";"steps","1"; "use_ctx","false"])
369 let plhs,prhs,prepare =
370 match gty with (* Extracting the lhs and rhs of the previous equality *)
371 NCic.Appl [_;_;plhs;prhs] -> plhs,prhs,(fun continuation -> continuation status)
372 | _ -> fail (lazy "You are not building an equaility chain")
376 let todo = [just'] @ (done_continuation status) in
379 let (_,_,rhs) = rhs in
380 block_tac [apply_tac ("",0,Ast.Appl [Ast.Ident ("trans_eq",None); Ast.NCic ty; Ast.NCic plhs;
381 rhs; Ast.NCic prhs]); branch_tac; just'; merge_tac]
386 fail (lazy "You are not building an equality chain")
389 let rec pp_metasenv_names (metasenv:NCic.metasenv) =
394 let meta_attrs,_,_ = conj in
395 let rec find_name_aux meta_attrs = match meta_attrs with
397 | hd :: tl -> match hd with
399 | _ -> find_name_aux tl
401 let name = find_name_aux meta_attrs
403 "[Goal: " ^ (string_of_int n) ^ ", Name: " ^ name ^ "]; " ^ (pp_metasenv_names tl)
406 let print_goals_names_tac s (status:#NTacStatus.tac_status) =
407 let (_,_,metasenv,_,_) = status#obj in
408 prerr_endline (s ^" -> Metasenv: " ^ (pp_metasenv_names metasenv)); status
410 (* Useful as it does not change the order in the list *)
411 let rec list_change_assoc k v = function
413 | (k',_v' as hd) :: tl -> if k' = k then (k',v) :: tl else hd :: (list_change_assoc k v tl)
416 let add_names_to_goals_tac (cl:NCic.constructor list ref) (status:#NTacStatus.tac_status) =
417 let add_name_to_goal name goal metasenv =
418 let (mattrs,ctx,t) = try List.assoc goal metasenv with _ -> assert false in
419 let mattrs = (`Name name) :: (List.filter (function `Name _ -> false | _ -> true) mattrs) in
420 let newconj = (mattrs,ctx,t) in
421 list_change_assoc goal newconj metasenv
424 (* It's important that this tactic is called before branching and right after the creation of
425 * the new goals, when they are still under focus *)
426 match status#stack with
427 [] -> fail (lazy "Can not add names to an empty stack")
428 | (g,_,_,_,_) :: _tl ->
429 let rec sublist n = function
431 | hd :: tl -> if n = 0 then [] else hd :: (sublist (n-1) tl)
433 List.map (fun _,sw -> goal_of_switch sw) (sublist (List.length !cl) g)
435 let rec add_names_to_goals g cl metasenv =
438 | hd::tl, (_,consname,_)::tl' ->
439 add_names_to_goals tl tl' (add_name_to_goal consname hd metasenv)
440 | _,_ -> fail (lazy "There are less goals than constructors")
442 let (olduri,oldint,metasenv,oldsubst,oldkind) = status#obj in
443 let newmetasenv = add_names_to_goals new_goals !cl metasenv
444 in status#set_obj(olduri,oldint,newmetasenv,oldsubst,oldkind)
447 let (olduri,oldint,metasenv,oldsubst,oldkind) = status#obj in
448 let remove_name_from_metaattrs =
449 List.filter (function `Name _ -> false | _ -> true) in
450 let rec add_names_to_metasenv cl metasenv =
451 match cl,metasenv with
453 | hd :: tl, mhd :: mtl ->
454 let _,consname,_ = hd in
455 let gnum,conj = mhd in
456 let mattrs,ctx,t = conj in
457 let mattrs = [`Name consname] @ (remove_name_from_metaattrs mattrs)
459 let newconj = mattrs,ctx,t in
460 let newmeta = gnum,newconj in
461 newmeta :: (add_names_to_metasenv tl mtl)
462 | _,[] -> assert false
464 let newmetasenv = add_names_to_metasenv !cl metasenv in
465 status#set_obj (olduri,oldint,newmetasenv,oldsubst,oldkind)
468 let unfocus_branch_tac status =
469 match status#stack with
471 | (g,t,k,tag,p) :: tl -> status#set_stack (([],g @+ t,k,tag,p)::tl)
474 let we_proceed_by_induction_on t1 t2 status =
475 let goal = extract_first_goal_from_status status in
476 let txt,len,t1 = t1 in
477 let t1 = txt, len, Ast.Appl [t1; Ast.Implicit `Vector] in
478 let indtyinfo = ref None in
479 let sort = ref (NCic.Rel 1) in
480 let cl = ref [] in (* this is a ref on purpose, as the block of code after sort_of_goal_tac in
481 block_tac acts as a block of asynchronous code, in which cl gets modified with the info retrieved
482 with analize_indty_tac, and later used to label each new goal with a costructor name. Using a
483 plain list this doesn't seem to work, as add_names_to_goals_tac would immediately act on an empty
484 list, instead of acting on the list of constructors *)
486 assert_tac t2 None status goal (block_tac [
487 analyze_indty_tac ~what:t1 indtyinfo;
488 sort_of_goal_tac sort;
490 let ity = HExtlib.unopt !indtyinfo in
491 let NReference.Ref (uri, _) = ref_of_indtyinfo ity in
493 NUri.name_of_uri uri ^ "_" ^
494 snd (NCicElim.ast_of_sort
495 (match !sort with NCic.Sort x -> x | _ -> assert false))
498 let l = [Ast.Ident (name,None)] in
499 (* Generating an implicit for each argument of the inductive type, plus one the
500 * predicate, plus an implicit for each constructor of the inductive type *)
501 let l = l @ HExtlib.mk_list (Ast.Implicit `JustOne) (ity.leftno+1+ity.consno) in
507 exact_tac ("",0,eliminator) status);
508 add_names_to_goals_tac cl;
512 add_parameter_tac "context" "induction"
515 | FirstTypeWrong -> fail (lazy "What you want to prove is different from the conclusion")
518 let we_proceed_by_cases_on ((txt,len,ast1) as t1) t2 status =
519 let goal = extract_first_goal_from_status status in
520 let npt1 = txt, len, Ast.Appl [ast1; Ast.Implicit `Vector] in
521 let indtyinfo = ref None in
524 assert_tac t2 None status goal (block_tac [
525 analyze_indty_tac ~what:npt1 indtyinfo;
526 cases_tac ~what:t1 ~where:("",0,(None,[],Some
530 let ity = HExtlib.unopt !indtyinfo in
531 cl := ity.cl; add_names_to_goals_tac cl status
533 branch_tac; push_goals_tac;
535 add_parameter_tac "context" "cases"
538 | FirstTypeWrong -> fail (lazy "What you want to prove is different from the conclusion")
541 let byinduction t1 id status =
542 let ctx = status_parameter "context" status in
543 if ctx <> "induction" then fail (lazy "You can't use this tactic outside of an induction context")
544 else suppose t1 id status
547 let name_of_conj conj =
548 let mattrs,_,_ = conj in
549 let rec search_name mattrs =
555 | _ -> search_name tl
559 let rec loc_of_goal goal l =
561 [] -> fail (lazy "Reached the end")
564 let g = goal_of_switch sw in
566 else loc_of_goal goal tl
569 let has_focused_goal status =
570 match status#stack with
572 | ([],_,_,_,_) :: _tl -> false
576 let focus_on_case_tac case status =
577 let (_,_,metasenv,_,_) = status#obj in
578 let rec goal_of_case case metasenv =
580 [] -> fail (lazy "The given case does not exist")
581 | (goal,conj) :: tl ->
582 if name_of_conj conj = case then goal
583 else goal_of_case case tl
585 let goal_to_focus = goal_of_case case metasenv in
587 match status#stack with
588 [] -> fail (lazy "There is nothing to prove")
589 | (g,t,k,tag,p) :: s ->
592 loc_of_goal goal_to_focus t
593 with _ -> fail (lazy "The given case is not part of the current induction/cases analysis
596 let curloc = if has_focused_goal status then
597 let goal = extract_first_goal_from_status status in
601 (((g @- curloc) @+ [loc]),(curloc @+ (t @- [loc])),k,tag,p) :: s
603 status#set_stack gstatus
606 let case id l status =
607 let ctx = status_parameter "context" status in
608 if ctx <> "induction" && ctx <> "cases" then fail (lazy "You can't use case outside of an
609 induction/cases analysis context")
612 if has_focused_goal status then fail (lazy "Finish the current case before switching")
616 let goal = extract_first_goal_from_status status in
617 let (_,_,metasenv,_,_) = status#obj in
618 let conj = NCicUtils.lookup_meta goal metasenv in
619 let name = name_of_conj conj in
626 (try_tac (assume id ("",0,ty))) :: (aux tl)
630 (* if name = id then block_tac continuation status *)
632 block_tac ([focus_on_case_tac id] @ continuation) status
637 let print_stack status = prerr_endline ("PRINT STACK: " ^ (pp status#stack)); id_tac status ;;
639 (* vim: ts=2: sw=0: et: