| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Proofview.Notations |
| 13 |
open Context.Named.Declaration |
| 14 |
|
| 15 |
module NamedDecl = Context.Named.Declaration |
| 16 |
|
| 17 |
let extract_prefix env info = |
| 18 |
let ctx1 = List.rev (EConstr.named_context env) in |
| 19 |
let ctx2 = List.rev (Evd.evar_context info) in |
| 20 |
let rec share l1 l2 accu = match l1, l2 with |
| 21 |
| d1 :: l1, d2 :: l2 -> |
| 22 |
if d1 == d2 then share l1 l2 (d1 :: accu) |
| 23 |
else (accu, d2 :: l2) |
| 24 |
| _ -> (accu, l2) |
| 25 |
in |
| 26 |
share ctx1 ctx2 [] |
| 27 |
|
| 28 |
let typecheck_evar ev env sigma = |
| 29 |
let info = Evd.find_undefined sigma ev in |
| 30 |
|
| 31 |
let type_hyp (sigma, env) decl = |
| 32 |
let t = NamedDecl.get_type decl in |
| 33 |
let sigma, _ = Typing.sort_of env sigma t in |
| 34 |
let sigma = match decl with |
| 35 |
| LocalAssum _ -> sigma |
| 36 |
| LocalDef (_,body,_) -> Typing.check env sigma body t |
| 37 |
in |
| 38 |
(sigma, EConstr.push_named decl env) |
| 39 |
in |
| 40 |
let (common, changed) = extract_prefix env info in |
| 41 |
let env = Environ.reset_with_named_context (EConstr.val_of_named_context common) env in |
| 42 |
let (sigma, env) = List.fold_left type_hyp (sigma, env) changed in |
| 43 |
|
| 44 |
let sigma, _ = Typing.sort_of env sigma (Evd.evar_concl info) in |
| 45 |
sigma |
| 46 |
|
| 47 |
let generic_refine ~typecheck f gl = |
| 48 |
let sigma = Proofview.Goal.sigma gl in |
| 49 |
let env = Proofview.Goal.env gl in |
| 50 |
let concl = Proofview.Goal.concl gl in |
| 51 |
let state = Proofview.Goal.state gl in |
| 52 |
|
| 53 |
|
| 54 |
let sigma = Evd.push_future_goals sigma in |
| 55 |
|
| 56 |
Proofview.Unsafe.tclEVARS sigma >>= fun () -> |
| 57 |
f >>= fun (v, c, principal) -> |
| 58 |
Proofview.tclEVARMAP >>= fun sigma -> |
| 59 |
Proofview.wrap_exceptions begin fun () -> |
| 60 |
|
| 61 |
let fold accu ev = typecheck_evar ev env accu in |
| 62 |
let sigma = if typecheck then Evd.fold_future_goals fold sigma else sigma in |
| 63 |
|
| 64 |
let sigma = if typecheck then Typing.check env sigma c concl else sigma in |
| 65 |
|
| 66 |
let self = Proofview.Goal.goal gl in |
| 67 |
let _ = |
| 68 |
if not (Evarutil.occur_evar_upto sigma self c) then () |
| 69 |
else Pretype_errors.error_occur_check env sigma self c |
| 70 |
in |
| 71 |
|
| 72 |
let future_goals, sigma = Evd.pop_future_goals sigma in |
| 73 |
|
| 74 |
let future_goals = Evd.FutureGoals.map_filter (Proofview.Unsafe.advance sigma) future_goals in |
| 75 |
let future_goals = Evd.FutureGoals.filter (fun ev -> not @@ Evd.mem_shelf ev sigma) future_goals in |
| 76 |
|
| 77 |
let sigma = match Proofview.Unsafe.advance sigma self with |
| 78 |
| None -> |
| 79 |
|
| 80 |
sigma |
| 81 |
| Some self -> |
| 82 |
let sigma = match principal with |
| 83 |
| None -> sigma |
| 84 |
| Some evk -> Evd.transfer_name self evk sigma |
| 85 |
in Evd.define self c sigma |
| 86 |
in |
| 87 |
|
| 88 |
let sigma = Proofview.Unsafe.mark_as_goals sigma (Evd.FutureGoals.comb future_goals) in |
| 89 |
let comb = CList.rev_map (fun x -> Proofview.goal_with_state x state) (Evd.FutureGoals.comb future_goals) in |
| 90 |
let trace () = Pp.(hov 2 (str"simple refine"++spc()++ |
| 91 |
Termops.Internal.print_constr_env env sigma c)) in |
| 92 |
Proofview.Trace.name_tactic trace (Proofview.tclUNIT v) >>= fun v -> |
| 93 |
Proofview.Unsafe.tclEVARS sigma <*> |
| 94 |
Proofview.Unsafe.tclSETGOALS comb <*> |
| 95 |
Proofview.tclUNIT v |
| 96 |
end |
| 97 |
|
| 98 |
let lift c = |
| 99 |
Proofview.tclEVARMAP >>= fun sigma -> |
| 100 |
Proofview.wrap_exceptions begin fun () -> |
| 101 |
let (sigma, c) = c sigma in |
| 102 |
Proofview.Unsafe.tclEVARS sigma >>= fun () -> |
| 103 |
Proofview.tclUNIT c |
| 104 |
end |
| 105 |
|
| 106 |
let make_refine_enter ~typecheck f gl = generic_refine ~typecheck (lift f) gl |
| 107 |
|
| 108 |
let refine ~typecheck f = |
| 109 |
let f evd = |
| 110 |
let (evd,c) = f evd in (evd,((), c, None)) |
| 111 |
in |
| 112 |
Proofview.Goal.enter (make_refine_enter ~typecheck f) |
| 113 |
|
| 114 |
let refine_with_principal ~typecheck f = |
| 115 |
let f evd = |
| 116 |
let (evd,c, principal) = f evd in (evd,((), c, principal)) |
| 117 |
in |
| 118 |
Proofview.Goal.enter (make_refine_enter ~typecheck f) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
let solve_constraints = |
| 125 |
let open Proofview in |
| 126 |
tclENV >>= fun env -> tclEVARMAP >>= fun sigma -> |
| 127 |
try let sigma = Evarconv.solve_unif_constraints_with_heuristics env sigma in |
| 128 |
Unsafe.tclEVARSADVANCE sigma |
| 129 |
with e when CErrors.noncritical e -> |
| 130 |
let e, info = Exninfo.capture e in |
| 131 |
tclZERO ~info e |