| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open CAst |
| 12 |
open Util |
| 13 |
open Names |
| 14 |
open Vernacexpr |
| 15 |
|
| 16 |
let smart_global r = |
| 17 |
let gr = Smartlocate.smart_global r in |
| 18 |
Dumpglob.add_glob ?loc:r.loc gr; |
| 19 |
gr |
| 20 |
|
| 21 |
let cache_bidi_hints (gr, ohint) = |
| 22 |
match ohint with |
| 23 |
| None -> Pretyping.clear_bidirectionality_hint (Global.env ()) gr |
| 24 |
| Some nargs -> Pretyping.add_bidirectionality_hint (Global.env ()) gr nargs |
| 25 |
|
| 26 |
let load_bidi_hints _ r = |
| 27 |
cache_bidi_hints r |
| 28 |
|
| 29 |
let subst_bidi_hints (subst, (gr, ohint as orig)) = |
| 30 |
let gr' = Globnames.subst_global_reference subst gr in |
| 31 |
if gr == gr' then orig else (gr', ohint) |
| 32 |
|
| 33 |
let discharge_bidi_hints (gr, ohint) = |
| 34 |
if Globnames.isVarRef gr && Global.is_in_section gr then None |
| 35 |
else |
| 36 |
let vars = Global.section_instance gr in |
| 37 |
let n = Array.length vars in |
| 38 |
Some (gr, Option.map ((+) n) ohint) |
| 39 |
|
| 40 |
let inBidiHints = |
| 41 |
let open Libobject in |
| 42 |
declare_object { (default_object "BIDIRECTIONALITY-HINTS" ) with |
| 43 |
load_function = load_bidi_hints; |
| 44 |
cache_function = cache_bidi_hints; |
| 45 |
classify_function = (fun o -> Substitute); |
| 46 |
subst_function = subst_bidi_hints; |
| 47 |
discharge_function = discharge_bidi_hints; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
let warn_arguments_assert = |
| 52 |
CWarnings.create ~name:"arguments-assert" ~category:CWarnings.CoreCategories.vernacular |
| 53 |
Pp.(fun sr -> |
| 54 |
strbrk "This command is just asserting the names of arguments of " ++ |
| 55 |
Printer.pr_global sr ++ strbrk". If this is what you want, add " ++ |
| 56 |
strbrk "': assert' to silence the warning. If you want " ++ |
| 57 |
strbrk "to clear implicit arguments, add ': clear implicits'. " ++ |
| 58 |
strbrk "If you want to clear notation scopes, add ': clear scopes'") |
| 59 |
|
| 60 |
let warn_scope_delimiter_depth = |
| 61 |
CWarnings.create ~name:"argument-scope-delimiter" ~category:Deprecation.Version.v8_19 |
| 62 |
Pp.(fun () -> |
| 63 |
strbrk "The '%' scope delimiter in 'Arguments' commands is deprecated, " ++ |
| 64 |
strbrk "use '%_' instead (available since 8.19). The '%' syntax will be " ++ |
| 65 |
strbrk "reused in a future version with the same semantics as in terms, " ++ |
| 66 |
strbrk "that is adding scope to the stack for all subterms. " ++ |
| 67 |
strbrk "Code can be adapted with a script like: " ++ |
| 68 |
strbrk "for f in $(find . -name '*.v'); do sed '/Arguments/ s/%/%_/g' -i $f ; done") |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
let vernac_arguments ~section_local reference args more_implicits flags = |
| 74 |
let env = Global.env () in |
| 75 |
let sigma = Evd.from_env env in |
| 76 |
let assert_flag = List.mem `Assert flags in |
| 77 |
let rename_flag = List.mem `Rename flags in |
| 78 |
let clear_scopes_flag = List.mem `ClearScopes flags in |
| 79 |
let extra_scopes_flag = List.mem `ExtraScopes flags in |
| 80 |
let clear_implicits_flag = List.mem `ClearImplicits flags in |
| 81 |
let default_implicits_flag = List.mem `DefaultImplicits flags in |
| 82 |
let never_unfold_flag = List.mem `SimplNeverUnfold flags in |
| 83 |
let nomatch_flag = List.mem `SimplDontExposeCase flags in |
| 84 |
let clear_red_flag = List.mem `ClearReduction flags in |
| 85 |
let clear_bidi_hint = List.mem `ClearBidiHint flags in |
| 86 |
|
| 87 |
let err_incompat x y = |
| 88 |
CErrors.user_err Pp.(str ("Options \""^x^"\" and \""^y^"\" are incompatible.")) in |
| 89 |
|
| 90 |
if assert_flag && rename_flag then |
| 91 |
err_incompat "assert" "rename"; |
| 92 |
if clear_scopes_flag && extra_scopes_flag then |
| 93 |
err_incompat "clear scopes" "extra scopes"; |
| 94 |
if clear_implicits_flag && default_implicits_flag then |
| 95 |
err_incompat "clear implicits" "default implicits"; |
| 96 |
|
| 97 |
let args, nargs_for_red, nargs_before_bidi, _i = |
| 98 |
List.fold_left (fun (args,red,bidi,i) arg -> |
| 99 |
match arg with |
| 100 |
| RealArg arg -> (arg::args,red,bidi,i+1) |
| 101 |
| VolatileArg -> |
| 102 |
if Option.has_some red |
| 103 |
then CErrors.user_err Pp.(str "The \"/\" modifier may only occur once."); |
| 104 |
(args,Some i,bidi,i) |
| 105 |
| BidiArg -> |
| 106 |
if Option.has_some bidi |
| 107 |
then CErrors.user_err Pp.(str "The \"&\" modifier may only occur once."); |
| 108 |
(args,red,Some i,i)) |
| 109 |
([],None,None,0) |
| 110 |
args |
| 111 |
in |
| 112 |
let args = List.rev args in |
| 113 |
|
| 114 |
let sr = smart_global reference in |
| 115 |
let inf_names = |
| 116 |
let ty, _ = Typeops.type_of_global_in_context env sr in |
| 117 |
List.map pi1 (Impargs.compute_implicits_names env sigma (EConstr.of_constr ty)) |
| 118 |
in |
| 119 |
let prev_names = |
| 120 |
try Arguments_renaming.arguments_names env sr with Not_found -> inf_names |
| 121 |
in |
| 122 |
let num_args = List.length inf_names in |
| 123 |
assert (Int.equal num_args (List.length prev_names)); |
| 124 |
|
| 125 |
let names_of args = List.map (fun a -> a.name) args in |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
let err_extra_args names = |
| 130 |
CErrors.user_err |
| 131 |
Pp.(strbrk "Extra arguments: " ++ |
| 132 |
prlist_with_sep pr_comma Name.print names ++ str ".") |
| 133 |
in |
| 134 |
let err_missing_args names = |
| 135 |
CErrors.user_err |
| 136 |
Pp.(strbrk "The following arguments are not declared: " ++ |
| 137 |
prlist_with_sep pr_comma Name.print names ++ str ".") |
| 138 |
in |
| 139 |
|
| 140 |
let rec check_extra_args extra_args = |
| 141 |
match extra_args with |
| 142 |
| [] -> () |
| 143 |
| { notation_scope = [] } :: _ -> |
| 144 |
CErrors.user_err Pp.(str"Extra arguments should specify scopes.") |
| 145 |
| { notation_scope = _ :: _ } :: args -> check_extra_args args |
| 146 |
in |
| 147 |
|
| 148 |
let args, scopes = |
| 149 |
let scopes = List.map (fun { notation_scope = s } -> s) args in |
| 150 |
if List.length args > num_args then |
| 151 |
let args, extra_args = List.chop num_args args in |
| 152 |
if extra_scopes_flag then |
| 153 |
(check_extra_args extra_args; (args, scopes)) |
| 154 |
else err_extra_args (names_of extra_args) |
| 155 |
else args, scopes |
| 156 |
in |
| 157 |
|
| 158 |
if Option.cata (fun n -> n > num_args) false nargs_for_red then |
| 159 |
CErrors.user_err Pp.(str "The \"/\" modifier should be put before any extra scope."); |
| 160 |
|
| 161 |
if Option.cata (fun n -> n > num_args) false nargs_before_bidi then |
| 162 |
CErrors.user_err Pp.(str "The \"&\" modifier should be put before any extra scope."); |
| 163 |
|
| 164 |
let scopes_specified = List.exists ((<>) []) scopes in |
| 165 |
|
| 166 |
if scopes_specified && clear_scopes_flag then |
| 167 |
CErrors.user_err Pp.(str "The \"clear scopes\" flag is incompatible with scope annotations."); |
| 168 |
|
| 169 |
let names = List.map (fun { name } -> name) args in |
| 170 |
let names = names :: List.map (List.map fst) more_implicits in |
| 171 |
|
| 172 |
let rename_flag_required = ref false in |
| 173 |
let example_renaming = ref None in |
| 174 |
let save_example_renaming renaming = |
| 175 |
rename_flag_required := !rename_flag_required |
| 176 |
|| not (Name.equal (fst renaming) Anonymous); |
| 177 |
if Option.is_empty !example_renaming then |
| 178 |
example_renaming := Some renaming |
| 179 |
in |
| 180 |
|
| 181 |
let rec names_union names1 names2 = |
| 182 |
match names1, names2 with |
| 183 |
| [], [] -> [] |
| 184 |
| _ :: _, [] -> names1 |
| 185 |
| [], _ :: _ -> names2 |
| 186 |
| (Name _ as name) :: names1, Anonymous :: names2 |
| 187 |
| Anonymous :: names1, (Name _ as name) :: names2 -> |
| 188 |
name :: names_union names1 names2 |
| 189 |
| name1 :: names1, name2 :: names2 -> |
| 190 |
if Name.equal name1 name2 then |
| 191 |
name1 :: names_union names1 names2 |
| 192 |
else CErrors.user_err Pp.(str "Argument lists should agree on the names they provide.") |
| 193 |
in |
| 194 |
|
| 195 |
let names = List.fold_left names_union [] names in |
| 196 |
|
| 197 |
let rec rename prev_names names = |
| 198 |
match prev_names, names with |
| 199 |
| [], [] -> [] |
| 200 |
| [], _ :: _ -> err_extra_args names |
| 201 |
| _ :: _, [] when assert_flag -> |
| 202 |
|
| 203 |
|
| 204 |
err_missing_args (List.lastn (List.length prev_names) inf_names) |
| 205 |
| _ :: _, [] -> prev_names |
| 206 |
| prev :: prev_names, Anonymous :: names -> |
| 207 |
prev :: rename prev_names names |
| 208 |
| prev :: prev_names, (Name id as name) :: names -> |
| 209 |
if not (Name.equal prev name) then save_example_renaming (prev,name); |
| 210 |
name :: rename prev_names names |
| 211 |
in |
| 212 |
|
| 213 |
let names = rename prev_names names in |
| 214 |
let renaming_specified = Option.has_some !example_renaming in |
| 215 |
|
| 216 |
if !rename_flag_required && not rename_flag then begin |
| 217 |
let msg = let open Pp in |
| 218 |
match !example_renaming with |
| 219 |
| None -> |
| 220 |
strbrk "To rename arguments the \"rename\" flag must be specified." |
| 221 |
| Some (o,n) -> |
| 222 |
strbrk "Flag \"rename\" expected to rename " ++ Name.print o ++ |
| 223 |
strbrk " into " ++ Name.print n ++ str "." |
| 224 |
in CErrors.user_err msg |
| 225 |
end; |
| 226 |
|
| 227 |
let implicits = |
| 228 |
List.map (fun { name; implicit_status = i } -> (name,i)) args |
| 229 |
in |
| 230 |
let implicits = implicits :: more_implicits in |
| 231 |
|
| 232 |
let implicits_specified = match implicits with |
| 233 |
| [l] -> List.exists (function _, Glob_term.Explicit -> false | _ -> true) l |
| 234 |
| _ -> true in |
| 235 |
|
| 236 |
if implicits_specified && clear_implicits_flag then |
| 237 |
CErrors.user_err Pp.(str "The \"clear implicits\" flag must be omitted if implicit annotations are given."); |
| 238 |
|
| 239 |
if implicits_specified && default_implicits_flag then |
| 240 |
CErrors.user_err Pp.(str "The \"default implicits\" flag is incompatible with implicit annotations."); |
| 241 |
|
| 242 |
let rargs = |
| 243 |
Util.List.map_filter (function (n, true) -> Some n | _ -> None) |
| 244 |
(Util.List.map_i (fun i { recarg_like = b } -> i, b) 0 args) |
| 245 |
in |
| 246 |
|
| 247 |
let red_behavior = |
| 248 |
let open Reductionops.ReductionBehaviour in |
| 249 |
match clear_red_flag, never_unfold_flag, nomatch_flag, rargs, nargs_for_red with |
| 250 |
| true, true, _, _, _ -> err_incompat "clear simpl" "simpl never" |
| 251 |
| true, false, true, _, _ -> err_incompat "clear simpl" "simpl nomatch" |
| 252 |
| true, false, false, _ :: _, _ -> err_incompat "clear simpl" "!" |
| 253 |
| true, false, false, [], Some _ -> err_incompat "clear simpl" "/" |
| 254 |
| false, true, false, [], None -> Some NeverUnfold |
| 255 |
| false, true, true, _, _ -> err_incompat "simpl never" "simpl nomatch" |
| 256 |
| false, true, _, _::_, _ -> err_incompat "simpl never" "!" |
| 257 |
| false, true, _, _, Some _ -> err_incompat "simpl never" "/" |
| 258 |
| _, false, false, [], None -> None |
| 259 |
| false, false, false, _, _ -> Some (UnfoldWhen { nargs = nargs_for_red; |
| 260 |
recargs = rargs; |
| 261 |
}) |
| 262 |
| false, false, true, _, _ -> Some (UnfoldWhenNoMatch { nargs = nargs_for_red; |
| 263 |
recargs = rargs; |
| 264 |
}) |
| 265 |
in |
| 266 |
|
| 267 |
|
| 268 |
let red_modifiers_specified = Option.has_some red_behavior in |
| 269 |
|
| 270 |
let bidi_hint_specified = Option.has_some nargs_before_bidi in |
| 271 |
|
| 272 |
if bidi_hint_specified && clear_bidi_hint then |
| 273 |
err_incompat "clear bidirectionality hint" "&"; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
if renaming_specified then begin |
| 279 |
Arguments_renaming.rename_arguments section_local sr names |
| 280 |
end; |
| 281 |
|
| 282 |
if scopes_specified || clear_scopes_flag then begin |
| 283 |
if List.exists (fun {v=d,_} -> d = Constrexpr.DelimUnboundedScope) (List.flatten scopes) then |
| 284 |
warn_scope_delimiter_depth (); |
| 285 |
let scopes = List.map (List.map (fun {loc;v=_d,k} -> |
| 286 |
try ignore (Notation.find_scope k); k |
| 287 |
with CErrors.UserError _ -> |
| 288 |
Notation.find_delimiters_scope ?loc k)) scopes |
| 289 |
in |
| 290 |
Notation.declare_arguments_scope section_local (smart_global reference) scopes |
| 291 |
end; |
| 292 |
|
| 293 |
if implicits_specified || clear_implicits_flag then |
| 294 |
Impargs.set_implicits section_local (smart_global reference) implicits; |
| 295 |
|
| 296 |
if default_implicits_flag then |
| 297 |
Impargs.declare_implicits section_local (smart_global reference); |
| 298 |
|
| 299 |
if red_modifiers_specified || clear_red_flag then begin |
| 300 |
match sr with |
| 301 |
| GlobRef.ConstRef c -> |
| 302 |
Reductionops.ReductionBehaviour.set |
| 303 |
~local:section_local c red_behavior |
| 304 |
|
| 305 |
| _ -> |
| 306 |
CErrors.user_err |
| 307 |
Pp.(strbrk "Modifiers of the behavior of the simpl tactic "++ |
| 308 |
strbrk "are relevant for constants only.") |
| 309 |
end; |
| 310 |
|
| 311 |
if bidi_hint_specified then begin |
| 312 |
let n = Option.get nargs_before_bidi in |
| 313 |
if section_local then |
| 314 |
Pretyping.add_bidirectionality_hint env sr n |
| 315 |
else |
| 316 |
Lib.add_leaf (inBidiHints (sr, Some n)) |
| 317 |
end; |
| 318 |
|
| 319 |
if clear_bidi_hint then begin |
| 320 |
if section_local then |
| 321 |
Pretyping.clear_bidirectionality_hint env sr |
| 322 |
else |
| 323 |
Lib.add_leaf (inBidiHints (sr, None)) |
| 324 |
end; |
| 325 |
|
| 326 |
if not (renaming_specified || |
| 327 |
implicits_specified || |
| 328 |
scopes_specified || |
| 329 |
red_modifiers_specified || |
| 330 |
bidi_hint_specified) && (List.is_empty flags) then |
| 331 |
warn_arguments_assert sr |