| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Names |
| 13 |
open Constr |
| 14 |
open EConstr |
| 15 |
open Namegen |
| 16 |
open Tactypes |
| 17 |
open Genarg |
| 18 |
open Stdarg |
| 19 |
open Tacarg |
| 20 |
open Geninterp |
| 21 |
open Pp |
| 22 |
|
| 23 |
exception CannotCoerceTo of string |
| 24 |
|
| 25 |
let pr_argument_type arg = |
| 26 |
let Val.Dyn (tag, _) = arg in |
| 27 |
Val.pr tag |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
let pr_value env v = |
| 33 |
let ppty = spc() ++ str "of type" ++ spc () ++ pr_argument_type v in |
| 34 |
let pr_with_env pr = |
| 35 |
match env with |
| 36 |
| Some (env,sigma) -> pr env sigma ++ ppty |
| 37 |
| None -> str "a value" ++ ppty |
| 38 |
in |
| 39 |
let open Genprint in |
| 40 |
match generic_val_print v with |
| 41 |
| TopPrinterBasic pr -> pr () ++ ppty |
| 42 |
| TopPrinterNeedsContext pr -> pr_with_env pr |
| 43 |
| TopPrinterNeedsContextAndLevel { default_already_surrounded; printer } -> |
| 44 |
pr_with_env (fun env sigma -> printer env sigma default_already_surrounded) |
| 45 |
|
| 46 |
|
| 47 |
type appl = |
| 48 |
| UnnamedAppl |
| 49 |
| GlbAppl of (Names.KerName.t * Val.t list) list |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
type tacvalue = |
| 54 |
| VFun of |
| 55 |
appl * |
| 56 |
Tacexpr.ltac_trace * |
| 57 |
Loc.t option * |
| 58 |
Val.t Id.Map.t * |
| 59 |
Name.t list * |
| 60 |
Tacexpr.glob_tactic_expr |
| 61 |
| VRec of Val.t Id.Map.t ref * Tacexpr.glob_tactic_expr |
| 62 |
|
| 63 |
let tacvalue_tag : tacvalue Val.typ = |
| 64 |
let tag = Val.create "tacvalue" in |
| 65 |
let pr = function |
| 66 |
| VFun (a,_,loc,ids,l,tac) -> |
| 67 |
let tac = if List.is_empty l then tac else CAst.make ?loc @@ Tacexpr.TacFun (l,tac) in |
| 68 |
let pr_env env sigma = if Id.Map.is_empty ids then mt () else cut () ++ str "where" ++ Id.Map.fold (fun id c pp -> cut () ++ Id.print id ++ str " := " ++ pr_value (Some (env,sigma)) c ++ pp) ids (mt ()) in |
| 69 |
Genprint.TopPrinterNeedsContext (fun env sigma -> v 0 (hov 0 (Pptactic.pr_glob_tactic env tac) ++ pr_env env sigma)) |
| 70 |
| _ -> Genprint.TopPrinterBasic (fun _ -> str "<tactic closure>") in |
| 71 |
let () = Genprint.register_val_print0 tag pr in |
| 72 |
tag |
| 73 |
|
| 74 |
let constr_context_tag : Constr_matching.context Val.typ = |
| 75 |
let tag = Val.create "constr_context" in |
| 76 |
let pr env sigma lev c : Pp.t = Printer.pr_econstr_n_env env sigma lev (Constr_matching.repr_context c) in |
| 77 |
let () = Genprint.register_val_print0 tag (Pptactic.make_constr_printer pr) in |
| 78 |
tag |
| 79 |
|
| 80 |
|
| 81 |
let constr_under_binders_tag : Ltac_pretype.constr_under_binders Val.typ = |
| 82 |
let tag = Val.create "constr_under_binders" in |
| 83 |
let () = Genprint.register_val_print0 tag (fun c -> |
| 84 |
Genprint.TopPrinterNeedsContext (fun env sigma -> Printer.pr_constr_under_binders_env env sigma c)) in |
| 85 |
tag |
| 86 |
|
| 87 |
|
| 88 |
let val_tag wit = match val_tag wit with |
| 89 |
| Val.Base t -> t |
| 90 |
| _ -> assert false |
| 91 |
|
| 92 |
let has_type : type a. Val.t -> a typed_abstract_argument_type -> bool = fun v wit -> |
| 93 |
let Val.Dyn (t, _) = v in |
| 94 |
match Val.eq t (val_tag wit) with |
| 95 |
| None -> false |
| 96 |
| Some Refl -> true |
| 97 |
|
| 98 |
let prj : type a. a Val.typ -> Val.t -> a option = fun t v -> |
| 99 |
let Val.Dyn (t', x) = v in |
| 100 |
match Val.eq t t' with |
| 101 |
| None -> None |
| 102 |
| Some Refl -> Some x |
| 103 |
|
| 104 |
let in_gen wit v = Val.Dyn (val_tag wit, v) |
| 105 |
let out_gen wit v = match prj (val_tag wit) v with None -> assert false | Some x -> x |
| 106 |
|
| 107 |
module Value = |
| 108 |
struct |
| 109 |
|
| 110 |
type t = Val.t |
| 111 |
|
| 112 |
let of_tacvalue v = Val.Dyn (tacvalue_tag, v) |
| 113 |
|
| 114 |
let to_tacvalue v = prj tacvalue_tag v |
| 115 |
|
| 116 |
let of_constr c = in_gen (topwit wit_constr) c |
| 117 |
|
| 118 |
let to_constr v = |
| 119 |
if has_type v (topwit wit_constr) then |
| 120 |
let c = out_gen (topwit wit_constr) v in |
| 121 |
Some c |
| 122 |
else match prj constr_under_binders_tag v with |
| 123 |
| Some (vars, c) -> |
| 124 |
begin match vars with [] -> Some c | _ -> None end |
| 125 |
| None -> None |
| 126 |
|
| 127 |
let of_uconstr c = in_gen (topwit wit_uconstr) c |
| 128 |
|
| 129 |
let to_uconstr v = |
| 130 |
if has_type v (topwit wit_uconstr) then |
| 131 |
Some (out_gen (topwit wit_uconstr) v) |
| 132 |
else None |
| 133 |
|
| 134 |
let of_constr_context v = Val.Dyn (constr_context_tag, v) |
| 135 |
|
| 136 |
let to_constr_context v = prj constr_context_tag v |
| 137 |
|
| 138 |
|
| 139 |
let of_constr_under_binders v = Val.Dyn (constr_under_binders_tag, v) |
| 140 |
|
| 141 |
let of_int i = in_gen (topwit wit_int) i |
| 142 |
|
| 143 |
let to_int v = |
| 144 |
if has_type v (topwit wit_int) then |
| 145 |
Some (out_gen (topwit wit_int) v) |
| 146 |
else None |
| 147 |
|
| 148 |
let of_ident id = in_gen (topwit wit_ident) id |
| 149 |
|
| 150 |
let to_ident v = |
| 151 |
if has_type v (topwit wit_ident) then |
| 152 |
Some (out_gen (topwit wit_ident) v) |
| 153 |
else None |
| 154 |
|
| 155 |
let to_list v = prj Val.typ_list v |
| 156 |
|
| 157 |
let to_option v = prj Val.typ_opt v |
| 158 |
|
| 159 |
let to_pair v = prj Val.typ_pair v |
| 160 |
|
| 161 |
let cast_error wit v = |
| 162 |
let pr_v = Pptactic.pr_value Pptactic.ltop v in |
| 163 |
let Val.Dyn (tag, _) = v in |
| 164 |
let tag = Val.pr tag in |
| 165 |
CErrors.user_err (str "Type error: value " ++ pr_v ++ str " is a " ++ tag |
| 166 |
++ str " while type " ++ Val.pr wit ++ str " was expected.") |
| 167 |
|
| 168 |
let unbox wit v ans = match ans with |
| 169 |
| None -> cast_error wit v |
| 170 |
| Some x -> x |
| 171 |
|
| 172 |
let rec prj : type a. a Val.tag -> Val.t -> a = fun tag v -> match tag with |
| 173 |
| Val.List tag -> List.map (fun v -> prj tag v) (unbox Val.typ_list v (to_list v)) |
| 174 |
| Val.Opt tag -> Option.map (fun v -> prj tag v) (unbox Val.typ_opt v (to_option v)) |
| 175 |
| Val.Pair (tag1, tag2) -> |
| 176 |
let (x, y) = unbox Val.typ_pair v (to_pair v) in |
| 177 |
(prj tag1 x, prj tag2 y) |
| 178 |
| Val.Base t -> |
| 179 |
let Val.Dyn (t', x) = v in |
| 180 |
match Val.eq t t' with |
| 181 |
| None -> cast_error t v |
| 182 |
| Some Refl -> x |
| 183 |
let rec tag_of_arg : type a b c. (a, b, c) genarg_type -> c Val.tag = fun wit -> match wit with |
| 184 |
| ExtraArg _ -> Geninterp.val_tag (topwit wit) |
| 185 |
| ListArg t -> Val.List (tag_of_arg t) |
| 186 |
| OptArg t -> Val.Opt (tag_of_arg t) |
| 187 |
| PairArg (t1, t2) -> Val.Pair (tag_of_arg t1, tag_of_arg t2) |
| 188 |
|
| 189 |
let val_cast arg v = prj (tag_of_arg arg) v |
| 190 |
|
| 191 |
let cast (Topwit wit) v = val_cast wit v |
| 192 |
|
| 193 |
end |
| 194 |
|
| 195 |
let is_variable env id = |
| 196 |
Id.List.mem id (Termops.ids_of_named_context (Environ.named_context env)) |
| 197 |
|
| 198 |
|
| 199 |
let constr_of_id env id = |
| 200 |
EConstr.mkVar (let _ = Environ.lookup_named id env in id) |
| 201 |
|
| 202 |
|
| 203 |
let coerce_to_constr_context v = |
| 204 |
match Value.to_constr_context v with |
| 205 |
| Some v -> v |
| 206 |
| None -> raise (CannotCoerceTo "a term context") |
| 207 |
|
| 208 |
let is_intro_pattern v = |
| 209 |
if has_type v (topwit wit_intropattern) then |
| 210 |
Some (out_gen (topwit wit_intropattern) v).CAst.v |
| 211 |
else |
| 212 |
None |
| 213 |
|
| 214 |
|
| 215 |
let coerce_var_to_ident fresh env sigma v = |
| 216 |
let fail () = raise (CannotCoerceTo "a fresh identifier") in |
| 217 |
match is_intro_pattern v with |
| 218 |
| Some (IntroNaming (IntroIdentifier id)) -> id |
| 219 |
| Some _ -> fail () |
| 220 |
| None -> |
| 221 |
if has_type v (topwit wit_intropattern) then |
| 222 |
match out_gen (topwit wit_intropattern) v with |
| 223 |
| { CAst.v=IntroNaming (IntroIdentifier id)} -> id |
| 224 |
| _ -> fail () |
| 225 |
else if has_type v (topwit wit_hyp) then |
| 226 |
out_gen (topwit wit_hyp) v |
| 227 |
else match Value.to_constr v with |
| 228 |
| None -> fail () |
| 229 |
| Some c -> |
| 230 |
|
| 231 |
if isVar sigma c && not (fresh && is_variable env (destVar sigma c)) then |
| 232 |
destVar sigma c |
| 233 |
else fail () |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
let coerce_to_ident_not_fresh sigma v = |
| 240 |
let fail () = raise (CannotCoerceTo "an identifier") in |
| 241 |
match is_intro_pattern v with |
| 242 |
| Some (IntroNaming (IntroIdentifier id)) -> id |
| 243 |
| Some _ -> fail () |
| 244 |
| None -> |
| 245 |
if has_type v (topwit wit_hyp) then |
| 246 |
out_gen (topwit wit_hyp) v |
| 247 |
else |
| 248 |
match Value.to_constr v with |
| 249 |
| None -> fail () |
| 250 |
| Some c -> |
| 251 |
match EConstr.kind sigma c with |
| 252 |
| Var id -> id |
| 253 |
| Meta m -> Id.of_string "x" |
| 254 |
| Evar (kn,_) -> |
| 255 |
begin match Evd.evar_ident kn sigma with |
| 256 |
| None -> fail () |
| 257 |
| Some id -> |
| 258 |
let (dp, id) = Libnames.repr_path id in |
| 259 |
if DirPath.is_empty dp then id else fail () |
| 260 |
end |
| 261 |
| Const (cst,_) -> Constant.label cst |
| 262 |
| Construct (cstr,_) -> |
| 263 |
let ref = GlobRef.ConstructRef cstr in |
| 264 |
let basename = Nametab.basename_of_global ref in |
| 265 |
basename |
| 266 |
| Ind (ind,_) -> |
| 267 |
let ref = GlobRef.IndRef ind in |
| 268 |
let basename = Nametab.basename_of_global ref in |
| 269 |
basename |
| 270 |
| Sort s -> |
| 271 |
begin |
| 272 |
match ESorts.kind sigma s with |
| 273 |
| Sorts.SProp -> Id.of_string "SProp" |
| 274 |
| Sorts.Prop -> Id.of_string "Prop" |
| 275 |
| Sorts.Set -> Id.of_string "Set" |
| 276 |
| Sorts.Type _ | Sorts.QSort _ -> Id.of_string "Type" |
| 277 |
end |
| 278 |
| _ -> fail() |
| 279 |
|
| 280 |
|
| 281 |
let coerce_to_intro_pattern sigma v = |
| 282 |
match is_intro_pattern v with |
| 283 |
| Some pat -> pat |
| 284 |
| None -> |
| 285 |
if has_type v (topwit wit_hyp) then |
| 286 |
let id = out_gen (topwit wit_hyp) v in |
| 287 |
IntroNaming (IntroIdentifier id) |
| 288 |
else match Value.to_constr v with |
| 289 |
| Some c when isVar sigma c -> |
| 290 |
|
| 291 |
|
| 292 |
IntroNaming (IntroIdentifier (destVar sigma c)) |
| 293 |
| _ -> raise (CannotCoerceTo "an introduction pattern") |
| 294 |
|
| 295 |
let coerce_to_intro_pattern_naming sigma v = |
| 296 |
match coerce_to_intro_pattern sigma v with |
| 297 |
| IntroNaming pat -> pat |
| 298 |
| _ -> raise (CannotCoerceTo "a naming introduction pattern") |
| 299 |
|
| 300 |
let coerce_to_hint_base v = |
| 301 |
match is_intro_pattern v with |
| 302 |
| Some (IntroNaming (IntroIdentifier id)) -> Id.to_string id |
| 303 |
| Some _ | None -> raise (CannotCoerceTo "a hint base name") |
| 304 |
|
| 305 |
let coerce_to_int v = |
| 306 |
if has_type v (topwit wit_int) then |
| 307 |
out_gen (topwit wit_int) v |
| 308 |
else raise (CannotCoerceTo "an integer") |
| 309 |
|
| 310 |
let coerce_to_constr env v = |
| 311 |
let fail () = raise (CannotCoerceTo "a term") in |
| 312 |
match is_intro_pattern v with |
| 313 |
| Some (IntroNaming (IntroIdentifier id)) -> |
| 314 |
(try ([], constr_of_id env id) with Not_found -> fail ()) |
| 315 |
| Some _ -> fail () |
| 316 |
| None -> |
| 317 |
if has_type v (topwit wit_constr) then |
| 318 |
let c = out_gen (topwit wit_constr) v in |
| 319 |
([], c) |
| 320 |
else match prj constr_under_binders_tag v with |
| 321 |
| Some v -> v |
| 322 |
| None -> |
| 323 |
if has_type v (topwit wit_hyp) then |
| 324 |
let id = out_gen (topwit wit_hyp) v in |
| 325 |
(try [], constr_of_id env id with Not_found -> fail ()) |
| 326 |
else fail () |
| 327 |
|
| 328 |
let coerce_to_uconstr v = |
| 329 |
if has_type v (topwit wit_uconstr) then |
| 330 |
out_gen (topwit wit_uconstr) v |
| 331 |
else |
| 332 |
raise (CannotCoerceTo "an untyped term") |
| 333 |
|
| 334 |
let coerce_to_closed_constr env v = |
| 335 |
let ids,c = coerce_to_constr env v in |
| 336 |
let () = if not (List.is_empty ids) then raise (CannotCoerceTo "a term") in |
| 337 |
c |
| 338 |
|
| 339 |
let coerce_to_evaluable_ref env sigma v = |
| 340 |
let open Evaluable in |
| 341 |
let fail () = raise (CannotCoerceTo "an evaluable reference") in |
| 342 |
let ev = |
| 343 |
match is_intro_pattern v with |
| 344 |
| Some (IntroNaming (IntroIdentifier id)) when is_variable env id -> EvalVarRef id |
| 345 |
| Some _ -> fail () |
| 346 |
| None -> |
| 347 |
if has_type v (topwit wit_hyp) then |
| 348 |
let id = out_gen (topwit wit_hyp) v in |
| 349 |
if Id.List.mem id (Termops.ids_of_context env) then EvalVarRef id |
| 350 |
else fail () |
| 351 |
else if has_type v (topwit wit_ref) then |
| 352 |
let open GlobRef in |
| 353 |
let r = out_gen (topwit wit_ref) v in |
| 354 |
match r with |
| 355 |
| VarRef var -> EvalVarRef var |
| 356 |
| ConstRef c -> EvalConstRef c |
| 357 |
| IndRef _ | ConstructRef _ -> fail () |
| 358 |
else if has_type v (topwit wit_smart_global) then |
| 359 |
let open GlobRef in |
| 360 |
let r = out_gen (topwit wit_smart_global) v in |
| 361 |
match r with |
| 362 |
| VarRef var -> EvalVarRef var |
| 363 |
| ConstRef c -> EvalConstRef c |
| 364 |
| IndRef _ | ConstructRef _ -> fail () |
| 365 |
else |
| 366 |
match Value.to_constr v with |
| 367 |
| Some c when isConst sigma c -> EvalConstRef (fst (destConst sigma c)) |
| 368 |
| Some c when isVar sigma c -> EvalVarRef (destVar sigma c) |
| 369 |
| _ -> fail () |
| 370 |
in if Tacred.is_evaluable env sigma ev then ev else fail () |
| 371 |
|
| 372 |
let coerce_to_constr_list env v = |
| 373 |
let v = Value.to_list v in |
| 374 |
match v with |
| 375 |
| Some l -> |
| 376 |
let map v = coerce_to_closed_constr env v in |
| 377 |
List.map map l |
| 378 |
| None -> raise (CannotCoerceTo "a term list") |
| 379 |
|
| 380 |
let coerce_to_intro_pattern_list ?loc sigma v = |
| 381 |
match Value.to_list v with |
| 382 |
| None -> raise (CannotCoerceTo "an intro pattern list") |
| 383 |
| Some l -> |
| 384 |
let map v = CAst.make ?loc @@ coerce_to_intro_pattern sigma v in |
| 385 |
List.map map l |
| 386 |
|
| 387 |
let coerce_to_hyp env sigma v = |
| 388 |
let fail () = raise (CannotCoerceTo "a variable") in |
| 389 |
match is_intro_pattern v with |
| 390 |
| Some (IntroNaming (IntroIdentifier id)) when is_variable env id -> id |
| 391 |
| Some _ -> fail () |
| 392 |
| None -> |
| 393 |
if has_type v (topwit wit_hyp) then |
| 394 |
let id = out_gen (topwit wit_hyp) v in |
| 395 |
if is_variable env id then id else fail () |
| 396 |
else match Value.to_constr v with |
| 397 |
| Some c when isVar sigma c -> destVar sigma c |
| 398 |
| _ -> fail () |
| 399 |
|
| 400 |
let coerce_to_hyp_list env sigma v = |
| 401 |
let v = Value.to_list v in |
| 402 |
match v with |
| 403 |
| Some l -> |
| 404 |
let map n = coerce_to_hyp env sigma n in |
| 405 |
List.map map l |
| 406 |
| None -> raise (CannotCoerceTo "a variable list") |
| 407 |
|
| 408 |
|
| 409 |
let coerce_to_reference sigma v = |
| 410 |
match Value.to_constr v with |
| 411 |
| Some c -> |
| 412 |
begin |
| 413 |
try fst (EConstr.destRef sigma c) |
| 414 |
with DestKO -> raise (CannotCoerceTo "a reference") |
| 415 |
end |
| 416 |
| None -> raise (CannotCoerceTo "a reference") |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
let coerce_to_quantified_hypothesis sigma v = |
| 421 |
match is_intro_pattern v with |
| 422 |
| Some (IntroNaming (IntroIdentifier id)) -> NamedHyp (CAst.make id) |
| 423 |
| Some _ -> raise (CannotCoerceTo "a quantified hypothesis") |
| 424 |
| None -> |
| 425 |
if has_type v (topwit wit_hyp) then |
| 426 |
let id = out_gen (topwit wit_hyp) v in |
| 427 |
NamedHyp (CAst.make id) |
| 428 |
else if has_type v (topwit wit_int) then |
| 429 |
AnonHyp (out_gen (topwit wit_int) v) |
| 430 |
else match Value.to_constr v with |
| 431 |
| Some c when isVar sigma c -> NamedHyp (CAst.make @@ destVar sigma c) |
| 432 |
| _ -> raise (CannotCoerceTo "a quantified hypothesis") |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
let coerce_to_decl_or_quant_hyp sigma v = |
| 437 |
if has_type v (topwit wit_int) then |
| 438 |
AnonHyp (out_gen (topwit wit_int) v) |
| 439 |
else |
| 440 |
try coerce_to_quantified_hypothesis sigma v |
| 441 |
with CannotCoerceTo _ -> |
| 442 |
raise (CannotCoerceTo "a declared or quantified hypothesis") |
| 443 |
|
| 444 |
let coerce_to_int_list v = |
| 445 |
match Value.to_list v with |
| 446 |
| None -> raise (CannotCoerceTo "an int list") |
| 447 |
| Some l -> |
| 448 |
List.map coerce_to_int l |
| 449 |
|
| 450 |
exception CoercionError of Id.t * (Environ.env * Evd.evar_map) option * Val.t * string |
| 451 |
|
| 452 |
let () = CErrors.register_handler begin function |
| 453 |
| CoercionError (id, env, v, s) -> |
| 454 |
Some (str "Ltac variable " ++ Id.print id ++ |
| 455 |
strbrk " is bound to" ++ spc () ++ pr_value env v ++ spc () ++ |
| 456 |
strbrk "which cannot be coerced to " ++ str s ++ str".") |
| 457 |
| _ -> None |
| 458 |
end |
| 459 |
|
| 460 |
let error_ltac_variable ?loc id env v s = |
| 461 |
Loc.raise ?loc (CoercionError (id, env, v, s)) |