| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Constrintern |
| 12 |
open Patternops |
| 13 |
open Pp |
| 14 |
open CAst |
| 15 |
open Namegen |
| 16 |
open Glob_term |
| 17 |
open Glob_ops |
| 18 |
open Tacred |
| 19 |
open CErrors |
| 20 |
open Util |
| 21 |
open Names |
| 22 |
open Nameops |
| 23 |
open Libnames |
| 24 |
open Tactic_debug |
| 25 |
open Constrexpr |
| 26 |
open Termops |
| 27 |
open Tacexpr |
| 28 |
open Genarg |
| 29 |
open Geninterp |
| 30 |
open Stdarg |
| 31 |
open Tacarg |
| 32 |
open Printer |
| 33 |
open Pretyping |
| 34 |
open Tactypes |
| 35 |
open Tactics |
| 36 |
open Locus |
| 37 |
open Tacintern |
| 38 |
open Taccoerce |
| 39 |
open Proofview.Notations |
| 40 |
open Context.Named.Declaration |
| 41 |
open Ltac_pretype |
| 42 |
|
| 43 |
let do_profile trace ?count_call tac = |
| 44 |
Profile_tactic.do_profile_gen (function |
| 45 |
| (_, c) :: _ -> Some (Pptactic.pp_ltac_call_kind c) |
| 46 |
| [] -> None) |
| 47 |
trace ?count_call tac |
| 48 |
|
| 49 |
let ltac_trace_info = Tactic_debug.ltac_trace_info |
| 50 |
|
| 51 |
let has_type : type a. Val.t -> a typed_abstract_argument_type -> bool = fun v wit -> |
| 52 |
let Val.Dyn (t, _) = v in |
| 53 |
let t' = match val_tag wit with |
| 54 |
| Val.Base t' -> t' |
| 55 |
| _ -> assert false |
| 56 |
in |
| 57 |
match Val.eq t t' with |
| 58 |
| None -> false |
| 59 |
| Some Refl -> true |
| 60 |
|
| 61 |
let prj : type a. a Val.typ -> Val.t -> a option = fun t v -> |
| 62 |
let Val.Dyn (t', x) = v in |
| 63 |
match Val.eq t t' with |
| 64 |
| None -> None |
| 65 |
| Some Refl -> Some x |
| 66 |
|
| 67 |
let in_list tag v = |
| 68 |
let tag = match tag with Val.Base tag -> tag | _ -> assert false in |
| 69 |
Val.Dyn (Val.typ_list, List.map (fun x -> Val.Dyn (tag, x)) v) |
| 70 |
let in_gen wit v = |
| 71 |
let t = match val_tag wit with |
| 72 |
| Val.Base t -> t |
| 73 |
| _ -> assert false |
| 74 |
in |
| 75 |
Val.Dyn (t, v) |
| 76 |
let out_gen wit v = |
| 77 |
let t = match val_tag wit with |
| 78 |
| Val.Base t -> t |
| 79 |
| _ -> assert false |
| 80 |
in |
| 81 |
match prj t v with None -> assert false | Some x -> x |
| 82 |
|
| 83 |
let val_tag wit = val_tag (topwit wit) |
| 84 |
|
| 85 |
let pr_argument_type arg = |
| 86 |
let Val.Dyn (tag, _) = arg in |
| 87 |
Val.pr tag |
| 88 |
|
| 89 |
type value = Val.t |
| 90 |
|
| 91 |
let push_appl appl args = |
| 92 |
match appl with |
| 93 |
| UnnamedAppl -> UnnamedAppl |
| 94 |
| GlbAppl l -> GlbAppl (List.map (fun (h,vs) -> (h,vs@args)) l) |
| 95 |
let pr_generic arg = |
| 96 |
let Val.Dyn (tag, _) = arg in |
| 97 |
str"<" ++ Val.pr tag ++ str ":(" ++ Pptactic.pr_value Pptactic.ltop arg ++ str ")>" |
| 98 |
let pr_appl h vs = |
| 99 |
Pptactic.pr_ltac_constant h ++ spc () ++ |
| 100 |
Pp.prlist_with_sep spc pr_generic vs |
| 101 |
let rec name_with_list appl t = |
| 102 |
match appl with |
| 103 |
| [] -> t |
| 104 |
| (h,vs)::l -> Proofview.Trace.name_tactic (fun () -> pr_appl h vs) (name_with_list l t) |
| 105 |
let name_if_glob appl t = |
| 106 |
match appl with |
| 107 |
| UnnamedAppl -> t |
| 108 |
| GlbAppl l -> name_with_list l t |
| 109 |
let combine_appl appl1 appl2 = |
| 110 |
match appl1,appl2 with |
| 111 |
| UnnamedAppl,a | a,UnnamedAppl -> a |
| 112 |
| GlbAppl l1 , GlbAppl l2 -> GlbAppl (l2@l1) |
| 113 |
|
| 114 |
let of_tacvalue = Value.of_tacvalue |
| 115 |
let to_tacvalue = Value.to_tacvalue |
| 116 |
|
| 117 |
|
| 118 |
let debug = ref DebugOff |
| 119 |
|
| 120 |
|
| 121 |
let set_debug pos = debug := pos |
| 122 |
|
| 123 |
|
| 124 |
let get_debug () = if Flags.async_proofs_is_worker () then DebugOff else !debug |
| 125 |
|
| 126 |
let log_trace = ref false |
| 127 |
|
| 128 |
let is_traced () = |
| 129 |
!log_trace || !debug <> DebugOff || Profile_tactic.get_profiling() |
| 130 |
|
| 131 |
|
| 132 |
let name_vfun appl vle = |
| 133 |
match to_tacvalue vle with |
| 134 |
| Some (VFun (appl0,trace,loc,lfun,vars,t)) -> |
| 135 |
of_tacvalue (VFun (combine_appl appl0 appl,trace,loc,lfun,vars,t)) |
| 136 |
| Some (VRec _) | None -> vle |
| 137 |
|
| 138 |
module TacStore = Geninterp.TacStore |
| 139 |
|
| 140 |
let f_avoid_ids : Id.Set.t TacStore.field = TacStore.field "f_avoid_ids" |
| 141 |
|
| 142 |
let f_debug : debug_info TacStore.field = TacStore.field "f_debug" |
| 143 |
let f_trace : ltac_trace TacStore.field = TacStore.field "f_trace" |
| 144 |
let f_loc : Loc.t TacStore.field = TacStore.field "f_loc" |
| 145 |
|
| 146 |
|
| 147 |
type interp_sign = Geninterp.interp_sign = |
| 148 |
{ lfun : value Id.Map.t |
| 149 |
; poly : PolyFlags.t |
| 150 |
; extra : TacStore.t } |
| 151 |
|
| 152 |
let add_extra_trace trace extra = TacStore.set extra f_trace trace |
| 153 |
let extract_trace ist = |
| 154 |
if is_traced () then match TacStore.get ist.extra f_trace with |
| 155 |
| None -> [],[] |
| 156 |
| Some trace -> trace |
| 157 |
else [],[] |
| 158 |
|
| 159 |
let add_extra_loc loc extra = |
| 160 |
match loc with |
| 161 |
| None -> extra |
| 162 |
| Some loc -> TacStore.set extra f_loc loc |
| 163 |
let extract_loc ist = TacStore.get ist.extra f_loc |
| 164 |
|
| 165 |
let ensure_loc loc ist = |
| 166 |
match loc with |
| 167 |
| None -> ist |
| 168 |
| Some loc -> |
| 169 |
match extract_loc ist with |
| 170 |
| None -> { ist with extra = TacStore.set ist.extra f_loc loc } |
| 171 |
| Some _ -> ist |
| 172 |
|
| 173 |
let print_top_val env v = Pptactic.pr_value Pptactic.ltop v |
| 174 |
|
| 175 |
let catching_error call_trace fail (e, info) = |
| 176 |
let inner_trace = |
| 177 |
Option.default [] (Exninfo.get info ltac_trace_info) |
| 178 |
in |
| 179 |
if List.is_empty call_trace && List.is_empty inner_trace then fail (e, info) |
| 180 |
else begin |
| 181 |
assert (CErrors.noncritical e); |
| 182 |
let inner_trace = List.filter (fun i -> not (List.memq i call_trace)) inner_trace in |
| 183 |
let new_trace = inner_trace @ call_trace in |
| 184 |
let located_exc = (e, Exninfo.add info ltac_trace_info new_trace) in |
| 185 |
fail located_exc |
| 186 |
end |
| 187 |
|
| 188 |
let update_loc loc (e, info as e') = |
| 189 |
let eloc = Loc.get_loc info in |
| 190 |
if Loc.finer eloc (Some loc) then e' |
| 191 |
else |
| 192 |
(e, Loc.add_loc info loc) |
| 193 |
|
| 194 |
let catch_error_with_trace_loc loc call_trace f x = |
| 195 |
try f x |
| 196 |
with e when CErrors.noncritical e -> |
| 197 |
let e = Exninfo.capture e in |
| 198 |
let e = Option.cata (fun loc -> update_loc loc e) e loc in |
| 199 |
catching_error call_trace Exninfo.iraise e |
| 200 |
|
| 201 |
let catch_error_loc loc tac = |
| 202 |
match loc with |
| 203 |
| None -> tac |
| 204 |
| Some loc -> |
| 205 |
Proofview.tclORELSE tac (fun exn -> |
| 206 |
let (e, info) = update_loc loc exn in |
| 207 |
Proofview.tclZERO ~info e) |
| 208 |
|
| 209 |
let wrap_error tac k = |
| 210 |
if is_traced () then Proofview.tclORELSE tac k else tac |
| 211 |
|
| 212 |
let wrap_error_loc loc tac k = |
| 213 |
if is_traced () then |
| 214 |
let k = match loc with |
| 215 |
| None -> k |
| 216 |
| Some loc -> fun e -> k (update_loc loc e) |
| 217 |
in |
| 218 |
Proofview.tclORELSE tac k |
| 219 |
else catch_error_loc loc tac |
| 220 |
|
| 221 |
let catch_error_tac call_trace tac = |
| 222 |
wrap_error |
| 223 |
tac |
| 224 |
(catching_error call_trace (fun (e, info) -> Proofview.tclZERO ~info e)) |
| 225 |
|
| 226 |
let catch_error_tac_loc loc call_trace tac = |
| 227 |
wrap_error_loc loc |
| 228 |
tac |
| 229 |
(catching_error call_trace (fun (e, info) -> Proofview.tclZERO ~info e)) |
| 230 |
|
| 231 |
let curr_debug ist = match TacStore.get ist.extra f_debug with |
| 232 |
| None -> DebugOff |
| 233 |
| Some level -> level |
| 234 |
|
| 235 |
let pr_closure env ist body = |
| 236 |
let pp_body = Pptactic.pr_glob_tactic env body in |
| 237 |
let pr_sep () = fnl () in |
| 238 |
let pr_iarg (id, arg) = |
| 239 |
let arg = pr_argument_type arg in |
| 240 |
hov 0 (Id.print id ++ spc () ++ str ":" ++ spc () ++ arg) |
| 241 |
in |
| 242 |
let pp_iargs = v 0 (prlist_with_sep pr_sep pr_iarg (Id.Map.bindings ist)) in |
| 243 |
pp_body ++ fnl() ++ str "in environment " ++ fnl() ++ pp_iargs |
| 244 |
|
| 245 |
let pr_inspect env expr result = |
| 246 |
let pp_expr = Pptactic.pr_glob_tactic env expr in |
| 247 |
let pp_result = |
| 248 |
match to_tacvalue result with |
| 249 |
| Some (VFun (_, _, _, ist, ul, b)) -> |
| 250 |
let body = if List.is_empty ul then b else CAst.make (TacFun (ul, b)) in |
| 251 |
str "a closure with body " ++ fnl() ++ pr_closure env ist body |
| 252 |
| Some (VRec (ist, body)) -> |
| 253 |
str "a recursive closure" ++ fnl () ++ pr_closure env !ist body |
| 254 |
| None -> |
| 255 |
let pp_type = pr_argument_type result in |
| 256 |
str "an object of type" ++ spc () ++ pp_type |
| 257 |
in |
| 258 |
pp_expr ++ fnl() ++ str "this is " ++ pp_result |
| 259 |
|
| 260 |
|
| 261 |
let constr_of_id env id = |
| 262 |
EConstr.mkVar (let _ = Environ.lookup_named id env in id) |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
let push_trace call ist = |
| 267 |
if is_traced () then match TacStore.get ist.extra f_trace with |
| 268 |
| None -> [call], [ist.lfun] |
| 269 |
| Some (trace, varmaps) -> call :: trace, ist.lfun :: varmaps |
| 270 |
else [],[] |
| 271 |
|
| 272 |
let propagate_trace ist loc id v = |
| 273 |
match to_tacvalue v with |
| 274 |
| None -> Proofview.tclUNIT v |
| 275 |
| Some tacv -> |
| 276 |
match tacv with |
| 277 |
| VFun (appl,_,_,lfun,it,b) -> |
| 278 |
let kn = |
| 279 |
match appl with |
| 280 |
| GlbAppl ((kn, _) :: _) -> Some kn |
| 281 |
| _ -> None |
| 282 |
in |
| 283 |
let t = if List.is_empty it then b else CAst.make (TacFun (it,b)) in |
| 284 |
let trace = push_trace(loc,LtacVarCall (kn,id,t)) ist in |
| 285 |
let ans = VFun (appl,trace,loc,lfun,it,b) in |
| 286 |
Proofview.tclUNIT (of_tacvalue ans) |
| 287 |
| VRec _ -> Proofview.tclUNIT v |
| 288 |
|
| 289 |
let append_trace trace v = |
| 290 |
match to_tacvalue v with |
| 291 |
| Some (VFun (appl,trace',loc,lfun,it,b)) -> of_tacvalue (VFun (appl,trace',loc,lfun,it,b)) |
| 292 |
| _ -> v |
| 293 |
|
| 294 |
|
| 295 |
let coerce_to_tactic loc id v = |
| 296 |
let fail () = user_err ?loc |
| 297 |
(str "Variable " ++ Id.print id ++ str " should be bound to a tactic.") |
| 298 |
in |
| 299 |
match to_tacvalue v with |
| 300 |
| Some (VFun (appl,trace,_,lfun,it,b)) -> of_tacvalue (VFun (appl,trace,loc,lfun,it,b)) |
| 301 |
| _ -> fail () |
| 302 |
|
| 303 |
let intro_pattern_of_ident id = CAst.make @@ IntroNaming (IntroIdentifier id) |
| 304 |
let value_of_ident id = |
| 305 |
in_gen (topwit wit_intropattern) (intro_pattern_of_ident id) |
| 306 |
|
| 307 |
let (+++) lfun1 lfun2 = Id.Map.fold Id.Map.add lfun1 lfun2 |
| 308 |
|
| 309 |
let extend_values_with_bindings (ln,lm) lfun = |
| 310 |
let of_cub c = match c with |
| 311 |
| [], c -> Value.of_constr c |
| 312 |
| _ -> Value.of_constr_under_binders c |
| 313 |
in |
| 314 |
|
| 315 |
|
| 316 |
let accu = Id.Map.map value_of_ident ln in |
| 317 |
let accu = lfun +++ accu in |
| 318 |
Id.Map.fold (fun id c accu -> Id.Map.add id (of_cub c) accu) lm accu |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
let is_variable env id = |
| 324 |
Id.List.mem id (ids_of_named_context (Environ.named_context env)) |
| 325 |
|
| 326 |
let debugging_step ist pp = |
| 327 |
match curr_debug ist with |
| 328 |
| DebugOn lev -> Tactic_debug.defer_output |
| 329 |
(fun _ -> (str "Level " ++ int lev ++ str": " ++ pp () ++ fnl())) |
| 330 |
| _ -> Proofview.NonLogical.return () |
| 331 |
|
| 332 |
let debugging_exception_step ist signal_anomaly e pp = |
| 333 |
let explain_exc = |
| 334 |
if signal_anomaly then explain_logic_error |
| 335 |
else explain_logic_error_no_anomaly in |
| 336 |
debugging_step ist (fun () -> |
| 337 |
pp() ++ spc() ++ str "raised the exception" ++ fnl() ++ explain_exc e) |
| 338 |
|
| 339 |
let ensure_freshness env = |
| 340 |
|
| 341 |
|
| 342 |
process_rel_context |
| 343 |
(fun d e -> EConstr.push_rel (Context.Rel.Declaration.set_name Anonymous d) e) env |
| 344 |
|
| 345 |
|
| 346 |
let try_interp_ltac_var coerce ist env {loc;v=id} = |
| 347 |
let v = Id.Map.find id ist.lfun in |
| 348 |
try coerce v with CannotCoerceTo s -> |
| 349 |
Taccoerce.error_ltac_variable ?loc id env v s |
| 350 |
|
| 351 |
let interp_ltac_var coerce ist env locid = |
| 352 |
try try_interp_ltac_var coerce ist env locid |
| 353 |
with Not_found -> anomaly (str "Detected '" ++ Id.print locid.v ++ str "' as ltac var at interning time.") |
| 354 |
|
| 355 |
let interp_ident ist env sigma id = |
| 356 |
try try_interp_ltac_var (coerce_var_to_ident false env sigma) ist (Some (env,sigma)) (CAst.make id) |
| 357 |
with Not_found -> id |
| 358 |
|
| 359 |
|
| 360 |
let interp_name ist env sigma = function |
| 361 |
| Anonymous -> Anonymous |
| 362 |
| Name id -> Name (interp_ident ist env sigma id) |
| 363 |
|
| 364 |
let interp_intro_pattern_var loc ist env sigma id = |
| 365 |
try try_interp_ltac_var (coerce_to_intro_pattern sigma) ist (Some (env,sigma)) (CAst.make ?loc id) |
| 366 |
with Not_found -> IntroNaming (IntroIdentifier id) |
| 367 |
|
| 368 |
let interp_intro_pattern_naming_var loc ist env sigma id = |
| 369 |
try try_interp_ltac_var (coerce_to_intro_pattern_naming sigma) ist (Some (env,sigma)) (CAst.make ?loc id) |
| 370 |
with Not_found -> IntroIdentifier id |
| 371 |
|
| 372 |
let interp_int ist ({loc;v=id} as locid) = |
| 373 |
try try_interp_ltac_var coerce_to_int ist None locid |
| 374 |
with Not_found -> |
| 375 |
user_err ?loc |
| 376 |
(str "Unbound variable " ++ Id.print id ++ str".") |
| 377 |
|
| 378 |
let interp_int_or_var ist = function |
| 379 |
| ArgVar locid -> interp_int ist locid |
| 380 |
| ArgArg n -> n |
| 381 |
|
| 382 |
let interp_int_as_list ist = function |
| 383 |
| ArgVar ({v=id} as locid) -> |
| 384 |
(try coerce_to_int_list (Id.Map.find id ist.lfun) |
| 385 |
with Not_found | CannotCoerceTo _ -> [interp_int ist locid]) |
| 386 |
| ArgArg n -> [n] |
| 387 |
|
| 388 |
let interp_int_list ist l = |
| 389 |
List.flatten (List.map (interp_int_as_list ist) l) |
| 390 |
|
| 391 |
|
| 392 |
let interp_hyp ist env sigma ({loc;v=id} as locid) = |
| 393 |
|
| 394 |
try try_interp_ltac_var (coerce_to_hyp env sigma) ist (Some (env,sigma)) locid |
| 395 |
with Not_found -> |
| 396 |
|
| 397 |
if is_variable env id then id |
| 398 |
else Loc.raise ?loc (Logic.RefinerError (env, sigma, Logic.NoSuchHyp id)) |
| 399 |
|
| 400 |
let interp_hyp_list_as_list ist env sigma ({loc;v=id} as x) = |
| 401 |
try coerce_to_hyp_list env sigma (Id.Map.find id ist.lfun) |
| 402 |
with Not_found | CannotCoerceTo _ -> [interp_hyp ist env sigma x] |
| 403 |
|
| 404 |
let interp_hyp_list ist env sigma l = |
| 405 |
List.flatten (List.map (interp_hyp_list_as_list ist env sigma) l) |
| 406 |
|
| 407 |
let interp_reference ist env sigma = function |
| 408 |
| ArgArg (_,r) -> r |
| 409 |
| ArgVar {loc;v=id} -> |
| 410 |
try try_interp_ltac_var (coerce_to_reference sigma) ist (Some (env,sigma)) (CAst.make ?loc id) |
| 411 |
with Not_found -> |
| 412 |
try |
| 413 |
GlobRef.VarRef (get_id (Environ.lookup_named id env)) |
| 414 |
with Not_found as exn -> |
| 415 |
let _, info = Exninfo.capture exn in |
| 416 |
Nametab.error_global_not_found ~info (qualid_of_ident ?loc id) |
| 417 |
|
| 418 |
let try_interp_evaluable env (loc, id) = |
| 419 |
let v = Environ.lookup_named id env in |
| 420 |
match v with |
| 421 |
| LocalDef _ -> Evaluable.EvalVarRef id |
| 422 |
| _ -> error_not_evaluable (GlobRef.VarRef id) |
| 423 |
|
| 424 |
let interp_evaluable ist env sigma = function |
| 425 |
| ArgArg (r,Some {loc;v=id}) -> |
| 426 |
|
| 427 |
begin |
| 428 |
try try_interp_evaluable env (loc, id) |
| 429 |
with Not_found as exn -> |
| 430 |
match r with |
| 431 |
| Evaluable.EvalConstRef _ -> r |
| 432 |
| Evaluable.EvalProjectionRef _ -> r |
| 433 |
| _ -> |
| 434 |
let _, info = Exninfo.capture exn in |
| 435 |
Nametab.error_global_not_found ~info (qualid_of_ident ?loc id) |
| 436 |
end |
| 437 |
| ArgArg (r,None) -> r |
| 438 |
| ArgVar {loc;v=id} -> |
| 439 |
try try_interp_ltac_var (coerce_to_evaluable_ref env sigma) ist (Some (env,sigma)) (CAst.make ?loc id) |
| 440 |
with Not_found -> |
| 441 |
try try_interp_evaluable env (loc, id) |
| 442 |
with Not_found as exn -> |
| 443 |
let _, info = Exninfo.capture exn in |
| 444 |
Nametab.error_global_not_found ~info (qualid_of_ident ?loc id) |
| 445 |
|
| 446 |
|
| 447 |
let interp_occurrences ist occs = |
| 448 |
Locusops.occurrences_map (interp_int_list ist) occs |
| 449 |
|
| 450 |
let interp_occurrences_expr ist occs = |
| 451 |
|
| 452 |
|
| 453 |
let occs = interp_occurrences ist occs in |
| 454 |
Locusops.occurrences_map (List.map (fun x -> ArgArg x)) occs |
| 455 |
|
| 456 |
let interp_hyp_location ist env sigma ((occs,id),hl) = |
| 457 |
((interp_occurrences_expr ist occs,interp_hyp ist env sigma id),hl) |
| 458 |
|
| 459 |
let interp_hyp_location_list_as_list ist env sigma ((occs,id),hl as x) = |
| 460 |
match occs,hl with |
| 461 |
| AllOccurrences,InHyp -> |
| 462 |
List.map (fun id -> ((AllOccurrences,id),InHyp)) |
| 463 |
(interp_hyp_list_as_list ist env sigma id) |
| 464 |
| _,_ -> [interp_hyp_location ist env sigma x] |
| 465 |
|
| 466 |
let interp_hyp_location_list ist env sigma l = |
| 467 |
List.flatten (List.map (interp_hyp_location_list_as_list ist env sigma) l) |
| 468 |
|
| 469 |
let interp_clause ist env sigma { onhyps=ol; concl_occs=occs } : clause = |
| 470 |
{ onhyps=Option.map (interp_hyp_location_list ist env sigma) ol; |
| 471 |
concl_occs=interp_occurrences_expr ist occs } |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
let extract_ltac_constr_values ist env = |
| 477 |
let fold id v accu = |
| 478 |
try |
| 479 |
let c = coerce_to_constr env v in |
| 480 |
Id.Map.add id c accu |
| 481 |
with CannotCoerceTo _ -> accu |
| 482 |
in |
| 483 |
Id.Map.fold fold ist.lfun Id.Map.empty |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
let rec intropattern_ids accu {loc;v=pat} = match pat with |
| 491 |
| IntroNaming (IntroIdentifier id) -> Id.Set.add id accu |
| 492 |
| IntroAction (IntroOrAndPattern (IntroAndPattern l)) -> |
| 493 |
List.fold_left intropattern_ids accu l |
| 494 |
| IntroAction (IntroOrAndPattern (IntroOrPattern ll)) -> |
| 495 |
List.fold_left intropattern_ids accu (List.flatten ll) |
| 496 |
| IntroAction (IntroInjection l) -> |
| 497 |
List.fold_left intropattern_ids accu l |
| 498 |
| IntroAction (IntroApplyOn ({v=c},pat)) -> intropattern_ids accu pat |
| 499 |
| IntroNaming (IntroAnonymous | IntroFresh _) |
| 500 |
| IntroAction (IntroWildcard | IntroRewrite _) |
| 501 |
| IntroForthcoming _ -> accu |
| 502 |
|
| 503 |
let extract_ids ids lfun accu = |
| 504 |
let fold id v accu = |
| 505 |
if has_type v (topwit wit_intropattern) then |
| 506 |
let {v=ipat} = out_gen (topwit wit_intropattern) v in |
| 507 |
if Id.List.mem id ids then accu |
| 508 |
else intropattern_ids accu (CAst.make ipat) |
| 509 |
else accu |
| 510 |
in |
| 511 |
Id.Map.fold fold lfun accu |
| 512 |
|
| 513 |
let default_fresh_id = Id.of_string "H" |
| 514 |
|
| 515 |
let interp_fresh_id ist env sigma l = |
| 516 |
let extract_ident ist env sigma id = |
| 517 |
try try_interp_ltac_var (coerce_to_ident_not_fresh sigma) |
| 518 |
ist (Some (env,sigma)) (CAst.make id) |
| 519 |
with Not_found -> id in |
| 520 |
let ids = List.map_filter (function ArgVar {v=id} -> Some id | _ -> None) l in |
| 521 |
let avoid = match TacStore.get ist.extra f_avoid_ids with |
| 522 |
| None -> Id.Set.empty |
| 523 |
| Some l -> l |
| 524 |
in |
| 525 |
let avoid = extract_ids ids ist.lfun avoid in |
| 526 |
let id = |
| 527 |
if List.is_empty l then default_fresh_id |
| 528 |
else |
| 529 |
let s = |
| 530 |
String.concat "" (List.map (function |
| 531 |
| ArgArg s -> s |
| 532 |
| ArgVar {v=id} -> Id.to_string (extract_ident ist env sigma id)) l) in |
| 533 |
let s = if CLexer.is_keyword (Procq.get_keyword_state()) s then s^"0" else s in |
| 534 |
Id.of_string s in |
| 535 |
Tactics.fresh_id_in_env avoid id env |
| 536 |
|
| 537 |
|
| 538 |
let extract_ltac_constr_context ist env sigma = |
| 539 |
let add_uconstr id v map = |
| 540 |
try Id.Map.add id (coerce_to_uconstr v) map |
| 541 |
with CannotCoerceTo _ -> map |
| 542 |
in |
| 543 |
let add_constr id v map = |
| 544 |
try Id.Map.add id (coerce_to_constr env v) map |
| 545 |
with CannotCoerceTo _ -> map |
| 546 |
in |
| 547 |
let add_ident id v map = |
| 548 |
try Id.Map.add id (coerce_var_to_ident false env sigma v) map |
| 549 |
with CannotCoerceTo _ -> map |
| 550 |
in |
| 551 |
let fold id v {idents;typed;untyped;genargs} = |
| 552 |
let idents = add_ident id v idents in |
| 553 |
let typed = add_constr id v typed in |
| 554 |
let untyped = add_uconstr id v untyped in |
| 555 |
{ idents ; typed ; untyped; genargs } |
| 556 |
in |
| 557 |
let empty = { idents = Id.Map.empty ;typed = Id.Map.empty ; untyped = Id.Map.empty; genargs = ist.lfun } in |
| 558 |
Id.Map.fold fold ist.lfun empty |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
let interp_glob_closure ist env sigma ?(kind=WithoutTypeConstraint) ?(pattern_mode=false) (term,term_expr_opt) = |
| 563 |
let closure = extract_ltac_constr_context ist env sigma in |
| 564 |
match term_expr_opt with |
| 565 |
| None -> { closure ; term } |
| 566 |
| Some term_expr -> |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
let constr_context = |
| 571 |
Id.Set.union |
| 572 |
(Id.Map.domain closure.typed) |
| 573 |
(Id.Map.domain closure.untyped) |
| 574 |
in |
| 575 |
let ltacvars = { |
| 576 |
ltac_vars = constr_context; |
| 577 |
ltac_bound = Id.Map.domain ist.lfun; |
| 578 |
ltac_extra = Genintern.Store.empty; |
| 579 |
} in |
| 580 |
{ closure ; term = intern_gen kind ~strict_check:false ~pattern_mode ~ltacvars env sigma term_expr } |
| 581 |
|
| 582 |
let interp_uconstr ist env sigma c = interp_glob_closure ist env sigma c |
| 583 |
|
| 584 |
let interp_gen kind ist pattern_mode flags env sigma c = |
| 585 |
let kind_for_intern = match kind with OfType _ -> WithoutTypeConstraint | _ -> kind in |
| 586 |
let { closure = constrvars ; term } = |
| 587 |
interp_glob_closure ist env sigma ~kind:kind_for_intern ~pattern_mode c in |
| 588 |
let vars = { |
| 589 |
ltac_constrs = constrvars.typed; |
| 590 |
ltac_uconstrs = constrvars.untyped; |
| 591 |
ltac_idents = constrvars.idents; |
| 592 |
ltac_genargs = ist.lfun; |
| 593 |
} in |
| 594 |
let loc = loc_of_glob_constr term in |
| 595 |
let trace = push_trace (loc,LtacConstrInterp (env,sigma,term,vars)) ist in |
| 596 |
let (stack, _) = trace in |
| 597 |
|
| 598 |
|
| 599 |
Tactic_debug.push_chunk trace; |
| 600 |
try |
| 601 |
let (evd,c) = |
| 602 |
catch_error_with_trace_loc loc stack (understand_ltac flags env sigma vars kind) term |
| 603 |
in |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
Proofview.NonLogical.run (db_constr (curr_debug ist) env evd c); |
| 608 |
Tactic_debug.pop_chunk (); |
| 609 |
(evd,c) |
| 610 |
with reraise -> |
| 611 |
let reraise = Exninfo.capture reraise in |
| 612 |
Tactic_debug.pop_chunk (); |
| 613 |
Exninfo.iraise reraise |
| 614 |
|
| 615 |
let constr_flags () = { |
| 616 |
use_coercions = true; |
| 617 |
use_typeclasses = UseTC; |
| 618 |
solve_unification_constraints = true; |
| 619 |
fail_evar = true; |
| 620 |
expand_evars = true; |
| 621 |
program_mode = false; |
| 622 |
poly = PolyFlags.default; |
| 623 |
undeclared_evars_rr = false; |
| 624 |
unconstrained_sorts = false; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
let interp_constr_gen kind ist env sigma c = |
| 629 |
let flags = { (constr_flags ()) with poly = ist.Geninterp.poly } in |
| 630 |
interp_gen kind ist false flags env sigma c |
| 631 |
|
| 632 |
let interp_constr = interp_constr_gen WithoutTypeConstraint |
| 633 |
|
| 634 |
let interp_type = interp_constr_gen IsType |
| 635 |
|
| 636 |
let open_constr_use_classes_flags () = { |
| 637 |
use_coercions = true; |
| 638 |
use_typeclasses = UseTC; |
| 639 |
solve_unification_constraints = true; |
| 640 |
fail_evar = false; |
| 641 |
expand_evars = false; |
| 642 |
program_mode = false; |
| 643 |
poly = PolyFlags.default; |
| 644 |
undeclared_evars_rr = false; |
| 645 |
unconstrained_sorts = false; |
| 646 |
} |
| 647 |
|
| 648 |
let open_constr_no_classes_flags () = { |
| 649 |
use_coercions = true; |
| 650 |
use_typeclasses = NoUseTC; |
| 651 |
solve_unification_constraints = true; |
| 652 |
fail_evar = false; |
| 653 |
expand_evars = false; |
| 654 |
program_mode = false; |
| 655 |
poly = PolyFlags.default; |
| 656 |
undeclared_evars_rr = false; |
| 657 |
unconstrained_sorts = false; |
| 658 |
} |
| 659 |
|
| 660 |
let pure_open_constr_flags = { |
| 661 |
use_coercions = true; |
| 662 |
use_typeclasses = NoUseTC; |
| 663 |
solve_unification_constraints = true; |
| 664 |
fail_evar = false; |
| 665 |
expand_evars = false; |
| 666 |
program_mode = false; |
| 667 |
poly = PolyFlags.default; |
| 668 |
undeclared_evars_rr = false; |
| 669 |
unconstrained_sorts = false; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
let interp_open_constr ?(expected_type=WithoutTypeConstraint) ?(flags=open_constr_no_classes_flags ()) ist env sigma c = |
| 674 |
interp_gen expected_type ist false flags env sigma c |
| 675 |
|
| 676 |
let interp_open_constr_with_classes ?(expected_type=WithoutTypeConstraint) ist env sigma c = |
| 677 |
interp_gen expected_type ist false (open_constr_use_classes_flags ()) env sigma c |
| 678 |
|
| 679 |
let interp_pure_open_constr ist = |
| 680 |
interp_gen WithoutTypeConstraint ist false pure_open_constr_flags |
| 681 |
|
| 682 |
let interp_typed_pattern ist env sigma c = |
| 683 |
let sigma, c = |
| 684 |
interp_gen WithoutTypeConstraint ist true pure_open_constr_flags env sigma c in |
| 685 |
|
| 686 |
|
| 687 |
legacy_bad_pattern_of_constr env sigma c |
| 688 |
|
| 689 |
|
| 690 |
let interp_constr_in_compound_list inj_fun dest_fun interp_fun ist env sigma l = |
| 691 |
let try_expand_ltac_var sigma x = |
| 692 |
try match DAst.get (fst (dest_fun x)) with |
| 693 |
| GVar id -> |
| 694 |
let v = Id.Map.find id ist.lfun in |
| 695 |
sigma, List.map inj_fun (coerce_to_constr_list env v) |
| 696 |
| _ -> |
| 697 |
raise Not_found |
| 698 |
with CannotCoerceTo _ | Not_found -> |
| 699 |
|
| 700 |
let sigma, c = interp_fun ist env sigma x in |
| 701 |
sigma, [c] in |
| 702 |
let sigma, l = List.fold_left_map try_expand_ltac_var sigma l in |
| 703 |
sigma, List.flatten l |
| 704 |
|
| 705 |
let interp_constr_list ist env sigma c = |
| 706 |
interp_constr_in_compound_list (fun x -> x) (fun x -> x) interp_constr ist env sigma c |
| 707 |
|
| 708 |
let interp_open_constr_list = |
| 709 |
interp_constr_in_compound_list (fun x -> x) (fun x -> x) interp_open_constr |
| 710 |
|
| 711 |
let interp_constr_with_occurrences ist env sigma (occs,c) = |
| 712 |
let (sigma,c_interp) = interp_constr ist env sigma c in |
| 713 |
sigma , (interp_occurrences ist occs, c_interp) |
| 714 |
|
| 715 |
let interp_evaluable_or_pattern ist env sigma = function |
| 716 |
| ArgVar {loc;v=id} -> |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
let coerce_eval_ref_or_constr x = |
| 721 |
try Inl (coerce_to_evaluable_ref env sigma x) |
| 722 |
with CannotCoerceTo _ -> |
| 723 |
let c = coerce_to_closed_constr env x in |
| 724 |
Inr (pattern_of_constr env sigma c) in |
| 725 |
(try try_interp_ltac_var coerce_eval_ref_or_constr ist (Some (env,sigma)) (CAst.make ?loc id) |
| 726 |
with Not_found as exn -> |
| 727 |
let _, info = Exninfo.capture exn in |
| 728 |
Nametab.error_global_not_found ~info (qualid_of_ident ?loc id)) |
| 729 |
| ArgArg _ as b -> Inl (interp_evaluable ist env sigma b) |
| 730 |
|
| 731 |
let interp_constr_with_occurrences_and_name_as_list = |
| 732 |
interp_constr_in_compound_list |
| 733 |
(fun c -> ((AllOccurrences,c),Anonymous)) |
| 734 |
(function ((occs,c),Anonymous) when occs == AllOccurrences -> c |
| 735 |
| _ -> raise Not_found) |
| 736 |
(fun ist env sigma (occ_c,na) -> |
| 737 |
let (sigma,c_interp) = interp_constr_with_occurrences ist env sigma occ_c in |
| 738 |
sigma, (c_interp, |
| 739 |
interp_name ist env sigma na)) |
| 740 |
|
| 741 |
let interp_red_expr ist env sigma r = |
| 742 |
let ist = { |
| 743 |
Redexpr.Interp.interp_occurrence_var = (fun x -> interp_int_list ist [ArgVar x]); |
| 744 |
interp_constr = interp_constr ist; |
| 745 |
interp_constr_list = (fun env sigma c -> interp_constr_list ist env sigma [c]); |
| 746 |
interp_evaluable = interp_evaluable ist; |
| 747 |
interp_pattern = interp_typed_pattern ist; |
| 748 |
interp_evaluable_or_pattern = interp_evaluable_or_pattern ist; |
| 749 |
} |
| 750 |
in |
| 751 |
Redexpr.Interp.interp_red_expr ist env sigma r |
| 752 |
|
| 753 |
let interp_strategy ist _env _sigma s = |
| 754 |
let interp_redexpr r = fun env sigma -> interp_red_expr ist env sigma r in |
| 755 |
let interp_constr c = (fst c, fun env sigma -> interp_open_constr ist env sigma c) in |
| 756 |
let s = Rewrite.map_strategy interp_constr interp_redexpr (fun x -> x) s in |
| 757 |
Rewrite.strategy_of_ast s |
| 758 |
|
| 759 |
let interp_may_eval f ist env sigma = function |
| 760 |
| ConstrEval (r,c) -> |
| 761 |
let (sigma,redexp) = interp_red_expr ist env sigma r in |
| 762 |
let (sigma,c_interp) = f ist env sigma c in |
| 763 |
let (redfun, _) = Redexpr.reduction_of_red_expr env redexp in |
| 764 |
redfun env sigma c_interp |
| 765 |
| ConstrContext ({loc;v=s},c) -> |
| 766 |
let (sigma,ic) = f ist env sigma c in |
| 767 |
let ctxt = |
| 768 |
try try_interp_ltac_var coerce_to_constr_context ist (Some (env, sigma)) (CAst.make ?loc s) |
| 769 |
with Not_found -> |
| 770 |
user_err ?loc (str "Unbound context identifier" ++ Id.print s ++ str".") |
| 771 |
in |
| 772 |
let c = Constr_matching.instantiate_context ctxt ic in |
| 773 |
Typing.solve_evars env sigma c |
| 774 |
| ConstrTypeOf c -> |
| 775 |
let (sigma,c_interp) = f ist env sigma c in |
| 776 |
let (sigma, t) = Typing.type_of ~refresh:true env sigma c_interp in |
| 777 |
(sigma, t) |
| 778 |
| ConstrTerm c -> |
| 779 |
try |
| 780 |
f ist env sigma c |
| 781 |
with reraise -> |
| 782 |
let reraise = Exninfo.capture reraise in |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
Proofview.NonLogical.run (debugging_exception_step ist false (fst reraise) (fun () -> |
| 787 |
str"interpretation of term " ++ pr_glob_constr_env env sigma (fst c))); |
| 788 |
Exninfo.iraise reraise |
| 789 |
|
| 790 |
|
| 791 |
let interp_constr_may_eval ist env sigma c = |
| 792 |
let (sigma,csr) = |
| 793 |
try |
| 794 |
interp_may_eval interp_constr ist env sigma c |
| 795 |
with reraise -> |
| 796 |
let reraise = Exninfo.capture reraise in |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
Proofview.NonLogical.run (debugging_exception_step ist false (fst reraise) (fun () -> str"evaluation of term")); |
| 801 |
Exninfo.iraise reraise |
| 802 |
in |
| 803 |
begin |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
Proofview.NonLogical.run (db_constr (curr_debug ist) env sigma csr); |
| 808 |
sigma , csr |
| 809 |
end |
| 810 |
|
| 811 |
|
| 812 |
let message_of_value v = |
| 813 |
let pr_with_env pr = |
| 814 |
Ftactic.enter begin fun gl -> |
| 815 |
let env = Proofview.Goal.env gl in |
| 816 |
let sigma = Proofview.Goal.sigma gl in |
| 817 |
Ftactic.return (pr env sigma) |
| 818 |
end |
| 819 |
in |
| 820 |
let open Genprint in |
| 821 |
match generic_val_print v with |
| 822 |
| TopPrinterBasic pr -> Ftactic.return (pr ()) |
| 823 |
| TopPrinterNeedsContext pr -> pr_with_env pr |
| 824 |
| TopPrinterNeedsContextAndLevel { default_ensure_surrounded; printer } -> |
| 825 |
pr_with_env (fun env sigma -> printer env sigma default_ensure_surrounded) |
| 826 |
|
| 827 |
let interp_message_token ist = function |
| 828 |
| MsgString s -> Ftactic.return (str s) |
| 829 |
| MsgInt n -> Ftactic.return (int n) |
| 830 |
| MsgIdent {loc;v=id} -> |
| 831 |
let v = try Some (Id.Map.find id ist.lfun) with Not_found -> None in |
| 832 |
match v with |
| 833 |
| None -> Ftactic.lift ( |
| 834 |
let info = Exninfo.reify () in |
| 835 |
Tacticals.tclZEROMSG ~info (Id.print id ++ str" not found.")) |
| 836 |
| Some v -> message_of_value v |
| 837 |
|
| 838 |
let interp_message ist l = |
| 839 |
let open Ftactic in |
| 840 |
Ftactic.List.map (interp_message_token ist) l >>= fun l -> |
| 841 |
Ftactic.return (prlist_with_sep spc (fun x -> x) l) |
| 842 |
|
| 843 |
let rec interp_intro_pattern ist env sigma = with_loc_val (fun ?loc -> function |
| 844 |
| IntroAction pat -> |
| 845 |
let pat = interp_intro_pattern_action ist env sigma pat in |
| 846 |
CAst.make ?loc @@ IntroAction pat |
| 847 |
| IntroNaming (IntroIdentifier id) -> |
| 848 |
CAst.make ?loc @@ interp_intro_pattern_var loc ist env sigma id |
| 849 |
| IntroNaming pat -> |
| 850 |
CAst.make ?loc @@ IntroNaming (interp_intro_pattern_naming loc ist env sigma pat) |
| 851 |
| IntroForthcoming _ as x -> CAst.make ?loc x) |
| 852 |
|
| 853 |
and interp_intro_pattern_naming loc ist env sigma = function |
| 854 |
| IntroFresh id -> IntroFresh (interp_ident ist env sigma id) |
| 855 |
| IntroIdentifier id -> interp_intro_pattern_naming_var loc ist env sigma id |
| 856 |
| IntroAnonymous as x -> x |
| 857 |
|
| 858 |
and interp_intro_pattern_action ist env sigma = function |
| 859 |
| IntroOrAndPattern l -> |
| 860 |
let l = interp_or_and_intro_pattern ist env sigma l in |
| 861 |
IntroOrAndPattern l |
| 862 |
| IntroInjection l -> |
| 863 |
let l = interp_intro_pattern_list_as_list ist env sigma l in |
| 864 |
IntroInjection l |
| 865 |
| IntroApplyOn ({loc;v=c},ipat) -> |
| 866 |
let c env sigma = interp_open_constr ist env sigma c in |
| 867 |
let ipat = interp_intro_pattern ist env sigma ipat in |
| 868 |
IntroApplyOn (CAst.make ?loc c,ipat) |
| 869 |
| IntroWildcard | IntroRewrite _ as x -> x |
| 870 |
|
| 871 |
and interp_or_and_intro_pattern ist env sigma = function |
| 872 |
| IntroAndPattern l -> |
| 873 |
let l = List.map (interp_intro_pattern ist env sigma) l in |
| 874 |
IntroAndPattern l |
| 875 |
| IntroOrPattern ll -> |
| 876 |
let ll = List.map (interp_intro_pattern_list_as_list ist env sigma) ll in |
| 877 |
IntroOrPattern ll |
| 878 |
|
| 879 |
and interp_intro_pattern_list_as_list ist env sigma = function |
| 880 |
| [{loc;v=IntroNaming (IntroIdentifier id)}] as l -> |
| 881 |
(try coerce_to_intro_pattern_list ?loc sigma (Id.Map.find id ist.lfun) |
| 882 |
with Not_found | CannotCoerceTo _ -> |
| 883 |
List.map (interp_intro_pattern ist env sigma) l) |
| 884 |
| l -> List.map (interp_intro_pattern ist env sigma) l |
| 885 |
|
| 886 |
let interp_intro_pattern_naming_option ist env sigma = function |
| 887 |
| None -> None |
| 888 |
| Some lpat -> Some (map_with_loc (fun ?loc pat -> interp_intro_pattern_naming loc ist env sigma pat) lpat) |
| 889 |
|
| 890 |
let interp_or_and_intro_pattern_option ist env sigma = function |
| 891 |
| None -> None |
| 892 |
| Some (ArgVar {loc;v=id}) -> |
| 893 |
(match interp_intro_pattern_var loc ist env sigma id with |
| 894 |
| IntroAction (IntroOrAndPattern l) -> Some (CAst.make ?loc l) |
| 895 |
| _ -> |
| 896 |
user_err ?loc (str "Cannot coerce to a disjunctive/conjunctive pattern.")) |
| 897 |
| Some (ArgArg {loc;v=l}) -> |
| 898 |
let l = interp_or_and_intro_pattern ist env sigma l in |
| 899 |
Some (CAst.make ?loc l) |
| 900 |
|
| 901 |
let interp_intro_pattern_option ist env sigma = function |
| 902 |
| None -> None |
| 903 |
| Some ipat -> |
| 904 |
let ipat = interp_intro_pattern ist env sigma ipat in |
| 905 |
Some ipat |
| 906 |
|
| 907 |
let interp_in_hyp_as ist env sigma (id,ipat) = |
| 908 |
let ipat = interp_intro_pattern_option ist env sigma ipat in |
| 909 |
(interp_hyp ist env sigma id,ipat) |
| 910 |
|
| 911 |
let interp_binding_name ist env sigma = function |
| 912 |
| AnonHyp n -> AnonHyp n |
| 913 |
| NamedHyp id -> |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
try try_interp_ltac_var (coerce_to_quantified_hypothesis sigma) ist (Some (env,sigma)) id |
| 918 |
with Not_found -> NamedHyp id |
| 919 |
|
| 920 |
let interp_declared_or_quantified_hypothesis ist env sigma = function |
| 921 |
| AnonHyp n -> AnonHyp n |
| 922 |
| NamedHyp id -> |
| 923 |
try try_interp_ltac_var |
| 924 |
(coerce_to_decl_or_quant_hyp sigma) ist (Some (env,sigma)) id |
| 925 |
with Not_found -> NamedHyp id |
| 926 |
|
| 927 |
let interp_binding ist env sigma {loc;v=(b,c)} = |
| 928 |
let sigma, c = interp_open_constr ist env sigma c in |
| 929 |
sigma, (CAst.make ?loc (interp_binding_name ist env sigma b,c)) |
| 930 |
|
| 931 |
let interp_bindings ist env sigma = function |
| 932 |
| NoBindings -> |
| 933 |
sigma, NoBindings |
| 934 |
| ImplicitBindings l -> |
| 935 |
let sigma, l = interp_open_constr_list ist env sigma l in |
| 936 |
sigma, ImplicitBindings l |
| 937 |
| ExplicitBindings l -> |
| 938 |
let sigma, l = List.fold_left_map (interp_binding ist env) sigma l in |
| 939 |
sigma, ExplicitBindings l |
| 940 |
|
| 941 |
let interp_constr_with_bindings ist env sigma (c,bl) = |
| 942 |
let sigma, bl = interp_bindings ist env sigma bl in |
| 943 |
let sigma, c = interp_constr ist env sigma c in |
| 944 |
sigma, (c,bl) |
| 945 |
|
| 946 |
let interp_open_constr_with_bindings ist env sigma (c,bl) = |
| 947 |
let sigma, bl = interp_bindings ist env sigma bl in |
| 948 |
let sigma, c = interp_open_constr ist env sigma c in |
| 949 |
sigma, (c, bl) |
| 950 |
|
| 951 |
let loc_of_bindings = function |
| 952 |
| NoBindings -> None |
| 953 |
| ImplicitBindings l -> loc_of_glob_constr (fst (List.last l)) |
| 954 |
| ExplicitBindings l -> (List.last l).loc |
| 955 |
|
| 956 |
let interp_open_constr_with_bindings_loc ist ((c,_),bl as cb) = |
| 957 |
let loc1 = loc_of_glob_constr c in |
| 958 |
let loc2 = loc_of_bindings bl in |
| 959 |
let loc = Loc.merge_opt loc1 loc2 in |
| 960 |
let f env sigma = interp_open_constr_with_bindings ist env sigma cb in |
| 961 |
(loc,f) |
| 962 |
|
| 963 |
let interp_destruction_arg ist gl arg = |
| 964 |
match arg with |
| 965 |
| keep,ElimOnConstr c -> |
| 966 |
keep,ElimOnConstr begin fun env sigma -> |
| 967 |
interp_open_constr_with_bindings ist env sigma c |
| 968 |
end |
| 969 |
| keep,ElimOnAnonHyp n as x -> x |
| 970 |
| keep,ElimOnIdent {loc;v=id} -> |
| 971 |
let error () = user_err ?loc |
| 972 |
(strbrk "Cannot coerce " ++ Id.print id ++ |
| 973 |
strbrk " neither to a quantified hypothesis nor to a term.") |
| 974 |
in |
| 975 |
let try_cast_id id' = |
| 976 |
if Tactics.is_quantified_hypothesis id' gl |
| 977 |
then keep,ElimOnIdent (CAst.make ?loc id') |
| 978 |
else |
| 979 |
(keep, ElimOnConstr begin fun env sigma -> |
| 980 |
try (sigma, (constr_of_id env id', NoBindings)) |
| 981 |
with Not_found -> |
| 982 |
user_err ?loc ( |
| 983 |
Id.print id ++ strbrk " binds to " ++ Id.print id' ++ strbrk " which is neither a declared nor a quantified hypothesis.") |
| 984 |
end) |
| 985 |
in |
| 986 |
try |
| 987 |
|
| 988 |
let v = Id.Map.find id ist.lfun in |
| 989 |
if has_type v (topwit wit_intropattern) then |
| 990 |
let v = out_gen (topwit wit_intropattern) v in |
| 991 |
match v with |
| 992 |
| {v=IntroNaming (IntroIdentifier id)} -> try_cast_id id |
| 993 |
| _ -> error () |
| 994 |
else if has_type v (topwit wit_hyp) then |
| 995 |
let id = out_gen (topwit wit_hyp) v in |
| 996 |
try_cast_id id |
| 997 |
else if has_type v (topwit wit_int) then |
| 998 |
keep,ElimOnAnonHyp (out_gen (topwit wit_int) v) |
| 999 |
else match Value.to_constr v with |
| 1000 |
| None -> error () |
| 1001 |
| Some c -> keep,ElimOnConstr (fun env sigma -> (sigma, (c,NoBindings))) |
| 1002 |
with Not_found -> |
| 1003 |
|
| 1004 |
if Tactics.is_quantified_hypothesis id gl then |
| 1005 |
keep,ElimOnIdent (CAst.make ?loc id) |
| 1006 |
else |
| 1007 |
let c = (DAst.make ?loc @@ GVar id,Some (CAst.make @@ CRef (qualid_of_ident ?loc id,None))) in |
| 1008 |
let f env sigma = |
| 1009 |
let (sigma,c) = interp_open_constr ist env sigma c in |
| 1010 |
(sigma, (c,NoBindings)) |
| 1011 |
in |
| 1012 |
keep,ElimOnConstr f |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
let head_with_value (lvar,lval) = |
| 1017 |
let rec head_with_value_rec lacc = function |
| 1018 |
| ([],[]) -> (lacc,[],[]) |
| 1019 |
| (vr::tvr,ve::tve) -> |
| 1020 |
(match vr with |
| 1021 |
| Anonymous -> head_with_value_rec lacc (tvr,tve) |
| 1022 |
| Name v -> head_with_value_rec ((v,ve)::lacc) (tvr,tve)) |
| 1023 |
| (vr,[]) -> (lacc,vr,[]) |
| 1024 |
| ([],ve) -> (lacc,[],ve) |
| 1025 |
in |
| 1026 |
head_with_value_rec [] (lvar,lval) |
| 1027 |
|
| 1028 |
let eval_pattern ist env sigma (bvars, _, pat) = |
| 1029 |
let closure = extract_ltac_constr_context ist env sigma in |
| 1030 |
let lvars = { |
| 1031 |
ltac_constrs = closure.typed; |
| 1032 |
ltac_uconstrs = closure.untyped; |
| 1033 |
ltac_idents = closure.idents; |
| 1034 |
ltac_genargs = closure.genargs; |
| 1035 |
} |
| 1036 |
in |
| 1037 |
(bvars, Patternops.interp_pattern env sigma lvars pat) |
| 1038 |
|
| 1039 |
let read_pattern ist env sigma = function |
| 1040 |
| Subterm (ido,c) -> Subterm (ido,eval_pattern ist env sigma c) |
| 1041 |
| Term c -> Term (eval_pattern ist env sigma c) |
| 1042 |
|
| 1043 |
|
| 1044 |
let cons_and_check_name id l = |
| 1045 |
if Id.List.mem id l then |
| 1046 |
user_err ( |
| 1047 |
str "Hypothesis pattern-matching variable " ++ Id.print id ++ |
| 1048 |
str " used twice in the same pattern.") |
| 1049 |
else id::l |
| 1050 |
|
| 1051 |
let rec read_match_goal_hyps ist env sigma lidh = function |
| 1052 |
| (Hyp ({loc;v=na} as locna,mp))::tl -> |
| 1053 |
let lidh' = Name.fold_right cons_and_check_name na lidh in |
| 1054 |
Hyp (locna,read_pattern ist env sigma mp):: |
| 1055 |
(read_match_goal_hyps ist env sigma lidh' tl) |
| 1056 |
| (Def ({loc;v=na} as locna,mv,mp))::tl -> |
| 1057 |
let lidh' = Name.fold_right cons_and_check_name na lidh in |
| 1058 |
Def (locna,read_pattern ist env sigma mv, read_pattern ist env sigma mp):: |
| 1059 |
(read_match_goal_hyps ist env sigma lidh' tl) |
| 1060 |
| [] -> [] |
| 1061 |
|
| 1062 |
|
| 1063 |
let rec read_match_rule ist env sigma = function |
| 1064 |
| (All tc)::tl -> (All tc)::(read_match_rule ist env sigma tl) |
| 1065 |
| (Pat (rl,mp,tc))::tl -> |
| 1066 |
Pat (read_match_goal_hyps ist env sigma [] rl, read_pattern ist env sigma mp,tc) |
| 1067 |
:: read_match_rule ist env sigma tl |
| 1068 |
| [] -> [] |
| 1069 |
|
| 1070 |
|
| 1071 |
let type_uconstr ?(flags = (constr_flags ())) |
| 1072 |
?(expected_type = WithoutTypeConstraint) ist c = |
| 1073 |
let flags = { flags with poly = ist.Geninterp.poly } in |
| 1074 |
begin fun env sigma -> |
| 1075 |
Pretyping.understand_uconstr ~flags ~expected_type env sigma c |
| 1076 |
end |
| 1077 |
|
| 1078 |
|
| 1079 |
let rec val_interp ist ?(appl=UnnamedAppl) (tac:glob_tactic_expr) : Val.t Ftactic.t = |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
let (loc,tac2) = CAst.(tac.loc, tac.v) in |
| 1087 |
let value_interp ist = |
| 1088 |
match tac2 with |
| 1089 |
| TacFun (it, body) -> |
| 1090 |
Ftactic.return (of_tacvalue (VFun (UnnamedAppl, extract_trace ist, extract_loc ist, ist.lfun, it, body))) |
| 1091 |
| TacLetIn (true,l,u) -> interp_letrec ist l u |
| 1092 |
| TacLetIn (false,l,u) -> interp_letin ist l u |
| 1093 |
| TacMatchGoal (lz,lr,lmr) -> interp_match_goal ist lz lr lmr |
| 1094 |
| TacMatch (lz,c,lmr) -> interp_match ist lz c lmr |
| 1095 |
| TacArg v -> interp_tacarg ist v |
| 1096 |
| _ -> |
| 1097 |
|
| 1098 |
Ftactic.return (of_tacvalue (VFun (UnnamedAppl, extract_trace ist, extract_loc ist, ist.lfun, [], tac))) |
| 1099 |
in |
| 1100 |
let open Ftactic in |
| 1101 |
Control.check_for_interrupt (); |
| 1102 |
match curr_debug ist with |
| 1103 |
| DebugOn lev -> |
| 1104 |
let eval v = |
| 1105 |
let ist = { ist with extra = TacStore.set ist.extra f_debug v } in |
| 1106 |
value_interp ist >>= fun v -> return (name_vfun appl v) |
| 1107 |
in |
| 1108 |
Tactic_debug.debug_prompt lev tac eval ist.lfun (TacStore.get ist.extra f_trace) |
| 1109 |
| _ -> value_interp ist >>= fun v -> return (name_vfun appl v) |
| 1110 |
|
| 1111 |
|
| 1112 |
and eval_tactic_ist ist tac : unit Proofview.tactic = |
| 1113 |
let (loc, tac2) = CAst.(tac.loc, tac.v) in |
| 1114 |
match tac2 with |
| 1115 |
| TacAtom t -> |
| 1116 |
let call = LtacAtomCall t in |
| 1117 |
let (stack, _) = push_trace(loc,call) ist in |
| 1118 |
do_profile stack |
| 1119 |
(catch_error_tac_loc loc stack (interp_atomic ist t)) |
| 1120 |
| TacFun _ | TacLetIn _ | TacMatchGoal _ | TacMatch _ -> interp_tactic ist tac |
| 1121 |
| TacId [] -> Proofview.tclLIFT (db_breakpoint (curr_debug ist) []) |
| 1122 |
| TacId s -> |
| 1123 |
let msgnl = |
| 1124 |
let open Ftactic in |
| 1125 |
interp_message ist s >>= fun msg -> |
| 1126 |
return (hov 0 msg , hov 0 msg) |
| 1127 |
in |
| 1128 |
let print (_,msgnl) = Proofview.(tclLIFT (NonLogical.print_info msgnl)) in |
| 1129 |
let log (msg,_) = Proofview.Trace.log (fun () -> msg) in |
| 1130 |
let break = Proofview.tclLIFT (db_breakpoint (curr_debug ist) s) in |
| 1131 |
Ftactic.run msgnl begin fun msgnl -> |
| 1132 |
print msgnl <*> log msgnl <*> break |
| 1133 |
end |
| 1134 |
| TacFail (g,n,s) -> |
| 1135 |
let msg = interp_message ist s in |
| 1136 |
let tac ~info l = Tacticals.tclFAILn ~info (interp_int_or_var ist n) l in |
| 1137 |
let tac = |
| 1138 |
match g with |
| 1139 |
| TacLocal -> |
| 1140 |
let info = Exninfo.reify () in |
| 1141 |
fun l -> Proofview.tclINDEPENDENT (tac ~info l) |
| 1142 |
| TacGlobal -> |
| 1143 |
let info = Exninfo.reify () in |
| 1144 |
tac ~info |
| 1145 |
in |
| 1146 |
Ftactic.run msg tac |
| 1147 |
| TacProgress tac -> Tacticals.tclPROGRESS (interp_tactic ist tac) |
| 1148 |
| TacAbstract (t,ido) -> |
| 1149 |
let call = LtacMLCall tac in |
| 1150 |
let (stack,_) = push_trace(None,call) ist in |
| 1151 |
do_profile stack |
| 1152 |
(catch_error_tac stack begin |
| 1153 |
Proofview.Goal.enter begin fun gl -> |
| 1154 |
let env = Proofview.Goal.env gl in |
| 1155 |
let sigma = Proofview.Goal.sigma gl in |
| 1156 |
Abstract.tclABSTRACT |
| 1157 |
(Option.map (interp_ident ist env sigma) ido) (interp_tactic ist t) |
| 1158 |
end end) |
| 1159 |
| TacThen (t1,t) -> |
| 1160 |
Tacticals.tclTHEN (interp_tactic ist t1) (interp_tactic ist t) |
| 1161 |
| TacDispatch tl -> |
| 1162 |
Proofview.tclDISPATCH (List.map (interp_tactic ist) tl) |
| 1163 |
| TacExtendTac (tf,t,tl) -> |
| 1164 |
Proofview.tclEXTEND (Array.map_to_list (interp_tactic ist) tf) |
| 1165 |
(interp_tactic ist t) |
| 1166 |
(Array.map_to_list (interp_tactic ist) tl) |
| 1167 |
| TacThens (t1,tl) -> Tacticals.tclTHENS (interp_tactic ist t1) (List.map (interp_tactic ist) tl) |
| 1168 |
| TacThens3parts (t1,tf,t,tl) -> |
| 1169 |
Tacticals.tclTHENS3PARTS (interp_tactic ist t1) |
| 1170 |
(Array.map (interp_tactic ist) tf) (interp_tactic ist t) (Array.map (interp_tactic ist) tl) |
| 1171 |
| TacDo (n,tac) -> Tacticals.tclDO (interp_int_or_var ist n) (interp_tactic ist tac) |
| 1172 |
| TacTimeout (n,tac) -> Tacticals.tclTIMEOUT (interp_int_or_var ist n) (interp_tactic ist tac) |
| 1173 |
| TacTime (s,tac) -> Tacticals.tclTIME s (interp_tactic ist tac) |
| 1174 |
| TacTry tac -> Tacticals.tclTRY (interp_tactic ist tac) |
| 1175 |
| TacRepeat tac -> Tacticals.tclREPEAT (interp_tactic ist tac) |
| 1176 |
| TacOr (tac1,tac2) -> |
| 1177 |
Tacticals.tclOR (interp_tactic ist tac1) (interp_tactic ist tac2) |
| 1178 |
| TacOnce tac -> |
| 1179 |
Tacticals.tclONCE (interp_tactic ist tac) |
| 1180 |
| TacExactlyOnce tac -> |
| 1181 |
Tacticals.tclEXACTLY_ONCE (interp_tactic ist tac) |
| 1182 |
| TacIfThenCatch (t,tt,te) -> |
| 1183 |
Tacticals.tclIFCATCH |
| 1184 |
(interp_tactic ist t) |
| 1185 |
(fun () -> interp_tactic ist tt) |
| 1186 |
(fun () -> interp_tactic ist te) |
| 1187 |
| TacOrelse (tac1,tac2) -> |
| 1188 |
Tacticals.tclORELSE (interp_tactic ist tac1) (interp_tactic ist tac2) |
| 1189 |
| TacFirst l -> Tacticals.tclFIRST (List.map (interp_tactic ist) l) |
| 1190 |
| TacSolve l -> Tacticals.tclSOLVE (List.map (interp_tactic ist) l) |
| 1191 |
| TacArg _ -> Ftactic.run (val_interp (ensure_loc loc ist) tac) (fun v -> tactic_of_value ist v) |
| 1192 |
| TacSelect (sel, tac) -> Goal_select.tclSELECT sel (interp_tactic ist tac) |
| 1193 |
|
| 1194 |
|
| 1195 |
| TacAlias (s,l) -> |
| 1196 |
let alias = Tacenv.interp_alias s in |
| 1197 |
Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (_name, poly) -> |
| 1198 |
let (>>=) = Ftactic.bind in |
| 1199 |
let interp_vars = Ftactic.List.map (fun v -> interp_tacarg ist v) l in |
| 1200 |
let tac l = |
| 1201 |
let addvar x v accu = Id.Map.add x v accu in |
| 1202 |
let lfun = List.fold_right2 addvar alias.Tacenv.alias_args l ist.lfun in |
| 1203 |
let trace = push_trace (loc,LtacNotationCall s) ist in |
| 1204 |
let ist = { |
| 1205 |
lfun |
| 1206 |
; poly |
| 1207 |
; extra = add_extra_loc loc (add_extra_trace trace ist.extra) } in |
| 1208 |
val_interp ist alias.Tacenv.alias_body >>= fun v -> |
| 1209 |
Ftactic.lift (tactic_of_value ist v) |
| 1210 |
in |
| 1211 |
let tac = |
| 1212 |
Ftactic.with_env interp_vars >>= fun (env, lr) -> |
| 1213 |
let name () = Pptactic.pr_alias (fun v -> print_top_val env v) 0 s lr in |
| 1214 |
Proofview.Trace.name_tactic name (tac lr) |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
in |
| 1219 |
let tac = |
| 1220 |
let len1 = List.length alias.Tacenv.alias_args in |
| 1221 |
let len2 = List.length l in |
| 1222 |
if len1 = len2 then tac |
| 1223 |
else |
| 1224 |
let info = Exninfo.reify () in |
| 1225 |
Tacticals.tclZEROMSG ~info |
| 1226 |
(str "Arguments length mismatch: \ |
| 1227 |
expected " ++ int len1 ++ str ", found " ++ int len2) |
| 1228 |
in |
| 1229 |
Ftactic.run tac (fun () -> Proofview.tclUNIT ()) |
| 1230 |
|
| 1231 |
| TacML (opn,l) -> |
| 1232 |
let trace = push_trace (Loc.tag ?loc @@ LtacMLCall tac) ist in |
| 1233 |
let ist = { ist with extra = TacStore.set ist.extra f_trace trace; } in |
| 1234 |
let tac = Tacenv.interp_ml_tactic opn in |
| 1235 |
let args = Ftactic.List.map_right (fun a -> interp_tacarg ist a) l in |
| 1236 |
let tac args = |
| 1237 |
let name () = Pptactic.pr_extend (fun v -> print_top_val () v) 0 opn args in |
| 1238 |
let (stack, _) = trace in |
| 1239 |
Proofview.Trace.name_tactic name (catch_error_tac_loc loc stack (tac args ist)) |
| 1240 |
in |
| 1241 |
Ftactic.run args tac |
| 1242 |
|
| 1243 |
and force_vrec ist v : Val.t Ftactic.t = |
| 1244 |
match to_tacvalue v with |
| 1245 |
| Some (VRec (lfun,body)) -> val_interp {ist with lfun = !lfun} body |
| 1246 |
| _ -> Ftactic.return v |
| 1247 |
|
| 1248 |
and interp_ltac_reference ?loc' mustbetac ist r : Val.t Ftactic.t = |
| 1249 |
match r with |
| 1250 |
| ArgVar {loc;v=id} -> |
| 1251 |
let v = |
| 1252 |
try Id.Map.find id ist.lfun |
| 1253 |
with Not_found -> in_gen (topwit wit_hyp) id |
| 1254 |
in |
| 1255 |
let open Ftactic in |
| 1256 |
force_vrec ist v >>= begin fun v -> |
| 1257 |
Ftactic.lift (propagate_trace ist loc id v) >>= fun v -> |
| 1258 |
if mustbetac then Ftactic.return (coerce_to_tactic loc id v) else Ftactic.return v |
| 1259 |
end |
| 1260 |
| ArgArg (loc,r) -> |
| 1261 |
Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (_name, poly) -> |
| 1262 |
let ids = extract_ids [] ist.lfun Id.Set.empty in |
| 1263 |
let loc_info = (Option.default loc loc',LtacNameCall r) in |
| 1264 |
let extra = TacStore.set ist.extra f_avoid_ids ids in |
| 1265 |
let trace = push_trace loc_info ist in |
| 1266 |
let extra = TacStore.set extra f_trace trace in |
| 1267 |
let ist = { lfun = Id.Map.empty; poly; extra } in |
| 1268 |
let appl = GlbAppl[r,[]] in |
| 1269 |
|
| 1270 |
|
| 1271 |
let ist = ensure_loc loc ist in |
| 1272 |
let (stack, _) = trace in |
| 1273 |
do_profile stack ~count_call:false |
| 1274 |
(catch_error_tac_loc loc stack |
| 1275 |
(val_interp ~appl ist (Tacenv.interp_ltac r))) |
| 1276 |
|
| 1277 |
and interp_tacarg ist arg : Val.t Ftactic.t = |
| 1278 |
match arg with |
| 1279 |
| TacGeneric (_,arg) -> interp_genarg ist arg |
| 1280 |
| Reference r -> interp_ltac_reference false ist r |
| 1281 |
| ConstrMayEval c -> |
| 1282 |
Ftactic.enter begin fun gl -> |
| 1283 |
let sigma = Proofview.Goal.sigma gl in |
| 1284 |
let env = Proofview.Goal.env gl in |
| 1285 |
let (sigma,c_interp) = interp_constr_may_eval ist env sigma c in |
| 1286 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1287 |
(Ftactic.return (Value.of_constr c_interp)) |
| 1288 |
end |
| 1289 |
| TacCall { v=(r,[]) } -> |
| 1290 |
interp_ltac_reference true ist r |
| 1291 |
| TacCall { loc; v=(f,l) } -> |
| 1292 |
let (>>=) = Ftactic.bind in |
| 1293 |
interp_ltac_reference true ist f >>= fun fv -> |
| 1294 |
Ftactic.List.map (fun a -> interp_tacarg ist a) l >>= fun largs -> |
| 1295 |
interp_app loc ist fv largs |
| 1296 |
| TacFreshId l -> |
| 1297 |
Ftactic.enter begin fun gl -> |
| 1298 |
let env = Proofview.Goal.env gl in |
| 1299 |
let sigma = Proofview.Goal.sigma gl in |
| 1300 |
let id = interp_fresh_id ist env sigma l in |
| 1301 |
Ftactic.return (in_gen (topwit wit_intropattern) (CAst.make @@ IntroNaming (IntroIdentifier id))) |
| 1302 |
end |
| 1303 |
| TacPretype c -> |
| 1304 |
Ftactic.enter begin fun gl -> |
| 1305 |
let sigma = Proofview.Goal.sigma gl in |
| 1306 |
let env = Proofview.Goal.env gl in |
| 1307 |
let c = interp_uconstr ist env sigma c in |
| 1308 |
let (sigma, c) = type_uconstr ist c env sigma in |
| 1309 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1310 |
(Ftactic.return (Value.of_constr c)) |
| 1311 |
end |
| 1312 |
| TacNumgoals -> |
| 1313 |
Ftactic.lift begin |
| 1314 |
let open Proofview.Notations in |
| 1315 |
Proofview.numgoals >>= fun i -> |
| 1316 |
Proofview.tclUNIT (Value.of_int i) |
| 1317 |
end |
| 1318 |
| Tacexp t -> val_interp ist t |
| 1319 |
|
| 1320 |
|
| 1321 |
and interp_app loc ist fv largs : Val.t Ftactic.t = |
| 1322 |
Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (_name, poly) -> |
| 1323 |
let (>>=) = Ftactic.bind in |
| 1324 |
match to_tacvalue fv with |
| 1325 |
| None | Some (VRec _) -> Tacticals.tclZEROMSG (str "Illegal tactic application.") |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
| Some (VFun(appl,trace,_,olfun,(_::_ as var),body) |
| 1331 |
|VFun(appl,trace,_,olfun,([] as var), |
| 1332 |
( {CAst.v=(TacFun _)} |
| 1333 |
| {CAst.v=(TacLetIn _)} |
| 1334 |
| {CAst.v=(TacMatchGoal _)} |
| 1335 |
| {CAst.v=(TacMatch _)} |
| 1336 |
| {CAst.v=(TacArg _)} as body))) -> |
| 1337 |
let (extfun,lvar,lval)=head_with_value (var,largs) in |
| 1338 |
let fold accu (id, v) = Id.Map.add id v accu in |
| 1339 |
let newlfun = List.fold_left fold olfun extfun in |
| 1340 |
if List.is_empty lvar then |
| 1341 |
begin wrap_error |
| 1342 |
begin |
| 1343 |
let ist = |
| 1344 |
{ lfun = newlfun |
| 1345 |
; poly |
| 1346 |
; extra = TacStore.set ist.extra f_trace trace |
| 1347 |
} in |
| 1348 |
let (stack, _) = trace in |
| 1349 |
do_profile stack ~count_call:false |
| 1350 |
(catch_error_tac_loc loc stack (val_interp (ensure_loc loc ist) body)) >>= fun v -> |
| 1351 |
Ftactic.return (name_vfun (push_appl appl largs) v) |
| 1352 |
end |
| 1353 |
begin fun (e, info) -> |
| 1354 |
Proofview.tclLIFT (debugging_exception_step ist false e (fun () -> str "evaluation")) <*> |
| 1355 |
Proofview.tclZERO ~info e |
| 1356 |
end |
| 1357 |
end >>= fun v -> |
| 1358 |
|
| 1359 |
let v = append_trace trace v in |
| 1360 |
let call_debug env = |
| 1361 |
Proofview.tclLIFT (debugging_step ist (fun () -> str"evaluation returns"++fnl()++pr_value env v)) in |
| 1362 |
begin |
| 1363 |
let open Genprint in |
| 1364 |
match generic_val_print v with |
| 1365 |
| TopPrinterBasic _ -> call_debug None |
| 1366 |
| TopPrinterNeedsContext _ | TopPrinterNeedsContextAndLevel _ -> |
| 1367 |
Proofview.Goal.enter (fun gl -> call_debug (Some (Proofview.Goal.env gl, Proofview.Goal.sigma gl))) |
| 1368 |
end <*> |
| 1369 |
if List.is_empty lval then Ftactic.return v else interp_app loc ist v lval |
| 1370 |
else |
| 1371 |
Ftactic.return (of_tacvalue (VFun(push_appl appl largs,trace,loc,newlfun,lvar,body))) |
| 1372 |
| Some (VFun(appl,trace,_,olfun,[],body)) -> |
| 1373 |
let extra_args = List.length largs in |
| 1374 |
let info = Exninfo.reify () in |
| 1375 |
Tacticals.tclZEROMSG ~info |
| 1376 |
(str "Illegal tactic application: got " ++ |
| 1377 |
str (string_of_int extra_args) ++ |
| 1378 |
str " extra " ++ str (String.plural extra_args "argument") ++ |
| 1379 |
str ".") |
| 1380 |
|
| 1381 |
|
| 1382 |
and tactic_of_value ist vle = |
| 1383 |
match to_tacvalue vle with |
| 1384 |
| Some vle -> |
| 1385 |
begin match vle with |
| 1386 |
| VFun (appl,trace,loc,lfun,[],t) -> |
| 1387 |
Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (_name, poly) -> |
| 1388 |
let ist = { |
| 1389 |
lfun = lfun; |
| 1390 |
poly; |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
extra = TacStore.set ist.extra f_trace (if Profile_tactic.get_profiling() then ([],[]) else trace); } in |
| 1395 |
let tac = name_if_glob appl (eval_tactic_ist ist t) in |
| 1396 |
let (stack, _) = trace in |
| 1397 |
do_profile stack (catch_error_tac_loc loc stack tac) |
| 1398 |
| VFun (appl,(stack,_),loc,vmap,vars,_) -> |
| 1399 |
let tactic_nm = |
| 1400 |
match appl with |
| 1401 |
UnnamedAppl -> "An unnamed user-defined tactic" |
| 1402 |
| GlbAppl apps -> |
| 1403 |
let nms = List.map (fun (kn,_) -> string_of_qualid (Tacenv.shortest_qualid_of_tactic kn)) apps in |
| 1404 |
match nms with |
| 1405 |
[] -> assert false |
| 1406 |
| kn::_ -> "The user-defined tactic \"" ^ kn ^ "\"" |
| 1407 |
in |
| 1408 |
let numargs = List.length vars in |
| 1409 |
let givenargs = |
| 1410 |
List.map (fun (arg,_) -> Names.Id.to_string arg) (Names.Id.Map.bindings vmap) in |
| 1411 |
let numgiven = List.length givenargs in |
| 1412 |
let info = Exninfo.reify () in |
| 1413 |
catch_error_tac stack @@ |
| 1414 |
Tacticals.tclZEROMSG ~info |
| 1415 |
Pp.(str tactic_nm ++ str " was not fully applied:" ++ spc() ++ |
| 1416 |
str "There is a missing argument for variable" ++ spc() ++ Name.print (List.hd vars) ++ |
| 1417 |
(if numargs > 1 then |
| 1418 |
spc() ++ str "and " ++ int (numargs - 1) ++ |
| 1419 |
str " more" |
| 1420 |
else mt()) ++ pr_comma() ++ |
| 1421 |
(match numgiven with |
| 1422 |
| 0 -> |
| 1423 |
str "no arguments at all were provided." |
| 1424 |
| 1 -> |
| 1425 |
str "1 argument was provided." |
| 1426 |
| _ -> |
| 1427 |
int numgiven ++ str " arguments were provided.")) |
| 1428 |
| VRec _ -> |
| 1429 |
let info = Exninfo.reify () in |
| 1430 |
Tacticals.tclZEROMSG ~info (str "A fully applied tactic is expected.") |
| 1431 |
end |
| 1432 |
| None -> |
| 1433 |
if has_type vle (topwit wit_tactic) then |
| 1434 |
let tac = out_gen (topwit wit_tactic) vle in |
| 1435 |
tactic_of_value ist tac |
| 1436 |
else |
| 1437 |
let name = |
| 1438 |
let Dyn (t, _) = vle in |
| 1439 |
Val.repr t |
| 1440 |
in |
| 1441 |
let info = Exninfo.reify () in |
| 1442 |
Tacticals.tclZEROMSG ~info (str "Expression does not evaluate to a tactic (got a " ++ str name ++ str ").") |
| 1443 |
|
| 1444 |
|
| 1445 |
and interp_letrec ist llc u = |
| 1446 |
Proofview.tclUNIT () >>= fun () -> |
| 1447 |
let lref = ref ist.lfun in |
| 1448 |
let fold accu ({v=na}, b) = |
| 1449 |
let v = of_tacvalue (VRec (lref, CAst.make (TacArg b))) in |
| 1450 |
Name.fold_right (fun id -> Id.Map.add id v) na accu |
| 1451 |
in |
| 1452 |
let lfun = List.fold_left fold ist.lfun llc in |
| 1453 |
let () = lref := lfun in |
| 1454 |
let ist = { ist with lfun } in |
| 1455 |
val_interp ist u |
| 1456 |
|
| 1457 |
|
| 1458 |
and interp_letin ist llc u = |
| 1459 |
let rec fold lfun = function |
| 1460 |
| [] -> |
| 1461 |
let ist = { ist with lfun } in |
| 1462 |
val_interp ist u |
| 1463 |
| ({v=na}, body) :: defs -> |
| 1464 |
Ftactic.bind (interp_tacarg ist body) (fun v -> |
| 1465 |
fold (Name.fold_right (fun id -> Id.Map.add id v) na lfun) defs) |
| 1466 |
in |
| 1467 |
fold ist.lfun llc |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
and interp_match_success ist { Tactic_matching.subst ; context ; terms ; lhs } = |
| 1472 |
Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (_name, poly) -> |
| 1473 |
let (>>=) = Ftactic.bind in |
| 1474 |
let lctxt = Id.Map.map Value.of_constr_context context in |
| 1475 |
let hyp_subst = Id.Map.map Value.of_constr terms in |
| 1476 |
let lfun = extend_values_with_bindings subst (lctxt +++ hyp_subst +++ ist.lfun) in |
| 1477 |
let ist = { ist with lfun } in |
| 1478 |
val_interp ist lhs >>= fun v -> |
| 1479 |
match to_tacvalue v with |
| 1480 |
| Some (VFun (appl,trace,loc,lfun,[],t)) -> |
| 1481 |
let ist = |
| 1482 |
{ lfun = lfun |
| 1483 |
; poly |
| 1484 |
; extra = TacStore.set ist.extra f_trace trace |
| 1485 |
} in |
| 1486 |
let tac = eval_tactic_ist ist t in |
| 1487 |
let dummy = VFun (appl, extract_trace ist, loc, Id.Map.empty, [], |
| 1488 |
CAst.make (TacId [])) in |
| 1489 |
let (stack, _) = trace in |
| 1490 |
catch_error_tac stack (tac <*> Ftactic.return (of_tacvalue dummy)) |
| 1491 |
| _ -> Ftactic.return v |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
and interp_match_successes lz ist s = |
| 1499 |
let general = |
| 1500 |
let open Tacticals in |
| 1501 |
let break (e, info) = match e with |
| 1502 |
| FailError (0, _) -> None |
| 1503 |
| FailError (n, s) -> Some (FailError (pred n, s), info) |
| 1504 |
| _ -> None |
| 1505 |
in |
| 1506 |
Proofview.tclBREAK break s >>= fun ans -> interp_match_success ist ans |
| 1507 |
in |
| 1508 |
match lz with |
| 1509 |
| General -> |
| 1510 |
general |
| 1511 |
| Select -> |
| 1512 |
begin |
| 1513 |
|
| 1514 |
let s = Proofview.tclONCE s in |
| 1515 |
s >>= fun ans -> interp_match_success ist ans |
| 1516 |
end |
| 1517 |
| Once -> |
| 1518 |
|
| 1519 |
Proofview.tclONCE general |
| 1520 |
|
| 1521 |
|
| 1522 |
and interp_match ist lz constr lmr = |
| 1523 |
let (>>=) = Ftactic.bind in |
| 1524 |
begin wrap_error |
| 1525 |
(interp_ltac_constr ist constr) |
| 1526 |
begin function |
| 1527 |
| (e, info) -> |
| 1528 |
Proofview.tclLIFT (debugging_exception_step ist true e |
| 1529 |
(fun () -> str "evaluation of the matched expression")) <*> |
| 1530 |
Proofview.tclZERO ~info e |
| 1531 |
end |
| 1532 |
end >>= fun constr -> |
| 1533 |
Ftactic.enter begin fun gl -> |
| 1534 |
let env = Proofview.Goal.env gl in |
| 1535 |
let sigma = Proofview.Goal.sigma gl in |
| 1536 |
let ilr = read_match_rule ist env sigma lmr in |
| 1537 |
interp_match_successes lz ist (Tactic_matching.match_term env sigma constr ilr) |
| 1538 |
end |
| 1539 |
|
| 1540 |
|
| 1541 |
and interp_match_goal ist lz lr lmr = |
| 1542 |
Ftactic.enter begin fun gl -> |
| 1543 |
let env = Proofview.Goal.env gl in |
| 1544 |
let sigma = Proofview.Goal.sigma gl in |
| 1545 |
let hyps = Proofview.Goal.hyps gl in |
| 1546 |
let hyps = if lr then List.rev hyps else hyps in |
| 1547 |
let concl = Proofview.Goal.concl gl in |
| 1548 |
let ilr = read_match_rule ist env sigma lmr in |
| 1549 |
interp_match_successes lz ist (Tactic_matching.match_goal env sigma hyps concl ilr) |
| 1550 |
end |
| 1551 |
|
| 1552 |
|
| 1553 |
and interp_genarg ist x : Val.t Ftactic.t = |
| 1554 |
let open Ftactic.Notations in |
| 1555 |
|
| 1556 |
let tag = genarg_tag x in |
| 1557 |
if argument_type_eq tag (unquote (topwit (wit_list wit_hyp))) then |
| 1558 |
interp_genarg_var_list ist x |
| 1559 |
else if argument_type_eq tag (unquote (topwit (wit_list wit_constr))) then |
| 1560 |
interp_genarg_constr_list ist x |
| 1561 |
else |
| 1562 |
let GenArg (Glbwit wit, x) as x0 = x in |
| 1563 |
match wit with |
| 1564 |
| ListArg wit -> |
| 1565 |
let map x = interp_genarg ist (Genarg.in_gen (glbwit wit) x) in |
| 1566 |
Ftactic.List.map map x >>= fun l -> |
| 1567 |
Ftactic.return (Val.Dyn (Val.typ_list, l)) |
| 1568 |
| OptArg wit -> |
| 1569 |
begin match x with |
| 1570 |
| None -> Ftactic.return (Val.Dyn (Val.typ_opt, None)) |
| 1571 |
| Some x -> |
| 1572 |
interp_genarg ist (Genarg.in_gen (glbwit wit) x) >>= fun x -> |
| 1573 |
Ftactic.return (Val.Dyn (Val.typ_opt, Some x)) |
| 1574 |
end |
| 1575 |
| PairArg (wit1, wit2) -> |
| 1576 |
let (p, q) = x in |
| 1577 |
interp_genarg ist (Genarg.in_gen (glbwit wit1) p) >>= fun p -> |
| 1578 |
interp_genarg ist (Genarg.in_gen (glbwit wit2) q) >>= fun q -> |
| 1579 |
Ftactic.return (Val.Dyn (Val.typ_pair, (p, q))) |
| 1580 |
| ExtraArg s -> |
| 1581 |
Geninterp.generic_interp ist x0 |
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
and interp_genarg_constr_list ist x = |
| 1587 |
Ftactic.enter begin fun gl -> |
| 1588 |
let env = Proofview.Goal.env gl in |
| 1589 |
let sigma = Proofview.Goal.sigma gl in |
| 1590 |
let lc = Genarg.out_gen (glbwit (wit_list wit_constr)) x in |
| 1591 |
let (sigma,lc) = interp_constr_list ist env sigma lc in |
| 1592 |
let lc = in_list (val_tag wit_constr) lc in |
| 1593 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1594 |
(Ftactic.return lc) |
| 1595 |
end |
| 1596 |
|
| 1597 |
and interp_genarg_var_list ist x = |
| 1598 |
Ftactic.enter begin fun gl -> |
| 1599 |
let env = Proofview.Goal.env gl in |
| 1600 |
let sigma = Proofview.Goal.sigma gl in |
| 1601 |
let lc = Genarg.out_gen (glbwit (wit_list wit_hyp)) x in |
| 1602 |
let lc = interp_hyp_list ist env sigma lc in |
| 1603 |
let lc = in_list (val_tag wit_hyp) lc in |
| 1604 |
Ftactic.return lc |
| 1605 |
end |
| 1606 |
|
| 1607 |
|
| 1608 |
and interp_ltac_constr ist e : EConstr.t Ftactic.t = |
| 1609 |
let (>>=) = Ftactic.bind in |
| 1610 |
begin wrap_error |
| 1611 |
(val_interp ist e) |
| 1612 |
begin function (err, info) -> match err with |
| 1613 |
| Not_found -> |
| 1614 |
Ftactic.enter begin fun gl -> |
| 1615 |
let env = Proofview.Goal.env gl in |
| 1616 |
Proofview.tclLIFT begin |
| 1617 |
debugging_step ist (fun () -> |
| 1618 |
str "evaluation failed for" ++ fnl() ++ |
| 1619 |
Pptactic.pr_glob_tactic env e) |
| 1620 |
end |
| 1621 |
<*> Proofview.tclZERO Not_found |
| 1622 |
end |
| 1623 |
| err -> Proofview.tclZERO ~info err |
| 1624 |
end |
| 1625 |
end >>= fun result -> |
| 1626 |
Ftactic.enter begin fun gl -> |
| 1627 |
let env = Proofview.Goal.env gl in |
| 1628 |
let sigma = Proofview.Goal.sigma gl in |
| 1629 |
try |
| 1630 |
let cresult = coerce_to_closed_constr env result in |
| 1631 |
Proofview.tclLIFT begin |
| 1632 |
debugging_step ist (fun () -> |
| 1633 |
Pptactic.pr_glob_tactic env e ++ fnl() ++ |
| 1634 |
str " has value " ++ fnl() ++ |
| 1635 |
pr_econstr_env env sigma cresult) |
| 1636 |
end <*> |
| 1637 |
Ftactic.return cresult |
| 1638 |
with CannotCoerceTo _ as exn -> |
| 1639 |
let _, info = Exninfo.capture exn in |
| 1640 |
let env = Proofview.Goal.env gl in |
| 1641 |
Tacticals.tclZEROMSG ~info |
| 1642 |
(str "Must evaluate to a closed term" ++ fnl() ++ |
| 1643 |
str "offending expression: " ++ fnl() ++ pr_inspect env e result) |
| 1644 |
end |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
and interp_tactic ist tac : unit Proofview.tactic = |
| 1649 |
Ftactic.run (val_interp ist tac) (fun v -> tactic_of_value ist v) |
| 1650 |
|
| 1651 |
|
| 1652 |
and name_atomic ?env tacexpr tac : unit Proofview.tactic = |
| 1653 |
begin match env with |
| 1654 |
| Some e -> Proofview.tclUNIT e |
| 1655 |
| None -> Proofview.tclENV |
| 1656 |
end >>= fun env -> |
| 1657 |
Proofview.tclEVARMAP >>= fun sigma -> |
| 1658 |
let name () = Pptactic.pr_atomic_tactic env sigma tacexpr in |
| 1659 |
Proofview.Trace.name_tactic name tac |
| 1660 |
|
| 1661 |
|
| 1662 |
and interp_atomic ist tac : unit Proofview.tactic = |
| 1663 |
match tac with |
| 1664 |
|
| 1665 |
| TacIntroPattern (ev,l) -> |
| 1666 |
Proofview.Goal.enter begin fun gl -> |
| 1667 |
let env = Proofview.Goal.env gl in |
| 1668 |
let sigma = Proofview.Goal.sigma gl in |
| 1669 |
let l' = interp_intro_pattern_list_as_list ist env sigma l in |
| 1670 |
name_atomic ~env |
| 1671 |
(TacIntroPattern (ev,l)) |
| 1672 |
|
| 1673 |
|
| 1674 |
(Tactics.intro_patterns ev l') |
| 1675 |
end |
| 1676 |
| TacApply (a,ev,cb,cl) -> |
| 1677 |
|
| 1678 |
Proofview.Trace.name_tactic (fun () -> Pp.str"<apply>") begin |
| 1679 |
Proofview.Goal.enter begin fun gl -> |
| 1680 |
let env = Proofview.Goal.env gl in |
| 1681 |
let sigma = Proofview.Goal.sigma gl in |
| 1682 |
let l = List.map (fun (k,c) -> |
| 1683 |
let loc, f = interp_open_constr_with_bindings_loc ist c in |
| 1684 |
let f = Tacticals.tactic_of_delayed f in |
| 1685 |
(k,(CAst.make ?loc f))) cb |
| 1686 |
in |
| 1687 |
let tac = match cl with |
| 1688 |
| [] -> Tactics.apply_with_delayed_bindings_gen a ev l |
| 1689 |
| cl -> |
| 1690 |
let cl = List.map (interp_in_hyp_as ist env sigma) cl in |
| 1691 |
List.fold_right (fun (id,ipat) -> Tactics.apply_delayed_in a ev id l ipat) cl Tacticals.tclIDTAC in |
| 1692 |
tac |
| 1693 |
end |
| 1694 |
end |
| 1695 |
| TacElim (ev,(keep,cb),cbo) -> |
| 1696 |
Proofview.Goal.enter begin fun gl -> |
| 1697 |
let env = Proofview.Goal.env gl in |
| 1698 |
let sigma = Proofview.Goal.sigma gl in |
| 1699 |
let sigma, cb = interp_open_constr_with_bindings ist env sigma cb in |
| 1700 |
let sigma, cbo = Option.fold_left_map (interp_open_constr_with_bindings ist env) sigma cbo in |
| 1701 |
let named_tac = |
| 1702 |
let tac = Tactics.elim ev keep cb cbo in |
| 1703 |
name_atomic ~env (TacElim (ev,(keep,cb),cbo)) tac |
| 1704 |
in |
| 1705 |
Tacticals.tclWITHHOLES ev named_tac sigma |
| 1706 |
end |
| 1707 |
| TacCase (ev,(keep,cb)) -> |
| 1708 |
Proofview.Goal.enter begin fun gl -> |
| 1709 |
let env = Proofview.Goal.env gl in |
| 1710 |
let sigma = Proofview.Goal.sigma gl in |
| 1711 |
let sigma, cb = interp_open_constr_with_bindings ist env sigma cb in |
| 1712 |
let named_tac = |
| 1713 |
let tac = Tactics.general_case_analysis ev keep cb in |
| 1714 |
name_atomic ~env (TacCase(ev,(keep,cb))) tac |
| 1715 |
in |
| 1716 |
Tacticals.tclWITHHOLES ev named_tac sigma |
| 1717 |
end |
| 1718 |
| TacMutualFix (id,n,l) -> |
| 1719 |
|
| 1720 |
Proofview.Trace.name_tactic (fun () -> Pp.str"<mutual fix>") begin |
| 1721 |
Proofview.Goal.enter begin fun gl -> |
| 1722 |
let env = Proofview.Goal.env gl in |
| 1723 |
let sigma = Proofview.Goal.sigma gl in |
| 1724 |
let f sigma (id,n,c) = |
| 1725 |
let (sigma,c_interp) = interp_type ist env sigma c in |
| 1726 |
sigma , (interp_ident ist env sigma id,n,c_interp) in |
| 1727 |
let (sigma,l_interp) = |
| 1728 |
Evd.MonadR.List.map_right (fun c sigma -> f sigma c) l sigma |
| 1729 |
in |
| 1730 |
Tacticals.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1731 |
(FixTactics.mutual_fix (interp_ident ist env sigma id) n l_interp) |
| 1732 |
end |
| 1733 |
end |
| 1734 |
| TacMutualCofix (id,l) -> |
| 1735 |
|
| 1736 |
Proofview.Trace.name_tactic (fun () -> Pp.str"<mutual cofix>") begin |
| 1737 |
Proofview.Goal.enter begin fun gl -> |
| 1738 |
let env = Proofview.Goal.env gl in |
| 1739 |
let sigma = Proofview.Goal.sigma gl in |
| 1740 |
let f sigma (id,c) = |
| 1741 |
let (sigma,c_interp) = interp_type ist env sigma c in |
| 1742 |
sigma , (interp_ident ist env sigma id,c_interp) in |
| 1743 |
let (sigma,l_interp) = |
| 1744 |
Evd.MonadR.List.map_right (fun c sigma -> f sigma c) l sigma |
| 1745 |
in |
| 1746 |
Tacticals.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1747 |
(FixTactics.mutual_cofix (interp_ident ist env sigma id) l_interp) |
| 1748 |
end |
| 1749 |
end |
| 1750 |
| TacAssert (ev,b,t,ipat,c) -> |
| 1751 |
Proofview.Goal.enter begin fun gl -> |
| 1752 |
let env = Proofview.Goal.env gl in |
| 1753 |
let sigma = Proofview.Goal.sigma gl in |
| 1754 |
let (sigma,c) = |
| 1755 |
let expected_type = |
| 1756 |
if Option.is_empty t then WithoutTypeConstraint else IsType in |
| 1757 |
let flags = open_constr_use_classes_flags () in |
| 1758 |
interp_open_constr ~expected_type ~flags ist env sigma c |
| 1759 |
in |
| 1760 |
let ipat' = interp_intro_pattern_option ist env sigma ipat in |
| 1761 |
let tac = Option.map (Option.map (interp_tactic ist)) t in |
| 1762 |
Tacticals.tclWITHHOLES ev |
| 1763 |
(name_atomic ~env |
| 1764 |
(TacAssert(ev,b,Option.map (Option.map ignore) t,ipat,c)) |
| 1765 |
(Tactics.forward b tac ipat' c)) sigma |
| 1766 |
end |
| 1767 |
| TacGeneralize cl -> |
| 1768 |
Proofview.Goal.enter begin fun gl -> |
| 1769 |
let sigma = Proofview.Goal.sigma gl in |
| 1770 |
let env = Proofview.Goal.env gl in |
| 1771 |
let sigma, cl = interp_constr_with_occurrences_and_name_as_list ist env sigma cl in |
| 1772 |
Tacticals.tclWITHHOLES false |
| 1773 |
(name_atomic ~env |
| 1774 |
(TacGeneralize cl) |
| 1775 |
(Generalize.generalize_gen cl)) sigma |
| 1776 |
end |
| 1777 |
| TacLetTac (ev,na,c,clp,b,eqpat) -> |
| 1778 |
Proofview.Goal.enter begin fun gl -> |
| 1779 |
let env = Proofview.Goal.env gl in |
| 1780 |
let sigma = Proofview.Goal.sigma gl in |
| 1781 |
let clp = interp_clause ist env sigma clp in |
| 1782 |
let eqpat = interp_intro_pattern_naming_option ist env sigma eqpat in |
| 1783 |
if Locusops.is_nowhere clp then |
| 1784 |
|
| 1785 |
let flags = open_constr_use_classes_flags () in |
| 1786 |
let (sigma,c_interp) = interp_open_constr ~flags ist env sigma c in |
| 1787 |
let na = interp_name ist env sigma na in |
| 1788 |
let let_tac = |
| 1789 |
if b then Tactics.pose_tac na c_interp |
| 1790 |
else |
| 1791 |
let id = Option.default (CAst.make IntroAnonymous) eqpat in |
| 1792 |
let with_eq = Some (true, id) in |
| 1793 |
Tactics.letin_tac with_eq na c_interp None Locusops.nowhere |
| 1794 |
in |
| 1795 |
Tacticals.tclWITHHOLES ev |
| 1796 |
(name_atomic ~env |
| 1797 |
(TacLetTac(ev,na,c_interp,clp,b,eqpat)) |
| 1798 |
let_tac) sigma |
| 1799 |
else |
| 1800 |
|
| 1801 |
let let_pat_tac b na c cl eqpat = |
| 1802 |
let id = Option.default (CAst.make IntroAnonymous) eqpat in |
| 1803 |
let with_eq = if b then None else Some (true,id) in |
| 1804 |
Tactics.letin_pat_tac ev with_eq na c cl |
| 1805 |
in |
| 1806 |
let (sigma',c) = interp_pure_open_constr ist env sigma c in |
| 1807 |
name_atomic ~env |
| 1808 |
(TacLetTac(ev,na,c,clp,b,eqpat)) |
| 1809 |
(Tacticals.tclWITHHOLES ev |
| 1810 |
(let_pat_tac b (interp_name ist env sigma na) |
| 1811 |
(Some sigma,c) clp eqpat) sigma') |
| 1812 |
end |
| 1813 |
|
| 1814 |
|
| 1815 |
| TacInductionDestruct (isrec,ev,(l,el)) -> |
| 1816 |
|
| 1817 |
|
| 1818 |
Proofview.Goal.enter begin fun gl -> |
| 1819 |
let env = Proofview.Goal.env gl in |
| 1820 |
let sigma = Proofview.Goal.sigma gl in |
| 1821 |
let l = |
| 1822 |
List.map begin fun (c,(ipato,ipats),cls) -> |
| 1823 |
|
| 1824 |
|
| 1825 |
let cp = c in |
| 1826 |
let c = interp_destruction_arg ist gl c in |
| 1827 |
let ipato = interp_intro_pattern_naming_option ist env sigma ipato in |
| 1828 |
let ipatsp = ipats in |
| 1829 |
let ipats = interp_or_and_intro_pattern_option ist env sigma ipats in |
| 1830 |
let cls = Option.map (interp_clause ist env sigma) cls in |
| 1831 |
((c,(ipato,ipats),cls),(cp,(ipato,ipatsp),cls)) |
| 1832 |
end l |
| 1833 |
in |
| 1834 |
let l,lp = List.split l in |
| 1835 |
let sigma,el = |
| 1836 |
Option.fold_left_map (interp_open_constr_with_bindings ist env) sigma el in |
| 1837 |
Tacticals.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1838 |
(name_atomic ~env |
| 1839 |
(TacInductionDestruct(isrec,ev,(lp,el))) |
| 1840 |
(Induction.induction_destruct isrec ev (l,el))) |
| 1841 |
end |
| 1842 |
|
| 1843 |
|
| 1844 |
| TacReduce (r,cl) -> |
| 1845 |
Proofview.Goal.enter begin fun gl -> |
| 1846 |
let env = Proofview.Goal.env gl in |
| 1847 |
let sigma = Proofview.Goal.sigma gl in |
| 1848 |
let (sigma,r_interp) = interp_red_expr ist env sigma r in |
| 1849 |
Tacticals.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1850 |
(Tactics.reduce r_interp (interp_clause ist env sigma cl)) |
| 1851 |
end |
| 1852 |
| TacChange (check,None,c,cl) -> |
| 1853 |
|
| 1854 |
Proofview.Trace.name_tactic (fun () -> Pp.str"<change>") begin |
| 1855 |
Proofview.Goal.enter begin fun gl -> |
| 1856 |
let env = Proofview.Goal.env gl in |
| 1857 |
let sigma = Proofview.Goal.sigma gl in |
| 1858 |
let is_onhyps = match cl.onhyps with |
| 1859 |
| None | Some [] -> true |
| 1860 |
| _ -> false |
| 1861 |
in |
| 1862 |
let is_onconcl = match cl.concl_occs with |
| 1863 |
| AtLeastOneOccurrence | AllOccurrences | NoOccurrences -> true |
| 1864 |
| _ -> false |
| 1865 |
in |
| 1866 |
let c_interp patvars env sigma = |
| 1867 |
let lfun' = Id.Map.fold (fun id c lfun -> |
| 1868 |
Id.Map.add id (Value.of_constr c) lfun) |
| 1869 |
patvars ist.lfun |
| 1870 |
in |
| 1871 |
let ist = { ist with lfun = lfun' } in |
| 1872 |
if is_onhyps && is_onconcl |
| 1873 |
then Changed (interp_type ist env sigma c) |
| 1874 |
else Changed (interp_constr ist env sigma c) |
| 1875 |
in |
| 1876 |
Tactics.change ~check None c_interp (interp_clause ist env sigma cl) |
| 1877 |
end |
| 1878 |
end |
| 1879 |
| TacChange (check,Some op,c,cl) -> |
| 1880 |
|
| 1881 |
Proofview.Trace.name_tactic (fun () -> Pp.str"<change>") begin |
| 1882 |
Proofview.Goal.enter begin fun gl -> |
| 1883 |
let env = Proofview.Goal.env gl in |
| 1884 |
let sigma = Proofview.Goal.sigma gl in |
| 1885 |
let op = interp_typed_pattern ist env sigma op in |
| 1886 |
let to_catch = function Not_found -> true | e -> CErrors.is_sync_anomaly e in |
| 1887 |
let c_interp patvars env sigma = |
| 1888 |
let lfun' = Id.Map.fold (fun id c lfun -> |
| 1889 |
Id.Map.add id (Value.of_constr c) lfun) |
| 1890 |
patvars ist.lfun |
| 1891 |
in |
| 1892 |
let env = ensure_freshness env in |
| 1893 |
let ist = { ist with lfun = lfun' } in |
| 1894 |
try |
| 1895 |
Changed (interp_constr ist env sigma c) |
| 1896 |
with e when to_catch e -> |
| 1897 |
user_err (strbrk "Failed to get enough information from the left-hand side to type the right-hand side.") |
| 1898 |
in |
| 1899 |
Tactics.change ~check (Some op) c_interp (interp_clause ist env sigma cl) |
| 1900 |
end |
| 1901 |
end |
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
| TacRewrite (ev,l,cl,by) -> |
| 1906 |
Proofview.Goal.enter begin fun gl -> |
| 1907 |
let l' = List.map (fun (b,m,(keep,c)) -> |
| 1908 |
let f env sigma = |
| 1909 |
interp_open_constr_with_bindings ist env sigma c |
| 1910 |
in |
| 1911 |
(b,m,keep,f)) l in |
| 1912 |
let env = Proofview.Goal.env gl in |
| 1913 |
let sigma = Proofview.Goal.sigma gl in |
| 1914 |
let cl = interp_clause ist env sigma cl in |
| 1915 |
name_atomic ~env |
| 1916 |
(TacRewrite (ev,l,cl,Option.map ignore by)) |
| 1917 |
(Equality.general_multi_rewrite ev l' cl |
| 1918 |
(Option.map (fun by -> Tacticals.tclCOMPLETE (interp_tactic ist by), |
| 1919 |
Equality.Naive) |
| 1920 |
by)) |
| 1921 |
end |
| 1922 |
| TacInversion (DepInversion (k,c,ids),hyp) -> |
| 1923 |
Proofview.Goal.enter begin fun gl -> |
| 1924 |
let env = Proofview.Goal.env gl in |
| 1925 |
let sigma = Proofview.Goal.sigma gl in |
| 1926 |
let (sigma,c_interp) = |
| 1927 |
match c with |
| 1928 |
| None -> sigma , None |
| 1929 |
| Some c -> |
| 1930 |
let (sigma,c_interp) = interp_constr ist env sigma c in |
| 1931 |
sigma , Some c_interp |
| 1932 |
in |
| 1933 |
let dqhyps = interp_declared_or_quantified_hypothesis ist env sigma hyp in |
| 1934 |
let ids_interp = interp_or_and_intro_pattern_option ist env sigma ids in |
| 1935 |
Tacticals.tclWITHHOLES false |
| 1936 |
(name_atomic ~env |
| 1937 |
(TacInversion(DepInversion(k,c_interp,ids),dqhyps)) |
| 1938 |
(Inv.dinv k c_interp ids_interp dqhyps)) sigma |
| 1939 |
end |
| 1940 |
| TacInversion (NonDepInversion (k,idl,ids),hyp) -> |
| 1941 |
Proofview.Goal.enter begin fun gl -> |
| 1942 |
let env = Proofview.Goal.env gl in |
| 1943 |
let sigma = Proofview.Goal.sigma gl in |
| 1944 |
let hyps = interp_hyp_list ist env sigma idl in |
| 1945 |
let dqhyps = interp_declared_or_quantified_hypothesis ist env sigma hyp in |
| 1946 |
let ids_interp = interp_or_and_intro_pattern_option ist env sigma ids in |
| 1947 |
name_atomic ~env |
| 1948 |
(TacInversion (NonDepInversion (k,hyps,ids),dqhyps)) |
| 1949 |
(Inv.inv_clause k ids_interp hyps dqhyps) |
| 1950 |
end |
| 1951 |
| TacInversion (InversionUsing (c,idl),hyp) -> |
| 1952 |
Proofview.Goal.enter begin fun gl -> |
| 1953 |
let env = Proofview.Goal.env gl in |
| 1954 |
let sigma = Proofview.Goal.sigma gl in |
| 1955 |
let (sigma,c_interp) = interp_constr ist env sigma c in |
| 1956 |
let dqhyps = interp_declared_or_quantified_hypothesis ist env sigma hyp in |
| 1957 |
let hyps = interp_hyp_list ist env sigma idl in |
| 1958 |
Tacticals.tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 1959 |
(name_atomic ~env |
| 1960 |
(TacInversion (InversionUsing (c_interp,hyps),dqhyps)) |
| 1961 |
(Leminv.lemInv_clause dqhyps c_interp hyps)) |
| 1962 |
end |
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
let default_ist () = |
| 1967 |
let extra = TacStore.set TacStore.empty f_debug (get_debug ()) in |
| 1968 |
{ lfun = Id.Map.empty; poly = PolyFlags.default; extra = extra } |
| 1969 |
|
| 1970 |
let eval_tactic t = |
| 1971 |
if get_debug () <> DebugOff then |
| 1972 |
Proofview.tclUNIT () >>= fun () -> |
| 1973 |
Proofview.tclLIFT (db_initialize true) <*> |
| 1974 |
eval_tactic_ist (default_ist ()) t |
| 1975 |
else |
| 1976 |
Proofview.tclUNIT () >>= fun () -> |
| 1977 |
eval_tactic_ist (default_ist ()) t |
| 1978 |
|
| 1979 |
let eval_tactic_ist ist t = |
| 1980 |
Proofview.tclLIFT (db_initialize false) <*> |
| 1981 |
eval_tactic_ist ist t |
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
module Value = struct |
| 1986 |
|
| 1987 |
include Taccoerce.Value |
| 1988 |
|
| 1989 |
let of_closure ist tac = |
| 1990 |
let closure = VFun (UnnamedAppl, extract_trace ist, None, ist.lfun, [], tac) in |
| 1991 |
of_tacvalue closure |
| 1992 |
|
| 1993 |
let apply_expr f args = |
| 1994 |
let fold arg (i, vars, lfun) = |
| 1995 |
let id = Id.of_string ("x" ^ string_of_int i) in |
| 1996 |
let x = Reference (ArgVar CAst.(make id)) in |
| 1997 |
(succ i, x :: vars, Id.Map.add id arg lfun) |
| 1998 |
in |
| 1999 |
let (_, args, lfun) = List.fold_right fold args (0, [], Id.Map.empty) in |
| 2000 |
let lfun = Id.Map.add (Id.of_string "F") f lfun in |
| 2001 |
let ist = { (default_ist ()) with lfun = lfun; } in |
| 2002 |
ist, CAst.make @@ TacArg (TacCall (CAst.make (ArgVar CAst.(make @@ Id.of_string "F"),args))) |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
let apply (f : value) (args: value list) = |
| 2007 |
let ist, tac = apply_expr f args in |
| 2008 |
eval_tactic_ist ist tac |
| 2009 |
|
| 2010 |
let apply_val (f : value) (args: value list) = |
| 2011 |
let ist, tac = apply_expr f args in |
| 2012 |
val_interp ist tac |
| 2013 |
|
| 2014 |
end |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
let interp_tac_gen lfun avoid_ids debug t = |
| 2020 |
Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (_name, poly) -> |
| 2021 |
Proofview.Goal.enter begin fun gl -> |
| 2022 |
let env = Proofview.Goal.env gl in |
| 2023 |
let extra = TacStore.set TacStore.empty f_debug debug in |
| 2024 |
let extra = TacStore.set extra f_avoid_ids avoid_ids in |
| 2025 |
let ist = { lfun; poly; extra } in |
| 2026 |
let ltacvars = Id.Map.domain lfun in |
| 2027 |
eval_tactic_ist ist |
| 2028 |
(intern_pure_tactic { (Genintern.empty_glob_sign ~strict:false env) with ltacvars } t) |
| 2029 |
end |
| 2030 |
|
| 2031 |
let interp t = interp_tac_gen Id.Map.empty Id.Set.empty (get_debug()) t |
| 2032 |
|
| 2033 |
|
| 2034 |
type ltac_expr = { |
| 2035 |
global: bool; |
| 2036 |
ast: Tacexpr.raw_tactic_expr; |
| 2037 |
} |
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
let hide_interp {global;ast} = |
| 2042 |
let hide_interp env = |
| 2043 |
let ist = Genintern.empty_glob_sign ~strict:false env in |
| 2044 |
let te = intern_pure_tactic ist ast in |
| 2045 |
let t = eval_tactic te in |
| 2046 |
t |
| 2047 |
in |
| 2048 |
if global then |
| 2049 |
Proofview.tclENV >>= fun env -> |
| 2050 |
hide_interp env |
| 2051 |
else |
| 2052 |
Proofview.Goal.enter begin fun gl -> |
| 2053 |
hide_interp (Proofview.Goal.env gl) |
| 2054 |
end |
| 2055 |
|
| 2056 |
let ComTactic.Interpreter hide_interp = ComTactic.register_tactic_interpreter "ltac1" hide_interp |
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
let register_interp0 wit f = |
| 2062 |
let open Ftactic.Notations in |
| 2063 |
let interp ist v = |
| 2064 |
f ist v >>= fun v -> Ftactic.return (Val.inject (val_tag wit) v) |
| 2065 |
in |
| 2066 |
Geninterp.register_interp0 wit interp |
| 2067 |
|
| 2068 |
let def_intern ist x = (ist, x) |
| 2069 |
let def_subst _ x = x |
| 2070 |
let def_interp ist x = Ftactic.return x |
| 2071 |
|
| 2072 |
let declare_uniform t = |
| 2073 |
Genintern.register_intern0 t def_intern; |
| 2074 |
Gensubst.register_subst0 t def_subst; |
| 2075 |
register_interp0 t def_interp |
| 2076 |
|
| 2077 |
let () = |
| 2078 |
declare_uniform wit_unit |
| 2079 |
|
| 2080 |
let () = |
| 2081 |
declare_uniform wit_int |
| 2082 |
|
| 2083 |
let () = |
| 2084 |
declare_uniform wit_nat |
| 2085 |
|
| 2086 |
let () = |
| 2087 |
declare_uniform wit_bool |
| 2088 |
|
| 2089 |
let () = |
| 2090 |
declare_uniform wit_string |
| 2091 |
|
| 2092 |
let lift f = (); fun ist x -> Ftactic.enter begin fun gl -> |
| 2093 |
let env = Proofview.Goal.env gl in |
| 2094 |
let sigma = Proofview.Goal.sigma gl in |
| 2095 |
Ftactic.return (f ist env sigma x) |
| 2096 |
end |
| 2097 |
|
| 2098 |
let lifts f = (); fun ist x -> Ftactic.enter begin fun gl -> |
| 2099 |
let env = Proofview.Goal.env gl in |
| 2100 |
let sigma = Proofview.Goal.sigma gl in |
| 2101 |
let (sigma, v) = f ist env sigma x in |
| 2102 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (Ftactic.return v) |
| 2103 |
end |
| 2104 |
|
| 2105 |
let interp_bindings' ist bl = Ftactic.return begin fun env sigma -> |
| 2106 |
interp_bindings ist env sigma bl |
| 2107 |
end |
| 2108 |
|
| 2109 |
let interp_constr_with_bindings' ist c = Ftactic.return begin fun env sigma -> |
| 2110 |
interp_constr_with_bindings ist env sigma c |
| 2111 |
end |
| 2112 |
|
| 2113 |
let interp_open_constr_with_bindings' ist c = Ftactic.return begin fun env sigma -> |
| 2114 |
interp_open_constr_with_bindings ist env sigma c |
| 2115 |
end |
| 2116 |
|
| 2117 |
let interp_destruction_arg' ist c = Ftactic.enter begin fun gl -> |
| 2118 |
Ftactic.return (interp_destruction_arg ist gl c) |
| 2119 |
end |
| 2120 |
|
| 2121 |
let interp_pre_ident ist env sigma s = |
| 2122 |
s |> Id.of_string |> interp_ident ist env sigma |> Id.to_string |
| 2123 |
|
| 2124 |
let () = |
| 2125 |
register_interp0 wit_int_or_var (fun ist n -> Ftactic.return (interp_int_or_var ist n)); |
| 2126 |
register_interp0 wit_nat_or_var (fun ist n -> Ftactic.return (interp_int_or_var ist n)); |
| 2127 |
register_interp0 wit_smart_global (lift interp_reference); |
| 2128 |
register_interp0 wit_ref (lift interp_reference); |
| 2129 |
register_interp0 wit_pre_ident (lift interp_pre_ident); |
| 2130 |
register_interp0 wit_ident (lift interp_ident); |
| 2131 |
register_interp0 wit_hyp (lift interp_hyp); |
| 2132 |
register_interp0 wit_intropattern (lift interp_intro_pattern) [@warning "-3"]; |
| 2133 |
register_interp0 wit_simple_intropattern (lift interp_intro_pattern); |
| 2134 |
register_interp0 wit_clause_dft_concl (lift interp_clause); |
| 2135 |
register_interp0 wit_constr (lifts interp_constr); |
| 2136 |
register_interp0 Redexpr.wit_red_expr (lifts interp_red_expr); |
| 2137 |
register_interp0 wit_quant_hyp (lift interp_declared_or_quantified_hypothesis); |
| 2138 |
register_interp0 wit_open_constr (lifts interp_open_constr); |
| 2139 |
register_interp0 wit_bindings interp_bindings'; |
| 2140 |
register_interp0 wit_constr_with_bindings interp_constr_with_bindings'; |
| 2141 |
register_interp0 wit_open_constr_with_bindings interp_open_constr_with_bindings'; |
| 2142 |
register_interp0 wit_destruction_arg interp_destruction_arg'; |
| 2143 |
() |
| 2144 |
|
| 2145 |
let () = |
| 2146 |
let interp ist tac = Ftactic.return (Value.of_closure ist tac) in |
| 2147 |
register_interp0 wit_tactic interp |
| 2148 |
|
| 2149 |
let () = |
| 2150 |
let interp ist tac = eval_tactic_ist ist tac >>= fun () -> Ftactic.return () in |
| 2151 |
register_interp0 wit_ltac interp |
| 2152 |
|
| 2153 |
let () = |
| 2154 |
register_interp0 wit_uconstr (fun ist c -> Ftactic.enter begin fun gl -> |
| 2155 |
Ftactic.return (interp_uconstr ist (Proofview.Goal.env gl) (Proofview.Goal.sigma gl) c) |
| 2156 |
end) |
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
let val_interp ist tac k = Ftactic.run (val_interp ist tac) k |
| 2162 |
|
| 2163 |
let interp_ltac_constr ist c k = Ftactic.run (interp_ltac_constr ist c) k |
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
let () = |
| 2169 |
let eval ?loc ~poly env sigma tycon (used_ntnvars,tac) = |
| 2170 |
let lfun = GlobEnv.lfun env in |
| 2171 |
let () = assert (Id.Set.subset used_ntnvars (Id.Map.domain lfun)) in |
| 2172 |
let extra = TacStore.set TacStore.empty f_debug (get_debug ()) in |
| 2173 |
let ist = { lfun; poly; extra; } in |
| 2174 |
let tac = eval_tactic_ist ist tac in |
| 2175 |
|
| 2176 |
|
| 2177 |
|
| 2178 |
let name = Id.of_string "ltac_gen" in |
| 2179 |
let sigma, ty = match tycon with |
| 2180 |
| Some ty -> sigma, ty |
| 2181 |
| None -> GlobEnv.new_type_evar env sigma ~src:(loc,Evar_kinds.InternalHole) |
| 2182 |
in |
| 2183 |
let (c, sigma) = Subproof.refine_by_tactic ~name ~poly (GlobEnv.renamed_env env) sigma ty tac in |
| 2184 |
let j = { Environ.uj_val = c; uj_type = ty } in |
| 2185 |
(j, sigma) |
| 2186 |
in |
| 2187 |
GlobEnv.register_constr_interp0 wit_ltac_in_term eval |
| 2188 |
|
| 2189 |
let vernac_debug b = |
| 2190 |
set_debug (if b then Tactic_debug.DebugOn 0 else Tactic_debug.DebugOff) |
| 2191 |
|
| 2192 |
let () = |
| 2193 |
let open Goptions in |
| 2194 |
declare_bool_option |
| 2195 |
{ optstage = Summary.Stage.Interp; |
| 2196 |
optdepr = None; |
| 2197 |
optkey = ["Ltac";"Debug"]; |
| 2198 |
optread = (fun () -> get_debug () != Tactic_debug.DebugOff); |
| 2199 |
optwrite = vernac_debug } |
| 2200 |
|
| 2201 |
let () = |
| 2202 |
let open Goptions in |
| 2203 |
declare_bool_option |
| 2204 |
{ optstage = Summary.Stage.Interp; |
| 2205 |
optdepr = None; |
| 2206 |
optkey = ["Ltac"; "Backtrace"]; |
| 2207 |
optread = (fun () -> !log_trace); |
| 2208 |
optwrite = (fun b -> log_trace := b) } |