Debian Codegraph @

plugins/ssr/ssrtacticals.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* This file is (C) Copyright 2006-2015 Microsoft Corporation and Inria. *)
12
13 open Names
14 open Constr
15 open Context
16 open Termops
17
18 open Proofview.Notations
19
20 open Ssrast
21 open Ssrcommon
22
23 module RelDecl = Context.Rel.Declaration
24 module NamedDecl = Context.Named.Declaration
25
26 (** Tacticals (+, -, *, done, by, do, =>, first, and last). *)
27
28 let get_index = function Locus.ArgArg i -> i | _ ->
29 anomaly "Uninterpreted index"
30 (* Toplevel constr must be globalized twice ! *)
31
32 (** The "first" and "last" tacticals. *)
33
34 let tclPERM perm tac =
35 Proofview.Goal.enter begin fun gls ->
36 tac <*> (Proofview.Unsafe.tclGETGOALS >>= fun gls -> Proofview.Unsafe.tclSETGOALS (perm gls))
37 end
38
39 let rot_hyps dir i hyps =
40 let n = List.length hyps in
41 if i = 0 then List.rev hyps else
42 if i > n then CErrors.user_err (Pp.str "Not enough goals") else
43 let rec rot i l_hyps = function
44 | hyp :: hyps' when i > 0 -> rot (i - 1) (hyp :: l_hyps) hyps'
45 | hyps' -> hyps' @ (List.rev l_hyps) in
46 rot (match dir with L2R -> i | R2L -> n - i) [] hyps
47
48 let tclSEQAT ist atac1 dir (ivar, ((_, atacs2), atac3)) =
49 let i = get_index ivar in
50 let evtac t = ssrevaltac ist t in
51 let tac1 = evtac atac1 in
52 if atacs2 = [] && atac3 <> None then tclPERM (rot_hyps dir i) tac1 else
53 let evotac = function Some atac -> evtac atac | _ -> Tacticals.tclIDTAC in
54 let tac3 = evotac atac3 in
55 let rec mk_pad n = if n > 0 then tac3 :: mk_pad (n - 1) else [] in
56 match dir, mk_pad (i - 1), List.map evotac atacs2 with
57 | L2R, [], [tac2] when atac3 = None -> Tacticals.tclTHENFIRST tac1 tac2
58 | L2R, [], [tac2] when atac3 = None -> Tacticals.tclTHENLAST tac1 tac2
59 | L2R, pad, tacs2 -> Tacticals.tclTHENSFIRSTn tac1 (Array.of_list (pad @ tacs2)) tac3
60 | R2L, pad, tacs2 -> Tacticals.tclTHENSLASTn tac1 tac3 (Array.of_list (tacs2 @ pad))
61
62 (** The "in" pseudo-tactical *)(* {{{ **********************************************)
63
64 let hidden_goal_tag = "the_hidden_goal"
65
66 let check_wgen_uniq gens =
67 let clears = List.flatten (List.map fst gens) in
68 check_hyps_uniq [] clears;
69 let ids = CList.map_filter
70 (function (_,Some ((id,_),_)) -> Some (hoi_id id) | _ -> None) gens in
71 let rec check ids = function
72 | id :: _ when List.mem id ids ->
73 errorstrm Pp.(str"Duplicate generalization " ++ Id.print id)
74 | id :: hyps -> check (id :: ids) hyps
75 | [] -> () in
76 check [] ids
77
78 let pf_clauseids gens clseq =
79 let keep_clears = List.map (fun (x, _) -> x, None) in
80 if gens <> [] then (check_wgen_uniq gens; gens) else
81 if clseq <> InAll && clseq <> InAllHyps then keep_clears gens else
82 CErrors.user_err (Pp.str "assumptions should be named explicitly")
83
84 let hidden_clseq = function InHyps | InHypsSeq | InAllHyps -> true | _ -> false
85
86 let posetac id cl = Tactics.pose_tac (Name id) cl
87
88 let hidetacs clseq idhide cl0 =
89 if not (hidden_clseq clseq) then [] else
90 [posetac idhide cl0;
91 convert_concl_no_check (EConstr.mkVar idhide)]
92
93 let endclausestac id_map clseq gl_id cl0 =
94 Proofview.Goal.enter begin fun gl ->
95 let sigma = Proofview.Goal.sigma gl in
96 let concl = Proofview.Goal.concl gl in
97 let not_hyp' id = not (List.mem_assoc id id_map) in
98 let orig_id id = try List.assoc id id_map with Not_found -> id in
99 let dc, c = EConstr.decompose_prod_decls sigma concl in
100 let hide_goal = hidden_clseq clseq in
101 let c_hidden =
102 hide_goal && EConstr.eq_constr sigma c (EConstr.mkVar gl_id) in
103 let rec fits forced = function
104 | (id, _) :: ids, decl :: dc' when RelDecl.get_name decl = Name id ->
105 fits true (ids, dc')
106 | ids, dc' ->
107 forced && ids = [] && (not hide_goal || dc' = [] && c_hidden) in
108 let rec unmark c = match EConstr.kind sigma c with
109 | Var id when hidden_clseq clseq && id = gl_id -> cl0
110 | Prod ({binder_name=Name id} as na, t, c') when List.mem_assoc id id_map ->
111 EConstr.mkProd ({na with binder_name=Name (orig_id id)}, unmark t, unmark c')
112 | LetIn ({binder_name=Name id} as na, v, t, c') when List.mem_assoc id id_map ->
113 EConstr.mkLetIn ({na with binder_name=Name (orig_id id)}, unmark v, unmark t, unmark c')
114 | _ -> EConstr.map sigma unmark c in
115 let utac hyp =
116 Tactics.convert_hyp ~check:false ~reorder:false (NamedDecl.map_constr unmark hyp) in
117 let utacs = List.map utac (Proofview.Goal.hyps gl) in
118 let ugtac =
119 Proofview.Goal.enter begin fun gl ->
120 let concl = Proofview.Goal.concl gl in
121 convert_concl_no_check (unmark concl)
122 end
123 in
124 let ctacs =
125 if hide_goal then [Tactics.clear [gl_id]]
126 else [] in
127 let mktac itacs = Tacticals.tclTHENLIST (itacs @ utacs @ ugtac :: ctacs) in
128 let itac (_, id) = Tactics.introduction id in
129 if fits false (id_map, List.rev dc) then mktac (List.map itac id_map) else
130 let all_ids = ids_of_rel_context dc @ Tacmach.pf_ids_of_hyps gl in
131 if List.for_all not_hyp' all_ids && not c_hidden then mktac [] else
132 errorstrm Pp.(str "tampering with discharged assumptions of \"in\" tactical")
133 end
134
135 let tclCLAUSES tac (gens, clseq) =
136 let open Proofview.Notations in
137 Proofview.Goal.enter begin fun gl ->
138 let env = Proofview.Goal.env gl in
139 if clseq = InGoal || clseq = InSeqGoal then tac else
140 let clr_gens = pf_clauseids gens clseq in
141 let clear = Tacticals.tclTHENLIST (List.rev(List.fold_right clr_of_wgen clr_gens [])) in
142 let gl_id = mk_anon_id hidden_goal_tag (Environ.named_context_val env) in
143 let cl0 = Proofview.Goal.concl gl in
144 let dtac =
145 Proofview.Goal.enter begin fun gl ->
146 let env = Proofview.Goal.env gl in
147 let sigma = Proofview.Goal.sigma gl in
148 let c = Proofview.Goal.concl gl in
149 let fold gen (sigma, args, c) = abs_wgen env sigma true mk_discharged_id gen (args, c) in
150 let sigma, args, c = List.fold_right fold gens (sigma, [], c) in
151 Proofview.Unsafe.tclEVARS sigma <*>
152 Tactics.apply_type ~typecheck:true c args
153 end
154 in
155 let endtac =
156 let id_map = CList.map_filter (function
157 | _, Some ((x,_),_) -> let id = hoi_id x in Some (mk_discharged_id id, id)
158 | _, None -> None) gens in
159 endclausestac id_map clseq gl_id cl0 in
160 Tacticals.tclTHENLIST (hidetacs clseq gl_id cl0 @ [dtac; clear; tac; endtac])
161 end
162
163 (** The "do" tactical. ********************************************************)
164
165 let hinttac ist is_by (is_or, atacs) =
166 Proofview.Goal.enter begin fun _ ->
167 let dtac = if is_by then donetac ~-1 else Tacticals.tclIDTAC in
168 let mktac = function
169 | Some atac -> Tacticals.tclTHEN (ssrevaltac ist atac) dtac
170 | _ -> dtac in
171 match List.map mktac atacs with
172 | [] -> if is_or then dtac else Tacticals.tclIDTAC
173 | [tac] -> tac
174 | tacs -> Tacticals.tclFIRST tacs
175 end
176
177 let ssrdotac ist (((n, m), tac), clauses) =
178 let mul = get_index n, m in
179 tclCLAUSES (tclMULT mul (hinttac ist false tac)) clauses