Debian Codegraph @

vernac/proof_using.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 open Names
12 open Environ
13 open Util
14 open Vernacexpr
15 open Context.Named.Declaration
16
17 module NamedDecl = Context.Named.Declaration
18
19 let all_collection_id = Id.of_string "All"
20 let known_names = Summary.ref [] ~name:"proofusing-nameset"
21
22 let is_known_name id = CList.mem_assoc_f Id.equal id !known_names
23
24 let rec close_fwd env sigma s =
25 let s' =
26 List.fold_left (fun s decl ->
27 let vb = match decl with
28 | LocalAssum _ -> Id.Set.empty
29 | LocalDef (_,b,_) -> Termops.global_vars_set env sigma b
30 in
31 let vty = Termops.global_vars_set env sigma (NamedDecl.get_type decl) in
32 let vbty = Id.Set.union vb vty in
33 if Id.Set.exists (fun v -> Id.Set.mem v s) vbty
34 then Id.Set.add (NamedDecl.get_id decl) (Id.Set.union s vbty) else s)
35 s (EConstr.named_context env)
36 in
37 if Id.Set.equal s s' then s else close_fwd env sigma s'
38
39 let set_of_type env sigma fixnames ty =
40 List.fold_right Id.Set.remove fixnames
41 (List.fold_left (fun acc ty ->
42 Id.Set.union (Termops.global_vars_set env sigma ty) acc)
43 Id.Set.empty ty)
44
45 let full_set fixnames env =
46 let add id ids = if List.mem_f Id.equal id fixnames then ids else Id.Set.add id ids in
47 List.fold_right add (List.map NamedDecl.get_id (named_context env)) Id.Set.empty
48
49 let warn_all_collection_precedence = CWarnings.create ~name:"all-collection-precedence" ~category:Deprecation.Version.v8_15
50 Pp.(fun () -> str "Variable " ++ Id.print all_collection_id ++ str " is shadowed by Collection named " ++ Id.print all_collection_id ++ str " containing all variables.")
51
52 let warn_collection_precedence = CWarnings.create ~name:"collection-precedence" ~category:Deprecation.Version.v8_15
53 Pp.(fun id -> Id.print id ++ str " is both name of a Collection and Variable, Collection " ++ Id.print id ++ str " takes precedence over Variable.")
54
55 let warn_redefine_collection = CWarnings.create ~name:"collection-redefinition" ~category:Deprecation.Version.v8_15
56 Pp.(fun id -> str "New Collection definition of " ++ Id.print id ++ str " shadows the previous one.")
57
58 let warn_variable_shadowing = CWarnings.create ~name:"variable-shadowing" ~category:Deprecation.Version.v8_15
59 Pp.(fun id -> Id.print id ++ str " was already a defined Variable, the name " ++ Id.print id ++ str " will refer to Collection when executing \"Proof using\" command.")
60
61 let err_redefine_all_collection () =
62 CErrors.user_err Pp.(str "\"" ++ Id.print all_collection_id ++ str "\" is a predefined collection containing all variables. It can't be redefined.")
63
64 let process_expr env sigma fixnames e v_ty =
65 let variable_exists id =
66 try ignore (lookup_named id env); true with | Not_found -> false in
67 let rec aux = function
68 | SsEmpty -> Id.Set.empty
69 | SsType -> v_ty
70 | SsSingl { CAst.v = id } -> set_of_id id
71 | SsUnion(e1,e2) -> Id.Set.union (aux e1) (aux e2)
72 | SsSubstr(e1,e2) -> Id.Set.diff (aux e1) (aux e2)
73 | SsCompl e -> Id.Set.diff (full_set fixnames env) (aux e)
74 | SsFwdClose e -> close_fwd env sigma (aux e)
75 and set_of_id id =
76 if Id.equal id all_collection_id then
77 begin
78 if variable_exists all_collection_id then
79 warn_all_collection_precedence ();
80 full_set fixnames env
81 end
82 else if is_known_name id then
83 begin
84 if variable_exists id then
85 warn_collection_precedence id;
86 aux (CList.assoc_f Id.equal id !known_names)
87 end
88 else
89 if List.exists (Id.equal id) fixnames then
90 CErrors.user_err Pp.(str "Invalid recursive variable: " ++ Id.print id ++ str ".")
91 else if not (List.exists (NamedDecl.get_id %> Id.equal id) (named_context env)) then
92 CErrors.user_err Pp.(str "Unknown variable: " ++ Id.print id ++ str ".")
93 else
94 Id.Set.singleton id
95 in
96 aux e
97
98 let process_expr env sigma fixnames e ty =
99 let v_ty = set_of_type env sigma fixnames ty in
100 let s = Id.Set.union v_ty (process_expr env sigma fixnames e v_ty) in
101 Id.Set.elements s
102
103 type t = Names.Id.Set.t
104
105 let definition_using env evd ~fixnames ~using ~terms =
106 let l = process_expr env evd fixnames using terms in
107 Names.Id.Set.(CList.fold_right add l empty)
108
109 let name_set id expr =
110 if Id.equal id all_collection_id then err_redefine_all_collection ();
111 if is_known_name id then warn_redefine_collection id;
112 if Termops.is_section_variable (Global.env ()) id then warn_variable_shadowing id;
113 known_names := (id,expr) :: !known_names
114
115 let minimize_hyps env ids =
116 let rec aux ids =
117 let ids' =
118 Id.Set.fold (fun id alive ->
119 let impl_by_id =
120 Id.Set.remove id (really_needed env (Id.Set.singleton id)) in
121 if Id.Set.is_empty impl_by_id then alive
122 else Id.Set.diff alive impl_by_id)
123 ids ids in
124 if Id.Set.equal ids ids' then ids else aux ids'
125 in
126 aux ids
127
128 let remove_ids_and_lets env s ids =
129 let not_ids id = not (Id.Set.mem id ids) in
130 let no_body id = named_body id env = None in
131 let deps id = really_needed env (Id.Set.singleton id) in
132 (Id.Set.filter (fun id ->
133 not_ids id &&
134 (no_body id ||
135 Id.Set.exists not_ids (Id.Set.filter no_body (deps id)))) s)
136
137 let record_proof_using expr =
138 Aux_file.record_in_aux "suggest_proof_using" expr
139
140 let debug_proof_using = CDebug.create ~name:"proof-using" ()
141
142 (* Variables in [skip] come from after the definition, so don't count
143 for "All". Used in the variable case since the env contains the
144 variable itself. *)
145 let suggest_common env ppid used ids_typ skip =
146 let module S = Id.Set in
147 let open Pp in
148 let pr_set parens s =
149 let wrap ppcmds =
150 if parens && S.cardinal s > 1 then str "(" ++ ppcmds ++ str ")"
151 else ppcmds in
152 wrap (prlist_with_sep (fun _ -> str" ") Id.print (S.elements s)) in
153
154 let needed = minimize_hyps env (remove_ids_and_lets env used ids_typ) in
155 let all_needed = really_needed env needed in
156 let all = List.fold_left (fun all d -> S.add (NamedDecl.get_id d) all)
157 S.empty (named_context env)
158 in
159 let all = S.diff all skip in
160 let fwd_typ = close_fwd env (Evd.from_env env) ids_typ in
161 let () = debug_proof_using (fun () ->
162 str "All " ++ pr_set false all ++ fnl() ++
163 str "Type " ++ pr_set false ids_typ ++ fnl() ++
164 str "needed " ++ pr_set false needed ++ fnl() ++
165 str "all_needed " ++ pr_set false all_needed ++ fnl() ++
166 str "Type* " ++ pr_set false fwd_typ)
167 in
168 let valid_exprs = ref [] in
169 let valid e = valid_exprs := e :: !valid_exprs in
170 if S.is_empty needed then valid (str "Type");
171 if S.equal all_needed fwd_typ then valid (str "Type*");
172 if S.equal all all_needed then valid(str "All");
173 valid (pr_set false needed);
174 Feedback.msg_info (
175 str"The proof of "++ ppid ++ spc() ++
176 str "should start with one of the following commands:"++spc()++
177 v 0 (
178 prlist_with_sep cut (fun x->str"Proof using " ++x++ str". ") !valid_exprs));
179 if Aux_file.recording ()
180 then
181 let s = string_of_ppcmds (prlist_with_sep (fun _ -> str";") (fun x->x) !valid_exprs) in
182 record_proof_using s
183
184 let suggest_proof_using = ref false
185
186 let () =
187 Goptions.(declare_bool_option
188 { optstage = Summary.Stage.Interp;
189 optdepr = None;
190 optkey = ["Suggest";"Proof";"Using"];
191 optread = (fun () -> !suggest_proof_using);
192 optwrite = ((:=) suggest_proof_using) })
193
194 let suggest_constant env kn =
195 if !suggest_proof_using
196 then begin
197 let open Declarations in
198 let body = lookup_constant kn env in
199 let used = Id.Set.of_list @@ List.map NamedDecl.get_id body.const_hyps in
200 let ids_typ = global_vars_set env body.const_type in
201 suggest_common env (Printer.pr_constant env kn) used ids_typ Id.Set.empty
202 end
203
204 let suggest_variable env id =
205 if !suggest_proof_using
206 then begin
207 match lookup_named id env with
208 | LocalDef (_,body,typ) ->
209 let ids_typ = global_vars_set env typ in
210 let ids_body = global_vars_set env body in
211 let used = Id.Set.union ids_body ids_typ in
212 suggest_common env (Id.print id) used ids_typ (Id.Set.singleton id)
213 | LocalAssum _ -> assert false
214 end
215
216 let value = ref None
217
218 let using_to_string us = Pp.string_of_ppcmds (Ppvernac.pr_using us)
219 let entry = Procq.eoi_entry G_vernac.section_subset_expr
220 let using_from_string us = Procq.Entry.parse entry
221 (Procq.Parsable.make (Gramlib.Stream.of_string ("( "^us^" )")))
222
223 let proof_using_opt_name = ["Default";"Proof";"Using"]
224 let () =
225 Goptions.(declare_stringopt_option
226 { optstage = Summary.Stage.Interp;
227 optdepr = None;
228 optkey = proof_using_opt_name;
229 optread = (fun () -> Option.map using_to_string !value);
230 optwrite = (fun b -> value := Option.map using_from_string b);
231 })
232
233 let get_default_proof_using () = !value