| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
open Pp |
| 19 |
open Util |
| 20 |
open Proofview_monad |
| 21 |
open Context.Named.Declaration |
| 22 |
|
| 23 |
|
| 24 |
type proofview = Proofview_monad.proofview |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type entry = (Environ.named_context_val * EConstr.constr * EConstr.types) list |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
let proofview p = |
| 39 |
List.map drop_state p.comb , p.solution |
| 40 |
|
| 41 |
let compact el ({ solution } as pv) = |
| 42 |
let nf c = Evarutil.nf_evar solution c in |
| 43 |
let nf0 c = EConstr.(to_constr ~abort_on_undefined_evars:false solution (of_constr c)) in |
| 44 |
let nf_hyps hyps = Environ.map_named_val (fun d -> map_constr nf0 d) hyps in |
| 45 |
let size = Evd.fold (fun _ _ i -> i+1) solution 0 in |
| 46 |
let new_el = List.map (fun (hyps,t,ty) -> nf_hyps hyps, nf t, nf ty) el in |
| 47 |
let pruned_solution = Evd.drop_all_defined solution in |
| 48 |
let apply_subst_einfo _ ei = Evd.map_evar_info nf ei in |
| 49 |
let new_solution = Evd.raw_map_undefined apply_subst_einfo pruned_solution in |
| 50 |
let new_size = Evd.fold (fun _ _ i -> i+1) new_solution 0 in |
| 51 |
Feedback.msg_info (Pp.str (Printf.sprintf "Evars: %d -> %d\n" size new_size)); |
| 52 |
new_el, { pv with solution = new_solution; } |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
type telescope = |
| 58 |
| TNil of Evd.evar_map |
| 59 |
| TCons of Environ.env * Evd.evar_map * EConstr.types * (Evd.evar_map -> EConstr.constr -> telescope) |
| 60 |
|
| 61 |
let map_telescope_evd f = function |
| 62 |
| TNil sigma -> TNil (f sigma) |
| 63 |
| TCons (env,sigma,ty,g) -> TCons(env,(f sigma),ty,g) |
| 64 |
|
| 65 |
let dependent_init = |
| 66 |
|
| 67 |
let src = Loc.tag @@ Evar_kinds.GoalEvar in |
| 68 |
|
| 69 |
let rec aux = function |
| 70 |
| TNil sigma -> [], { solution = sigma; comb = [] } |
| 71 |
| TCons (env, sigma, typ, t) -> |
| 72 |
let (sigma, econstr) = Evarutil.new_evar env sigma ~src ~typeclass_candidate:false typ in |
| 73 |
let (gl, _) = EConstr.destEvar sigma econstr in |
| 74 |
let ret, { solution = sol; comb = comb } = aux (t sigma econstr) in |
| 75 |
let entry = (Environ.named_context_val env, econstr, typ) :: ret in |
| 76 |
entry, { solution = sol; comb = with_empty_state gl :: comb } |
| 77 |
in |
| 78 |
fun t -> |
| 79 |
let t = map_telescope_evd Evd.push_future_goals t in |
| 80 |
let entry, v = aux t in |
| 81 |
|
| 82 |
let _goals, solution = Evd.pop_future_goals v.solution in |
| 83 |
entry, { v with solution } |
| 84 |
|
| 85 |
let init = |
| 86 |
let rec aux sigma = function |
| 87 |
| [] -> TNil sigma |
| 88 |
| (env,g)::l -> TCons (env,sigma,g,(fun sigma _ -> aux sigma l)) |
| 89 |
in |
| 90 |
fun sigma l -> dependent_init (aux sigma l) |
| 91 |
|
| 92 |
let initial_goals initial = initial |
| 93 |
|
| 94 |
let finished = function |
| 95 |
| {comb = []} -> true |
| 96 |
| _ -> false |
| 97 |
|
| 98 |
let return { solution=defs } = defs |
| 99 |
|
| 100 |
let return_constr { solution = defs } c = Evarutil.nf_evar defs c |
| 101 |
|
| 102 |
let partial_proof entry pv = CList.map (return_constr pv) (CList.map pi2 entry) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
let cleared_alias evd g = |
| 107 |
let evk = drop_state g in |
| 108 |
let state = get_state g in |
| 109 |
Option.map (fun g -> goal_with_state g state) (Evarutil.advance evd evk) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
let undefined_evars defs l = |
| 114 |
let fold evk (seen, ans as accu) = match Evarutil.advance defs evk with |
| 115 |
| None -> accu |
| 116 |
| Some evk -> |
| 117 |
if Evar.Set.mem evk seen then accu |
| 118 |
else (Evar.Set.add evk seen, evk :: ans) |
| 119 |
in |
| 120 |
snd @@ List.fold_right fold l (Evar.Set.empty, []) |
| 121 |
|
| 122 |
let undefined defs l = |
| 123 |
let fold gl (seen, ans as accu) = match cleared_alias defs gl with |
| 124 |
| None -> accu |
| 125 |
| Some gl -> |
| 126 |
let evk = drop_state gl in |
| 127 |
if Evar.Set.mem evk seen then accu |
| 128 |
else (Evar.Set.add evk seen, gl :: ans) |
| 129 |
in |
| 130 |
snd @@ List.fold_right fold l (Evar.Set.empty, []) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
type focus_context = goal_with_state list * goal_with_state list |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
let focus_context sigma (left,right) = |
| 151 |
(undefined_evars sigma (List.map drop_state left), undefined_evars sigma (List.map drop_state right)) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
let focus_sublist i j l = |
| 162 |
let (left,sub_right) = CList.goto (i-1) l in |
| 163 |
let (sub, right) = |
| 164 |
try CList.chop (j-i+1) sub_right |
| 165 |
with Failure _ -> raise CList.IndexOutOfRange |
| 166 |
in |
| 167 |
(sub, (left,right)) |
| 168 |
|
| 169 |
|
| 170 |
let unfocus_sublist (left,right) s = |
| 171 |
CList.rev_append left (s@right) |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
let focus i j sp = |
| 180 |
let (new_comb, (left, right)) = focus_sublist i j sp.comb in |
| 181 |
( { sp with comb = new_comb } , (left, right) ) |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
let find_evar_in_pv id pv = |
| 187 |
let ev = Evd.evar_key id pv.solution in |
| 188 |
let comb = CList.map drop_state pv.comb in |
| 189 |
try ev, Some (CList.index Evar.equal ev comb) |
| 190 |
with Not_found -> ev, None |
| 191 |
|
| 192 |
|
| 193 |
let unfocus (left, right) sp = |
| 194 |
{ sp with comb = undefined sp.solution (unfocus_sublist (left, right) sp.comb) } |
| 195 |
|
| 196 |
let with_empty_state = Proofview_monad.with_empty_state |
| 197 |
let drop_state = Proofview_monad.drop_state |
| 198 |
let goal_with_state = Proofview_monad.goal_with_state |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
) |
| 233 |
|
| 234 |
module Proof = Logical |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
type +'a tactic = 'a Proof.t |
| 244 |
|
| 245 |
|
| 246 |
let apply ~name ~poly env t sp = |
| 247 |
let open Logic_monad in |
| 248 |
NewProfile.profile "Proofview.apply" (fun () -> |
| 249 |
let ans = Proof.repr (Proof.run t P.{trace=false; name; poly} (sp,env)) in |
| 250 |
let ans = Logic_monad.NonLogical.run ans in |
| 251 |
match ans with |
| 252 |
| Nil (e, info) -> Exninfo.iraise (TacticFailure e, info) |
| 253 |
| Cons ((r, (state, env), status, info), _) -> |
| 254 |
r, state, env, status, Trace.to_tree info) |
| 255 |
() |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
let tclUNIT = Proof.return |
| 262 |
|
| 263 |
|
| 264 |
let tclBIND = Proof.(>>=) |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
let tclTHEN = Proof.(>>) |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
let tclIGNORE = Proof.ignore |
| 273 |
|
| 274 |
module Monad = Proof |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
let tclZERO ?(info=Exninfo.null) e = |
| 283 |
if not (CErrors.noncritical e) then |
| 284 |
CErrors.anomaly (Pp.str "tclZERO receiving critical error: " ++ CErrors.print e); |
| 285 |
Proof.zero (e, info) |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
let tclOR = Proof.plus |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
let tclORELSE t1 t2 = |
| 298 |
let open Logic_monad in |
| 299 |
let open Proof in |
| 300 |
split t1 >>= function |
| 301 |
| Nil e -> t2 e |
| 302 |
| Cons (a,t1') -> plus (return a) t1' |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
let tclIFCATCH a s f = |
| 308 |
let open Logic_monad in |
| 309 |
let open Proof in |
| 310 |
split a >>= function |
| 311 |
| Nil e -> f e |
| 312 |
| Cons (x,a') -> plus (s x) (fun e -> (a' e) >>= fun x' -> (s x')) |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
let tclONCE = Proof.once |
| 318 |
|
| 319 |
exception MoreThanOneSuccess |
| 320 |
let _ = CErrors.register_handler begin function |
| 321 |
| MoreThanOneSuccess -> |
| 322 |
Some (Pp.str "This tactic has more than one success.") |
| 323 |
| _ -> None |
| 324 |
end |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
let tclEXACTLY_ONCE e t = |
| 335 |
let open Logic_monad in |
| 336 |
let open Proof in |
| 337 |
split t >>= function |
| 338 |
| Nil (e, info) -> tclZERO ~info e |
| 339 |
| Cons (x,k) -> |
| 340 |
let info = Exninfo.null in |
| 341 |
Proof.split (k (e, Exninfo.null)) >>= function |
| 342 |
| Nil _ -> tclUNIT x |
| 343 |
| _ -> tclZERO ~info MoreThanOneSuccess |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
type 'a case = |
| 348 |
| Fail of Exninfo.iexn |
| 349 |
| Next of 'a * (Exninfo.iexn -> 'a tactic) |
| 350 |
let tclCASE t = |
| 351 |
let open Logic_monad in |
| 352 |
let map = function |
| 353 |
| Nil e -> Fail e |
| 354 |
| Cons (x, t) -> Next (x, t) |
| 355 |
in |
| 356 |
Proof.map map (Proof.split t) |
| 357 |
|
| 358 |
let tclBREAK = Proof.break |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
type goal_range_selector = |
| 366 |
| NthSelector of int |
| 367 |
| RangeSelector of (int * int) |
| 368 |
| IdSelector of Libnames.qualid |
| 369 |
|
| 370 |
exception NoSuchGoals of int |
| 371 |
exception CannotSelectShelvedAndFocused |
| 372 |
|
| 373 |
let _ = CErrors.register_handler begin function |
| 374 |
| NoSuchGoals n -> |
| 375 |
Some (str "No such " ++ str (String.plural n "goal") ++ str ".") |
| 376 |
| CannotSelectShelvedAndFocused -> |
| 377 |
Some (str "Cannot simultaneously select shelved and unshelved goals.") |
| 378 |
| _ -> None |
| 379 |
end |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
let tclFOCUS ?nosuchgoal i j t = |
| 386 |
let nosuchgoal ~info = Option.default (tclZERO ~info (NoSuchGoals (j+1-i))) nosuchgoal in |
| 387 |
let open Proof in |
| 388 |
Pv.get >>= fun initial -> |
| 389 |
try |
| 390 |
let (focused,context) = focus i j initial in |
| 391 |
Pv.set focused >> |
| 392 |
t >>= fun result -> |
| 393 |
Pv.modify (fun next -> unfocus context next) >> |
| 394 |
return result |
| 395 |
with CList.IndexOutOfRange as exn -> |
| 396 |
let _, info = Exninfo.capture exn in |
| 397 |
nosuchgoal ~info |
| 398 |
|
| 399 |
let tclTRYFOCUS i j t = tclFOCUS ~nosuchgoal:(tclUNIT ()) i j t |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
let tclFOCUSSHELF ?(nosuchgoal=tclZERO (NoSuchGoals 1)) evs t = |
| 405 |
if CList.is_empty evs then nosuchgoal |
| 406 |
else |
| 407 |
let open Proof in |
| 408 |
Comb.get >>= fun initial_comb -> |
| 409 |
Comb.set (CList.map with_empty_state evs) >> |
| 410 |
t >>= fun result -> |
| 411 |
Comb.get >>= fun subgoals -> |
| 412 |
Comb.set initial_comb >> |
| 413 |
let subgoals = CList.filter_map (fun ev -> |
| 414 |
let ev = drop_state ev in |
| 415 |
|
| 416 |
if (CList.mem_f Evar.equal ev evs) then None else Some ev) |
| 417 |
subgoals |
| 418 |
in |
| 419 |
Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution (undefined_evars pv.solution subgoals) }) >> |
| 420 |
return result |
| 421 |
|
| 422 |
let tclFOCUSLIST ?(nosuchgoal=tclZERO (NoSuchGoals 0)) l t = |
| 423 |
let open Proof in |
| 424 |
Comb.get >>= fun comb -> |
| 425 |
let n = CList.length comb in |
| 426 |
let ok (i, j) = 1 <= i && i <= j && j <= n in |
| 427 |
if not (CList.for_all ok l) then nosuchgoal |
| 428 |
else |
| 429 |
match l with |
| 430 |
| [] -> nosuchgoal |
| 431 |
| (mi, _) :: _ -> |
| 432 |
|
| 433 |
|
| 434 |
let mi = CList.fold_left (fun m (i, _) -> min m i) mi l in |
| 435 |
|
| 436 |
|
| 437 |
let left, sub_right = CList.goto (mi-1) comb in |
| 438 |
let p x _ = CList.exists (fun (i, j) -> i <= x + mi && x + mi <= j) l in |
| 439 |
let sub, right = CList.partitioni p sub_right in |
| 440 |
let mj = mi - 1 + CList.length sub in |
| 441 |
Comb.set (CList.rev_append left (sub @ right)) >> |
| 442 |
tclFOCUS mi mj t |
| 443 |
|
| 444 |
let tclFOCUSSELECTORLIST ?(nosuchgoal=tclZERO (NoSuchGoals 0)) l t = |
| 445 |
let open Proof in |
| 446 |
Pv.get >>= fun initial -> |
| 447 |
try |
| 448 |
let (ranges, shelved_evars) = |
| 449 |
CList.partition_map (function |
| 450 |
| NthSelector n -> Left (n, n) |
| 451 |
| RangeSelector r -> Left r |
| 452 |
| IdSelector id -> |
| 453 |
match find_evar_in_pv id initial with |
| 454 |
| ev, Some n -> Left (n, n) |
| 455 |
| ev, None -> Right ev ) l in |
| 456 |
match CList.is_empty ranges, CList.is_empty shelved_evars with |
| 457 |
| true, true -> nosuchgoal |
| 458 |
| true, false -> tclFOCUSSHELF ~nosuchgoal shelved_evars t |
| 459 |
| false, true -> tclFOCUSLIST ~nosuchgoal ranges t |
| 460 |
| false, false -> tclZERO CannotSelectShelvedAndFocused |
| 461 |
with Not_found -> nosuchgoal |
| 462 |
|
| 463 |
|
| 464 |
let tclFOCUSID ?(nosuchgoal=tclZERO (NoSuchGoals 1)) id t = |
| 465 |
let open Proof in |
| 466 |
Pv.get >>= fun initial -> |
| 467 |
try |
| 468 |
match find_evar_in_pv id initial with |
| 469 |
| ev, Some n -> |
| 470 |
|
| 471 |
let (focused,context) = focus n n initial in |
| 472 |
Pv.set focused >> |
| 473 |
t >>= fun result -> |
| 474 |
Pv.modify (fun next -> unfocus context next) >> |
| 475 |
return result |
| 476 |
| ev, None -> |
| 477 |
|
| 478 |
tclFOCUSSHELF ~nosuchgoal [ev] t |
| 479 |
with Not_found -> nosuchgoal |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
exception SizeMismatch of int*int |
| 484 |
let _ = CErrors.register_handler begin function |
| 485 |
| SizeMismatch (i,j) -> |
| 486 |
let open Pp in |
| 487 |
Some ( |
| 488 |
str"Incorrect number of goals" ++ spc() ++ |
| 489 |
str"(expected "++int i++str(String.plural i " tactic") ++ str", was given "++ int j++str").") |
| 490 |
| _ -> None |
| 491 |
end |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
let iter_goal i = |
| 499 |
let open Proof in |
| 500 |
Comb.get >>= fun initial -> |
| 501 |
Proof.List.fold_left begin fun (subgoals as cur) goal -> |
| 502 |
Solution.get >>= fun step -> |
| 503 |
match cleared_alias step goal with |
| 504 |
| None -> return cur |
| 505 |
| Some goal -> |
| 506 |
Comb.set [goal] >> |
| 507 |
i goal >> |
| 508 |
Proof.map (fun comb -> comb :: subgoals) Comb.get |
| 509 |
end [] initial >>= fun subgoals -> |
| 510 |
Solution.get >>= fun evd -> |
| 511 |
Comb.set CList.(undefined evd (flatten (rev subgoals))) |
| 512 |
|
| 513 |
|
| 514 |
let map_goal i = |
| 515 |
let rev = List.rev in |
| 516 |
let open Proof in |
| 517 |
Comb.get >>= fun initial -> |
| 518 |
Proof.List.fold_left begin fun (acc, subgoals as cur) goal -> |
| 519 |
Solution.get >>= fun step -> |
| 520 |
match cleared_alias step goal with |
| 521 |
| None -> return cur |
| 522 |
| Some goal -> |
| 523 |
Comb.set [goal] >> |
| 524 |
i goal >>= fun res -> |
| 525 |
Proof.map (fun comb -> comb :: subgoals) Comb.get >>= fun x -> |
| 526 |
return (res :: acc, x) |
| 527 |
end ([],[]) initial >>= fun (results_rev, subgoals) -> |
| 528 |
Solution.get >>= fun evd -> |
| 529 |
Comb.set CList.(undefined evd (flatten (rev subgoals))) >> |
| 530 |
return (rev results_rev) |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
let fold_left2_goal i s l = |
| 538 |
let open Proof in |
| 539 |
Pv.get >>= fun initial -> |
| 540 |
let err = |
| 541 |
return () >>= fun () -> |
| 542 |
tclZERO (SizeMismatch (CList.length initial.comb,CList.length l)) |
| 543 |
in |
| 544 |
Proof.List.fold_left2 err begin fun ((r,subgoals) as cur) goal a -> |
| 545 |
Solution.get >>= fun step -> |
| 546 |
match cleared_alias step goal with |
| 547 |
| None -> return cur |
| 548 |
| Some goal -> |
| 549 |
Comb.set [goal] >> |
| 550 |
i goal a r >>= fun r -> |
| 551 |
Proof.map (fun comb -> (r, comb :: subgoals)) Comb.get |
| 552 |
end (s,[]) initial.comb l >>= fun (r,subgoals) -> |
| 553 |
Solution.get >>= fun evd -> |
| 554 |
Comb.set CList.(undefined evd (flatten (rev subgoals))) >> |
| 555 |
return r |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
let tclDISPATCHGEN0 join tacs = |
| 577 |
match tacs with |
| 578 |
| [] -> |
| 579 |
begin |
| 580 |
let open Proof in |
| 581 |
Comb.get >>= function |
| 582 |
| [] -> tclUNIT (join []) |
| 583 |
| comb -> tclZERO (SizeMismatch (CList.length comb,0)) |
| 584 |
end |
| 585 |
| [tac] -> |
| 586 |
begin |
| 587 |
let open Proof in |
| 588 |
Pv.get >>= function |
| 589 |
| { comb=[goal] ; solution } -> |
| 590 |
begin match cleared_alias solution goal with |
| 591 |
| None -> tclUNIT (join []) |
| 592 |
| Some _ -> Proof.map (fun res -> join [res]) tac |
| 593 |
end |
| 594 |
| {comb} -> tclZERO (SizeMismatch(CList.length comb,1)) |
| 595 |
end |
| 596 |
| _ -> |
| 597 |
let iter _ t cur = Proof.map (fun y -> y :: cur) t in |
| 598 |
let ans = fold_left2_goal iter [] tacs in |
| 599 |
Proof.map join ans |
| 600 |
|
| 601 |
let tclDISPATCHGEN join tacs = |
| 602 |
let branch t = InfoL.tag (Info.DBranch) t in |
| 603 |
let tacs = CList.map branch tacs in |
| 604 |
InfoL.tag (Info.Dispatch) (tclDISPATCHGEN0 join tacs) |
| 605 |
|
| 606 |
let tclDISPATCH tacs = tclDISPATCHGEN ignore tacs |
| 607 |
|
| 608 |
let tclDISPATCHL tacs = tclDISPATCHGEN CList.rev tacs |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
let extend_to_list startxs rx endxs l = |
| 615 |
|
| 616 |
let rec duplicate acc = function |
| 617 |
| [] -> acc |
| 618 |
| _::rest -> duplicate (rx::acc) rest |
| 619 |
in |
| 620 |
let rec tail to_match rest = |
| 621 |
match rest, to_match with |
| 622 |
| [] , _::_ -> raise (SizeMismatch(0,0)) |
| 623 |
| _::rest , _::to_match -> tail to_match rest |
| 624 |
| _ , [] -> duplicate endxs rest |
| 625 |
in |
| 626 |
let rec copy pref rest = |
| 627 |
match rest,pref with |
| 628 |
| [] , _::_ -> raise (SizeMismatch(0,0)) |
| 629 |
| _::rest, a::pref -> a::(copy pref rest) |
| 630 |
| _ , [] -> tail endxs rest |
| 631 |
in |
| 632 |
copy startxs l |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
let tclEXTEND tacs1 rtac tacs2 = |
| 640 |
let open Proof in |
| 641 |
Comb.get >>= fun comb -> |
| 642 |
try |
| 643 |
let tacs = extend_to_list tacs1 rtac tacs2 comb in |
| 644 |
tclDISPATCH tacs |
| 645 |
with SizeMismatch _ -> |
| 646 |
tclZERO (SizeMismatch( |
| 647 |
CList.length comb, |
| 648 |
(CList.length tacs1)+(CList.length tacs2))) |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
let tclINDEPENDENT tac = |
| 655 |
let open Proof in |
| 656 |
Pv.get >>= fun initial -> |
| 657 |
match initial.comb with |
| 658 |
| [] -> tclUNIT () |
| 659 |
| [_] -> tac |
| 660 |
| _ -> |
| 661 |
let tac = InfoL.tag (Info.DBranch) tac in |
| 662 |
InfoL.tag (Info.Dispatch) (iter_goal (fun _ -> tac)) |
| 663 |
|
| 664 |
let tclINDEPENDENTL tac = |
| 665 |
let open Proof in |
| 666 |
Pv.get >>= fun initial -> |
| 667 |
match initial.comb with |
| 668 |
| [] -> tclUNIT [] |
| 669 |
| [_] -> tac >>= fun x -> return [x] |
| 670 |
| _ -> |
| 671 |
let tac = InfoL.tag (Info.DBranch) tac in |
| 672 |
InfoL.tag (Info.Dispatch) (map_goal (fun _ -> tac)) |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
let shelve = |
| 678 |
let open Proof in |
| 679 |
Comb.get >>= fun initial -> |
| 680 |
Comb.set [] >> |
| 681 |
InfoL.leaf (Info.Tactic (fun () -> Pp.str"shelve")) >> |
| 682 |
let initial = CList.map drop_state initial in |
| 683 |
Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution initial }) |
| 684 |
|
| 685 |
let shelve_goals l = |
| 686 |
let open Proof in |
| 687 |
Comb.get >>= fun initial -> |
| 688 |
let comb = CList.filter (fun g -> not (CList.mem (drop_state g) l)) initial in |
| 689 |
Comb.set comb >> |
| 690 |
InfoL.leaf (Info.Tactic (fun () -> Pp.str"shelve_goals")) >> |
| 691 |
Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution l }) |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
let depends_on sigma src tgt = |
| 697 |
let evi = Evd.find_undefined sigma tgt in |
| 698 |
Evar.Set.mem src (Evd.evars_of_filtered_evar_info sigma (Evarutil.nf_evar_info sigma evi)) |
| 699 |
|
| 700 |
let unifiable_delayed g l = |
| 701 |
CList.exists (fun (tgt, lazy evs) -> not (Evar.equal g tgt) && Evar.Set.mem g evs) l |
| 702 |
|
| 703 |
let free_evars sigma l = |
| 704 |
let cache = Evarutil.create_undefined_evars_cache () in |
| 705 |
let map ev = |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
let EvarInfo evi = Evd.find sigma ev in |
| 710 |
let fevs = lazy (Evarutil.filtered_undefined_evars_of_evar_info ~cache sigma evi) in |
| 711 |
(ev, fevs) |
| 712 |
in |
| 713 |
List.map map l |
| 714 |
|
| 715 |
let free_evars_with_state sigma l = |
| 716 |
let cache = Evarutil.create_undefined_evars_cache () in |
| 717 |
let map ev = |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
let ev = drop_state ev in |
| 722 |
let EvarInfo evi = Evd.find sigma ev in |
| 723 |
let fevs = lazy (Evarutil.filtered_undefined_evars_of_evar_info ~cache sigma evi) in |
| 724 |
(ev, fevs) |
| 725 |
in |
| 726 |
List.map map l |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
let unifiable_delayed_with_state sigma g l = |
| 733 |
let g = drop_state g in |
| 734 |
unifiable_delayed g l |
| 735 |
|
| 736 |
let unifiable sigma g l = |
| 737 |
let l = free_evars sigma l in |
| 738 |
unifiable_delayed g l |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
let partition_unifiable sigma l = |
| 745 |
let fevs = free_evars_with_state sigma l in |
| 746 |
CList.partition (fun g -> unifiable_delayed_with_state sigma g fevs) l |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
let shelve_unifiable_informative = |
| 752 |
let open Proof in |
| 753 |
Pv.get >>= fun initial -> |
| 754 |
let (u,n) = partition_unifiable initial.solution initial.comb in |
| 755 |
Comb.set n >> |
| 756 |
InfoL.leaf (Info.Tactic (fun () -> Pp.str"shelve_unifiable")) >> |
| 757 |
let u = CList.map drop_state u in |
| 758 |
Pv.modify (fun pv -> { pv with solution = Evd.shelve pv.solution u }) >> |
| 759 |
tclUNIT u |
| 760 |
|
| 761 |
let shelve_unifiable = |
| 762 |
let open Proof in |
| 763 |
shelve_unifiable_informative >>= fun _ -> tclUNIT () |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
let guard_no_unifiable = |
| 768 |
let open Proof in |
| 769 |
Pv.get >>= fun initial -> |
| 770 |
let (u,n) = partition_unifiable initial.solution initial.comb in |
| 771 |
match u with |
| 772 |
| [] -> tclUNIT None |
| 773 |
| gls -> |
| 774 |
let l = CList.map (fun g -> Evd.dependent_evar_ident (drop_state g) initial.solution) gls in |
| 775 |
let l = CList.map (fun id -> Names.Name id) l in |
| 776 |
tclUNIT (Some l) |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
let unshelve l p = |
| 781 |
let solution = Evd.unshelve p.solution l in |
| 782 |
let l = List.map with_empty_state l in |
| 783 |
|
| 784 |
let l = undefined p.solution l in |
| 785 |
{ comb = p.comb@l; solution } |
| 786 |
|
| 787 |
let filter_shelf f pv = |
| 788 |
{ pv with solution = Evd.filter_shelf f pv.solution } |
| 789 |
|
| 790 |
let mark_in_evm ~goal evd evars = |
| 791 |
let evd = |
| 792 |
if goal then |
| 793 |
let mark evd content = |
| 794 |
let EvarInfo info = Evd.find evd content in |
| 795 |
let source = match Evd.evar_source info with |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
| loc, (Evar_kinds.QuestionMark { Evar_kinds.qm_name=Names.Name id} | |
| 801 |
Evar_kinds.ImplicitArg (_,(_,id),_)) -> loc, Evar_kinds.VarInstance id |
| 802 |
| _, (Evar_kinds.VarInstance _ | Evar_kinds.GoalEvar) as x -> x |
| 803 |
| loc,_ -> loc,Evar_kinds.GoalEvar |
| 804 |
in |
| 805 |
Evd.update_source evd content source |
| 806 |
in CList.fold_left mark evd evars |
| 807 |
else evd |
| 808 |
in |
| 809 |
let tcs = Evd.get_typeclass_evars evd in |
| 810 |
let evset = Evar.Set.of_list evars in |
| 811 |
Evd.set_typeclass_evars evd (Evar.Set.diff tcs evset) |
| 812 |
|
| 813 |
let with_shelf tac = |
| 814 |
let open Proof in |
| 815 |
Pv.get >>= fun pv -> |
| 816 |
let { solution } = pv in |
| 817 |
Pv.set { pv with solution = Evd.push_shelf @@ Evd.push_future_goals solution } >> |
| 818 |
tac >>= fun ans -> |
| 819 |
Pv.get >>= fun npv -> |
| 820 |
let { solution = sigma } = npv in |
| 821 |
let gls, sigma = Evd.pop_shelf sigma in |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
let fgl, sigma = Evd.pop_future_goals sigma in |
| 826 |
|
| 827 |
let gls' = CList.rev_append (Evd.FutureGoals.comb fgl) gls in |
| 828 |
let gls' = undefined_evars sigma gls' in |
| 829 |
let sigma = mark_in_evm ~goal:false sigma gls' in |
| 830 |
let npv = { npv with solution = sigma } in |
| 831 |
Pv.set npv >> tclUNIT (gls', ans) |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
let goodmod p m = |
| 836 |
if m = 0 then 0 else |
| 837 |
let p' = p mod m in |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
if p' < 0 then p'+m else p' |
| 842 |
|
| 843 |
let cycle n = |
| 844 |
let open Proof in |
| 845 |
InfoL.leaf (Info.Tactic (fun () -> Pp.(str"cycle "++int n))) >> |
| 846 |
Comb.modify begin fun initial -> |
| 847 |
let l = CList.length initial in |
| 848 |
let n' = goodmod n l in |
| 849 |
let (front,rear) = CList.chop n' initial in |
| 850 |
rear@front |
| 851 |
end |
| 852 |
|
| 853 |
let swap i j = |
| 854 |
let open Proof in |
| 855 |
InfoL.leaf (Info.Tactic (fun () -> Pp.(hov 2 (str"swap"++spc()++int i++spc()++int j)))) >> |
| 856 |
Comb.modify begin fun initial -> |
| 857 |
let l = CList.length initial in |
| 858 |
let i = if i>0 then i-1 else i and j = if j>0 then j-1 else j in |
| 859 |
let i = goodmod i l and j = goodmod j l in |
| 860 |
CList.map_i begin fun k x -> |
| 861 |
match k with |
| 862 |
| k when Int.equal k i -> CList.nth initial j |
| 863 |
| k when Int.equal k j -> CList.nth initial i |
| 864 |
| _ -> x |
| 865 |
end 0 initial |
| 866 |
end |
| 867 |
|
| 868 |
let revgoals = |
| 869 |
let open Proof in |
| 870 |
InfoL.leaf (Info.Tactic (fun () -> Pp.str"revgoals")) >> |
| 871 |
Comb.modify CList.rev |
| 872 |
|
| 873 |
let numgoals = |
| 874 |
let open Proof in |
| 875 |
Comb.get >>= fun comb -> |
| 876 |
return (CList.length comb) |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
let tclEVARMAP = Solution.get |
| 883 |
|
| 884 |
let tclENV = Env.get |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
let mark_as_unsafe = Status.put false |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
let give_up evs pv = |
| 896 |
let solution = List.fold_left (fun sigma ev -> Evd.give_up (drop_state ev) sigma) pv.solution evs in |
| 897 |
{ pv with solution } |
| 898 |
|
| 899 |
let give_up = |
| 900 |
let open Proof in |
| 901 |
Comb.get >>= fun initial -> |
| 902 |
Comb.set [] >> |
| 903 |
mark_as_unsafe >> |
| 904 |
InfoL.leaf (Info.Tactic (fun () -> Pp.str"give_up")) >> |
| 905 |
Pv.modify (give_up initial) |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
module Progress = struct |
| 911 |
|
| 912 |
let eq_constr evd extended_evd = |
| 913 |
Evarutil.eq_constr_univs_test ~evd ~extended_evd |
| 914 |
|
| 915 |
|
| 916 |
let eq_named_context_val sigma1 sigma2 ctx1 ctx2 = |
| 917 |
let r_eq _ _ = true in |
| 918 |
let c1 = EConstr.named_context_of_val ctx1 and c2 = EConstr.named_context_of_val ctx2 in |
| 919 |
let eq_named_declaration d1 d2 = |
| 920 |
match d1, d2 with |
| 921 |
| LocalAssum (i1,t1), LocalAssum (i2,t2) -> |
| 922 |
Context.eq_annot Names.Id.equal r_eq i1 i2 && eq_constr sigma1 sigma2 t1 t2 |
| 923 |
| LocalDef (i1,c1,t1), LocalDef (i2,c2,t2) -> |
| 924 |
Context.eq_annot Names.Id.equal r_eq i1 i2 && eq_constr sigma1 sigma2 c1 c2 |
| 925 |
&& eq_constr sigma1 sigma2 t1 t2 |
| 926 |
| _ -> |
| 927 |
false |
| 928 |
in |
| 929 |
|
| 930 |
List.for_all2eq eq_named_declaration c1 c2 |
| 931 |
|
| 932 |
let eq_evar_body (type a1 a2) sigma1 sigma2 (b1 : a1 Evd.evar_body) (b2 : a2 Evd.evar_body) = |
| 933 |
let open Evd in |
| 934 |
match b1, b2 with |
| 935 |
| Evar_empty, Evar_empty -> true |
| 936 |
| Evar_defined t1, Evar_defined t2 -> eq_constr sigma1 sigma2 t1 t2 |
| 937 |
| _ -> false |
| 938 |
|
| 939 |
let eq_evar_concl (type a1 a2) sigma1 sigma2 (e1 : a1 Evd.evar_info) (e2 : a2 Evd.evar_info) = |
| 940 |
let open Evd in |
| 941 |
match Evd.evar_body e1, Evd.evar_body e2 with |
| 942 |
| Evar_empty, Evar_empty -> eq_constr sigma1 sigma2 (Evd.evar_concl e1) (Evd.evar_concl e2) |
| 943 |
| Evar_defined _, Evar_defined _ -> true |
| 944 |
| _ -> false |
| 945 |
|
| 946 |
let eq_evar_info sigma1 sigma2 ei1 ei2 = |
| 947 |
eq_evar_concl sigma1 sigma2 ei1 ei2 && |
| 948 |
eq_named_context_val sigma1 sigma2 (Evd.evar_hyps ei1) (Evd.evar_hyps ei2) && |
| 949 |
eq_evar_body sigma1 sigma2 (Evd.evar_body ei1) (Evd.evar_body ei2) |
| 950 |
|
| 951 |
let fast_eq_evar_body (type a1 a2) (e1 : a1 Evd.evar_info) (e2 : a2 Evd.evar_info) = |
| 952 |
let open Evd in |
| 953 |
match Evd.evar_body e1, Evd.evar_body e2 with |
| 954 |
| Evar_empty, Evar_empty -> true |
| 955 |
| Evar_defined _, Evar_defined _ -> true |
| 956 |
| _ -> false |
| 957 |
|
| 958 |
let fast_eq_named_context_val ctx1 ctx2 = |
| 959 |
let r_eq _ _ = true in |
| 960 |
let c1 = EConstr.named_context_of_val ctx1 in |
| 961 |
let c2 = EConstr.named_context_of_val ctx2 in |
| 962 |
let eq_named_declaration d1 d2 = match d1, d2 with |
| 963 |
| LocalAssum (i1, _), LocalAssum (i2, _) -> Context.eq_annot Names.Id.equal r_eq i1 i2 |
| 964 |
| LocalDef (i1, _, _), LocalDef (i2, _, _) -> Context.eq_annot Names.Id.equal r_eq i1 i2 |
| 965 |
| _ -> false |
| 966 |
in |
| 967 |
List.for_all2eq eq_named_declaration c1 c2 |
| 968 |
|
| 969 |
let fast_eq_evar_info ei1 ei2 = |
| 970 |
fast_eq_evar_body ei1 ei2 && |
| 971 |
fast_eq_named_context_val (Evd.evar_hyps ei1) (Evd.evar_hyps ei2) |
| 972 |
|
| 973 |
|
| 974 |
let goal_equal ~evd ~extended_evd evar extended_evar = |
| 975 |
let EvarInfo evi = Evd.find evd evar in |
| 976 |
let EvarInfo extended_evi = Evd.find extended_evd extended_evar in |
| 977 |
if fast_eq_evar_info evi extended_evi then |
| 978 |
eq_evar_info evd extended_evd evi extended_evi |
| 979 |
else false |
| 980 |
|
| 981 |
end |
| 982 |
|
| 983 |
let tclPROGRESS t = |
| 984 |
let open Proof in |
| 985 |
Pv.get >>= fun initial -> |
| 986 |
t >>= fun res -> |
| 987 |
Pv.get >>= fun final -> |
| 988 |
|
| 989 |
|
| 990 |
let quick_test = |
| 991 |
initial.solution == final.solution && initial.comb == final.comb |
| 992 |
in |
| 993 |
let test = |
| 994 |
quick_test || |
| 995 |
(CList.same_length initial.comb final.comb && |
| 996 |
Util.List.for_all2eq begin fun i f -> |
| 997 |
Progress.goal_equal ~evd:initial.solution |
| 998 |
~extended_evd:final.solution (drop_state i) (drop_state f) |
| 999 |
end initial.comb final.comb) |
| 1000 |
in |
| 1001 |
if not test then |
| 1002 |
tclUNIT res |
| 1003 |
else |
| 1004 |
let info = Exninfo.reify () in |
| 1005 |
tclZERO ~info (CErrors.UserError Pp.(str "Failed to progress.")) |
| 1006 |
|
| 1007 |
let () = CErrors.register_handler begin function |
| 1008 |
| Logic_monad.Tac_Timeout -> |
| 1009 |
Some (Pp.str "Tactic timeout!") |
| 1010 |
| _ -> None |
| 1011 |
end |
| 1012 |
|
| 1013 |
let tclTIMEOUTF n t = |
| 1014 |
let open Proof in |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
let t = Proof.lift (Logic_monad.NonLogical.return ()) >> t in |
| 1021 |
Proof.get >>= fun initial -> |
| 1022 |
Proof.current >>= fun envvar -> |
| 1023 |
Proof.lift begin |
| 1024 |
let open Logic_monad.NonLogical in |
| 1025 |
timeout n (Proof.repr (Proof.run t envvar initial)) >>= fun r -> |
| 1026 |
match r with |
| 1027 |
| Error info -> return (Util.Inr (Logic_monad.Tac_Timeout, info)) |
| 1028 |
| Ok (Logic_monad.Nil e) -> return (Util.Inr e) |
| 1029 |
| Ok (Logic_monad.Cons (r, _)) -> return (Util.Inl r) |
| 1030 |
end >>= function |
| 1031 |
| Util.Inl (res,s,m,i) -> |
| 1032 |
Proof.set s >> |
| 1033 |
Proof.put m >> |
| 1034 |
Proof.update (fun _ -> i) >> |
| 1035 |
return res |
| 1036 |
| Util.Inr (e, info) -> tclZERO ~info e |
| 1037 |
|
| 1038 |
let tclTIMEOUT n t = tclTIMEOUTF (float_of_int n) t |
| 1039 |
|
| 1040 |
let tclTIME s t = |
| 1041 |
let pr_time t1 t2 n msg = |
| 1042 |
let msg = |
| 1043 |
if n = 0 then |
| 1044 |
str msg |
| 1045 |
else |
| 1046 |
str (msg ^ " after ") ++ int n ++ str (String.plural n " backtracking") |
| 1047 |
in |
| 1048 |
Feedback.msg_info(str "Tactic call" ++ pr_opt str s ++ str " ran for " ++ |
| 1049 |
System.fmt_time_difference t1 t2 ++ str " " ++ surround msg) in |
| 1050 |
let rec aux n t = |
| 1051 |
let open Proof in |
| 1052 |
tclUNIT () >>= fun () -> |
| 1053 |
let tstart = System.get_time() in |
| 1054 |
Proof.split t >>= let open Logic_monad in function |
| 1055 |
| Nil (e, info) -> |
| 1056 |
begin |
| 1057 |
let tend = System.get_time() in |
| 1058 |
pr_time tstart tend n "failure"; |
| 1059 |
tclZERO ~info e |
| 1060 |
end |
| 1061 |
| Cons (x,k) -> |
| 1062 |
let tend = System.get_time() in |
| 1063 |
pr_time tstart tend n "success"; |
| 1064 |
tclOR (tclUNIT x) (fun e -> aux (n+1) (k e)) |
| 1065 |
in aux 0 t |
| 1066 |
|
| 1067 |
let tclProofInfo = |
| 1068 |
let open Proof in |
| 1069 |
Logical.current >>= fun P.{name; poly} -> |
| 1070 |
tclUNIT (name, poly) |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
module Unsafe = struct |
| 1075 |
|
| 1076 |
let (>>=) = tclBIND |
| 1077 |
|
| 1078 |
let tclEVARS evd = |
| 1079 |
Pv.modify (fun ps -> { ps with solution = evd }) |
| 1080 |
|
| 1081 |
let tclNEWGOALS ?(before = false) gls = |
| 1082 |
Pv.modify begin fun step -> |
| 1083 |
let gls = undefined step.solution gls in |
| 1084 |
let comb = if before then gls @ step.comb else step.comb @ gls in |
| 1085 |
{ step with comb } |
| 1086 |
end |
| 1087 |
|
| 1088 |
let tclNEWSHELVED gls = |
| 1089 |
Pv.modify begin fun step -> |
| 1090 |
let gls = undefined_evars step.solution gls in |
| 1091 |
{ step with solution = Evd.shelve step.solution gls } |
| 1092 |
end |
| 1093 |
|
| 1094 |
let tclGETSHELF = tclEVARMAP >>= fun sigma -> tclUNIT @@ Evd.shelf sigma |
| 1095 |
|
| 1096 |
let tclSETENV = Env.set |
| 1097 |
|
| 1098 |
let tclGETGOALS = Comb.get |
| 1099 |
|
| 1100 |
let tclSETGOALS = Comb.set |
| 1101 |
|
| 1102 |
let tclEVARSADVANCE evd = |
| 1103 |
Pv.modify (fun ps -> { solution = evd; comb = undefined evd ps.comb }) |
| 1104 |
|
| 1105 |
let tclEVARUNIVCONTEXT ctx = |
| 1106 |
Pv.modify (fun ps -> { ps with solution = Evd.set_universe_context ps.solution ctx }) |
| 1107 |
|
| 1108 |
let push_future_goals p = |
| 1109 |
{ p with solution = Evd.push_future_goals p.solution } |
| 1110 |
|
| 1111 |
let mark_as_goals evd content = |
| 1112 |
mark_in_evm ~goal:true evd content |
| 1113 |
|
| 1114 |
let advance = Evarutil.advance |
| 1115 |
|
| 1116 |
let undefined = undefined |
| 1117 |
|
| 1118 |
let mark_unresolvables evm evs = |
| 1119 |
mark_in_evm ~goal:false evm evs |
| 1120 |
|
| 1121 |
let mark_as_unresolvables p evs = |
| 1122 |
{ p with solution = mark_in_evm ~goal:false p.solution evs } |
| 1123 |
|
| 1124 |
let update_sigma_univs ugraph pv = |
| 1125 |
{ pv with solution = Evd.update_sigma_univs ugraph pv.solution } |
| 1126 |
|
| 1127 |
let purge_side_effects pv = |
| 1128 |
let effs = Evd.eval_side_effects pv.solution in |
| 1129 |
{ pv with solution = Evd.set_side_effects Evd.empty_side_effects pv.solution }, effs |
| 1130 |
|
| 1131 |
end |
| 1132 |
|
| 1133 |
module UnsafeRepr = Proof.Unsafe |
| 1134 |
|
| 1135 |
let (>>=) = tclBIND |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
let catchable_exception = function |
| 1140 |
| Logic_monad.Exception _ -> false |
| 1141 |
| e -> CErrors.noncritical e |
| 1142 |
|
| 1143 |
|
| 1144 |
module Goal = struct |
| 1145 |
|
| 1146 |
type t = { |
| 1147 |
env : Environ.env; |
| 1148 |
sigma : Evd.evar_map; |
| 1149 |
concl : EConstr.constr ; |
| 1150 |
state : StateStore.t; |
| 1151 |
self : Evar.t ; |
| 1152 |
} |
| 1153 |
|
| 1154 |
let state { state=state } = state |
| 1155 |
|
| 1156 |
let env {env} = env |
| 1157 |
let sigma {sigma} = sigma |
| 1158 |
let hyps {env} = EConstr.named_context env |
| 1159 |
let concl {concl} = concl |
| 1160 |
let relevance {sigma; self} = |
| 1161 |
Evd.evar_relevance (Evd.find_undefined sigma self) |
| 1162 |
|
| 1163 |
let gmake_with info env sigma goal state = |
| 1164 |
{ env = Environ.reset_with_named_context (Evd.evar_filtered_hyps info) env ; |
| 1165 |
sigma = sigma ; |
| 1166 |
concl = Evd.evar_concl info; |
| 1167 |
state = state ; |
| 1168 |
self = goal } |
| 1169 |
|
| 1170 |
let gmake env sigma goal = |
| 1171 |
let state = get_state goal in |
| 1172 |
let goal = drop_state goal in |
| 1173 |
let info = Evd.find_undefined sigma goal in |
| 1174 |
gmake_with info env sigma goal state |
| 1175 |
|
| 1176 |
let enter f = |
| 1177 |
let f gl = InfoL.tag (Info.DBranch) (f gl) in |
| 1178 |
InfoL.tag (Info.Dispatch) begin |
| 1179 |
iter_goal begin fun goal -> |
| 1180 |
Env.get >>= fun env -> |
| 1181 |
tclEVARMAP >>= fun sigma -> |
| 1182 |
try f (gmake env sigma goal) |
| 1183 |
with e when catchable_exception e -> |
| 1184 |
let (e, info) = Exninfo.capture e in |
| 1185 |
tclZERO ~info e |
| 1186 |
end |
| 1187 |
end |
| 1188 |
|
| 1189 |
let enter_one ?(__LOC__=__LOC__) f = |
| 1190 |
let open Proof in |
| 1191 |
Comb.get >>= function |
| 1192 |
| [goal] -> begin |
| 1193 |
Env.get >>= fun env -> |
| 1194 |
tclEVARMAP >>= fun sigma -> |
| 1195 |
try f (gmake env sigma goal) |
| 1196 |
with e when catchable_exception e -> |
| 1197 |
let (e, info) = Exninfo.capture e in |
| 1198 |
tclZERO ~info e |
| 1199 |
end |
| 1200 |
| _ -> |
| 1201 |
CErrors.anomaly Pp.(str __LOC__ ++ str " enter_one") |
| 1202 |
|
| 1203 |
let goals = |
| 1204 |
Pv.get >>= fun step -> |
| 1205 |
let sigma = step.solution in |
| 1206 |
let map goal = |
| 1207 |
match cleared_alias sigma goal with |
| 1208 |
| None -> None |
| 1209 |
| Some goal -> |
| 1210 |
let oinfo = Evd.find_undefined sigma (drop_state goal) in |
| 1211 |
let gl = |
| 1212 |
Env.get >>= fun env -> |
| 1213 |
tclEVARMAP >>= fun sigma -> |
| 1214 |
let state = get_state goal in |
| 1215 |
let goal = drop_state goal in |
| 1216 |
let EvarInfo info = Evd.find sigma goal in |
| 1217 |
let goal = { |
| 1218 |
env = Environ.reset_with_named_context (Evd.evar_filtered_hyps info) env ; |
| 1219 |
sigma = sigma ; |
| 1220 |
concl = Evd.evar_concl oinfo; |
| 1221 |
state = state; |
| 1222 |
self = goal; |
| 1223 |
} in |
| 1224 |
tclUNIT goal |
| 1225 |
in |
| 1226 |
Some gl |
| 1227 |
in |
| 1228 |
tclUNIT (CList.map_filter map step.comb) |
| 1229 |
|
| 1230 |
let unsolved { self=self } = |
| 1231 |
tclEVARMAP >>= fun sigma -> |
| 1232 |
tclUNIT (not (Option.is_empty (Evarutil.advance sigma self))) |
| 1233 |
|
| 1234 |
|
| 1235 |
let goal { self=self } = self |
| 1236 |
|
| 1237 |
end |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
module Trace = struct |
| 1244 |
|
| 1245 |
let record_info_trace = InfoL.record_trace |
| 1246 |
|
| 1247 |
let log m = InfoL.leaf (Info.Msg m) |
| 1248 |
let name_tactic m t = InfoL.tag (Info.Tactic m) t |
| 1249 |
|
| 1250 |
let pr_info env sigma ?(lvl=0) info = |
| 1251 |
assert (lvl >= 0); |
| 1252 |
Info.(print env sigma (collapse lvl info)) |
| 1253 |
|
| 1254 |
end |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
module NonLogical = Logic_monad.NonLogical |
| 1261 |
|
| 1262 |
let tclLIFT = Proof.lift |
| 1263 |
|
| 1264 |
let tclCHECKINTERRUPT = |
| 1265 |
tclLIFT (NonLogical.make Control.check_for_interrupt) |
| 1266 |
|
| 1267 |
let wrap_exceptions f = |
| 1268 |
try f () |
| 1269 |
with e when catchable_exception e -> |
| 1270 |
let (e, info) = Exninfo.capture e in tclZERO ~info e |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
module Notations = struct |
| 1275 |
let (>>=) = tclBIND |
| 1276 |
let (<*>) = tclTHEN |
| 1277 |
let (<+>) t1 t2 = tclOR t1 (fun _ -> t2) |
| 1278 |
end |