| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
open Util |
| 33 |
|
| 34 |
module FocusKind = Dyn.Make() |
| 35 |
|
| 36 |
type 'a focus_kind = 'a FocusKind.tag |
| 37 |
type reason = NotThisWay | AlreadyNoFocus |
| 38 |
type unfocusable = |
| 39 |
| Cannot of reason |
| 40 |
| Loose |
| 41 |
| Strict |
| 42 |
type 'a focus_condition = |
| 43 |
| CondNo of bool * 'a focus_kind |
| 44 |
| CondDone of bool * 'a focus_kind |
| 45 |
| CondEndStack of 'a focus_kind |
| 46 |
|
| 47 |
let new_focus_kind = FocusKind.create |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
exception CannotUnfocusThisWay |
| 55 |
|
| 56 |
|
| 57 |
exception NoSuchGoals of int * int |
| 58 |
|
| 59 |
exception NoSuchGoal of Libnames.qualid option |
| 60 |
|
| 61 |
exception FullyUnfocused |
| 62 |
|
| 63 |
let _ = CErrors.register_handler begin function |
| 64 |
| CannotUnfocusThisWay -> |
| 65 |
Some (Pp.str "This proof is focused, but cannot be unfocused this way") |
| 66 |
| NoSuchGoals (i,j) when Int.equal i j -> |
| 67 |
Some Pp.(str "[Focus] No such goal (" ++ int i ++ str").") |
| 68 |
| NoSuchGoals (i,j) -> |
| 69 |
Some Pp.(str "[Focus] Not every goal in range ["++ int i ++ str","++int j++str"] exist.") |
| 70 |
| NoSuchGoal (Some id) -> |
| 71 |
Some Pp.(str "[Focus] No such goal: " ++ Libnames.pr_qualid id ++ str ".") |
| 72 |
| NoSuchGoal None -> |
| 73 |
Some Pp.(str "[Focus] No such goal.") |
| 74 |
| FullyUnfocused -> |
| 75 |
Some (Pp.str "The proof is not focused") |
| 76 |
| _ -> None |
| 77 |
end |
| 78 |
|
| 79 |
let check_cond_kind c k = |
| 80 |
let kind_of_cond = function |
| 81 |
| CondNo (_,k) | CondDone(_,k) | CondEndStack k -> k in |
| 82 |
FocusKind.eq (kind_of_cond c) k |
| 83 |
|
| 84 |
let equal_kind c k = match FocusKind.eq c k with |
| 85 |
| None -> false |
| 86 |
| Some _ -> true |
| 87 |
|
| 88 |
let test_cond c k1 pw = |
| 89 |
match c with |
| 90 |
| CondNo(_, k) when equal_kind k k1 -> Strict |
| 91 |
| CondNo(true, _) -> Loose |
| 92 |
| CondNo(false, _) -> Cannot NotThisWay |
| 93 |
| CondDone(_, k) when equal_kind k k1 && Proofview.finished pw -> Strict |
| 94 |
| CondDone(true, _) -> Loose |
| 95 |
| CondDone(false, _) -> Cannot NotThisWay |
| 96 |
| CondEndStack k when equal_kind k k1 -> Strict |
| 97 |
| CondEndStack _ -> Cannot AlreadyNoFocus |
| 98 |
|
| 99 |
let no_cond ?(loose_end=false) k = CondNo (loose_end, k) |
| 100 |
let done_cond ?(loose_end=false) k = CondDone (loose_end,k) |
| 101 |
|
| 102 |
type focus_element = FocusElt : 'a focus_condition * 'a * Proofview.focus_context -> focus_element |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
type t = |
| 107 |
{ proofview: Proofview.proofview |
| 108 |
|
| 109 |
; entry : Proofview.entry |
| 110 |
|
| 111 |
; focus_stack: focus_element list |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
; name : Names.Id.t |
| 116 |
|
| 117 |
; poly : PolyFlags.t |
| 118 |
|
| 119 |
; typing_flags : Declarations.typing_flags option |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
let proof p = |
| 125 |
let (goals,sigma) = Proofview.proofview p.proofview in |
| 126 |
|
| 127 |
|
| 128 |
let rec map_minus_one f = function |
| 129 |
| [] -> assert false |
| 130 |
| [_] -> [] |
| 131 |
| a::l -> f a :: (map_minus_one f l) |
| 132 |
in |
| 133 |
let map (FocusElt (_, _, c)) = Proofview.focus_context sigma c in |
| 134 |
let stack = map_minus_one map p.focus_stack in |
| 135 |
(goals,stack,sigma) |
| 136 |
|
| 137 |
let rec unroll_focus pv = function |
| 138 |
| FocusElt (_,_,ctx)::stk -> unroll_focus (Proofview.unfocus ctx pv) stk |
| 139 |
| [] -> pv |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
let is_done p = |
| 145 |
Proofview.finished p.proofview && |
| 146 |
Proofview.finished (unroll_focus p.proofview p.focus_stack) |
| 147 |
|
| 148 |
|
| 149 |
let partial_proof p = Proofview.partial_proof p.entry p.proofview |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
let push_focus cond inf context pr = |
| 156 |
{ pr with focus_stack = FocusElt(cond,inf,context)::pr.focus_stack } |
| 157 |
|
| 158 |
type any_focus_condition = AnyFocusCond : 'a focus_condition -> any_focus_condition |
| 159 |
|
| 160 |
|
| 161 |
let cond_of_focus pr = |
| 162 |
match pr.focus_stack with |
| 163 |
| FocusElt (cond,_,_)::_ -> AnyFocusCond cond |
| 164 |
| _ -> raise FullyUnfocused |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
let pop_focus pr = |
| 169 |
match pr.focus_stack with |
| 170 |
| focus::other_focuses -> |
| 171 |
{ pr with focus_stack = other_focuses }, focus |
| 172 |
| _ -> |
| 173 |
raise FullyUnfocused |
| 174 |
|
| 175 |
|
| 176 |
let _focus cond inf i j pr = |
| 177 |
let focused, context = Proofview.focus i j pr.proofview in |
| 178 |
let pr = push_focus cond inf context pr in |
| 179 |
{ pr with proofview = focused } |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
let _unfocus pr = |
| 185 |
let pr, FocusElt (_,_,fc) = pop_focus pr in |
| 186 |
{ pr with proofview = Proofview.unfocus fc pr.proofview } |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
let focus cond inf i pr = |
| 192 |
try _focus cond inf i i pr |
| 193 |
with CList.IndexOutOfRange -> raise (NoSuchGoals (i,i)) |
| 194 |
|
| 195 |
|
| 196 |
let focus_id cond inf id pr = |
| 197 |
let (focused_goals, evar_map) = Proofview.proofview pr.proofview in |
| 198 |
begin match try Some (Evd.evar_key id evar_map) with Not_found -> None with |
| 199 |
| Some ev -> |
| 200 |
begin match CList.index_opt Evar.equal ev focused_goals with |
| 201 |
| Some i -> |
| 202 |
|
| 203 |
_focus cond inf i i pr |
| 204 |
| None -> |
| 205 |
if Evd.mem_shelf ev evar_map then |
| 206 |
|
| 207 |
let proofview = Proofview.unshelve [ev] pr.proofview in |
| 208 |
let pr = { pr with proofview } in |
| 209 |
let (focused_goals, _) = Proofview.proofview pr.proofview in |
| 210 |
let i = |
| 211 |
|
| 212 |
try CList.index Evar.equal ev focused_goals |
| 213 |
with Not_found -> assert false |
| 214 |
in |
| 215 |
_focus cond inf i i pr |
| 216 |
else |
| 217 |
raise CannotUnfocusThisWay |
| 218 |
end |
| 219 |
| None -> |
| 220 |
raise (NoSuchGoal (Some id)) |
| 221 |
end |
| 222 |
|
| 223 |
let rec unfocus kind pr () = |
| 224 |
let AnyFocusCond cond = cond_of_focus pr in |
| 225 |
match test_cond cond kind pr.proofview with |
| 226 |
| Cannot NotThisWay -> raise CannotUnfocusThisWay |
| 227 |
| Cannot AlreadyNoFocus -> raise FullyUnfocused |
| 228 |
| Strict -> |
| 229 |
let pr = _unfocus pr in |
| 230 |
pr |
| 231 |
| Loose -> |
| 232 |
begin try |
| 233 |
let pr = _unfocus pr in |
| 234 |
unfocus kind pr () |
| 235 |
with FullyUnfocused -> raise CannotUnfocusThisWay |
| 236 |
end |
| 237 |
|
| 238 |
exception NoSuchFocus |
| 239 |
|
| 240 |
let rec get_in_focus_stack : type a. a focus_kind -> _ -> a = fun kind stack -> |
| 241 |
match stack with |
| 242 |
| FocusElt (cond,inf,_)::stack -> |
| 243 |
begin match check_cond_kind cond kind with |
| 244 |
| Some Refl -> inf |
| 245 |
| None -> get_in_focus_stack kind stack |
| 246 |
end |
| 247 |
| [] -> raise NoSuchFocus |
| 248 |
let get_at_focus kind pr = |
| 249 |
get_in_focus_stack kind pr.focus_stack |
| 250 |
|
| 251 |
let is_last_focus kind pr = |
| 252 |
let FocusElt (cond,_,_) = List.hd pr.focus_stack in |
| 253 |
Option.has_some (check_cond_kind cond kind) |
| 254 |
|
| 255 |
let no_focused_goal p = |
| 256 |
Proofview.finished p.proofview |
| 257 |
|
| 258 |
let rec maximal_unfocus k p = |
| 259 |
if no_focused_goal p then |
| 260 |
try maximal_unfocus k (unfocus k p ()) |
| 261 |
with FullyUnfocused | CannotUnfocusThisWay -> p |
| 262 |
else p |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
let end_of_stack_kind = new_focus_kind "end_of_stack" |
| 268 |
let end_of_stack = CondEndStack end_of_stack_kind |
| 269 |
|
| 270 |
let unfocused = is_last_focus end_of_stack_kind |
| 271 |
|
| 272 |
let unfocus_all p = unfocus end_of_stack_kind p () |
| 273 |
|
| 274 |
let start ~name ~poly ?typing_flags sigma goals = |
| 275 |
let entry, proofview = Proofview.init sigma goals in |
| 276 |
let pr = |
| 277 |
{ proofview |
| 278 |
; entry |
| 279 |
; focus_stack = [] |
| 280 |
; name |
| 281 |
; poly |
| 282 |
; typing_flags |
| 283 |
} in |
| 284 |
_focus end_of_stack () 1 (List.length goals) pr |
| 285 |
|
| 286 |
let dependent_start ~name ~poly ?typing_flags goals = |
| 287 |
let entry, proofview = Proofview.dependent_init goals in |
| 288 |
let pr = |
| 289 |
{ proofview |
| 290 |
; entry |
| 291 |
; focus_stack = [] |
| 292 |
; name |
| 293 |
; poly |
| 294 |
; typing_flags |
| 295 |
} in |
| 296 |
let number_of_goals = List.length (Proofview.initial_goals pr.entry) in |
| 297 |
_focus end_of_stack () 1 number_of_goals pr |
| 298 |
|
| 299 |
let compact p = |
| 300 |
let entry, proofview = Proofview.compact p.entry p.proofview in |
| 301 |
{ p with proofview; entry } |
| 302 |
|
| 303 |
let update_sigma_univs ugraph p = |
| 304 |
let proofview = Proofview.Unsafe.update_sigma_univs ugraph p.proofview in |
| 305 |
{ p with proofview } |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
let run_tactic env tac pr = |
| 312 |
let open Proofview.Notations in |
| 313 |
let tac = |
| 314 |
|
| 315 |
Proofview.with_shelf tac >>= fun (shelf, v) -> |
| 316 |
Proofview.Unsafe.tclNEWSHELVED shelf <*> |
| 317 |
Proofview.tclUNIT v |
| 318 |
in |
| 319 |
let { name; poly; proofview } = pr in |
| 320 |
let (result,proofview,env,status,info_trace) = |
| 321 |
Proofview.apply ~name ~poly env tac proofview |
| 322 |
in |
| 323 |
let sigma = Proofview.return proofview in |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
let proofview = Proofview.filter_shelf (Evd.is_undefined sigma) proofview in |
| 329 |
{ pr with proofview },(env,status,info_trace),result |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
let unshelve p = |
| 336 |
let sigma = Proofview.return p.proofview in |
| 337 |
let shelf = Evd.shelf sigma in |
| 338 |
let proofview = Proofview.unshelve shelf p.proofview in |
| 339 |
{ p with proofview } |
| 340 |
|
| 341 |
let background_subgoals p = |
| 342 |
let it, _ = Proofview.proofview (unroll_focus p.proofview p.focus_stack) in |
| 343 |
it |
| 344 |
|
| 345 |
let all_goals p = |
| 346 |
let add gs set = |
| 347 |
List.fold_left (fun s g -> Evar.Set.add g s) set gs in |
| 348 |
let (goals,stack,sigma) = proof p in |
| 349 |
let set = add goals Evar.Set.empty in |
| 350 |
let set = List.fold_left (fun s gs -> let (g1, g2) = gs in add g1 (add g2 set)) set stack in |
| 351 |
let set = add (Evd.shelf sigma) set in |
| 352 |
let set = Evar.Set.union (Evd.given_up sigma) set in |
| 353 |
let bgoals = background_subgoals p in |
| 354 |
add bgoals set |
| 355 |
|
| 356 |
type data = |
| 357 |
{ sigma : Evd.evar_map |
| 358 |
|
| 359 |
; goals : Evar.t list |
| 360 |
|
| 361 |
; entry : Proofview.entry |
| 362 |
|
| 363 |
; stack : (Evar.t list * Evar.t list) list |
| 364 |
|
| 365 |
; name : Names.Id.t |
| 366 |
|
| 367 |
; poly : PolyFlags.t |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
let data { proofview; focus_stack; entry; name; poly } = |
| 372 |
let goals, sigma = Proofview.proofview proofview in |
| 373 |
|
| 374 |
|
| 375 |
let rec map_minus_one f = function |
| 376 |
| [] -> assert false |
| 377 |
| [_] -> [] |
| 378 |
| a::l -> f a :: (map_minus_one f l) |
| 379 |
in |
| 380 |
let map (FocusElt (_, _, c)) = Proofview.focus_context sigma c in |
| 381 |
let stack = map_minus_one map focus_stack in |
| 382 |
{ sigma; goals; entry; stack; name; poly } |
| 383 |
|
| 384 |
let pr_goal e = Pp.(str "GOAL:" ++ int (Evar.repr e)) |
| 385 |
|
| 386 |
let goal_uid e = string_of_int (Evar.repr e) |
| 387 |
|
| 388 |
let pr_proof p = |
| 389 |
let { goals=fg_goals; stack=bg_goals; sigma } = data p in |
| 390 |
let entry = Proofview.partial_proof p.entry p.proofview in |
| 391 |
Pp.( |
| 392 |
let pr_goal_list = prlist_with_sep spc pr_goal in |
| 393 |
let rec aux acc = function |
| 394 |
| [] -> acc |
| 395 |
| (before,after)::stack -> |
| 396 |
aux (pr_goal_list before ++ spc () ++ str "{" ++ acc ++ str "}" ++ spc () ++ |
| 397 |
pr_goal_list after) stack in |
| 398 |
str "[" ++ str "focus structure: " ++ |
| 399 |
aux (pr_goal_list fg_goals) bg_goals ++ str ";" ++ spc () ++ |
| 400 |
str "shelved: " ++ pr_goal_list (Evd.shelf sigma) ++ str ";" ++ spc () ++ |
| 401 |
str "given up: " ++ pr_goal_list (Evar.Set.elements @@ Evd.given_up sigma) ++ spc() ++ |
| 402 |
str "partial proof: " ++ prlist_with_sep spc (Termops.Internal.print_constr_env (Global.env()) sigma) entry ++ |
| 403 |
str "]" |
| 404 |
) |
| 405 |
|
| 406 |
let { Goptions.get = use_unification_heuristics } = |
| 407 |
Goptions.declare_bool_option_and_ref |
| 408 |
~key:["Solve";"Unification";"Constraints"] |
| 409 |
~value:true |
| 410 |
() |
| 411 |
|
| 412 |
exception SuggestNoSuchGoals of int * t |
| 413 |
|
| 414 |
exception FailedConstraints of exn |
| 415 |
|
| 416 |
let () = CErrors.register_handler (function |
| 417 |
| FailedConstraints e -> |
| 418 |
Some Pp.(CErrors.print e ++ spc() ++ |
| 419 |
hov 1 |
| 420 |
(str "(while solving unification constraints," ++ spc() ++ |
| 421 |
str "see flag \"Solve Unification Constraints\")")) |
| 422 |
| _ -> None) |
| 423 |
|
| 424 |
let solve_constraints = |
| 425 |
Proofview.tclOR Refine.solve_constraints |
| 426 |
(fun (e,info) -> Proofview.tclZERO ~info (FailedConstraints e)) |
| 427 |
|
| 428 |
let solve ?with_end_tac env gi info_lvl tac pr = |
| 429 |
let tac = match with_end_tac with |
| 430 |
| None -> tac |
| 431 |
| Some etac -> Proofview.tclTHEN tac etac in |
| 432 |
let tac = match info_lvl with |
| 433 |
| None -> tac |
| 434 |
| Some _ -> Proofview.Trace.record_info_trace tac |
| 435 |
in |
| 436 |
let nosuchgoal = |
| 437 |
let info = Exninfo.reify () in |
| 438 |
Proofview.tclZERO ~info (SuggestNoSuchGoals (1,pr)) |
| 439 |
in |
| 440 |
let tac = Goal_select.tclSELECT ~nosuchgoal gi tac in |
| 441 |
let tac = |
| 442 |
if use_unification_heuristics () then |
| 443 |
Proofview.tclTHEN tac solve_constraints |
| 444 |
else tac |
| 445 |
in |
| 446 |
let env = Environ.update_typing_flags ?typing_flags:pr.typing_flags env in |
| 447 |
let (p, (env, status, info), ()) = run_tactic env tac pr in |
| 448 |
let sigma = (data p).sigma in |
| 449 |
let () = |
| 450 |
match info_lvl with |
| 451 |
| None -> () |
| 452 |
| Some i -> Feedback.msg_info (Pp.hov 0 (Proofview.Trace.pr_info env sigma ~lvl:i info)) |
| 453 |
in |
| 454 |
(p, status) |
| 455 |
|
| 456 |
let get_goal_context_gen pf i = |
| 457 |
let { sigma; goals } = data pf in |
| 458 |
let goal = try List.nth goals (i-1) with Failure _ -> raise (NoSuchGoal None) in |
| 459 |
let env = Evd.evar_filtered_env (Global.env ()) (Evd.find_undefined sigma goal) in |
| 460 |
(sigma, env) |
| 461 |
|
| 462 |
let get_proof_context p = |
| 463 |
try get_goal_context_gen p 1 |
| 464 |
with |
| 465 |
| NoSuchGoal _ -> |
| 466 |
|
| 467 |
let { sigma } = data p in |
| 468 |
sigma, Global.env () |
| 469 |
|
| 470 |
let purge_side_effects p = |
| 471 |
let proofview, eff = Proofview.Unsafe.purge_side_effects p.proofview in |
| 472 |
{ p with proofview }, eff |