-rw-r--r-- 20094 holtrace-20261122/holtrace_restore.ml raw
Printf.printf("===== loading holtrace_restore\n");;
(*
XXX: should reject partial final line on restore
XXX: could automatically rename constants etc. to avoid collisions
but this would be two-pass, so easier as a separate tool
XXX: could inject new theorems into Toploop bindings
XXX: inf_def now allows alpha-equivalent variants of existing def;
maybe also allow alpha-equivalence for typedef?
*)
module Holtrace_restore: sig
val holtrace_restore: (string * thm) list -> string -> unit
val holtrace_restore_time: (string * thm) list -> string -> unit
val holtrace_restore_allownew: (string * thm) list -> string -> unit
val holtrace_restore_allownew_time: (string * thm) list -> string -> unit
val holtrace_restore_skipthmchecks: (string * thm) list -> string -> unit
val holtrace_restore_skipthmchecks_time: (string * thm) list -> string -> unit
val holtrace_restore_skipthmchecks_allownew: (string * thm) list -> string -> unit
val holtrace_restore_skipthmchecks_allownew_time: (string * thm) list -> string -> unit
val holtrace_restore_worker: (unit -> float) -> bool -> bool -> bool -> (string * thm) list -> string -> unit
val holtrace_link_hook: (term -> thm option) ref
val holtrace_restore_id2type: int -> hol_type
val holtrace_restore_id2term: int -> term
val holtrace_restore_id2thm: int -> thm
val holtrace_restore_name2thm: string -> thm
end = struct
(* ===== parsing *)
let parse_subid line pos id =
let linelen = String.length line in
let rec parse_nonneg pos result =
assert(result < 288230376151711744);
if pos >= linelen then pos,result else
let c = line.[pos] in
if c = ' ' then pos+1,result else
if c >= '0' && c <= '9' then
parse_nonneg (pos+1) (result*32+(Char.code c-48))
else if c >= 'A' && c <= 'V' then
parse_nonneg (pos+1) (result*32+(Char.code c-55))
else pos,result
in
let parse_allowneg pos =
if pos >= linelen then pos,0 else
let c = line.[pos] in
if c = '-' then
let outpos,n = parse_nonneg (pos+1) 0 in
outpos,id-n
else let outpos,n = parse_nonneg pos 0 in
assert(outpos > pos);
outpos,n
in
let parse_core pos =
if pos >= linelen then pos,0 else
match line.[pos] with
| '!' -> pos+1,id-1
| '@' -> pos+1,id-2
| '#' -> pos+1,id-3
| '$' -> pos+1,id-4
| '%' -> pos+1,id-5
| '^' -> pos+1,id-6
| '&' -> pos+1,id-7
| '*' -> pos+1,id-8
| '(' -> pos+1,id-9
| ')' -> pos+1,id-10
| _ -> parse_allowneg pos
in
let pos,n = parse_core pos in
assert(n >= 0 && (n < id || id < 0));
pos,n
let parse_name line pos =
String.sub line pos (String.length line - pos)
let parse_pairs line pos id xfrm =
let rec work result pos =
if pos = String.length(line) then result else
let pos,idfrom = parse_subid line pos id in
let pos,idto = parse_subid line pos id in
work ((xfrm idto,xfrm idfrom)::result) pos
in
rev(work [] pos)
let parse_many_subid_reverse many line pos id =
let rec work result many pos =
if many <= 0 then pos,result else
let pos,n = parse_subid line pos id in
work (n::result) (many-1) pos
in work [] many pos
(* ===== types *)
let type_id = ref 0
let some_type = bool_ty
let holtrace_types = Dynarray.make 0 some_type
let holtrace_types_add x =
assert(Dynarray.length holtrace_types = !type_id);
Dynarray.add_last holtrace_types x;
type_id := 1 + !type_id
let holtrace_vartype line =
holtrace_types_add (mk_vartype(parse_name line 1))
let holtrace_type line =
let pos,n = parse_subid line 1 (-1) in
let pos,revargs = parse_many_subid_reverse n line pos !type_id in
let args = rev revargs in
let prevtypes = map (Dynarray.get holtrace_types) args in
let name = parse_name line pos in
holtrace_types_add (mk_type(name,prevtypes))
(* ===== terms *)
let term_id = ref 0
let some_term = mk_const("=",[])
let holtrace_terms = Dynarray.make 0 some_term
let holtrace_terms_add x =
assert(Dynarray.length holtrace_terms = !term_id);
Dynarray.add_last holtrace_terms x;
term_id := 1 + !term_id
let holtrace_const line =
let pos,tyid = parse_subid line 1 !type_id in
let ty = Dynarray.get holtrace_types tyid in
let name = parse_name line pos in
holtrace_terms_add (mk_mconst(name,ty))
let holtrace_var line =
let pos,tyid = parse_subid line 1 !type_id in
let ty = Dynarray.get holtrace_types tyid in
let name = parse_name line pos in
holtrace_terms_add (mk_var(name,ty))
let holtrace_comb line =
let pos,tyid = parse_subid line 1 !type_id in
let pos,id0 = parse_subid line pos !term_id in
let pos,id1 = parse_subid line pos !term_id in
assert(pos = String.length line);
let t0 = Dynarray.get holtrace_terms id0 in
let t1 = Dynarray.get holtrace_terms id1 in
holtrace_terms_add (mk_comb(t0,t1))
let holtrace_abs line =
let pos,tyid = parse_subid line 1 !type_id in
let pos,id0 = parse_subid line pos !term_id in
let pos,id1 = parse_subid line pos !term_id in
assert(pos = String.length line);
let t0 = Dynarray.get holtrace_terms id0 in
let t1 = Dynarray.get holtrace_terms id1 in
holtrace_terms_add (mk_abs(t0,t1))
(* ===== announcements *)
let announce label th =
print_types_of_subterms := 2;
Printf.printf "%s including types:\n%s\n" label (string_of_thm th);
print_types_of_subterms := 1;
Printf.printf "%s concise:\n%s\n%!" label (string_of_thm th)
(* ===== axioms *)
let stats_axiom_matching = ref 0
let stats_axiom_creating = ref 0
let holtrace_link_hook = ref (fun (_:term) -> (None : thm option))
let holtrace_axiom allownewaxioms term =
let target = [],term in
let istarget f = (dest_thm f = target) in
try
let th = List.find istarget (axioms()) in
Printf.printf "matching axiom\n%!";
stats_axiom_matching := 1 + !stats_axiom_matching;
announce "axiom" th;
th
with Not_found ->
match !holtrace_link_hook term with
| Some th when dest_thm th = target -> begin
announce "axiom link" th;
th
end
| Some _ -> failwith "holtrace_link_hook: theorem does not match the axiom line"
| None ->
if allownewaxioms then begin
Printf.printf "creating axiom\n%!";
stats_axiom_creating := 1 + !stats_axiom_creating;
let th = new_axiom term in
announce "axiom" th;
th
end else
failwith "allownewaxioms not set, refusing new axiom"
(* ===== theorems indexed by inf_id *)
let thm_id = ref 0
let inf_id = ref 0
let typedef_id = ref 0
let some_thm = REFL some_term
let holtrace_thms = Dynarray.make 0 some_thm
let infs = Dynarray.make 0 some_thm
let typedefs = Dynarray.make 0 some_thm
let name2thm = Hashtbl.create 100
let infs_add x =
assert(Dynarray.length infs = !inf_id);
Dynarray.add_last infs x;
inf_id := 1 + !inf_id
let typedefs_add x =
assert(Dynarray.length typedefs = !typedef_id);
Dynarray.add_last typedefs x;
typedef_id := 1 + !typedef_id
let inf_term line inf =
let pos,id0 = parse_subid line 1 !term_id in
assert(pos = String.length line);
let t0 = Dynarray.get holtrace_terms id0 in
infs_add (inf t0)
let inf_thTH line inf =
let pos,id0 = parse_subid line 1 !thm_id in
let pos,id1 = parse_subid line pos !thm_id in
assert(pos = String.length line);
let t0 = Dynarray.get holtrace_thms id0 in
let t1 = Dynarray.get holtrace_thms id1 in
infs_add (inf t0 t1)
let inf_termth line inf =
let pos,id0 = parse_subid line 1 !term_id in
let pos,id1 = parse_subid line pos !thm_id in
assert(pos = String.length line);
let t0 = Dynarray.get holtrace_terms id0 in
let t1 = Dynarray.get holtrace_thms id1 in
infs_add (inf t0 t1)
let constname2thm = Hashtbl.create 1009
let constname2thm_init() =
Hashtbl.clear constname2thm;
let doit th =
let c,_ = dest_eq(concl th) in
let cname,_ = dest_const c in
Hashtbl.add constname2thm cname th
in
List.iter doit (definitions())
let stats_def_matching = ref 0
let stats_def_creating = ref 0
let inf_def allownewdefs line =
let defC term name =
let v,t = dest_eq term in
let vname,vtype = dest_var v in
if vname <> name then
failwith "name mismatch"
else
if Hashtbl.mem constname2thm vname then begin
Printf.printf "matching constant %s\n%!" vname;
stats_def_matching := 1 + !stats_def_matching;
let oldth = Hashtbl.find constname2thm vname in
let c = mk_mconst(vname,vtype) in
let newconcl = mk_eq(c,t) in
let newconclrefl = REFL newconcl in
let result = EQ_MP newconclrefl oldth in
announce ("constant "^vname) result;
result
end else begin
if allownewdefs then begin
Printf.printf "creating constant %s\n%!" vname;
stats_def_creating := 1 + !stats_def_creating;
let result = new_basic_definition term in
announce ("constant "^vname) result;
result
end else failwith("allownewdefs not set (maybe try holtrace_restore_allownew?), refusing new definition of "^vname)
end
in
let pos,id0 = parse_subid line 1 !term_id in
let name = parse_name line pos in
let t0 = Dynarray.get holtrace_terms id0 in
infs_add (defC t0 name)
let is_known_type_theorem concl =
let ok f = (dest_thm f = ([],concl)) in
exists ok (holtrace_typedef_thms())
let find_type_theorem concl =
let ok f = (dest_thm f = ([],concl)) in
find ok (holtrace_typedef_thms())
let inf_typedef_retrieve reptype repprop abstypename absname repname tyvarnames =
let tyvars = map mk_vartype tyvarnames in
let abstype = mk_type(abstypename,tyvars) in
let abs = mk_mconst(absname,mk_fun_ty reptype abstype) in
let rep = mk_mconst(repname,mk_fun_ty abstype reptype) in
let absvar = mk_var("a",abstype) in
let repvar = mk_var("r",reptype) in
let atarget = mk_eq(mk_comb(abs,mk_comb(rep,absvar)),absvar) in
let rtarget = mk_eq(mk_comb(repprop,repvar),mk_eq(mk_comb(rep,mk_comb(abs,repvar)),repvar)) in
let athm = find_type_theorem atarget in
let rthm = find_type_theorem rtarget in
athm,rthm
let can_inf_typedef_retrieve reptype repprop abstypename absname repname tyvarnames =
try
let _,_ = inf_typedef_retrieve reptype repprop abstypename absname repname tyvarnames in
true
with Failure _ -> false
let stats_typedef_matching = ref 0
let stats_typedef_creating = ref 0
let inf_typedef allownewdefs line =
let pos,reptype = parse_subid line 1 !type_id in
let pos,repprop = parse_subid line pos !term_id in
let pos,repth = parse_subid line pos !thm_id in
let word = parse_name line pos in
let reptype = Dynarray.get holtrace_types reptype in
let repprop = Dynarray.get holtrace_terms repprop in
let repth = Dynarray.get holtrace_thms repth in
let thconcl =
match dest_thm repth with
| [],concl -> concl
| _ -> failwith line
in
let thprop,_ =
try dest_comb thconcl
with Failure _ -> failwith line
in
assert(repprop = thprop);
assert(word.[0] = ']');
let args = String.split_on_char ']' word in
assert(hd(rev args) = "");
let args = rev(tl(rev args)) in
match args with
| ""::abstypename::absname::repname::tyvarnames ->
if can_inf_typedef_retrieve reptype repprop abstypename absname repname tyvarnames
then begin
Printf.printf "matching typedef %s\n%!" abstypename;
stats_typedef_matching := 1 + !stats_typedef_matching;
let athm,rthm = inf_typedef_retrieve reptype repprop abstypename absname repname tyvarnames in
announce ("typedef rthm "^abstypename) rthm;
announce ("typedef athm "^abstypename) athm;
typedefs_add rthm;
infs_add athm
end else begin
if allownewdefs then begin
Printf.printf "creating typedef %s\n%!" abstypename;
stats_typedef_creating := 1 + !stats_typedef_creating;
let athm,rthm = new_basic_type_definition abstypename (absname,repname) repth in
announce ("typedef rthm "^abstypename) rthm;
announce ("typedef athm "^abstypename) athm;
typedefs_add rthm;
infs_add athm
end else failwith("allownewdefs not set (maybe try holtrace_restore_allownew?), refusing new definition of "^abstypename)
end
| _ -> failwith line
let inf_typedef_followup line =
let pos,typedef = parse_subid line 1 !typedef_id in
let rthm = Dynarray.get typedefs typedef in
infs_add rthm
let inf_inst line =
let pos,id0 = parse_subid line 1 !thm_id in
let xfrm = Dynarray.get holtrace_terms in
let arg = parse_pairs line pos !term_id xfrm in
let t0 = Dynarray.get holtrace_thms id0 in
let thm = INST arg t0 in
infs_add thm
let inf_inst_type line =
let pos,id0 = parse_subid line 1 !thm_id in
let argmap = Dynarray.get holtrace_types in
let arg = parse_pairs line pos !type_id argmap in
let t0 = Dynarray.get holtrace_thms id0 in
let thm = INST_TYPE arg t0 in
infs_add thm
let holtrace_thms_add x =
while Dynarray.length holtrace_thms < !thm_id do
Dynarray.add_last holtrace_thms some_thm
done;
assert(Dynarray.length holtrace_thms = !thm_id);
Dynarray.add_last holtrace_thms x;
thm_id := 1 + !thm_id
let holtrace_thm checkthmstatements line =
let linelen = String.length(line) in
let rec all_subid result pos =
if pos >= linelen then result else
let pos,n = parse_subid line pos !term_id in
all_subid (n::result) pos
in
let chhhid = all_subid [] 1 in
let chhh = map (Dynarray.get holtrace_terms) chhhid in
let c = hd chhh in
let hhh = rev(tl chhh) in
let thm = Dynarray.get infs (!inf_id-1) in
if checkthmstatements then assert((hhh,c) = dest_thm(thm));
holtrace_thms_add thm
let thmname_acceptable_firstchar c =
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_')
let thmname_acceptable_char c =
thmname_acceptable_firstchar c || (c == '\'') || (c == '.')
let thmname_acceptable s =
String.length s > 0
&& thmname_acceptable_firstchar s.[0]
&& String.for_all thmname_acceptable_char s
let stats_thm_matching = ref 0
let stats_thm_creating = ref 0
let holtrace_thmname previousthms line =
let pos,id0 = parse_subid line 1 !thm_id in
let name = parse_name line pos in
if not(thmname_acceptable name) then
failwith ("refusing unusual characters in theorem name "^name)
else if Hashtbl.mem name2thm name then
failwith "theorem name already used"
else begin
let th = Dynarray.get holtrace_thms id0 in
Hashtbl.add name2thm name th;
if exists (fun (n,t) -> n = name) previousthms then begin
Printf.printf "matching thm %s\n%!" name;
stats_thm_matching := 1 + !stats_thm_matching;
let known = assoc name previousthms in
assert(dest_thm known = dest_thm th)
end else begin
Printf.printf "creating thm %s\n%!" name;
stats_thm_creating := 1 + !stats_thm_creating;
end;
announce ("thm "^name) th
end
(* ===== main *)
let holtrace_restore_worker bench allownewaxioms allownewdefs checkthmstatements previousthms fn =
let total_time = ref 0.0 in
total_time := !total_time -. bench();
stats_axiom_matching := 0;
stats_axiom_creating := 0;
stats_def_matching := 0;
stats_def_creating := 0;
stats_typedef_matching := 0;
stats_typedef_creating := 0;
stats_thm_matching := 0;
stats_thm_creating := 0;
type_id := 0;
term_id := 0;
thm_id := 0;
inf_id := 0;
typedef_id := 0;
Dynarray.clear holtrace_types;
Dynarray.clear holtrace_terms;
Dynarray.clear holtrace_thms;
Dynarray.clear infs;
Dynarray.clear typedefs;
Hashtbl.clear name2thm;
constname2thm_init();
let tag_occurrences = Array.make 256 0 in
let tag_time = Array.make 256 0.0 in
let linecount = ref 0 in
let fd = open_in_bin fn in
try
while true do
let line = input_line fd in
linecount := !linecount + 1;
if !linecount = 1 then assert(line = "HOLTrace 1")
else
let tag = line.[0] in
let tagcode = Char.code(tag) in
tag_occurrences.(tagcode) <- tag_occurrences.(tagcode) + 1;
tag_time.(tagcode) <- tag_time.(tagcode) -. bench();
( match tag with
| 'a' -> holtrace_vartype line
| 'b' -> holtrace_type line
| 'c' -> holtrace_const line
| 'd' -> holtrace_var line
| 'e' -> holtrace_comb line
| 'f' -> holtrace_abs line
| 't' -> holtrace_thm checkthmstatements line
| '/' -> holtrace_thmname previousthms line
| 'X' -> inf_term line (holtrace_axiom allownewaxioms)
| 'B' -> inf_term line BETA
| 'R' -> inf_term line REFL
| 'U' -> inf_term line ASSUME
| 'F' -> inf_termth line ABS
| 'P' -> inf_thTH line EQ_MP
| 'V' -> inf_thTH line TRANS
| 'Z' -> inf_thTH line DEDUCT_ANTISYM_RULE
| 'E' -> inf_thTH line (fun t0 t1 -> MK_COMB(t0,t1))
| 'S' -> inf_inst line
| 'T' -> inf_inst_type line
| 'C' -> inf_def allownewdefs line
| 'A' -> inf_typedef allownewdefs line
| 'D' -> inf_typedef_followup line
| ';' -> ()
| ',' -> ()
| _ -> failwith line
);
tag_time.(tagcode) <- tag_time.(tagcode) +. bench();
done
with End_of_file -> close_in fd;
assert(!linecount >= 1);
total_time := !total_time +. bench();
Printf.printf "types: %d\n" !type_id;
Printf.printf "terms: %d\n" !term_id;
Printf.printf "theorems: %d\n" !thm_id;
Printf.printf "inferences: %d\n" !inf_id;
Printf.printf "axioms: %d = matching %d + creating %d\n" (!stats_axiom_matching + !stats_axiom_creating) !stats_axiom_matching !stats_axiom_creating;
Printf.printf "typedefs: %d = matching %d + creating %d\n" !typedef_id !stats_typedef_matching !stats_typedef_creating;
Printf.printf "constants: %d = matching %d + creating %d\n" (!stats_def_matching + !stats_def_creating) !stats_def_matching !stats_def_creating;
Printf.printf "named thms: %d = matching %d + creating %d\n" (!stats_thm_matching + !stats_thm_creating) !stats_thm_matching !stats_thm_creating;
for c = 33 to 126 do
if tag_occurrences.(c) > 0 then
Printf.printf "tag %c %12f seconds %12d occurrences\n" (Char.chr(c)) tag_time.(c) tag_occurrences.(c)
done;
Printf.printf "total %12f seconds\n%!" !total_time
let holtrace_restore = holtrace_restore_worker (fun x -> 0.0) false false true
let holtrace_restore_time = holtrace_restore_worker Sys.time false false true
let holtrace_restore_allownew = holtrace_restore_worker (fun x -> 0.0) true true true
let holtrace_restore_allownew_time = holtrace_restore_worker Sys.time true true true
let holtrace_restore_skipthmchecks = holtrace_restore_worker (fun x -> 0.0) false false false
let holtrace_restore_skipthmchecks_time = holtrace_restore_worker Sys.time false false false
let holtrace_restore_skipthmchecks_allownew = holtrace_restore_worker (fun x -> 0.0) true true false
let holtrace_restore_skipthmchecks_allownew_time = holtrace_restore_worker Sys.time true true false
let holtrace_restore_id2type = Dynarray.get holtrace_types
let holtrace_restore_id2term = Dynarray.get holtrace_terms
let holtrace_restore_id2thm = Dynarray.get holtrace_thms
let holtrace_restore_name2thm = Hashtbl.find name2thm
end
include Holtrace_restore
Printf.printf("===== finished loading holtrace_restore\n");;