| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Names |
| 13 |
open Context |
| 14 |
|
| 15 |
open Ltac_plugin |
| 16 |
|
| 17 |
open Proofview |
| 18 |
open Notations |
| 19 |
|
| 20 |
open Ssrcommon |
| 21 |
open Ssrast |
| 22 |
|
| 23 |
module AdaptorDb = struct |
| 24 |
|
| 25 |
type kind = Forward | Backward | Equivalence |
| 26 |
|
| 27 |
module AdaptorKind = struct |
| 28 |
type t = kind |
| 29 |
let compare = Stdlib.compare |
| 30 |
end |
| 31 |
module AdaptorMap = Map.Make(AdaptorKind) |
| 32 |
|
| 33 |
let term_view_adaptor_db = |
| 34 |
Summary.ref ~name:"view_adaptor_db" AdaptorMap.empty |
| 35 |
|
| 36 |
let get k = |
| 37 |
try AdaptorMap.find k !term_view_adaptor_db |
| 38 |
with Not_found -> [] |
| 39 |
|
| 40 |
let cache_adaptor (k, t) = |
| 41 |
let lk = get k in |
| 42 |
if not (List.exists (Glob_ops.glob_constr_eq t) lk) then |
| 43 |
term_view_adaptor_db := AdaptorMap.add k (t :: lk) !term_view_adaptor_db |
| 44 |
|
| 45 |
let subst_adaptor (subst, (k, t as a)) = |
| 46 |
let t' = Detyping.subst_glob_constr (Global.env()) subst t in |
| 47 |
if t' == t then a else k, t' |
| 48 |
|
| 49 |
let in_db = |
| 50 |
let open Libobject in |
| 51 |
declare_object @@ global_object_nodischarge "VIEW_ADAPTOR_DB" |
| 52 |
~cache:cache_adaptor |
| 53 |
~subst:(Some subst_adaptor) |
| 54 |
|
| 55 |
let declare kind terms = |
| 56 |
List.iter (fun term -> Lib.add_leaf (in_db (kind,term))) |
| 57 |
(List.rev terms) |
| 58 |
|
| 59 |
end |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
let reduce_or l = tclUNIT (List.fold_left (||) false l) |
| 64 |
|
| 65 |
module State : sig |
| 66 |
|
| 67 |
|
| 68 |
val vsINIT : view:EConstr.t -> subject_name:Id.t list -> to_clear:Id.t list -> unit tactic |
| 69 |
val vsPUSH : (EConstr.t -> (EConstr.t * Id.t list) tactic) -> unit tactic |
| 70 |
val vsCONSUME : (names:Id.t list -> EConstr.t -> to_clear:Id.t list -> unit tactic) -> unit tactic |
| 71 |
|
| 72 |
|
| 73 |
val vsCONSUME_IF_PRESENT : (names:Id.t list -> EConstr.t option -> to_clear:Id.t list -> bool tactic) -> bool tactic |
| 74 |
val vsASSERT_EMPTY : unit tactic |
| 75 |
|
| 76 |
end = struct |
| 77 |
|
| 78 |
type vstate = { |
| 79 |
subject_name : Id.t list; |
| 80 |
|
| 81 |
view : EConstr.t; |
| 82 |
to_clear : Id.t list; |
| 83 |
} |
| 84 |
|
| 85 |
include Ssrcommon.MakeState(struct |
| 86 |
type state = vstate option |
| 87 |
let init = None |
| 88 |
let name = "ssrview" |
| 89 |
end) |
| 90 |
|
| 91 |
let vsINIT ~view ~subject_name ~to_clear = |
| 92 |
tclSET (Some { subject_name; view; to_clear }) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
let vsBOOTSTRAP = Goal.enter_one ~__LOC__ begin fun gl -> |
| 97 |
let env = Proofview.Goal.env gl in |
| 98 |
let concl = Goal.concl gl in |
| 99 |
let id = |
| 100 |
let open EConstr in |
| 101 |
match kind_of_type (Goal.sigma gl) concl with |
| 102 |
| ProdType({binder_name=Name.Name id}, _, _) |
| 103 |
when Ssrcommon.is_discharged_id id -> id |
| 104 |
| _ -> mk_anon_id "view_subject" (Environ.named_context_val env) in |
| 105 |
let view = EConstr.mkVar id in |
| 106 |
Ssrcommon.tclINTRO_ID id <*> |
| 107 |
tclSET (Some { subject_name = [id]; view; to_clear = [] }) |
| 108 |
end |
| 109 |
|
| 110 |
let rec vsPUSH k = |
| 111 |
tclINDEPENDENT (tclGET (function |
| 112 |
| Some { subject_name; view; to_clear } -> |
| 113 |
k view >>= fun (view, clr) -> |
| 114 |
tclSET (Some { subject_name; view; to_clear = to_clear @ clr }) |
| 115 |
| None -> vsBOOTSTRAP <*> vsPUSH k)) |
| 116 |
|
| 117 |
let rec vsCONSUME k = |
| 118 |
tclINDEPENDENT (tclGET (function |
| 119 |
| Some { subject_name; view; to_clear } -> |
| 120 |
tclSET None <*> |
| 121 |
k ~names:subject_name view ~to_clear |
| 122 |
| None -> vsBOOTSTRAP <*> vsCONSUME k)) |
| 123 |
|
| 124 |
let vsCONSUME_IF_PRESENT k = |
| 125 |
tclINDEPENDENTL (tclGET1 (function |
| 126 |
| Some { subject_name; view; to_clear } -> |
| 127 |
tclSET None <*> |
| 128 |
k ~names:subject_name (Some view) ~to_clear |
| 129 |
| None -> k ~names:[] None ~to_clear:[])) >>= reduce_or |
| 130 |
|
| 131 |
let vsASSERT_EMPTY = |
| 132 |
tclGET (function |
| 133 |
| Some _ -> anomaly ("vsASSERT_EMPTY: not empty") |
| 134 |
| _ -> tclUNIT ()) |
| 135 |
|
| 136 |
end |
| 137 |
|
| 138 |
let intern_constr_expr { Genintern.genv; ltacvars = vars } sigma ce = |
| 139 |
let ltacvars = { |
| 140 |
Constrintern.empty_ltac_sign with Constrintern.ltac_vars = vars } in |
| 141 |
Constrintern.intern_gen Pretyping.WithoutTypeConstraint ~ltacvars genv sigma ce |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
let is_tac_in_term ?extra_scope { annotation; body; glob_env; interp_env } = |
| 150 |
Goal.(enter_one ~__LOC__ begin fun goal -> |
| 151 |
let genv = env goal in |
| 152 |
let sigma = sigma goal in |
| 153 |
let ist = Ssrcommon.option_assert_get glob_env (Pp.str"not a term") in |
| 154 |
|
| 155 |
let ist = |
| 156 |
let open Genintern in |
| 157 |
{ |
| 158 |
ltacvars = ist.ast_ltacvars; |
| 159 |
extra = ist.ast_extra; |
| 160 |
intern_sign = ist.ast_intern_sign; |
| 161 |
genv; |
| 162 |
strict_check = true; |
| 163 |
} |
| 164 |
in |
| 165 |
|
| 166 |
let body = |
| 167 |
match extra_scope with |
| 168 |
| None -> body |
| 169 |
| Some s -> CAst.make Constrexpr.(CDelimiters(DelimUnboundedScope,s,body)) |
| 170 |
in |
| 171 |
|
| 172 |
let g = intern_constr_expr ist sigma body in |
| 173 |
match DAst.get g with |
| 174 |
| Glob_term.GGenarg x |
| 175 |
when Genarg.has_type x (Genarg.glbwit Tacarg.wit_ltac_in_term) |
| 176 |
-> |
| 177 |
let _, tac = Genarg.out_gen (Genarg.glbwit Tacarg.wit_ltac_in_term) x in |
| 178 |
tclUNIT (`Tac tac) |
| 179 |
| _ -> tclUNIT (`Term (annotation, interp_env, g)) |
| 180 |
end) |
| 181 |
|
| 182 |
|
| 183 |
let tclINJ_CONSTR_IST ist p = |
| 184 |
let fresh_id = Ssrcommon.mk_internal_id "ssr_inj_constr_in_glob" in |
| 185 |
let ist = { |
| 186 |
ist with Geninterp.lfun = |
| 187 |
Id.Map.add fresh_id (Taccoerce.Value.of_constr p) ist.Geninterp.lfun} in |
| 188 |
tclUNIT (ist,Glob_term.GVar fresh_id) |
| 189 |
|
| 190 |
let mkGHole = |
| 191 |
DAst.make |
| 192 |
(Glob_term.GHole (GInternalHole)) |
| 193 |
let rec mkGHoles n = if n > 0 then mkGHole :: mkGHoles (n - 1) else [] |
| 194 |
let mkGApp f args = |
| 195 |
if args = [] then f |
| 196 |
else DAst.make (Glob_term.GApp (f, args)) |
| 197 |
|
| 198 |
|
| 199 |
let interp_glob ist glob = Goal.enter_one ~__LOC__ begin fun goal -> |
| 200 |
let env = Goal.env goal in |
| 201 |
let sigma = Goal.sigma goal in |
| 202 |
Ssrprinters.debug_ssr (fun () -> |
| 203 |
Pp.(str"interp-in: " ++ Printer.pr_glob_constr_env env sigma glob)); |
| 204 |
try |
| 205 |
let sigma,term = Tacinterp.interp_open_constr ist env sigma (glob,None) in |
| 206 |
Ssrprinters.debug_ssr (fun () -> |
| 207 |
Pp.(str"interp-out: " ++ Printer.pr_econstr_env env sigma term)); |
| 208 |
tclUNIT (env,sigma,term) |
| 209 |
with e when CErrors.noncritical e -> |
| 210 |
let e, info = Exninfo.capture e in |
| 211 |
Ssrprinters.debug_ssr (fun () -> |
| 212 |
Pp.(str"interp-err: " ++ Printer.pr_glob_constr_env env sigma glob)); |
| 213 |
tclZERO ~info e |
| 214 |
end |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
let tclKeepOpenConstr (_env, sigma, t) = Unsafe.tclEVARS sigma <*> tclUNIT t |
| 222 |
|
| 223 |
let tclADD_CLEAR_IF_ID (env, ist, t) x = |
| 224 |
Ssrprinters.debug_ssr (fun () -> |
| 225 |
Pp.(str"tclADD_CLEAR_IF_ID: " ++ Printer.pr_econstr_env env ist t)); |
| 226 |
let hd, args = EConstr.decompose_app ist t in |
| 227 |
match EConstr.kind ist hd with |
| 228 |
| Constr.Var id when Ssrcommon.not_section_id id -> tclUNIT (x, [id]) |
| 229 |
| _ -> tclUNIT (x,[]) |
| 230 |
|
| 231 |
let tclPAIR p x = tclUNIT (x, p) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
let guess_max_implicits ist glob = |
| 241 |
Proofview.tclORELSE |
| 242 |
(interp_glob ist (mkGApp glob (mkGHoles 6)) >>= fun (env,sigma,term) -> |
| 243 |
let term_ty = Retyping.get_type_of env sigma term in |
| 244 |
let ctx, _ = Reductionops.whd_decompose_prod env sigma term_ty in |
| 245 |
tclUNIT (List.length ctx + 6)) |
| 246 |
(fun _ -> tclUNIT 5) |
| 247 |
|
| 248 |
let pad_to_inductive ist glob = Goal.enter_one ~__LOC__ begin fun goal -> |
| 249 |
interp_glob ist glob >>= fun (env, sigma, term as ot) -> |
| 250 |
let term_ty = Retyping.get_type_of env sigma term in |
| 251 |
let ctx, i = Reductionops.whd_decompose_prod env sigma term_ty in |
| 252 |
let rel_ctx = |
| 253 |
List.map (fun (a,b) -> Context.Rel.Declaration.LocalAssum(a,b)) ctx in |
| 254 |
if not (Ssrcommon.isAppInd (EConstr.push_rel_context rel_ctx env) sigma i) |
| 255 |
then Tacticals.tclZEROMSG Pp.(str"not an inductive") |
| 256 |
else tclUNIT (mkGApp glob (mkGHoles (List.length ctx))) |
| 257 |
>>= tclADD_CLEAR_IF_ID ot |
| 258 |
end |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
let interp_view ~clear_if_id ist v p = |
| 269 |
let is_specialize hd = |
| 270 |
match DAst.get hd with Glob_term.GHole _ -> true | _ -> false in |
| 271 |
|
| 272 |
tclINJ_CONSTR_IST ist p >>= fun (ist, p_id) -> |
| 273 |
let p_id = DAst.make p_id in |
| 274 |
match DAst.get v with |
| 275 |
| Glob_term.GApp (hd, rargs) when is_specialize hd -> |
| 276 |
Ssrprinters.debug_ssr (fun () -> Pp.(str "specialize")); |
| 277 |
interp_glob ist (mkGApp p_id rargs) |
| 278 |
>>= tclKeepOpenConstr >>= tclPAIR [] |
| 279 |
| _ -> |
| 280 |
Ssrprinters.debug_ssr (fun () -> Pp.(str "view")); |
| 281 |
|
| 282 |
let adaptors = AdaptorDb.(get Forward) in |
| 283 |
Proofview.tclORELSE |
| 284 |
(pad_to_inductive ist v >>= fun (vpad,clr) -> |
| 285 |
Ssrcommon.tclFIRSTa (List.map |
| 286 |
(fun a -> interp_glob ist (mkGApp a [vpad; p_id])) adaptors) |
| 287 |
>>= tclPAIR clr) |
| 288 |
(fun _ -> |
| 289 |
guess_max_implicits ist v >>= fun n -> |
| 290 |
Ssrcommon.tclFIRSTi (fun n -> |
| 291 |
interp_glob ist (mkGApp v (mkGHoles n @ [p_id]))) n |
| 292 |
>>= fun x -> tclADD_CLEAR_IF_ID x x) |
| 293 |
>>= fun (ot,clr) -> |
| 294 |
if clear_if_id |
| 295 |
then tclKeepOpenConstr ot >>= tclPAIR clr |
| 296 |
else tclKeepOpenConstr ot >>= tclPAIR [] |
| 297 |
|
| 298 |
|
| 299 |
let pile_up_view ~clear_if_id (annotation, ist, v) = |
| 300 |
let ist = Ssrcommon.option_assert_get ist (Pp.str"not a term") in |
| 301 |
let clear_if_id = clear_if_id && annotation <> `Parens in |
| 302 |
State.vsPUSH (fun p -> interp_view ~clear_if_id ist v p) |
| 303 |
|
| 304 |
let finalize_view s0 ?(simple_types=true) p = |
| 305 |
Goal.enter_one ~__LOC__ begin fun g -> |
| 306 |
let env = Goal.env g in |
| 307 |
let sigma = Goal.sigma g in |
| 308 |
let evars_of_p = Evd.evars_of_term sigma p in |
| 309 |
let filter x _ = Evar.Set.mem x evars_of_p in |
| 310 |
let sigma = Evd.push_shelf sigma in |
| 311 |
let sigma = Typeclasses.resolve_typeclasses ~fail:false ~filter env sigma in |
| 312 |
let _, sigma = Evd.pop_shelf sigma in |
| 313 |
let p = Reductionops.nf_evar sigma p in |
| 314 |
let get_body : type a. a Evd.evar_body -> EConstr.t = function Evd.Evar_defined x -> x | Evd.Evar_empty -> assert false in |
| 315 |
let evars_of_econstr sigma t = |
| 316 |
Evarutil.undefined_evars_of_term sigma t in |
| 317 |
let rigid_of s = |
| 318 |
List.fold_left (fun l k -> |
| 319 |
if Evd.is_defined sigma k then |
| 320 |
let EvarInfo evi = Evd.find sigma k in |
| 321 |
let bo = get_body (Evd.evar_body evi) in |
| 322 |
k :: l @ Evar.Set.elements (evars_of_econstr sigma bo) |
| 323 |
else l |
| 324 |
) [] s in |
| 325 |
let env0 = Proofview.Goal.env s0 in |
| 326 |
let sigma0 = Proofview.Goal.sigma s0 in |
| 327 |
let und0 = |
| 328 |
let EvarInfo g0info = Evd.find sigma0 (Proofview.Goal.goal s0) in |
| 329 |
let g0 = Evd.evars_of_filtered_evar_info sigma0 g0info in |
| 330 |
List.filter (fun k -> Evar.Set.mem k g0) |
| 331 |
(List.map fst (Evar.Map.bindings (Evd.undefined_map sigma0))) in |
| 332 |
let rigid = rigid_of und0 in |
| 333 |
let p, to_prune, _ucst = abs_evars env0 sigma0 ~rigid (sigma, p) in |
| 334 |
let p = if simple_types then abs_cterm env0 sigma0 (List.length to_prune) p else p in |
| 335 |
Ssrprinters.debug_ssr (fun () -> Pp.(str"view@finalized: " ++ |
| 336 |
Printer.pr_econstr_env env sigma p)); |
| 337 |
let sigma = List.fold_left Evd.remove sigma to_prune in |
| 338 |
Unsafe.tclEVARS sigma <*> |
| 339 |
tclUNIT p |
| 340 |
end |
| 341 |
|
| 342 |
let pose_proof subject_name p = |
| 343 |
Generalize.generalize [p] <*> |
| 344 |
begin match subject_name with |
| 345 |
| id :: _ -> Ssrcommon.tclRENAME_HD_PROD (Name.Name id) |
| 346 |
| _ -> tclUNIT() end |
| 347 |
<*> |
| 348 |
Tactics.reduce_after_refine |
| 349 |
|
| 350 |
|
| 351 |
let rec apply_all_views_aux ~clear_if_id vs finalization conclusion s0 = |
| 352 |
match vs with |
| 353 |
| [] -> finalization s0 (fun name p -> |
| 354 |
(match p with |
| 355 |
| None -> conclusion ~to_clear:[] |
| 356 |
| Some p -> |
| 357 |
pose_proof name p <*> conclusion ~to_clear:name) <*> |
| 358 |
tclUNIT false) |
| 359 |
| v :: vs -> |
| 360 |
Ssrprinters.debug_ssr (fun () -> Pp.(str"piling...")); |
| 361 |
is_tac_in_term ~extra_scope:"ssripat" v >>= function |
| 362 |
| `Term v -> |
| 363 |
Ssrprinters.debug_ssr (fun () -> Pp.(str"..a term")); |
| 364 |
pile_up_view ~clear_if_id v <*> |
| 365 |
apply_all_views_aux ~clear_if_id vs finalization conclusion s0 |
| 366 |
| `Tac tac -> |
| 367 |
Ssrprinters.debug_ssr (fun () -> Pp.(str"..a tactic")); |
| 368 |
finalization s0 (fun name p -> |
| 369 |
(match p with |
| 370 |
| None -> tclUNIT () |
| 371 |
| Some p -> pose_proof name p) <*> |
| 372 |
Tacinterp.eval_tactic tac <*> |
| 373 |
if vs = [] then begin |
| 374 |
Ssrprinters.debug_ssr (fun () -> Pp.(str"..was the last view")); |
| 375 |
conclusion ~to_clear:name <*> tclUNIT true |
| 376 |
end else |
| 377 |
Tactics.clear name <*> |
| 378 |
tclINDEPENDENTL begin |
| 379 |
Ssrprinters.debug_ssr (fun () -> Pp.(str"..was NOT the last view")); |
| 380 |
Proofview.Goal.enter_one begin fun gl -> |
| 381 |
apply_all_views_aux ~clear_if_id vs finalization conclusion gl |
| 382 |
end |
| 383 |
end >>= reduce_or) |
| 384 |
|
| 385 |
let apply_all_views vs ~conclusion ~clear_if_id = |
| 386 |
let finalization s0 k = |
| 387 |
State.vsCONSUME_IF_PRESENT (fun ~names t ~to_clear -> |
| 388 |
match t with |
| 389 |
| None -> k [] None |
| 390 |
| Some t -> |
| 391 |
finalize_view s0 t >>= fun p -> k (names @ to_clear) (Some p)) in |
| 392 |
Proofview.Goal.enter_one begin fun gl -> |
| 393 |
apply_all_views_aux ~clear_if_id vs finalization conclusion gl |
| 394 |
end |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
let apply_all_views_to subject ~simple_types vs ~conclusion = begin |
| 399 |
let rec process_all_vs = function |
| 400 |
| [] -> tclUNIT () |
| 401 |
| v :: vs -> is_tac_in_term v >>= function |
| 402 |
| `Tac _ -> Ssrcommon.errorstrm Pp.(str"tactic view not supported") |
| 403 |
| `Term v -> pile_up_view ~clear_if_id:false v <*> process_all_vs vs in |
| 404 |
State.vsASSERT_EMPTY <*> |
| 405 |
State.vsINIT ~subject_name:[] ~to_clear:[] ~view:subject <*> |
| 406 |
Proofview.Goal.enter_one begin fun s0 -> |
| 407 |
process_all_vs vs <*> |
| 408 |
State.vsCONSUME (fun ~names:_ t ~to_clear:_ -> |
| 409 |
finalize_view s0 ~simple_types t >>= conclusion) |
| 410 |
end |
| 411 |
end |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
let tclIPAT_VIEWS ~views:vs ?(clear_if_id=false) ~conclusion () = |
| 416 |
tclINDEPENDENTL begin |
| 417 |
State.vsASSERT_EMPTY <*> |
| 418 |
apply_all_views vs ~conclusion ~clear_if_id >>= fun was_tac -> |
| 419 |
State.vsASSERT_EMPTY <*> |
| 420 |
tclUNIT was_tac |
| 421 |
end >>= reduce_or |
| 422 |
|
| 423 |
let tclWITH_FWD_VIEWS ~simple_types ~subject ~views:vs ~conclusion = |
| 424 |
tclINDEPENDENT (apply_all_views_to subject ~simple_types vs ~conclusion) |
| 425 |
|
| 426 |
|