| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
module SynclassDyn = Dyn.Make() |
| 13 |
|
| 14 |
open Pp |
| 15 |
open Util |
| 16 |
open CAst |
| 17 |
open CErrors |
| 18 |
open Names |
| 19 |
open Libnames |
| 20 |
open Libobject |
| 21 |
open Nametab |
| 22 |
open Tac2expr |
| 23 |
open Tac2print |
| 24 |
open Tac2intern |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
module Pltac = |
| 29 |
struct |
| 30 |
let ltac2_expr = Procq.Entry.make "ltac2_expr" |
| 31 |
let tac2expr_in_env = Procq.Entry.make "tac2expr_in_env" |
| 32 |
|
| 33 |
let q_ident = Procq.Entry.make "q_ident" |
| 34 |
let q_bindings = Procq.Entry.make "q_bindings" |
| 35 |
let q_with_bindings = Procq.Entry.make "q_with_bindings" |
| 36 |
let q_intropattern = Procq.Entry.make "q_intropattern" |
| 37 |
let q_intropatterns = Procq.Entry.make "q_intropatterns" |
| 38 |
let q_destruction_arg = Procq.Entry.make "q_destruction_arg" |
| 39 |
let q_induction_clause = Procq.Entry.make "q_induction_clause" |
| 40 |
let q_conversion = Procq.Entry.make "q_conversion" |
| 41 |
let q_orient = Procq.Entry.make "q_orient" |
| 42 |
let q_rewriting = Procq.Entry.make "q_rewriting" |
| 43 |
let q_clause = Procq.Entry.make "q_clause" |
| 44 |
let q_dispatch = Procq.Entry.make "q_dispatch" |
| 45 |
let q_occurrences = Procq.Entry.make "q_occurrences" |
| 46 |
let q_reference = Procq.Entry.make "q_reference" |
| 47 |
let q_strategy_flag = Procq.Entry.make "q_strategy_flag" |
| 48 |
let q_constr_matching = Procq.Entry.make "q_constr_matching" |
| 49 |
let q_goal_matching = Procq.Entry.make "q_goal_matching" |
| 50 |
let q_hintdb = Procq.Entry.make "q_hintdb" |
| 51 |
let q_move_location = Procq.Entry.make "q_move_location" |
| 52 |
let q_pose = Procq.Entry.make "q_pose" |
| 53 |
let q_assert = Procq.Entry.make "q_assert" |
| 54 |
end |
| 55 |
|
| 56 |
let () = |
| 57 |
let entries = [ |
| 58 |
Procq.Entry.Any Pltac.ltac2_expr; |
| 59 |
] in |
| 60 |
Procq.register_grammars_by_name "ltac2" entries |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
type tacdef = { |
| 65 |
tacdef_local : bool; |
| 66 |
tacdef_mutable : bool; |
| 67 |
tacdef_expr : glb_tacexpr; |
| 68 |
tacdef_type : type_scheme; |
| 69 |
tacdef_deprecation : Deprecation.t option; |
| 70 |
} |
| 71 |
|
| 72 |
let define_tacdef ((_,kn), def) = |
| 73 |
let data = { |
| 74 |
Tac2env.gdata_expr = def.tacdef_expr; |
| 75 |
gdata_type = def.tacdef_type; |
| 76 |
gdata_mutable = def.tacdef_mutable; |
| 77 |
gdata_deprecation = def.tacdef_deprecation; |
| 78 |
gdata_mutation_history = []; |
| 79 |
} in |
| 80 |
Tac2env.define_global kn data |
| 81 |
|
| 82 |
let push_tacdef visibility ((sp, kn), def) = |
| 83 |
if not def.tacdef_local then Tac2env.push_ltac visibility sp (TacConstant kn) |
| 84 |
|
| 85 |
let load_tacdef i obj = |
| 86 |
push_tacdef (Until i) obj; |
| 87 |
define_tacdef obj |
| 88 |
|
| 89 |
let open_tacdef i obj = push_tacdef (Exactly i) obj |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
let cache_tacdef ((sp, kn), def as obj) = |
| 94 |
|
| 95 |
Tac2env.push_ltac (Until 1) sp (TacConstant kn); |
| 96 |
define_tacdef obj |
| 97 |
|
| 98 |
let subst_tacdef (subst, def) = |
| 99 |
let expr' = subst_expr subst def.tacdef_expr in |
| 100 |
let type' = subst_type_scheme subst def.tacdef_type in |
| 101 |
if expr' == def.tacdef_expr && type' == def.tacdef_type then def |
| 102 |
else { def with tacdef_expr = expr'; tacdef_type = type' } |
| 103 |
|
| 104 |
let classify_tacdef o = Substitute |
| 105 |
|
| 106 |
let inTacDef : Id.t -> tacdef -> obj = |
| 107 |
declare_named_object {(default_object "TAC2-DEFINITION") with |
| 108 |
cache_function = cache_tacdef; |
| 109 |
load_function = load_tacdef; |
| 110 |
open_function = filtered_open open_tacdef; |
| 111 |
subst_function = subst_tacdef; |
| 112 |
classify_function = classify_tacdef} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
type typdef = { |
| 117 |
typdef_local : bool; |
| 118 |
typdef_abstract : bool; |
| 119 |
typdef_expr : glb_quant_typedef; |
| 120 |
} |
| 121 |
|
| 122 |
let change_kn_label kn id = |
| 123 |
let mp = KerName.modpath kn in |
| 124 |
KerName.make mp id |
| 125 |
|
| 126 |
let change_sp_label sp id = |
| 127 |
let (dp, _) = Libnames.repr_path sp in |
| 128 |
Libnames.make_path dp id |
| 129 |
|
| 130 |
let push_typedef_contents visibility sp kn (_, def) = match def with |
| 131 |
| GTydDef _ -> () |
| 132 |
| GTydAlg { galg_constructors = cstrs } -> |
| 133 |
|
| 134 |
let iter (user_warns, c, _) = |
| 135 |
let spc = change_sp_label sp c in |
| 136 |
let knc = change_kn_label kn c in |
| 137 |
Tac2env.push_constructor ?user_warns visibility spc knc |
| 138 |
in |
| 139 |
List.iter iter cstrs |
| 140 |
| GTydRec fields -> |
| 141 |
|
| 142 |
let iter (c, _, _) = |
| 143 |
let spc = change_sp_label sp c in |
| 144 |
let knc = change_kn_label kn c in |
| 145 |
Tac2env.push_projection visibility spc knc |
| 146 |
in |
| 147 |
List.iter iter fields |
| 148 |
| GTydOpn -> () |
| 149 |
|
| 150 |
let push_typedef visibility sp kn def = |
| 151 |
Tac2env.push_type visibility sp kn; |
| 152 |
push_typedef_contents visibility sp kn def |
| 153 |
|
| 154 |
let next i = |
| 155 |
let ans = !i in |
| 156 |
let () = incr i in |
| 157 |
ans |
| 158 |
|
| 159 |
let define_typedef kn (params, def as qdef) = match def with |
| 160 |
| GTydDef _ -> |
| 161 |
Tac2env.define_type kn qdef |
| 162 |
| GTydAlg { galg_constructors = cstrs } -> |
| 163 |
|
| 164 |
let constant = ref 0 in |
| 165 |
let nonconstant = ref 0 in |
| 166 |
let iter (_warn, c, args) = |
| 167 |
let knc = change_kn_label kn c in |
| 168 |
let tag = if List.is_empty args then next constant else next nonconstant in |
| 169 |
let data = { |
| 170 |
Tac2env.cdata_prms = params; |
| 171 |
cdata_type = kn; |
| 172 |
cdata_args = args; |
| 173 |
cdata_indx = Some tag; |
| 174 |
} in |
| 175 |
Tac2env.define_constructor knc data |
| 176 |
in |
| 177 |
Tac2env.define_type kn qdef; |
| 178 |
List.iter iter cstrs |
| 179 |
| GTydRec fs -> |
| 180 |
|
| 181 |
let iter i (id, mut, t) = |
| 182 |
let knp = change_kn_label kn id in |
| 183 |
let proj = { |
| 184 |
Tac2env.pdata_prms = params; |
| 185 |
pdata_type = kn; |
| 186 |
pdata_ptyp = t; |
| 187 |
pdata_mutb = mut; |
| 188 |
pdata_indx = i; |
| 189 |
} in |
| 190 |
Tac2env.define_projection knp proj |
| 191 |
in |
| 192 |
Tac2env.define_type kn qdef; |
| 193 |
List.iteri iter fs |
| 194 |
| GTydOpn -> |
| 195 |
Tac2env.define_type kn qdef |
| 196 |
|
| 197 |
let perform_typdef vs ((sp, kn), def) = |
| 198 |
let expr = def.typdef_expr in |
| 199 |
let expr = if def.typdef_abstract then fst expr, GTydDef None else expr in |
| 200 |
let () = if not def.typdef_local then push_typedef vs sp kn expr in |
| 201 |
define_typedef kn expr |
| 202 |
|
| 203 |
let load_typdef i obj = perform_typdef (Until i) obj |
| 204 |
let open_typdef i obj = perform_typdef (Exactly i) obj |
| 205 |
|
| 206 |
let cache_typdef ((sp, kn), def) = |
| 207 |
let () = push_typedef (Until 1) sp kn def.typdef_expr in |
| 208 |
define_typedef kn def.typdef_expr |
| 209 |
|
| 210 |
let subst_typdef (subst, def) = |
| 211 |
let expr' = subst_quant_typedef subst def.typdef_expr in |
| 212 |
if expr' == def.typdef_expr then def else { def with typdef_expr = expr' } |
| 213 |
|
| 214 |
let classify_typdef o = Substitute |
| 215 |
|
| 216 |
let inTypDef : Id.t -> typdef -> obj = |
| 217 |
declare_named_object {(default_object "TAC2-TYPE-DEFINITION") with |
| 218 |
cache_function = cache_typdef; |
| 219 |
load_function = load_typdef; |
| 220 |
open_function = filtered_open open_typdef; |
| 221 |
subst_function = subst_typdef; |
| 222 |
classify_function = classify_typdef} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
type extension_data = { |
| 227 |
edata_warn : UserWarn.t option; |
| 228 |
edata_name : Id.t; |
| 229 |
edata_args : int glb_typexpr list; |
| 230 |
} |
| 231 |
|
| 232 |
type typext = { |
| 233 |
typext_local : bool; |
| 234 |
typext_prms : int; |
| 235 |
typext_type : type_constant; |
| 236 |
typext_expr : extension_data list; |
| 237 |
} |
| 238 |
|
| 239 |
let push_typext vis prefix def = |
| 240 |
let iter data = |
| 241 |
let spc = Libnames.add_path_suffix prefix.obj_path data.edata_name in |
| 242 |
let knc = KerName.make prefix.obj_mp data.edata_name in |
| 243 |
let user_warns = data.edata_warn in |
| 244 |
Tac2env.push_constructor ?user_warns vis spc knc |
| 245 |
in |
| 246 |
List.iter iter def.typext_expr |
| 247 |
|
| 248 |
let define_typext mp def = |
| 249 |
let iter data = |
| 250 |
let knc = KerName.make mp data.edata_name in |
| 251 |
let cdata = { |
| 252 |
Tac2env.cdata_prms = def.typext_prms; |
| 253 |
cdata_type = def.typext_type; |
| 254 |
cdata_args = data.edata_args; |
| 255 |
cdata_indx = None; |
| 256 |
} in |
| 257 |
Tac2env.define_constructor knc cdata |
| 258 |
in |
| 259 |
List.iter iter def.typext_expr |
| 260 |
|
| 261 |
let cache_typext (prefix, def) = |
| 262 |
let () = define_typext prefix.obj_mp def in |
| 263 |
push_typext (Until 1) prefix def |
| 264 |
|
| 265 |
let perform_typext vs (prefix, def) = |
| 266 |
let () = if not def.typext_local then push_typext vs prefix def in |
| 267 |
define_typext prefix.obj_mp def |
| 268 |
|
| 269 |
let load_typext i obj = perform_typext (Until i) obj |
| 270 |
let open_typext i obj = perform_typext (Exactly i) obj |
| 271 |
|
| 272 |
let subst_typext (subst, e) = |
| 273 |
let open Mod_subst in |
| 274 |
let subst_data data = |
| 275 |
let edata_args = List.Smart.map (fun e -> subst_type subst e) data.edata_args in |
| 276 |
if edata_args == data.edata_args then data |
| 277 |
else { data with edata_args } |
| 278 |
in |
| 279 |
let typext_type = subst_kn subst e.typext_type in |
| 280 |
let typext_expr = List.Smart.map subst_data e.typext_expr in |
| 281 |
if typext_type == e.typext_type && typext_expr == e.typext_expr then |
| 282 |
e |
| 283 |
else |
| 284 |
{ e with typext_type; typext_expr } |
| 285 |
|
| 286 |
let classify_typext o = Substitute |
| 287 |
|
| 288 |
let inTypExt : typext -> obj = |
| 289 |
declare_named_object_gen {(default_object "TAC2-TYPE-EXTENSION") with |
| 290 |
cache_function = cache_typext; |
| 291 |
load_function = load_typext; |
| 292 |
open_function = filtered_open open_typext; |
| 293 |
subst_function = subst_typext; |
| 294 |
classify_function = classify_typext} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
let inline_rec_tactic tactics = |
| 300 |
let map (id, e) = |
| 301 |
let map_body ({loc;v=id}, e) = CAst.(make ?loc @@ CPatVar (Name id)), e in |
| 302 |
let bnd = List.map map_body tactics in |
| 303 |
let var_of_id {loc;v=id} = |
| 304 |
let qid = qualid_of_ident ?loc id in |
| 305 |
CAst.make ?loc @@ CTacRef (RelId qid) |
| 306 |
in |
| 307 |
let loc0 = e.loc in |
| 308 |
let e = CAst.make ?loc:loc0 @@ CTacLet (true, bnd, var_of_id id) in |
| 309 |
(id, e) |
| 310 |
in |
| 311 |
List.map map tactics |
| 312 |
|
| 313 |
let check_lowercase {loc;v=id} = |
| 314 |
if Tac2env.is_constructor (Libnames.qualid_of_ident id) then |
| 315 |
user_err ?loc (str "The identifier " ++ Id.print id ++ str " must start with a lowercase letter.") |
| 316 |
|
| 317 |
let check_uppercase {loc;v=id} = |
| 318 |
if not @@ Tac2env.is_constructor (Libnames.qualid_of_ident id) then |
| 319 |
user_err ?loc (str "The identifier " ++ Id.print id ++ str " must start with an uppercase letter.") |
| 320 |
|
| 321 |
let pp_not_value_reason = function |
| 322 |
| MutString -> str "(it contains a string literal, and strings are mutable)" |
| 323 |
| Application -> str "(it contains an application)" |
| 324 |
| MutDef kn -> str "(it contains mutable definition " ++ pr_tacref Id.Set.empty kn |
| 325 |
| MutCtor kn -> str "(it contains a constructor of type " ++ pr_typref kn ++ str " which has mutable fields)" |
| 326 |
| MutProj kn -> str "(it contains a mutable projection from type " ++ pr_typref kn ++ str ")" |
| 327 |
| MaybeValButNotSupported -> mt() |
| 328 |
|
| 329 |
let check_value ?loc e = |
| 330 |
match check_value e with |
| 331 |
| None -> () |
| 332 |
| Some reason -> |
| 333 |
let ppreason = pp_not_value_reason reason in |
| 334 |
user_err ?loc |
| 335 |
(str "Tactic definition must be a syntactical value" ++ |
| 336 |
(if ismt ppreason then mt() else spc() ++ ppreason) ++ str "." ++ spc() ++ |
| 337 |
str "Consider using a thunk.") |
| 338 |
|
| 339 |
let check_ltac_exists {loc;v=id} = |
| 340 |
let kn = Lib.make_kn id in |
| 341 |
let exists = |
| 342 |
try let _ = Tac2env.interp_global kn in true with Not_found -> false |
| 343 |
in |
| 344 |
if exists then |
| 345 |
user_err ?loc (str "Tactic " ++ Names.Id.print id ++ str " already exists") |
| 346 |
|
| 347 |
|
| 348 |
let register_ltac ?deprecation ?(local = false) ?(mut = false) isrec tactics = |
| 349 |
let map ({loc;v=na}, e) = |
| 350 |
let id = match na with |
| 351 |
| Anonymous -> |
| 352 |
user_err ?loc (str "Tactic definition must have a name") |
| 353 |
| Name id -> id |
| 354 |
in |
| 355 |
let () = check_lowercase CAst.(make ?loc id) in |
| 356 |
(CAst.(make ?loc id), e) |
| 357 |
in |
| 358 |
let tactics = List.map map tactics in |
| 359 |
let tactics = |
| 360 |
if isrec then inline_rec_tactic tactics else tactics |
| 361 |
in |
| 362 |
let map (lid, ({loc=eloc} as e)) = |
| 363 |
let (e, t) = intern ~strict:true [] e in |
| 364 |
let () = check_value ?loc:eloc e in |
| 365 |
let () = check_ltac_exists lid in |
| 366 |
(lid.v, e, t) |
| 367 |
in |
| 368 |
let defs = List.map map tactics in |
| 369 |
let iter (id, e, t) = |
| 370 |
let def = { |
| 371 |
tacdef_local = local; |
| 372 |
tacdef_mutable = mut; |
| 373 |
tacdef_expr = e; |
| 374 |
tacdef_type = t; |
| 375 |
tacdef_deprecation = deprecation; |
| 376 |
} in |
| 377 |
Lib.add_leaf (inTacDef id def) |
| 378 |
in |
| 379 |
List.iter iter defs |
| 380 |
|
| 381 |
let qualid_to_ident qid = |
| 382 |
if qualid_is_ident qid then CAst.make ?loc:qid.CAst.loc @@ qualid_basename qid |
| 383 |
else user_err ?loc:qid.CAst.loc (str "Identifier expected") |
| 384 |
|
| 385 |
let register_typedef ?(local = false) ?(abstract=false) isrec types = |
| 386 |
let same_name ({v=id1}, _) ({v=id2}, _) = Id.equal id1 id2 in |
| 387 |
let () = match List.duplicates same_name types with |
| 388 |
| [] -> () |
| 389 |
| ({loc;v=id}, _) :: _ -> |
| 390 |
user_err ?loc (str "Multiple definition of the type name " ++ Id.print id) |
| 391 |
in |
| 392 |
let () = |
| 393 |
let check_existing_type ({v=id},_) = |
| 394 |
let (_, kn) = Lib.make_foname id in |
| 395 |
try let _ = Tac2env.interp_type kn in |
| 396 |
user_err (str "Multiple definition of the type name " ++ Id.print id) |
| 397 |
with Not_found -> () |
| 398 |
in |
| 399 |
List.iter check_existing_type types |
| 400 |
in |
| 401 |
let check ({loc;v=id}, (params, def)) = |
| 402 |
let same_name {v=id1} {v=id2} = Id.equal id1 id2 in |
| 403 |
let () = match List.duplicates same_name params with |
| 404 |
| [] -> () |
| 405 |
| {loc;v=id} :: _ -> |
| 406 |
user_err ?loc (str "The type parameter " ++ Id.print id ++ |
| 407 |
str " occurs several times") |
| 408 |
in |
| 409 |
match def with |
| 410 |
| CTydDef _ -> |
| 411 |
if isrec then |
| 412 |
user_err ?loc (str "The type abbreviation " ++ Id.print id ++ |
| 413 |
str " cannot be recursive") |
| 414 |
| CTydAlg cs -> |
| 415 |
let same_name (_, id1, _) (_, id2, _) = Id.equal id1.v id2.v in |
| 416 |
let () = match List.duplicates same_name cs with |
| 417 |
| [] -> () |
| 418 |
| (_, id, _) :: _ -> |
| 419 |
user_err (str "Multiple definitions of the constructor " ++ Id.print id.v) |
| 420 |
in |
| 421 |
let () = List.iter (fun (_,id,_) -> check_uppercase id) cs in |
| 422 |
let () = |
| 423 |
let check_existing_ctor (_, id, _) = |
| 424 |
let (_, kn) = Lib.make_foname id.v in |
| 425 |
try let _ = Tac2env.interp_constructor kn in |
| 426 |
user_err (str "Constructor already defined in this module " ++ Id.print id.v) |
| 427 |
with Not_found -> () |
| 428 |
in |
| 429 |
List.iter check_existing_ctor cs |
| 430 |
in |
| 431 |
() |
| 432 |
| CTydRec ps -> |
| 433 |
let same_name (id1, _, _) (id2, _, _) = Id.equal id1 id2 in |
| 434 |
let () = match List.duplicates same_name ps with |
| 435 |
| [] -> () |
| 436 |
| (id, _, _) :: _ -> |
| 437 |
user_err (str "Multiple definitions of the projection " ++ Id.print id) |
| 438 |
in |
| 439 |
() |
| 440 |
| CTydOpn -> |
| 441 |
if isrec then |
| 442 |
user_err ?loc (str "The open type declaration " ++ Id.print id ++ |
| 443 |
str " cannot be recursive"); |
| 444 |
if abstract then |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
user_err ?loc (str "Open types currently do not support #[abstract].") |
| 450 |
in |
| 451 |
let () = List.iter check types in |
| 452 |
let self = |
| 453 |
if isrec then |
| 454 |
let fold accu ({v=id}, (params, _)) = |
| 455 |
Id.Map.add id (Lib.make_kn id, List.length params) accu |
| 456 |
in |
| 457 |
List.fold_left fold Id.Map.empty types |
| 458 |
else Id.Map.empty |
| 459 |
in |
| 460 |
let map ({v=id}, def) = |
| 461 |
let typdef = { |
| 462 |
typdef_local = local; |
| 463 |
typdef_abstract = abstract; |
| 464 |
typdef_expr = intern_typedef self def; |
| 465 |
} in |
| 466 |
(id, typdef) |
| 467 |
in |
| 468 |
let types = List.map map types in |
| 469 |
let iter (id, def) = Lib.add_leaf (inTypDef id def) in |
| 470 |
List.iter iter types |
| 471 |
|
| 472 |
let register_primitive ?deprecation ?(local = false) ({loc;v=id} as lid) t ml = |
| 473 |
let () = check_ltac_exists lid in |
| 474 |
let t = intern_open_type t in |
| 475 |
let () = |
| 476 |
try let _ = Tac2env.interp_primitive ml in () with Not_found -> |
| 477 |
user_err ?loc (str "Unregistered primitive " ++ |
| 478 |
quote (str ml.mltac_plugin) ++ spc () ++ quote (str ml.mltac_tactic)) |
| 479 |
in |
| 480 |
let e = GTacPrm ml in |
| 481 |
let def = { |
| 482 |
tacdef_local = local; |
| 483 |
tacdef_mutable = false; |
| 484 |
tacdef_expr = e; |
| 485 |
tacdef_type = t; |
| 486 |
tacdef_deprecation = deprecation; |
| 487 |
} in |
| 488 |
Lib.add_leaf (inTacDef id def) |
| 489 |
|
| 490 |
let register_open ?(local = false) qid (params, def) = |
| 491 |
let kn = |
| 492 |
try Tac2env.locate_type qid |
| 493 |
with Not_found -> |
| 494 |
user_err ?loc:qid.CAst.loc (str "Unbound type " ++ pr_qualid qid) |
| 495 |
in |
| 496 |
let (tparams, t) = Tac2env.interp_type kn in |
| 497 |
let () = match t with |
| 498 |
| GTydOpn -> () |
| 499 |
| GTydAlg _ | GTydRec _ | GTydDef _ -> |
| 500 |
user_err ?loc:qid.CAst.loc (str "Type " ++ pr_qualid qid ++ str " is not an open type") |
| 501 |
in |
| 502 |
let () = |
| 503 |
if not (Int.equal (List.length params) tparams) then |
| 504 |
Tac2intern.error_nparams_mismatch ?loc:qid.CAst.loc (List.length params) tparams |
| 505 |
in |
| 506 |
match def with |
| 507 |
| CTydOpn -> () |
| 508 |
| CTydAlg def -> |
| 509 |
let () = |
| 510 |
let same_name (_, id1, _) (_, id2, _) = Id.equal id1.v id2.v in |
| 511 |
let () = match List.duplicates same_name def with |
| 512 |
| [] -> () |
| 513 |
| (_, id, _) :: _ -> |
| 514 |
user_err (str "Multiple definitions of the constructor " ++ Id.print id.v) |
| 515 |
in |
| 516 |
let check_existing_ctor (_, id, _) = |
| 517 |
let (_, kn) = Lib.make_foname id.v in |
| 518 |
try let _ = Tac2env.interp_constructor kn in |
| 519 |
user_err (str "Constructor already defined in this module " ++ Id.print id.v) |
| 520 |
with Not_found -> () |
| 521 |
in |
| 522 |
let () = List.iter check_existing_ctor def in |
| 523 |
() |
| 524 |
in |
| 525 |
let intern_type t = |
| 526 |
let tpe = CTydDef (Some t) in |
| 527 |
let (_, ans) = intern_typedef Id.Map.empty (params, tpe) in |
| 528 |
match ans with |
| 529 |
| GTydDef (Some t) -> t |
| 530 |
| _ -> assert false |
| 531 |
in |
| 532 |
let map (atts, id, tpe) = |
| 533 |
let () = check_uppercase id in |
| 534 |
let warn = Attributes.parse Attributes.user_warns atts in |
| 535 |
let tpe = List.map intern_type tpe in |
| 536 |
{ edata_warn = warn; edata_name = id.v; edata_args = tpe } |
| 537 |
in |
| 538 |
let def = List.map map def in |
| 539 |
let def = { |
| 540 |
typext_local = local; |
| 541 |
typext_type = kn; |
| 542 |
typext_prms = tparams; |
| 543 |
typext_expr = def; |
| 544 |
} in |
| 545 |
Lib.add_leaf (inTypExt def) |
| 546 |
| CTydRec _ | CTydDef _ -> |
| 547 |
user_err ?loc:qid.CAst.loc (str "Extensions only accept inductive constructors") |
| 548 |
|
| 549 |
let register_type ?local ?abstract isrec types = match types with |
| 550 |
| [qid, true, def] -> |
| 551 |
let () = if isrec then user_err ?loc:qid.CAst.loc (str "Extensions cannot be recursive.") in |
| 552 |
let () = if Option.default false abstract |
| 553 |
then user_err ?loc:qid.loc (str "Extensions cannot be abstract.") |
| 554 |
in |
| 555 |
register_open ?local qid def |
| 556 |
| _ -> |
| 557 |
let map (qid, redef, def) = |
| 558 |
let () = if redef then |
| 559 |
user_err ?loc:qid.loc (str "Types can only be extended one by one") |
| 560 |
in |
| 561 |
(qualid_to_ident qid, def) |
| 562 |
in |
| 563 |
let types = List.map map types in |
| 564 |
register_typedef ?local ?abstract isrec types |
| 565 |
|
| 566 |
let load_import_type i ((sp,kn),orig) = |
| 567 |
let def = Tac2env.interp_type orig in |
| 568 |
push_typedef_contents (Until i) sp orig def |
| 569 |
|
| 570 |
let open_import_type i ((sp,kn),orig) = |
| 571 |
let def = Tac2env.interp_type orig in |
| 572 |
push_typedef_contents (Exactly i) sp orig def |
| 573 |
|
| 574 |
let cache_import_type o = load_import_type 1 o |
| 575 |
|
| 576 |
let inImportType = |
| 577 |
declare_named_object |
| 578 |
{ (default_object "TAC2-IMPORT-TYPE") with |
| 579 |
cache_function = cache_import_type; |
| 580 |
load_function = load_import_type; |
| 581 |
open_function = filtered_open open_import_type; |
| 582 |
subst_function = (fun (subst,kn) -> Mod_subst.subst_kn subst kn); |
| 583 |
|
| 584 |
classify_function = (fun _ -> Substitute); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
let import_type qid as_id = |
| 589 |
let () = |
| 590 |
if Lib.sections_are_opened() then |
| 591 |
|
| 592 |
CErrors.user_err Pp.(str "Ltac2 Import Type not supported in sections.") |
| 593 |
in |
| 594 |
match Tac2env.locate_type qid with |
| 595 |
| exception Not_found -> |
| 596 |
CErrors.user_err ?loc:qid.loc Pp.(str "Unknown Ltac2 type " ++ pr_qualid qid ++ str ".") |
| 597 |
| orig -> |
| 598 |
let params, _ = Tac2env.interp_type orig in |
| 599 |
let alias_def = GTypRef (Other orig, List.init params (fun i -> GTypVar i)) in |
| 600 |
Lib.add_leaf (inTypDef as_id { |
| 601 |
typdef_local = false; |
| 602 |
typdef_abstract = false; |
| 603 |
typdef_expr = params, GTydDef (Some alias_def); |
| 604 |
}); |
| 605 |
Lib.add_leaf (inImportType as_id orig) |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
type 'a token = |
| 610 |
| TacTerm of string |
| 611 |
| TacNonTerm of Name.t * 'a |
| 612 |
|
| 613 |
type syntax_class_rule = |
| 614 |
| SyntaxRule : (raw_tacexpr, _, 'a) Procq.Symbol.t * ('a -> raw_tacexpr) -> syntax_class_rule |
| 615 |
|
| 616 |
module Tac2Custom = KerName |
| 617 |
|
| 618 |
type used_levels = Int.Set.t Tac2Custom.Map.t |
| 619 |
|
| 620 |
let no_used_levels = Tac2Custom.Map.empty |
| 621 |
|
| 622 |
let union_used_levels a b = |
| 623 |
Tac2Custom.Map.union (fun _ a b -> Some (Int.Set.union a b)) a b |
| 624 |
|
| 625 |
|
| 626 |
type 'glb syntax_class_decl = { |
| 627 |
intern_synclass : sexpr list -> used_levels * 'glb; |
| 628 |
interp_synclass : 'glb -> syntax_class_rule; |
| 629 |
} |
| 630 |
|
| 631 |
type syntax_class = SynclassDyn.t |
| 632 |
|
| 633 |
module SynclassInterpMap = SynclassDyn.Map(struct |
| 634 |
type 'a t = 'a -> syntax_class_rule |
| 635 |
end) |
| 636 |
|
| 637 |
let syntax_class_interns : (sexpr list -> used_levels * SynclassDyn.t) Id.Map.t ref = |
| 638 |
ref Id.Map.empty |
| 639 |
|
| 640 |
let syntax_class_interps = ref SynclassInterpMap.empty |
| 641 |
|
| 642 |
module CustomV = struct |
| 643 |
include Tac2Custom |
| 644 |
let is_var _ = None |
| 645 |
let stage = Summary.Stage.Synterp |
| 646 |
let summary_name = "ltac2_customentrytab" |
| 647 |
end |
| 648 |
module CustomTab = Nametab.EasyNoWarn(CustomV)() |
| 649 |
|
| 650 |
let ltac2_custom_map : raw_tacexpr Procq.Entry.t Tac2Custom.Map.t Procq.GramState.field = |
| 651 |
Procq.GramState.field "ltac2_custom_map" |
| 652 |
|
| 653 |
let ltac2_custom_entry : (Tac2Custom.t, raw_tacexpr) Procq.entry_command = |
| 654 |
Procq.create_entry_command "ltac2" { |
| 655 |
eext_fun = (fun kn e state -> |
| 656 |
let map = Option.default Tac2Custom.Map.empty (Procq.GramState.get state ltac2_custom_map) in |
| 657 |
let map = Tac2Custom.Map.add kn e map in |
| 658 |
Procq.GramState.set state ltac2_custom_map map); |
| 659 |
eext_name = (fun kn -> "custom-ltac2:" ^ Tac2Custom.to_string kn); |
| 660 |
eext_eq = Tac2Custom.equal; |
| 661 |
} |
| 662 |
|
| 663 |
let find_custom_entry kn = |
| 664 |
Tac2Custom.Map.get kn @@ Option.get @@ Procq.GramState.get (Procq.gramstate()) ltac2_custom_map |
| 665 |
|
| 666 |
let () = |
| 667 |
Metasyntax.register_custom_grammar_for_print @@ fun name -> |
| 668 |
match CustomTab.locate name with |
| 669 |
| exception Not_found -> None |
| 670 |
| name -> Some [Any (find_custom_entry name)] |
| 671 |
|
| 672 |
let load_custom_entry i ((sp,kn),local) = |
| 673 |
let () = CustomTab.push (Until i) sp kn in |
| 674 |
let () = Procq.extend_entry_command ltac2_custom_entry kn in |
| 675 |
let () = assert (not local) in |
| 676 |
() |
| 677 |
|
| 678 |
let import_custom_entry i ((sp,kn),local) = |
| 679 |
let () = CustomTab.push (Exactly i) sp kn in |
| 680 |
() |
| 681 |
|
| 682 |
let cache_custom_entry o = |
| 683 |
load_custom_entry 1 o; |
| 684 |
import_custom_entry 1 o |
| 685 |
|
| 686 |
let inCustomEntry : Id.t -> bool -> Libobject.obj = |
| 687 |
declare_named_object { |
| 688 |
(default_object "Ltac2 custom entry") with |
| 689 |
object_stage = Synterp; |
| 690 |
cache_function = cache_custom_entry; |
| 691 |
load_function = load_custom_entry; |
| 692 |
open_function = filtered_open import_custom_entry; |
| 693 |
subst_function = (fun (_,x) -> x); |
| 694 |
classify_function = (fun local -> if local then Dispose else Substitute); |
| 695 |
} |
| 696 |
|
| 697 |
let check_custom_entry_name id = |
| 698 |
|
| 699 |
if Id.Map.mem id !syntax_class_interns then |
| 700 |
CErrors.user_err |
| 701 |
Pp.(str "Cannot declare " ++ Id.print id ++ |
| 702 |
str " as a ltac2 custom entry:" ++ spc() ++ |
| 703 |
str "that name is already used for a builtin syntactic class.") |
| 704 |
else if CustomTab.exists (Lib.make_path id) then |
| 705 |
CErrors.user_err Pp.(str "Ltac2 custom entry " ++ Id.print id ++ str " already exists.") |
| 706 |
|
| 707 |
let register_custom_entry name = |
| 708 |
let name = name.CAst.v in |
| 709 |
check_custom_entry_name name; |
| 710 |
|
| 711 |
|
| 712 |
let local = false in |
| 713 |
Lib.add_leaf (inCustomEntry name local) |
| 714 |
|
| 715 |
let register_syntax_class id (s:_ syntax_class_decl) = |
| 716 |
assert (not (Id.Map.mem id !syntax_class_interns)); |
| 717 |
let tag = SynclassDyn.create (Id.to_string id) in |
| 718 |
let intern args = |
| 719 |
let used, data = s.intern_synclass args in |
| 720 |
used, SynclassDyn.Dyn (tag, data) |
| 721 |
in |
| 722 |
syntax_class_interns := Id.Map.add id intern !syntax_class_interns; |
| 723 |
syntax_class_interps := SynclassInterpMap.add tag s.interp_synclass !syntax_class_interps |
| 724 |
|
| 725 |
let level_name lev = string_of_int lev |
| 726 |
|
| 727 |
let terminal_synclass_tag : string SynclassDyn.tag = SynclassDyn.create "<terminal>" |
| 728 |
|
| 729 |
let interp_terminal str : syntax_class_rule = |
| 730 |
let v_unit = CAst.make @@ CTacCst (AbsKn (Tuple 0)) in |
| 731 |
SyntaxRule (Procq.Symbol.token (Tok.PIDENT (Some str)), (fun _ -> v_unit)) |
| 732 |
|
| 733 |
let () = |
| 734 |
syntax_class_interps := SynclassInterpMap.add terminal_synclass_tag interp_terminal !syntax_class_interps |
| 735 |
|
| 736 |
type custom_synclass_data = { |
| 737 |
custom_synclass_name : Tac2Custom.t; |
| 738 |
custom_synclass_level : int option; |
| 739 |
} |
| 740 |
|
| 741 |
let interp_custom_entry data : syntax_class_rule = |
| 742 |
let ename = data.custom_synclass_name in |
| 743 |
let entry = find_custom_entry ename in |
| 744 |
match data.custom_synclass_level with |
| 745 |
| None -> |
| 746 |
SyntaxRule (Procq.Symbol.nterm entry, (fun expr -> expr)) |
| 747 |
| Some lev -> |
| 748 |
SyntaxRule (Procq.Symbol.nterml entry (level_name lev), (fun expr -> expr)) |
| 749 |
|
| 750 |
let custom_synclass_tag : custom_synclass_data SynclassDyn.tag = SynclassDyn.create "<custom>" |
| 751 |
|
| 752 |
let () = |
| 753 |
syntax_class_interps := SynclassInterpMap.add custom_synclass_tag interp_custom_entry !syntax_class_interps |
| 754 |
|
| 755 |
let intern_custom_entry ?loc qid ename args : used_levels * custom_synclass_data = |
| 756 |
let lev = |
| 757 |
match args with |
| 758 |
| [] -> None |
| 759 |
| [SexprInt {CAst.v=lev}] -> Some lev |
| 760 |
| _ :: _ -> |
| 761 |
CErrors.user_err ?loc |
| 762 |
Pp.(str "Invalid arguments for ltac2 custom entry " ++ pr_qualid qid ++ str ".") |
| 763 |
in |
| 764 |
let used = match lev with |
| 765 |
| None -> no_used_levels |
| 766 |
| Some lev -> Tac2Custom.Map.singleton ename (Int.Set.singleton lev) |
| 767 |
in |
| 768 |
used, { custom_synclass_name = ename; |
| 769 |
custom_synclass_level = lev; |
| 770 |
} |
| 771 |
|
| 772 |
let intern_syntactic_class ?loc qid args = |
| 773 |
let is_class = |
| 774 |
if qualid_is_ident qid then Id.Map.find_opt (qualid_basename qid) !syntax_class_interns |
| 775 |
else None |
| 776 |
in |
| 777 |
match is_class with |
| 778 |
| Some intern -> intern args |
| 779 |
| None -> |
| 780 |
match CustomTab.locate qid with |
| 781 |
| kn -> |
| 782 |
let used, data = intern_custom_entry ?loc qid kn args in |
| 783 |
used, SynclassDyn.Dyn (custom_synclass_tag, data) |
| 784 |
| exception Not_found -> |
| 785 |
CErrors.user_err ?loc (str "Unknown syntactic class" ++ spc () ++ pr_qualid qid) |
| 786 |
|
| 787 |
module ParseToken = |
| 788 |
struct |
| 789 |
|
| 790 |
let loc_of_token = function |
| 791 |
| SexprStr {loc} -> loc |
| 792 |
| SexprInt {loc} -> loc |
| 793 |
| SexprRec (loc, _, _) -> Some loc |
| 794 |
|
| 795 |
let intern_syntax_class = function |
| 796 |
| SexprRec (_, {loc;v=Some id}, toks) -> |
| 797 |
intern_syntactic_class id toks |
| 798 |
| SexprStr {v=str} -> no_used_levels, SynclassDyn.Dyn (terminal_synclass_tag, str) |
| 799 |
| tok -> |
| 800 |
let loc = loc_of_token tok in |
| 801 |
CErrors.user_err ?loc (str "Invalid parsing token") |
| 802 |
|
| 803 |
let check_name na = |
| 804 |
match na.CAst.v with |
| 805 |
| None -> Anonymous |
| 806 |
| Some id -> |
| 807 |
let id = if qualid_is_ident id then qualid_basename id |
| 808 |
else CErrors.user_err ?loc:id.loc Pp.(str "Must be an identifier.") |
| 809 |
in |
| 810 |
let () = check_lowercase (CAst.make ?loc:na.CAst.loc id) in |
| 811 |
Name id |
| 812 |
|
| 813 |
let parse_token = function |
| 814 |
| SexprStr {v=s} -> no_used_levels, TacTerm s |
| 815 |
| SexprRec (_, na, [tok]) -> |
| 816 |
let na = check_name na in |
| 817 |
let used, syntax_class = intern_syntax_class tok in |
| 818 |
used, TacNonTerm (na, syntax_class) |
| 819 |
| tok -> |
| 820 |
let loc = loc_of_token tok in |
| 821 |
CErrors.user_err ?loc (str "Invalid parsing token") |
| 822 |
|
| 823 |
let name_of_token = function |
| 824 |
| SexprStr _ -> Anonymous |
| 825 |
| SexprRec (_, na, _) -> check_name na |
| 826 |
| tok -> |
| 827 |
let loc = loc_of_token tok in |
| 828 |
CErrors.user_err ?loc (str "Invalid parsing token") |
| 829 |
|
| 830 |
let rec print_syntax_class = function |
| 831 |
| SexprStr s -> str s.CAst.v |
| 832 |
| SexprInt i -> int i.CAst.v |
| 833 |
| SexprRec (_, {v=na}, []) -> Option.cata pr_qualid (str "_") na |
| 834 |
| SexprRec (_, {v=na}, e) -> |
| 835 |
Option.cata pr_qualid (str "_") na ++ str "(" ++ pr_sequence print_syntax_class e ++ str ")" |
| 836 |
|
| 837 |
let print_token = function |
| 838 |
| SexprStr {v=s} -> quote (str s) |
| 839 |
| SexprRec (_, {v=na}, [tok]) -> print_syntax_class tok |
| 840 |
| _ -> assert false |
| 841 |
|
| 842 |
end |
| 843 |
|
| 844 |
let intern_syntax_class = ParseToken.intern_syntax_class |
| 845 |
|
| 846 |
type synext = { |
| 847 |
synext_kn : KerName.t; |
| 848 |
|
| 849 |
synext_raw : sexpr list; |
| 850 |
synext_used : used_levels; |
| 851 |
synext_tok : SynclassDyn.t token list; |
| 852 |
synext_entry : Tac2Custom.t option * int; |
| 853 |
synext_loc : bool; |
| 854 |
synext_depr : Deprecation.t option; |
| 855 |
} |
| 856 |
|
| 857 |
type krule = |
| 858 |
| KRule : |
| 859 |
(raw_tacexpr, _, 'act, Loc.t -> raw_tacexpr) Procq.Rule.t * |
| 860 |
((Loc.t -> (Name.t * raw_tacexpr) list -> raw_tacexpr) -> 'act) -> krule |
| 861 |
|
| 862 |
let interp_syntax_class (SynclassDyn.Dyn (tag, data)) = |
| 863 |
let interp = SynclassInterpMap.find tag !syntax_class_interps in |
| 864 |
interp data |
| 865 |
|
| 866 |
let rec get_rule (tok : SynclassDyn.t token list) : krule = match tok with |
| 867 |
| [] -> KRule (Procq.Rule.stop, fun k loc -> k loc []) |
| 868 |
| TacNonTerm (na, v) :: tok -> |
| 869 |
let SyntaxRule (syntax_class, inj) = interp_syntax_class v in |
| 870 |
let KRule (rule, act) = get_rule tok in |
| 871 |
let rule = Procq.Rule.next rule syntax_class in |
| 872 |
let act k e = act (fun loc acc -> k loc ((na, inj e) :: acc)) in |
| 873 |
KRule (rule, act) |
| 874 |
| TacTerm t :: tok -> |
| 875 |
let KRule (rule, act) = get_rule tok in |
| 876 |
let rule = Procq.(Rule.next rule (Symbol.token (CLexer.terminal t))) in |
| 877 |
let act k _ = act k in |
| 878 |
KRule (rule, act) |
| 879 |
|
| 880 |
let deprecated_ltac2_notation = |
| 881 |
Deprecation.create_warning |
| 882 |
~object_name:"Ltac2 notation" |
| 883 |
~warning_name_if_no_since:"deprecated-ltac2-notation" |
| 884 |
(fun (toks : sexpr list) -> pr_sequence ParseToken.print_token toks) |
| 885 |
|
| 886 |
let ltac2_levels = Procq.GramState.field "ltac2_levels" |
| 887 |
|
| 888 |
|
| 889 |
let fresh_level st entry lev = |
| 890 |
match entry with |
| 891 |
| None -> st, None |
| 892 |
| Some entry -> |
| 893 |
let all_levels = Option.default Tac2Custom.Map.empty @@ Procq.GramState.get st ltac2_levels in |
| 894 |
let entry_levels = Option.default Int.Set.empty @@ Tac2Custom.Map.find_opt entry all_levels in |
| 895 |
let last_before = Int.Set.find_first_opt (fun lev' -> lev' >= lev) entry_levels in |
| 896 |
if Option.equal Int.equal last_before (Some lev) then st, None |
| 897 |
else |
| 898 |
let pos = match last_before with |
| 899 |
| None -> Gramlib.Gramext.First |
| 900 |
| Some lev' -> Gramlib.Gramext.After (level_name lev') |
| 901 |
in |
| 902 |
let entry_levels = Int.Set.add lev entry_levels in |
| 903 |
let all_levels = Tac2Custom.Map.add entry entry_levels all_levels in |
| 904 |
let st = Procq.GramState.set st ltac2_levels all_levels in |
| 905 |
st, Some pos |
| 906 |
|
| 907 |
let check_levels st used_levels = |
| 908 |
let all_levels = Option.default Tac2Custom.Map.empty @@ Procq.GramState.get st ltac2_levels in |
| 909 |
let iter kn used = |
| 910 |
let known = Option.default Int.Set.empty (Tac2Custom.Map.find_opt kn all_levels) in |
| 911 |
let missing = Int.Set.diff used known in |
| 912 |
if not (Int.Set.is_empty missing) then |
| 913 |
CErrors.user_err |
| 914 |
Pp.(str "Unknown " ++ str (String.plural (Int.Set.cardinal missing) "level") ++ |
| 915 |
str " for ltac2 custom entry " ++ CustomTab.pr kn) |
| 916 |
in |
| 917 |
Tac2Custom.Map.iter iter used_levels |
| 918 |
|
| 919 |
let perform_notation syn st = |
| 920 |
let tok = syn.synext_tok in |
| 921 |
let used = syn.synext_used in |
| 922 |
let KRule (rule, act) = get_rule tok in |
| 923 |
let mk loc args = |
| 924 |
let () = match syn.synext_depr with |
| 925 |
| None -> () |
| 926 |
| Some depr -> deprecated_ltac2_notation ~loc (syn.synext_raw, depr) |
| 927 |
in |
| 928 |
let map (na, e) = |
| 929 |
((CAst.make ?loc:e.loc na), e) |
| 930 |
in |
| 931 |
let bnd = List.map map args in |
| 932 |
CAst.make ~loc @@ CTacSyn (bnd, syn.synext_kn) |
| 933 |
in |
| 934 |
let rule = Procq.Production.make rule (act mk) in |
| 935 |
let entry, lev = syn.synext_entry in |
| 936 |
let st, fresh = fresh_level st entry lev in |
| 937 |
let () = check_levels st used in |
| 938 |
let pos = Some (level_name lev) in |
| 939 |
let rule = match fresh with |
| 940 |
| None -> Procq.Reuse (pos, [rule]) |
| 941 |
| Some pos' -> |
| 942 |
|
| 943 |
Procq.Fresh (pos', [pos, Some BothA, [rule]]) |
| 944 |
in |
| 945 |
let entry = match entry with |
| 946 |
| None -> Pltac.ltac2_expr |
| 947 |
| Some entry -> find_custom_entry entry |
| 948 |
in |
| 949 |
[Procq.ExtendRule (entry, rule)], st |
| 950 |
|
| 951 |
let ltac2_notation = |
| 952 |
Procq.create_grammar_command "ltac2-notation" { gext_fun = perform_notation; gext_eq = (==) } |
| 953 |
|
| 954 |
let cache_synext syn = |
| 955 |
Procq.extend_grammar_command ~ignore_kw:false ltac2_notation syn |
| 956 |
|
| 957 |
let subst_synext (subst, syn) = |
| 958 |
let kn = Mod_subst.subst_kn subst syn.synext_kn in |
| 959 |
if kn == syn.synext_kn then syn |
| 960 |
else { syn with synext_kn = kn } |
| 961 |
|
| 962 |
let classify_synext o = |
| 963 |
if o.synext_loc then Dispose else Substitute |
| 964 |
|
| 965 |
let ltac2_notation_cat = Libobject.create_category "ltac2.notations" |
| 966 |
|
| 967 |
let inTac2Notation : synext -> obj = |
| 968 |
declare_object {(default_object "TAC2-NOTATION") with |
| 969 |
object_stage = Summary.Stage.Synterp; |
| 970 |
cache_function = cache_synext; |
| 971 |
open_function = simple_open ~cat:ltac2_notation_cat cache_synext; |
| 972 |
subst_function = subst_synext; |
| 973 |
classify_function = classify_synext} |
| 974 |
|
| 975 |
let cache_synext_interp (local,kn,tac) = |
| 976 |
Tac2env.define_notation kn tac |
| 977 |
|
| 978 |
let subst_notation_data subst = function |
| 979 |
| Tac2env.UntypedNota body as n -> |
| 980 |
let body' = Tac2intern.subst_rawexpr subst body in |
| 981 |
if body' == body then n else UntypedNota body' |
| 982 |
| TypedNota { nota_prms=prms; nota_argtys=argtys; nota_ty=ty; nota_body=body } as n -> |
| 983 |
let body' = Tac2intern.subst_expr subst body in |
| 984 |
let argtys' = Id.Map.Smart.map (subst_type subst) argtys in |
| 985 |
let ty' = subst_type subst ty in |
| 986 |
if body' == body && argtys' == argtys && ty' == ty then n |
| 987 |
else TypedNota {nota_body=body'; nota_argtys=argtys'; nota_ty=ty'; nota_prms=prms} |
| 988 |
|
| 989 |
let subst_synext_interp (subst, (local,kn,tac as o)) = |
| 990 |
let tac' = subst_notation_data subst tac in |
| 991 |
let kn' = Mod_subst.subst_kn subst kn in |
| 992 |
if kn' == kn && tac' == tac then o else |
| 993 |
(local, kn', tac') |
| 994 |
|
| 995 |
let classify_synext_interp (local,_,_) = |
| 996 |
if local then Dispose else Substitute |
| 997 |
|
| 998 |
let inTac2NotationInterp : (bool*KerName.t*Tac2env.notation_data) -> obj = |
| 999 |
declare_object {(default_object "TAC2-NOTATION-INTERP") with |
| 1000 |
cache_function = cache_synext_interp; |
| 1001 |
open_function = simple_open ~cat:ltac2_notation_cat cache_synext_interp; |
| 1002 |
subst_function = subst_synext_interp; |
| 1003 |
classify_function = classify_synext_interp} |
| 1004 |
|
| 1005 |
type abbreviation = { |
| 1006 |
abbr_body : raw_tacexpr; |
| 1007 |
abbr_depr : Deprecation.t option; |
| 1008 |
} |
| 1009 |
|
| 1010 |
let perform_abbreviation visibility ((sp, kn), abbr) = |
| 1011 |
let () = Tac2env.push_ltac visibility sp (TacAlias kn) in |
| 1012 |
Tac2env.define_alias ?deprecation:abbr.abbr_depr kn abbr.abbr_body |
| 1013 |
|
| 1014 |
let load_abbreviation i obj = perform_abbreviation (Until i) obj |
| 1015 |
let open_abbreviation i obj = perform_abbreviation (Exactly i) obj |
| 1016 |
|
| 1017 |
let cache_abbreviation ((sp, kn), abbr) = |
| 1018 |
let () = Tac2env.push_ltac (Until 1) sp (TacAlias kn) in |
| 1019 |
Tac2env.define_alias ?deprecation:abbr.abbr_depr kn abbr.abbr_body |
| 1020 |
|
| 1021 |
let subst_abbreviation (subst, abbr) = |
| 1022 |
let body' = subst_rawexpr subst abbr.abbr_body in |
| 1023 |
if body' == abbr.abbr_body then abbr |
| 1024 |
else { abbr_body = body'; abbr_depr = abbr.abbr_depr } |
| 1025 |
|
| 1026 |
let classify_abbreviation o = Substitute |
| 1027 |
|
| 1028 |
let inTac2Abbreviation : Id.t -> abbreviation -> obj = |
| 1029 |
declare_named_object {(default_object "TAC2-ABBREVIATION") with |
| 1030 |
cache_function = cache_abbreviation; |
| 1031 |
load_function = load_abbreviation; |
| 1032 |
open_function = filtered_open ~cat:ltac2_notation_cat open_abbreviation; |
| 1033 |
subst_function = subst_abbreviation; |
| 1034 |
classify_function = classify_abbreviation} |
| 1035 |
|
| 1036 |
let rec string_of_syntax_class = function |
| 1037 |
| SexprStr s -> Printf.sprintf "str(%s)" s.CAst.v |
| 1038 |
| SexprInt i -> Printf.sprintf "int(%i)" i.CAst.v |
| 1039 |
| SexprRec (_, {v=na}, []) -> Option.cata string_of_qualid "_" na |
| 1040 |
| SexprRec (_, {v=na}, e) -> |
| 1041 |
Printf.sprintf "%s(%s)" (Option.cata string_of_qualid "_" na) (String.concat " " (List.map string_of_syntax_class e)) |
| 1042 |
|
| 1043 |
let string_of_token = function |
| 1044 |
| SexprStr {v=s} -> Printf.sprintf "str(%s)" s |
| 1045 |
| SexprRec (_, {v=na}, [tok]) -> string_of_syntax_class tok |
| 1046 |
| _ -> assert false |
| 1047 |
|
| 1048 |
let make_fresh_key tokens = |
| 1049 |
let prods = String.concat "_" (List.map string_of_token tokens) in |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
let hash = (ModPath.hash (Lib.current_mp ())) land 0x7FFFFFFF in |
| 1054 |
let lbl = Id.of_string_soft (Printf.sprintf "%s_%08X" prods hash) in |
| 1055 |
Lib.make_kn lbl |
| 1056 |
|
| 1057 |
type notation_interpretation_data = |
| 1058 |
| Abbreviation of Id.t * Deprecation.t option * raw_tacexpr |
| 1059 |
| Synext of bool * KerName.t * Id.Set.t * raw_tacexpr |
| 1060 |
|
| 1061 |
type notation_target = qualid option * int option |
| 1062 |
|
| 1063 |
let pr_register_notation tkn (entry,lev) body = |
| 1064 |
let pptarget = match entry, lev with |
| 1065 |
| None, None -> mt() |
| 1066 |
| None, Some lev -> spc() ++ str ": " ++ int lev |
| 1067 |
| Some entry, None -> spc() ++ str ": " ++ pr_qualid entry |
| 1068 |
| Some entry, Some lev -> |
| 1069 |
spc() ++ str ": " ++ pr_qualid entry ++ str "(" ++ int lev ++ str ")" |
| 1070 |
in |
| 1071 |
prlist_with_sep spc Tac2print.pr_syntax_class tkn ++ |
| 1072 |
pptarget ++ spc() ++ |
| 1073 |
hov 2 (str ":= " ++ Tac2print.pr_rawexpr_gen E5 ~avoid:Id.Set.empty body) |
| 1074 |
|
| 1075 |
let pr_register_abbreviation id body = |
| 1076 |
Id.print id.CAst.v ++ |
| 1077 |
hov 2 (str ":= " ++ Tac2print.pr_rawexpr_gen E5 ~avoid:Id.Set.empty body) |
| 1078 |
|
| 1079 |
let register_abbreviation atts id body = |
| 1080 |
let deprecation = Attributes.(parse deprecation) atts in |
| 1081 |
let () = check_lowercase id in |
| 1082 |
Abbreviation(id.CAst.v, deprecation, body) |
| 1083 |
|
| 1084 |
let warn_deprecated_notation_for_abbreviation = |
| 1085 |
CWarnings.create ~name:"ltac2-notation-for-abbreviation" ~category:Deprecation.Version.v9_2 |
| 1086 |
(fun () -> strbrk "Use of \"Ltac2 Notation\" keyword for abbreviations is deprecated, use \"Ltac2 Abbreviation\" instead.") |
| 1087 |
|
| 1088 |
let tactic_qualid = qualid_of_ident (Id.of_string "tactic") |
| 1089 |
|
| 1090 |
let register_notation atts tkn (entry,lev) body = |
| 1091 |
match tkn, entry, lev with |
| 1092 |
| [SexprRec (_, {loc;v=Some id}, [])], None, None -> |
| 1093 |
warn_deprecated_notation_for_abbreviation (); |
| 1094 |
let id = if qualid_is_ident id then qualid_basename id |
| 1095 |
else CErrors.user_err ?loc:id.loc Pp.(str "Must be an identifier.") |
| 1096 |
in |
| 1097 |
register_abbreviation atts CAst.(make ?loc id) body |
| 1098 |
| _ -> |
| 1099 |
let deprecation, local = Attributes.(parse Notations.(deprecation ++ locality)) atts in |
| 1100 |
let local = Option.default false local in |
| 1101 |
|
| 1102 |
let entries = List.map ParseToken.name_of_token tkn in |
| 1103 |
let fold accu tok = match tok with |
| 1104 |
| Name id -> Id.Set.add id accu |
| 1105 |
| Anonymous -> accu |
| 1106 |
in |
| 1107 |
let ids = List.fold_left fold Id.Set.empty entries in |
| 1108 |
let entry = match entry with |
| 1109 |
| Some entry -> |
| 1110 |
if qualid_eq entry tactic_qualid then None |
| 1111 |
else begin |
| 1112 |
try Some (CustomTab.locate entry) |
| 1113 |
with Not_found -> CErrors.user_err Pp.(str "Unknown entry " ++ pr_qualid entry ++ str ".") |
| 1114 |
end |
| 1115 |
| None -> None |
| 1116 |
in |
| 1117 |
|
| 1118 |
let lev = if Option.has_some entry then |
| 1119 |
let lev = match lev with |
| 1120 |
| Some lev -> lev |
| 1121 |
| None -> user_err (str "Custom entry level must be explicit.") |
| 1122 |
in |
| 1123 |
let () = if lev < 0 then user_err (str "Custom entry levels must be nonnegative.") in |
| 1124 |
lev |
| 1125 |
else match lev with |
| 1126 |
| Some n -> |
| 1127 |
let () = |
| 1128 |
if n < 0 || n > 6 then |
| 1129 |
user_err (str "Notation levels must range between 0 and 6") |
| 1130 |
in |
| 1131 |
n |
| 1132 |
| None -> |
| 1133 |
|
| 1134 |
begin match tkn with |
| 1135 |
| SexprStr s :: _ when Names.Id.is_valid s.CAst.v -> 1 |
| 1136 |
| _ -> 5 |
| 1137 |
end |
| 1138 |
in |
| 1139 |
let key = make_fresh_key tkn in |
| 1140 |
let tokens = List.rev_map ParseToken.parse_token tkn in |
| 1141 |
let used, tokens = List.split tokens in |
| 1142 |
let used = List.fold_left union_used_levels no_used_levels used in |
| 1143 |
let ext = { |
| 1144 |
synext_kn = key; |
| 1145 |
synext_raw = tkn; |
| 1146 |
synext_used = used; |
| 1147 |
synext_tok = tokens; |
| 1148 |
synext_entry = (entry,lev); |
| 1149 |
synext_loc = local; |
| 1150 |
synext_depr = deprecation; |
| 1151 |
} in |
| 1152 |
Lib.add_leaf (inTac2Notation ext); |
| 1153 |
Synext (local,key,ids,body) |
| 1154 |
|
| 1155 |
let register_notation_interpretation = function |
| 1156 |
| Abbreviation (id, deprecation, body) -> |
| 1157 |
let body = Tac2intern.globalize Id.Set.empty body in |
| 1158 |
let abbr = { abbr_body = body; abbr_depr = deprecation } in |
| 1159 |
Lib.add_leaf (inTac2Abbreviation id abbr) |
| 1160 |
| Synext (local,kn,ids,body) -> |
| 1161 |
let data = intern_notation_data ids body in |
| 1162 |
Lib.add_leaf (inTac2NotationInterp (local,kn,data)) |
| 1163 |
|
| 1164 |
type redefinition = { |
| 1165 |
redef_local : Libobject.locality; |
| 1166 |
redef_kn : ltac_constant; |
| 1167 |
redef_body : glb_tacexpr; |
| 1168 |
redef_old : Id.t option; |
| 1169 |
} |
| 1170 |
|
| 1171 |
let perform_redefinition (prefix,redef) = |
| 1172 |
let kn = redef.redef_kn in |
| 1173 |
let data = Tac2env.interp_global kn in |
| 1174 |
let body = match redef.redef_old with |
| 1175 |
| None -> redef.redef_body |
| 1176 |
| Some id -> |
| 1177 |
|
| 1178 |
GTacLet (false, [Name id, data.Tac2env.gdata_expr], redef.redef_body) |
| 1179 |
in |
| 1180 |
let history = if Option.has_some redef.redef_old then data.gdata_mutation_history else [] in |
| 1181 |
let data = { |
| 1182 |
data with |
| 1183 |
gdata_expr = body; |
| 1184 |
gdata_mutation_history = prefix.Libobject.obj_mp :: history; |
| 1185 |
} |
| 1186 |
in |
| 1187 |
Tac2env.define_global kn data |
| 1188 |
|
| 1189 |
let load_redefinition _ (_,redef as o) = |
| 1190 |
match redef.redef_local with |
| 1191 |
| Local -> assert false |
| 1192 |
| Export -> () |
| 1193 |
| SuperGlobal -> perform_redefinition o |
| 1194 |
|
| 1195 |
let open_redefinition (_,redef as o) = |
| 1196 |
match redef.redef_local with |
| 1197 |
| Local -> assert false |
| 1198 |
| Export -> perform_redefinition o |
| 1199 |
| SuperGlobal -> () |
| 1200 |
|
| 1201 |
let subst_redefinition (subst, redef) = |
| 1202 |
let kn = Mod_subst.subst_kn subst redef.redef_kn in |
| 1203 |
let body = Tac2intern.subst_expr subst redef.redef_body in |
| 1204 |
if kn == redef.redef_kn && body == redef.redef_body then redef |
| 1205 |
else { redef_local = redef.redef_local; |
| 1206 |
redef_kn = kn; |
| 1207 |
redef_body = body; |
| 1208 |
redef_old = redef.redef_old; |
| 1209 |
} |
| 1210 |
|
| 1211 |
let classify_redefinition o = match o.redef_local with |
| 1212 |
| Local -> Dispose |
| 1213 |
| Export | SuperGlobal -> Substitute |
| 1214 |
|
| 1215 |
let inTac2Redefinition : redefinition -> obj = |
| 1216 |
declare_named_object_gen |
| 1217 |
{(default_object "TAC2-REDEFINITION") with |
| 1218 |
cache_function = perform_redefinition; |
| 1219 |
load_function = load_redefinition; |
| 1220 |
open_function = simple_open open_redefinition; |
| 1221 |
subst_function = subst_redefinition; |
| 1222 |
classify_function = classify_redefinition; |
| 1223 |
} |
| 1224 |
|
| 1225 |
let register_redefinition ~local qid old ({loc=eloc} as e) = |
| 1226 |
let local = match local with |
| 1227 |
| None -> if Lib.sections_are_opened() then Local else Export |
| 1228 |
| Some local -> Locality.check_locality_nodischarge Local; local |
| 1229 |
in |
| 1230 |
let kn = |
| 1231 |
try Tac2env.locate_ltac qid |
| 1232 |
with Not_found -> user_err ?loc:qid.CAst.loc (str "Unknown tactic " ++ pr_qualid qid) |
| 1233 |
in |
| 1234 |
let kn = match kn with |
| 1235 |
| TacConstant kn -> kn |
| 1236 |
| TacAlias _ -> |
| 1237 |
user_err ?loc:qid.CAst.loc (str "Cannot redefine syntactic abbreviations") |
| 1238 |
in |
| 1239 |
let data = Tac2env.interp_global kn in |
| 1240 |
let () = |
| 1241 |
if not (data.Tac2env.gdata_mutable) then |
| 1242 |
user_err ?loc:qid.CAst.loc (str "The tactic " ++ pr_qualid qid ++ str " is not declared as mutable") |
| 1243 |
in |
| 1244 |
let ctx = match old with |
| 1245 |
| None -> [] |
| 1246 |
| Some { CAst.v = id } -> [id, data.Tac2env.gdata_type] |
| 1247 |
in |
| 1248 |
let (e, t) = intern ~strict:true ctx e in |
| 1249 |
let () = check_value ?loc:eloc e in |
| 1250 |
let () = |
| 1251 |
if not (Tac2intern.check_subtype t data.Tac2env.gdata_type) then |
| 1252 |
let name = int_name () in |
| 1253 |
user_err ?loc:qid.CAst.loc (str "Type " ++ pr_glbtype name (snd t) ++ |
| 1254 |
str " is not a subtype of " ++ pr_glbtype name (snd data.Tac2env.gdata_type)) |
| 1255 |
in |
| 1256 |
let old = Option.map (fun { CAst.v = id } -> id) old in |
| 1257 |
let def = { |
| 1258 |
redef_local = local; |
| 1259 |
redef_kn = kn; |
| 1260 |
redef_body = e; |
| 1261 |
redef_old = old; |
| 1262 |
} in |
| 1263 |
Lib.add_leaf (inTac2Redefinition def) |
| 1264 |
|
| 1265 |
let perform_eval ~pstate e = |
| 1266 |
let env = Global.env () in |
| 1267 |
let (e, ty) = Tac2intern.intern ~strict:false [] e in |
| 1268 |
let v = Tac2interp.interp Tac2interp.empty_environment e in |
| 1269 |
let proof = |
| 1270 |
match pstate with |
| 1271 |
| None -> |
| 1272 |
let sigma = Evd.from_env env in |
| 1273 |
let name = Id.of_string "ltac2" in |
| 1274 |
Proof.start ~name ~poly:PolyFlags.default sigma [] |
| 1275 |
| Some pstate -> |
| 1276 |
Declare.Proof.get pstate |
| 1277 |
in |
| 1278 |
let (proof, _, ans) = Proof.run_tactic (Global.env ()) v proof in |
| 1279 |
let { Proof.sigma } = Proof.data proof in |
| 1280 |
let name = int_name () in |
| 1281 |
Feedback.msg_notice (str "- : " ++ pr_glbtype name (snd ty) |
| 1282 |
++ spc () ++ str "=" ++ spc () ++ |
| 1283 |
Tac2print.pr_valexpr env sigma ans (snd ty)) |
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
let warn_modtype = CWarnings.create ~name:"ltac2-in-modtype" ~category:CWarnings.CoreCategories.ltac2 ~default:AsError |
| 1288 |
Pp.(fun what -> strbrk "Ltac2 " ++ str what ++ strbrk " should not be defined inside module types: functor application to arguments of this module type will be unchecked") |
| 1289 |
|
| 1290 |
|
| 1291 |
let check_modtype what = |
| 1292 |
if Lib.is_modtype () |
| 1293 |
then warn_modtype what |
| 1294 |
|
| 1295 |
let abstract_att = Attributes.bool_attribute ~name:"abstract" |
| 1296 |
|
| 1297 |
let register_struct atts str = match str with |
| 1298 |
| StrVal (mut, isrec, e) -> |
| 1299 |
check_modtype "definitions"; |
| 1300 |
let deprecation, local = Attributes.(parse Notations.(deprecation ++ locality)) atts in |
| 1301 |
register_ltac ?deprecation ?local ~mut isrec e |
| 1302 |
| StrTyp (isrec, t) -> |
| 1303 |
check_modtype "types"; |
| 1304 |
let local, abstract = Attributes.(parse Notations.(locality ++ abstract_att)) atts in |
| 1305 |
register_type ?local ?abstract isrec t |
| 1306 |
| StrPrm (id, t, ml) -> |
| 1307 |
check_modtype "externals"; |
| 1308 |
let deprecation, local = Attributes.(parse Notations.(deprecation ++ locality)) atts in |
| 1309 |
register_primitive ?deprecation ?local id t ml |
| 1310 |
| StrMut (qid, old, e) -> |
| 1311 |
let local = Attributes.(parse explicit_hint_locality) atts in |
| 1312 |
register_redefinition ~local qid old e |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
let { Goptions.get = compact_bt } = |
| 1317 |
Goptions.declare_bool_option_and_ref ~key:["Ltac2";"Backtrace";"Compact"] ~value:true () |
| 1318 |
|
| 1319 |
let pr_frame = function |
| 1320 |
| FrAnon e -> |
| 1321 |
if compact_bt() then str "Call <fun>" |
| 1322 |
else str "Call {" ++ pr_glbexpr ~avoid:Id.Set.empty e ++ str "}" |
| 1323 |
| FrLtac kn -> |
| 1324 |
str "Call " ++ pr_tacref Id.Set.empty kn |
| 1325 |
| FrPrim ml -> |
| 1326 |
str "Prim <" ++ str ml.mltac_plugin ++ str ":" ++ str ml.mltac_tactic ++ str ">" |
| 1327 |
| FrExtn (tag, arg) -> |
| 1328 |
if compact_bt() then fmt "Extn <%s>" (Tac2dyn.Arg.repr tag) |
| 1329 |
else |
| 1330 |
let obj = Tac2env.interp_ml_object tag in |
| 1331 |
let env = Global.env () in |
| 1332 |
let sigma = Evd.from_env env in |
| 1333 |
str "Extn " ++ str (Tac2dyn.Arg.repr tag) ++ str ":" ++ spc () ++ |
| 1334 |
obj.Tac2env.ml_print env sigma arg |
| 1335 |
|
| 1336 |
let print_raw_error kn args = |
| 1337 |
let t_exn = KerName.make Tac2env.rocq_prefix (Id.of_string "exn") in |
| 1338 |
let v = Tac2ffi.of_open (kn, args) in |
| 1339 |
let t = GTypRef (Other t_exn, []) in |
| 1340 |
let c = Tac2print.pr_valexpr (Global.env ()) Evd.empty v t in |
| 1341 |
hov 0 (str "Uncaught Ltac2 exception:" ++ spc () ++ hov 0 c) |
| 1342 |
|
| 1343 |
let print_error kn args = |
| 1344 |
let env = Global.env() in |
| 1345 |
let sigma = Evd.from_env env in |
| 1346 |
let user_print = KerName.make Tac2quote.Refs.control_prefix (Id.of_string "print_exn") in |
| 1347 |
let user_print = Tac2interp.eval_global user_print in |
| 1348 |
let user_print = Tac2ffi.(to_fun1 of_exn (to_option to_pp)) user_print in |
| 1349 |
let user_print () = |
| 1350 |
let res, _, _, _, _ = |
| 1351 |
Proofview.apply ~name:(Id.of_string_soft "ltac2 error printing") ~poly:PolyFlags.default |
| 1352 |
env |
| 1353 |
(user_print (Tac2interp.LtacError (kn, args), Exninfo.null)) |
| 1354 |
(snd @@ Proofview.init sigma []) |
| 1355 |
in |
| 1356 |
res |
| 1357 |
in |
| 1358 |
match user_print() with |
| 1359 |
| Some msg -> msg |
| 1360 |
| None -> print_raw_error kn args |
| 1361 |
| exception e when CErrors.noncritical e -> |
| 1362 |
let e = Exninfo.capture e in |
| 1363 |
let ppe = match e with |
| 1364 |
| Tac2interp.LtacError (kn', args'), _info -> |
| 1365 |
|
| 1366 |
|
| 1367 |
print_raw_error kn' args' |
| 1368 |
| _ -> CErrors.iprint e |
| 1369 |
in |
| 1370 |
print_raw_error kn args ++ fnl() ++ |
| 1371 |
hov 2 (str "Custom Ltac2 printer failed:" ++ spc() ++ ppe) |
| 1372 |
|
| 1373 |
let () = register_handler begin function |
| 1374 |
| Tac2interp.LtacError (kn, args) -> Some (print_error kn args) |
| 1375 |
| _ -> None |
| 1376 |
end |
| 1377 |
|
| 1378 |
let () = CErrors.register_additional_error_info begin fun info -> |
| 1379 |
if !Tac2bt.print_ltac2_backtrace then |
| 1380 |
let bt = Exninfo.get info Tac2bt.backtrace in |
| 1381 |
match bt with |
| 1382 |
| None -> None |
| 1383 |
| Some bt -> |
| 1384 |
let bt = List.rev bt in |
| 1385 |
let bt = |
| 1386 |
str "Backtrace:" ++ fnl () ++ prlist_with_sep fnl pr_frame bt ++ fnl () |
| 1387 |
in |
| 1388 |
Some bt |
| 1389 |
else None |
| 1390 |
end |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
let print_constant ~print_def qid ?info data = |
| 1395 |
let e = data.Tac2env.gdata_expr in |
| 1396 |
let (_, t) = data.Tac2env.gdata_type in |
| 1397 |
let ismut = if data.gdata_mutable then spc() ++ str "(* mutable *)" else mt() in |
| 1398 |
let history = if not print_def then mt() |
| 1399 |
else match data.gdata_mutation_history with |
| 1400 |
| [] -> mt () |
| 1401 |
| mods -> |
| 1402 |
let pr_one mp = |
| 1403 |
let qid = try Nametab.shortest_qualid_of_module mp |
| 1404 |
with Not_found -> |
| 1405 |
try Nametab.shortest_qualid_of_dir (DirOpenModule mp) |
| 1406 |
with Not_found -> Nametab.shortest_qualid_of_dir (DirOpenModtype mp) |
| 1407 |
in |
| 1408 |
pr_qualid qid |
| 1409 |
in |
| 1410 |
let redef = prlist_with_sep fnl pr_one mods in |
| 1411 |
fnl () ++ str "Redefined by:" ++ fnl () ++ redef |
| 1412 |
in |
| 1413 |
let name = int_name () in |
| 1414 |
let def = if print_def then |
| 1415 |
fnl () ++ hov 2 |
| 1416 |
(pr_qualid qid ++ spc () ++ str ":=" ++ spc () ++ pr_glbexpr ~avoid:Id.Set.empty e) |
| 1417 |
else mt() |
| 1418 |
in |
| 1419 |
let info = match info with |
| 1420 |
| None -> mt() |
| 1421 |
| Some info -> fnl() ++ fnl() ++ hov 2 (str "Compiled as" ++ spc() ++ str info.Tac2env.source) |
| 1422 |
in |
| 1423 |
hov 0 ( |
| 1424 |
hov 2 (pr_qualid qid ++ spc () ++ str ":" ++ spc () ++ pr_glbtype name t ++ ismut) ++ def ++ info ++ history |
| 1425 |
) |
| 1426 |
|
| 1427 |
let print_type ~print_def qid kn = |
| 1428 |
let nparams, data = Tac2env.interp_type kn in |
| 1429 |
let name = int_name () in |
| 1430 |
let params = List.init nparams (fun i -> GTypVar i) in |
| 1431 |
let ty = match params with |
| 1432 |
| [] -> pr_qualid qid |
| 1433 |
| [t] -> pr_glbtype name t ++ spc() ++ pr_qualid qid |
| 1434 |
| _ -> surround (prlist_with_sep pr_comma (pr_glbtype name) params) ++ spc() ++ pr_qualid qid |
| 1435 |
in |
| 1436 |
let def = if not print_def || (match data with GTydDef None -> true | _ -> false) |
| 1437 |
then mt() |
| 1438 |
else spc() ++ str ":= " ++ match data with |
| 1439 |
| GTydDef None -> assert false |
| 1440 |
| GTydDef (Some t) -> pr_glbtype name t |
| 1441 |
| GTydAlg { galg_constructors = [] } -> str "[ ]" |
| 1442 |
| GTydAlg { galg_constructors = ctors } -> |
| 1443 |
let pr_ctor (_, id, argtys) = |
| 1444 |
|
| 1445 |
hov 0 |
| 1446 |
(Id.print id ++ if CList.is_empty argtys then mt() |
| 1447 |
else spc() ++surround (prlist_with_sep pr_comma (pr_glbtype name) argtys)) |
| 1448 |
in |
| 1449 |
hv 0 (str "[ " ++ prlist_with_sep (fun () -> spc() ++ str "| ") pr_ctor ctors ++ str " ]") |
| 1450 |
| GTydRec fields -> |
| 1451 |
let pr_field (id, ismut, t) = |
| 1452 |
hov 0 ((if ismut then str "mutable " else mt()) ++ Id.print id |
| 1453 |
++ spc() ++ str ": " ++ pr_glbtype name t) ++ str ";" |
| 1454 |
in |
| 1455 |
hv 2 (str "{ " ++ prlist_with_sep spc pr_field fields ++ str " }") |
| 1456 |
| GTydOpn -> |
| 1457 |
let ctors = KerName.Map.bindings (Tac2env.find_all_constructors_in_type kn) in |
| 1458 |
if CList.is_empty ctors then str "[ .. ]" |
| 1459 |
else |
| 1460 |
let pr_ctor (ckn, cdata) = |
| 1461 |
let argtys = cdata.Tac2env.cdata_args in |
| 1462 |
hov 0 |
| 1463 |
(Tac2print.pr_constructor ckn ++ if CList.is_empty argtys then mt() |
| 1464 |
else spc() ++surround (prlist_with_sep pr_comma (pr_glbtype name) argtys)) |
| 1465 |
in |
| 1466 |
hov 0 (str "[ .." ++ spc() ++ str "| " |
| 1467 |
++ prlist_with_sep (fun () -> spc() ++ str "| ") pr_ctor ctors |
| 1468 |
++ str " ]") |
| 1469 |
in |
| 1470 |
hov 2 (ty ++ def) |
| 1471 |
|
| 1472 |
let print_tacref ~print_def qid = function |
| 1473 |
| TacConstant kn -> |
| 1474 |
let data = Tac2env.interp_global kn in |
| 1475 |
let info = Option.map fst (Tac2env.get_compiled_global kn) in |
| 1476 |
print_constant ~print_def qid data ?info |
| 1477 |
| TacAlias kn -> |
| 1478 |
let { Tac2env.alias_body = body } = Tac2env.interp_alias kn in |
| 1479 |
str "Notation" ++ spc() ++ pr_qualid qid ++ str " :=" ++ spc() |
| 1480 |
++ Tac2print.pr_rawexpr_gen E5 ~avoid:Id.Set.empty body |
| 1481 |
|
| 1482 |
let print_constructor qid kn = |
| 1483 |
let cdata = Tac2env.interp_constructor kn in |
| 1484 |
let name = int_name () in |
| 1485 |
let ty = GTypRef (Other cdata.cdata_type, List.init cdata.cdata_prms (fun i -> GTypVar i)) in |
| 1486 |
let ty = List.fold_right (fun arg ty -> GTypArrow (arg,ty)) cdata.cdata_args ty in |
| 1487 |
pr_qualid qid ++ spc() ++ str ": " ++ Tac2print.pr_glbtype name ty |
| 1488 |
|
| 1489 |
let locatable_ltac2 = "Ltac2" |
| 1490 |
|
| 1491 |
type ltac2_object = |
| 1492 |
| Type of type_constant |
| 1493 |
| Constructor of ltac_constructor |
| 1494 |
| TacRef of tacref |
| 1495 |
|
| 1496 |
let locate_object qid = |
| 1497 |
try Type (Tac2env.locate_type qid) |
| 1498 |
with Not_found -> |
| 1499 |
try Constructor (Tac2env.locate_constructor qid) |
| 1500 |
with Not_found -> |
| 1501 |
TacRef (Tac2env.locate_ltac qid) |
| 1502 |
|
| 1503 |
let locate_all_object qid = |
| 1504 |
let open Tac2env in |
| 1505 |
(List.map (fun x -> Type x) (locate_extended_all_type qid)) |
| 1506 |
@ (List.map (fun x -> Constructor x) (locate_extended_all_constructor qid)) |
| 1507 |
@ (List.map (fun x -> TacRef x) (locate_extended_all_ltac qid)) |
| 1508 |
|
| 1509 |
let shortest_qualid_of_object = function |
| 1510 |
| Type kn -> Tac2env.shortest_qualid_of_type kn |
| 1511 |
| Constructor kn -> Tac2env.shortest_qualid_of_constructor kn |
| 1512 |
| TacRef kn -> Tac2env.shortest_qualid_of_ltac Id.Set.empty kn |
| 1513 |
|
| 1514 |
let path_of_object = function |
| 1515 |
| Type kn -> Tac2env.path_of_type kn |
| 1516 |
| Constructor kn -> Tac2env.path_of_constructor kn |
| 1517 |
| TacRef kn -> Tac2env.path_of_ltac kn |
| 1518 |
|
| 1519 |
let print_object ~print_def qid = function |
| 1520 |
| Type kn -> str "Ltac2 Type" ++ spc() ++ print_type ~print_def qid kn |
| 1521 |
| Constructor kn -> str "Ltac2 constructor" ++ spc() ++ print_constructor qid kn |
| 1522 |
| TacRef kn -> str "Ltac2 " ++ print_tacref ~print_def qid kn |
| 1523 |
|
| 1524 |
let () = |
| 1525 |
let open Prettyp in |
| 1526 |
let locate qid = try Some (qid, locate_object qid) with Not_found -> None in |
| 1527 |
let locate_all qid = List.map (fun x -> qid, x) (locate_all_object qid) in |
| 1528 |
let shortest_qualid (_,kn) = shortest_qualid_of_object kn in |
| 1529 |
let name (_,kn) = |
| 1530 |
let hdr = match kn with |
| 1531 |
| Type _ -> str "Ltac2 Type" |
| 1532 |
| TacRef (TacConstant _) -> str "Ltac2" |
| 1533 |
| TacRef (TacAlias _) -> str "Ltac2 Notation" |
| 1534 |
| Constructor _ -> str "Ltac2 Constructor" |
| 1535 |
in |
| 1536 |
hdr ++ spc () ++ pr_path (path_of_object kn) |
| 1537 |
in |
| 1538 |
let print (qid,kn) = print_object ~print_def:true qid kn in |
| 1539 |
let about (qid,kn) = print_object ~print_def:false qid kn in |
| 1540 |
register_locatable locatable_ltac2 { |
| 1541 |
locate; |
| 1542 |
locate_all; |
| 1543 |
shortest_qualid; |
| 1544 |
name; |
| 1545 |
print; |
| 1546 |
about; |
| 1547 |
} |
| 1548 |
|
| 1549 |
let print_located_tactic qid = |
| 1550 |
Feedback.msg_notice (Prettyp.print_located_other (Global.env ()) locatable_ltac2 qid) |
| 1551 |
|
| 1552 |
let print_ltac2 qid = |
| 1553 |
if Tac2env.is_constructor qid then |
| 1554 |
let kn = |
| 1555 |
try Tac2env.locate_constructor qid |
| 1556 |
with Not_found -> user_err ?loc:qid.CAst.loc (str "Unknown constructor " ++ pr_qualid qid) |
| 1557 |
in |
| 1558 |
Feedback.msg_notice (print_constructor qid kn) |
| 1559 |
else |
| 1560 |
let kn = |
| 1561 |
try Tac2env.locate_ltac qid |
| 1562 |
with Not_found -> user_err ?loc:qid.CAst.loc (str "Unknown tactic " ++ pr_qualid qid) |
| 1563 |
in |
| 1564 |
Feedback.msg_notice (print_tacref ~print_def:true qid kn) |
| 1565 |
|
| 1566 |
let print_ltac2_type qid = |
| 1567 |
match Tac2env.locate_type qid with |
| 1568 |
| exception Not_found -> user_err ?loc:qid.CAst.loc (str "Unknown Ltac2 type " ++ pr_qualid qid) |
| 1569 |
| kn -> |
| 1570 |
Feedback.msg_notice (print_type ~print_def:true qid kn) |
| 1571 |
|
| 1572 |
let print_signatures () = |
| 1573 |
let entries = KerName.Map.bindings (Tac2env.globals ()) in |
| 1574 |
let sort (kn1, _) (kn2, _) = KerName.compare kn1 kn2 in |
| 1575 |
let entries = List.sort sort entries in |
| 1576 |
let map (kn, entry) = |
| 1577 |
let qid = |
| 1578 |
try Some (Tac2env.shortest_qualid_of_ltac Id.Set.empty (TacConstant kn)) |
| 1579 |
with Not_found -> None |
| 1580 |
in |
| 1581 |
match qid with |
| 1582 |
| None -> None |
| 1583 |
| Some qid -> Some (qid, entry) |
| 1584 |
in |
| 1585 |
let entries = List.map_filter map entries in |
| 1586 |
let pr_entry (qid, data) = |
| 1587 |
hov 2 (print_constant ~print_def:false qid data) |
| 1588 |
in |
| 1589 |
Feedback.msg_notice (prlist_with_sep fnl pr_entry entries) |
| 1590 |
|
| 1591 |
let typecheck_expr e = |
| 1592 |
let e, (_,t) = Tac2intern.intern ~strict:false [] e in |
| 1593 |
let name = int_name() in |
| 1594 |
let pp = |
| 1595 |
pr_glbexpr_gen E5 ~avoid:Id.Set.empty e ++ spc() ++ |
| 1596 |
str ":" ++ spc() ++ pr_glbtype name t |
| 1597 |
in |
| 1598 |
Feedback.msg_notice pp |
| 1599 |
|
| 1600 |
let globalize_expr e = |
| 1601 |
let avoid = Id.Set.empty in |
| 1602 |
let e = Tac2intern.debug_globalize_allow_ext avoid e in |
| 1603 |
Feedback.msg_notice (Tac2print.pr_rawexpr_gen E5 ~avoid e) |
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
let ltac2_interp e = |
| 1608 |
let loc = e.loc in |
| 1609 |
let (e, t) = intern ~strict:false [] e in |
| 1610 |
let () = check_unit ?loc t in |
| 1611 |
let tac = Tac2interp.interp Tac2interp.empty_environment e in |
| 1612 |
Proofview.tclIGNORE tac |
| 1613 |
|
| 1614 |
let ComTactic.Interpreter ltac2_interp = ComTactic.register_tactic_interpreter "rocq-runtime.plugins.ltac2" ltac2_interp |
| 1615 |
|
| 1616 |
let call ~pstate g ~with_end_tac tac = |
| 1617 |
let g = Option.default (Goal_select.get_default_goal_selector()) g in |
| 1618 |
ComTactic.solve ~pstate ~with_end_tac g ~info:None (ltac2_interp tac) |
| 1619 |
|
| 1620 |
let call_par ~pstate tac = |
| 1621 |
ComTactic.solve_parallel ~pstate ~info:None (ltac2_interp tac) ~abstract:false |
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
let register_prim_alg name params def = |
| 1626 |
let id = Id.of_string name in |
| 1627 |
let def = List.map (fun (cstr, tpe) -> (None, Id.of_string_soft cstr, tpe)) def in |
| 1628 |
let getn (const, nonconst) (_, c, args) = match args with |
| 1629 |
| [] -> (succ const, nonconst) |
| 1630 |
| _ :: _ -> (const, succ nonconst) |
| 1631 |
in |
| 1632 |
let nconst, nnonconst = List.fold_left getn (0, 0) def in |
| 1633 |
let alg = { |
| 1634 |
galg_constructors = def; |
| 1635 |
galg_nconst = nconst; |
| 1636 |
galg_nnonconst = nnonconst; |
| 1637 |
} in |
| 1638 |
let def = (params, GTydAlg alg) in |
| 1639 |
let def = { typdef_local = false; typdef_abstract = false; typdef_expr = def } in |
| 1640 |
Lib.add_leaf (inTypDef id def) |
| 1641 |
|
| 1642 |
let rocq_def n = KerName.make Tac2env.rocq_prefix (Id.of_string n) |
| 1643 |
|
| 1644 |
let def_unit = { |
| 1645 |
typdef_local = false; |
| 1646 |
typdef_abstract = false; |
| 1647 |
typdef_expr = 0, GTydDef (Some (GTypRef (Tuple 0, []))); |
| 1648 |
} |
| 1649 |
|
| 1650 |
let t_list = rocq_def "list" |
| 1651 |
|
| 1652 |
let () = |
| 1653 |
let obj () = |
| 1654 |
let unit = Id.of_string "unit" in |
| 1655 |
Lib.add_leaf (inTypDef unit def_unit); |
| 1656 |
register_prim_alg "list" 1 [ |
| 1657 |
("[]", []); |
| 1658 |
("::", [GTypVar 0; GTypRef (Other t_list, [GTypVar 0])]); |
| 1659 |
]; |
| 1660 |
in |
| 1661 |
Mltop.(declare_cache_obj_full (interp_only_obj obj) "rocq-runtime.plugins.ltac2") |