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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
(*i camlp4deps: "parsing/grammar.cma" i*)
open Ast;;
open Coqast;;
open Tacmach;;
open Tacticals;;
open Proof_trees;;
open Pp;;
open Pptactic;;
open Util;;
open Proof_type;;
open Tacexpr;;
open Genarg;;
(* Compacting and uncompacting proof commands *)
type report_tree =
Report_node of bool *int * report_tree list
| Mismatch of int * int
| Tree_fail of report_tree
| Failed of int;;
type report_card =
Ngoals of int
| Goals_mismatch of int
| Recursive_fail of report_tree
| Fail;;
type card_holder = report_card ref;;
type report_holder = report_tree list ref;;
(* This tactical receives an integer and a tactic and checks that the
tactic produces that number of goals. It never fails but signals failure
by updating the boolean reference given as third argument to false.
It is especially suited for use in checked_thens below. *)
let check_subgoals_count : card_holder -> int -> bool ref -> tactic -> tactic =
fun card_holder count flag t g ->
try
let (gls, v) as result = t g in
let len = List.length (sig_it gls) in
card_holder :=
(if len = count then
(flag := true;
Ngoals count)
else
(flag := false;
Goals_mismatch len));
result
with
e -> card_holder := Fail;
flag := false;
tclIDTAC g;;
let no_failure = function
[Report_node(true,_,_)] -> true
| _ -> false;;
let check_subgoals_count2
: card_holder -> int -> bool ref -> (report_holder -> tactic) -> tactic =
fun card_holder count flag t g ->
let new_report_holder = ref ([] : report_tree list) in
let (gls, v) as result = t new_report_holder g in
let succeeded = no_failure !new_report_holder in
let len = List.length (sig_it gls) in
card_holder :=
(if (len = count) & succeeded then
(flag := true;
Ngoals count)
else
(flag := false;
Recursive_fail (List.hd !new_report_holder)));
result;;
(*
let traceable = function
Node(_, "TACTICLIST", a::b::tl) -> true
| _ -> false;;
*)
let traceable = function
| TacThen _ | TacThens _ -> true
| _ -> false;;
let rec collect_status = function
Report_node(true,_,_)::tl -> collect_status tl
| [] -> true
| _ -> false;;
(* This tactical receives a tactic and executes it, reporting information
about success in the report holder and a boolean reference. *)
let count_subgoals : card_holder -> bool ref -> tactic -> tactic =
fun card_holder flag t g ->
try
let (gls, _) as result = t g in
card_holder := (Ngoals(List.length (sig_it gls)));
flag := true;
result
with
e -> card_holder := Fail;
flag := false;
tclIDTAC g;;
let count_subgoals2
: card_holder -> bool ref -> (report_holder -> tactic) -> tactic =
fun card_holder flag t g ->
let new_report_holder = ref([] : report_tree list) in
let (gls, v) as result = t new_report_holder g in
let succeeded = no_failure !new_report_holder in
if succeeded then
(flag := true;
card_holder := Ngoals (List.length (sig_it gls)))
else
(flag := false;
card_holder := Recursive_fail(List.hd !new_report_holder));
result;;
let rec local_interp : glob_tactic_expr -> report_holder -> tactic = function
(*
Node(_, "TACTICLIST", [a;Node(_, "TACLIST", l)]) ->
(fun report_holder -> checked_thens report_holder a l)
| Node(_, "TACTICLIST", a::((Node(_, "TACLIST", l))as b)::c::tl) ->
local_interp(ope ("TACTICLIST", (ope("TACTICLIST", [a;b]))::c::tl))
| Node(_, "TACTICLIST", [a;b]) ->
(fun report_holder -> checked_then report_holder a b)
| Node(_, "TACTICLIST", a::b::c::tl) ->
local_interp(ope ("TACTICLIST", (ope("TACTICLIST", [a;b]))::c::tl))
| ast ->
(fun report_holder g ->
try
let (gls, _) as result = Tacinterp.interp ast g in
report_holder := (Report_node(true, List.length (sig_it gls), []))
::!report_holder;
result
with e -> (report_holder := (Failed 1)::!report_holder;
tclIDTAC g))
*)
TacThens (a,l) ->
(fun report_holder -> checked_thens report_holder a l)
| TacThen (a,b) ->
(fun report_holder -> checked_then report_holder a b)
| t ->
(fun report_holder g ->
try
let (gls, _) as result = Tacinterp.eval_tactic t g in
report_holder := (Report_node(true, List.length (sig_it gls), []))
::!report_holder;
result
with e -> (report_holder := (Failed 1)::!report_holder;
tclIDTAC g))
(* This tactical receives a tactic and a list of tactics as argument.
It applies the first tactic and then maps the list of tactics to
various produced sub-goals. This tactic will never fail, but reports
are added in the report_holder in the following way:
- In case of partial success, a new report_tree is added to the report_holder
- In case of failure of the first tactic, with no more indications
then Failed 0 is added to the report_holder,
- In case of partial failure of the first tactic then (Failed n) is added to
the report holder.
- In case of success of the first tactic, but count mismatch, then
Mismatch n is added to the report holder. *)
and checked_thens: report_holder -> glob_tactic_expr -> glob_tactic_expr list -> tactic =
(fun report_holder t1 l g ->
let flag = ref true in
let traceable_t1 = traceable t1 in
let card_holder = ref Fail in
let new_holder = ref ([]:report_tree list) in
let tac_t1 =
if traceable_t1 then
(check_subgoals_count2 card_holder (List.length l)
flag (local_interp t1))
else
(check_subgoals_count card_holder (List.length l)
flag (Tacinterp.eval_tactic t1)) in
let (gls, _) as result =
tclTHEN_i tac_t1
(fun i ->
if !flag then
(fun g ->
let tac_i = (List.nth l i) in
if traceable tac_i then
local_interp tac_i new_holder g
else
try
let (gls,_) as result = Tacinterp.eval_tactic tac_i g in
let len = List.length (sig_it gls) in
new_holder :=
(Report_node(true, len, []))::!new_holder;
result
with
e -> (new_holder := (Failed 1)::!new_holder;
tclIDTAC g))
else
tclIDTAC) g in
let new_goal_list = sig_it gls in
(if !flag then
report_holder :=
(Report_node(collect_status !new_holder,
(List.length new_goal_list),
List.rev !new_holder))::!report_holder
else
report_holder :=
(match !card_holder with
Goals_mismatch(n) -> Mismatch(n, List.length l)
| Recursive_fail tr -> Tree_fail tr
| Fail -> Failed 1
| _ -> errorlabstrm "check_thens"
(str "this case should not happen in check_thens"))::
!report_holder);
result)
(* This tactical receives two tactics as argument, it executes the
first tactic and applies the second one to all the produced goals,
reporting information about the success of all tactics in the report
holder. It never fails. *)
and checked_then: report_holder -> glob_tactic_expr -> glob_tactic_expr -> tactic =
(fun report_holder t1 t2 g ->
let flag = ref true in
let card_holder = ref Fail in
let tac_t1 =
if traceable t1 then
(count_subgoals2 card_holder flag (local_interp t1))
else
(count_subgoals card_holder flag (Tacinterp.eval_tactic t1)) in
let new_tree_holder = ref ([] : report_tree list) in
let (gls, _) as result =
tclTHEN tac_t1
(fun (g:goal sigma) ->
if !flag then
if traceable t2 then
local_interp t2 new_tree_holder g
else
try
let (gls, _) as result = Tacinterp.eval_tactic t2 g in
new_tree_holder :=
(Report_node(true, List.length (sig_it gls),[]))::
!new_tree_holder;
result
with
e ->
(new_tree_holder := ((Failed 1)::!new_tree_holder);
tclIDTAC g)
else
tclIDTAC g) g in
(if !flag then
report_holder :=
(Report_node(collect_status !new_tree_holder,
List.length (sig_it gls),
List.rev !new_tree_holder))::!report_holder
else
report_holder :=
(match !card_holder with
Recursive_fail tr -> Tree_fail tr
| Fail -> Failed 1
| _ -> error "this case should not happen in check_then")::!report_holder);
result);;
(* This tactic applies the given tactic only to those subgoals designated
by the list of integers given as extra arguments.
*)
let on_then = function [t1;t2;l] ->
let t1 = out_gen wit_tactic t1 in
let t2 = out_gen wit_tactic t2 in
let l = out_gen (wit_list0 wit_int) l in
tclTHEN_i (Tacinterp.eval_tactic t1)
(fun i ->
if List.mem (i + 1) l then
(Tacinterp.eval_tactic t2)
else
tclIDTAC)
| _ -> anomaly "bad arguments for on_then";;
let mkOnThen t1 t2 selected_indices =
let a = in_gen rawwit_tactic t1 in
let b = in_gen rawwit_tactic t2 in
let l = in_gen (wit_list0 rawwit_int) selected_indices in
TacAtom (dummy_loc, TacExtend (dummy_loc, "OnThen", [a;b;l]));;
(* Analyzing error reports *)
(*
let rec select_success n = function
[] -> []
| Report_node(true,_,_)::tl -> (Num((0,0),n))::select_success (n+1) tl
| _::tl -> select_success (n+1) tl;;
*)
let rec select_success n = function
[] -> []
| Report_node(true,_,_)::tl -> n::select_success (n+1) tl
| _::tl -> select_success (n+1) tl;;
(*
let rec expand_tactic = function
Node(loc1, "TACTICLIST", [a;Node(loc2,"TACLIST", l)]) ->
Node(loc1, "TACTICLIST",
[expand_tactic a;
Node(loc2, "TACLIST", List.map expand_tactic l)])
| Node(loc1, "TACTICLIST", a::((Node(loc2, "TACLIST", l))as b)::c::tl) ->
expand_tactic (Node(loc1, "TACTICLIST",
(Node(loc1, "TACTICLIST", [a;b]))::c::tl))
| Node(loc1, "TACTICLIST", [a;b]) ->
Node(loc1, "TACTICLIST",[expand_tactic a;expand_tactic b])
| Node(loc1, "TACTICLIST", a::b::c::tl) ->
expand_tactic (Node(loc1, "TACTICLIST",
(Node(loc1, "TACTICLIST", [a;b]))::c::tl))
| any -> any;;
*)
(* Useless: already in binary form...
let rec expand_tactic = function
TacThens (a,l) -> TacThens (expand_tactic a, List.map expand_tactic l)
| TacThen (a,b) -> TacThen (expand_tactic a, expand_tactic b)
| any -> any;;
*)
(*
let rec reconstruct_success_tac ast =
match ast with
Node(_, "TACTICLIST", [a;Node(_,"TACLIST",l)]) ->
(function
Report_node(true, n, l) -> ast
| Report_node(false, n, rl) ->
ope("TACTICLIST",[a;ope("TACLIST",
List.map2 reconstruct_success_tac l rl)])
| Failed n -> ope("Idtac",[])
| Tree_fail r -> reconstruct_success_tac a r
| Mismatch (n,p) -> a)
| Node(_, "TACTICLIST", [a;b]) ->
(function
Report_node(true, n, l) -> ast
| Report_node(false, n, rl) ->
let selected_indices = select_success 1 rl in
ope("OnThen", a::b::selected_indices)
| Failed n -> ope("Idtac",[])
| Tree_fail r -> reconstruct_success_tac a r
| _ -> error "this error case should not happen in a THEN tactic")
| _ ->
(function
Report_node(true, n, l) -> ast
| Failed n -> ope("Idtac",[])
| _ ->
errorlabstrm
"this error case should not happen on an unknown tactic"
(str "error in reconstruction with " ++ fnl () ++
(gentacpr ast)));;
*)
let rec reconstruct_success_tac (tac:glob_tactic_expr) =
match tac with
TacThens (a,l) ->
(function
Report_node(true, n, l) -> tac
| Report_node(false, n, rl) ->
TacThens (a,List.map2 reconstruct_success_tac l rl)
| Failed n -> TacId ""
| Tree_fail r -> reconstruct_success_tac a r
| Mismatch (n,p) -> a)
| TacThen (a,b) ->
(function
Report_node(true, n, l) -> tac
| Report_node(false, n, rl) ->
let selected_indices = select_success 1 rl in
TacAtom (dummy_loc,TacExtend (dummy_loc,"OnThen",
[in_gen globwit_tactic a;
in_gen globwit_tactic b;
in_gen (wit_list0 globwit_int) selected_indices]))
| Failed n -> TacId ""
| Tree_fail r -> reconstruct_success_tac a r
| _ -> error "this error case should not happen in a THEN tactic")
| _ ->
(function
Report_node(true, n, l) -> tac
| Failed n -> TacId ""
| _ ->
errorlabstrm
"this error case should not happen on an unknown tactic"
(str "error in reconstruction with " ++ fnl () ++
(pr_glob_tactic tac)));;
let rec path_to_first_error = function
| Report_node(true, _, l) ->
let rec find_first_error n = function
| (Report_node(true, _, _))::tl -> find_first_error (n + 1) tl
| it::tl -> n, it
| [] -> error "no error detected" in
let p, t = find_first_error 1 l in
p::(path_to_first_error t)
| _ -> [];;
(*
let rec flatten_then_list tail = function
| Node(_, "TACTICLIST", [a;b]) ->
flatten_then_list ((flatten_then b)::tail) a
| ast -> ast::tail
and flatten_then = function
Node(_, "TACTICLIST", [a;b]) ->
ope("TACTICLIST", flatten_then_list [flatten_then b] a)
| Node(_, "TACLIST", l) ->
ope("TACLIST", List.map flatten_then l)
| Node(_, "OnThen", t1::t2::l) ->
ope("OnThen", (flatten_then t1)::(flatten_then t2)::l)
| ast -> ast;;
*)
let debug_tac = function
[(Tacexp ast)] ->
(fun g ->
let report = ref ([] : report_tree list) in
let result = local_interp ast report g in
let clean_ast = (* expand_tactic *) ast in
let report_tree =
try List.hd !report with
Failure "hd" -> (msgnl (str "report is empty"); Failed 1) in
let success_tac =
reconstruct_success_tac clean_ast report_tree in
let compact_success_tac = (* flatten_then *) success_tac in
msgnl (fnl () ++
str "========= Successful tactic =============" ++
fnl () ++
pr_glob_tactic compact_success_tac ++ fnl () ++
str "========= End of successful tactic ============");
result)
| _ -> error "wrong arguments for debug_tac";;
(* TODO ... used ?
add_tactic "DebugTac" debug_tac;;
*)
(*
hide_tactic "OnThen" on_then;;
*)
Refiner.add_tactic "OnThen" on_then;;
(*
let rec clean_path p ast l =
match ast, l with
Node(_, "TACTICLIST", ([_;_] as tacs)), fst::tl ->
fst::(clean_path 0 (List.nth tacs (fst - 1)) tl)
| Node(_, "TACTICLIST", tacs), 2::tl ->
let rank = (List.length tacs) - p in
rank::(clean_path 0 (List.nth tacs (rank - 1)) tl)
| Node(_, "TACTICLIST", tacs), 1::tl ->
clean_path (p+1) ast tl
| Node(_, "TACLIST", tacs), fst::tl ->
fst::(clean_path 0 (List.nth tacs (fst - 1)) tl)
| _, [] -> []
| _, _ -> failwith "this case should not happen in clean_path";;
*)
let rec clean_path tac l =
match tac, l with
| TacThen (a,b), fst::tl ->
fst::(clean_path (if fst = 1 then a else b) tl)
| TacThens (a,l), 1::tl ->
1::(clean_path a tl)
| TacThens (a,tacs), 2::fst::tl ->
2::fst::(clean_path (List.nth tacs (fst - 1)) tl)
| _, [] -> []
| _, _ -> failwith "this case should not happen in clean_path";;
let rec report_error
: glob_tactic_expr -> goal sigma option ref -> glob_tactic_expr ref -> int list ref ->
int list -> tactic =
fun tac the_goal the_ast returned_path path ->
match tac with
TacThens (a,l) ->
let the_card_holder = ref Fail in
let the_flag = ref false in
let the_exn = ref (Failure "") in
tclTHENS
(fun g ->
let result =
check_subgoals_count
the_card_holder
(List.length l)
the_flag
(fun g2 ->
try
(report_error a the_goal the_ast returned_path (1::path) g2)
with
e -> (the_exn := e; raise e))
g in
if !the_flag then
result
else
(match !the_card_holder with
Fail ->
the_ast := TacThens (!the_ast, l);
raise !the_exn
| Goals_mismatch p ->
the_ast := tac;
returned_path := path;
error ("Wrong number of tactics: expected " ^
(string_of_int (List.length l)) ^ " received " ^
(string_of_int p))
| _ -> error "this should not happen"))
(let rec fold_num n = function
[] -> []
| t::tl -> (report_error t the_goal the_ast returned_path (n::2::path))::
(fold_num (n + 1) tl) in
fold_num 1 l)
| TacThen (a,b) ->
let the_count = ref 1 in
tclTHEN
(fun g ->
try
report_error a the_goal the_ast returned_path (1::path) g
with
e ->
(the_ast := TacThen (!the_ast, b);
raise e))
(fun g ->
try
let result =
report_error b the_goal the_ast returned_path (2::path) g in
the_count := !the_count + 1;
result
with
e ->
if !the_count > 1 then
msgnl
(str "in branch no " ++ int !the_count ++
str " after tactic " ++ pr_glob_tactic a);
raise e)
| tac ->
(fun g ->
try
Tacinterp.eval_tactic tac g
with
e ->
(the_ast := tac;
the_goal := Some g;
returned_path := path;
raise e));;
let strip_some = function
Some n -> n
| None -> failwith "No optional value";;
let descr_first_error tac =
(fun g ->
let the_goal = ref (None : goal sigma option) in
let the_ast = ref tac in
let the_path = ref ([] : int list) in
try
let result = report_error tac the_goal the_ast the_path [] g in
msgnl (str "no Error here");
result
with
e ->
(msgnl (str "Execution of this tactic raised message " ++ fnl () ++
fnl () ++ Cerrors.explain_exn e ++ fnl () ++
fnl () ++ str "on goal" ++ fnl () ++
pr_goal (sig_it (strip_some !the_goal)) ++ fnl () ++
str "faulty tactic is" ++ fnl () ++ fnl () ++
pr_glob_tactic ((*flatten_then*) !the_ast) ++ fnl ());
tclIDTAC g))
(* TODO ... used ??
add_tactic "DebugTac2" descr_first_error;;
*)
(*
TACTIC EXTEND DebugTac2
[ ??? ] -> [ descr_first_error tac ]
END
*)
|