-rw-r--r-- 15012 holtrace-20261122/holtrace_save.ml raw
Printf.printf("===== loading holtrace_save\n");;
(*
XXX: could do more extensive dedup here to save memory:
rebuild types, rebuild terms, re-prove theorems
XXX: but really want external hashcons to speed up hashing
or at least caching hashes on object creation, even without dedup
XXX: maybe should randomize hashing
XXX: might encounter integer overflows on 32-bit machines
*)
module Holtrace_save: sig
val REFL: term -> thm
val TRANS: thm -> thm -> thm
val MK_COMB: thm * thm -> thm
val ABS: term -> thm -> thm
val BETA: term -> thm
val ASSUME: term -> thm
val EQ_MP: thm -> thm -> thm
val DEDUCT_ANTISYM_RULE: thm -> thm -> thm
val INST_TYPE: (hol_type * hol_type) list -> thm -> thm
val INST: (term * term) list -> thm -> thm
val new_axiom: term -> thm
val new_basic_definition: term -> thm
val new_basic_type_definition: string -> string * string -> thm -> thm * thm
val holtrace_nameonethm: string * thm -> unit
val holtrace_namethm: (string * thm) list -> unit
val holtrace_stop: unit -> unit
val holtrace_typedef_thms: unit -> thm list
end = struct
let orig_REFL = REFL
let orig_TRANS = TRANS
let orig_MK_COMB = MK_COMB
let orig_ABS = ABS
let orig_BETA = BETA
let orig_ASSUME = ASSUME
let orig_EQ_MP = EQ_MP
let orig_DEDUCT_ANTISYM_RULE = DEDUCT_ANTISYM_RULE
let orig_INST_TYPE = INST_TYPE
let orig_INST = INST
let orig_new_axiom = new_axiom
let orig_new_basic_definition = new_basic_definition
let orig_new_basic_type_definition = new_basic_type_definition
let envvar = Sys.getenv_opt("HOLTRACE_SAVE")
let enabled = Option.is_some envvar
let fd =
if enabled then
open_out_bin (Option.get envvar)
else
(* presumably there is an easier way to do this *)
let fd = open_out_bin "/dev/null" in
Out_channel.close fd;
fd
let tracing = ref enabled
let trace_flush() = if !tracing then Out_channel.flush fd
let trace_char c = if !tracing then output_char fd c
let trace_string s = if !tracing then output_string fd s
let trace_stop() = if !tracing then begin Out_channel.close fd; tracing := false end
(* ===== holtrace encoding *)
let rec base32 n =
if n < 0 then "-" ^ (base32(-n))
else if n == 0 then ""
else
let d = n mod 32 in
let c = Char.chr(d + (if d < 10 then 48 else 55)) in
base32(n/32) ^ String.make 1 c
let is_base32char c =
(c >= '0' && c <= '9') || (c >= 'A' && c <= 'V')
let simpleencode n =
match n with
| -1 -> "!"
| -2 -> "@"
| -3 -> "#"
| -4 -> "$"
| -5 -> "%"
| -6 -> "^"
| -7 -> "&"
| -8 -> "*"
| -9 -> "("
| -10 -> ")"
| 0 -> "0"
| _ -> base32 n
let rec base32_len n =
if n < 0 then 1 + base32_len(-n)
else if n == 0 then 0
else 1 + base32_len(n/32)
let simpleencode_len n =
if n >= -10 && n <= 9 then 1
else base32_len n
let is_printablenospaces s =
String.for_all (fun c -> c > ' ' && c <= '~') s
let need_version = ref true
let trace tag idlist s =
if not(is_printablenospaces s) then
failwith "bad name"
else
if !need_version then begin
trace_string "HOLTrace 1\n";
need_version := false
end;
trace_char tag;
let idlist = map simpleencode idlist in
let args =
if String.length s = 0 then idlist
else idlist@[s]
in
let lastbyte = ref ' ' in
let doit s = (
(* s is nonempty now *)
let firstbyte = s.[0] in
if is_base32char !lastbyte && (is_base32char firstbyte || (firstbyte == ' '))
then trace_char ' ';
trace_string s;
lastbyte := s.[String.length s - 1]
) in
List.iter doit args;
trace_char '\n';
trace_flush()
let compress reference id =
if id < 0 then failwith "internal bug, negative integer" else
let y = id-reference in
if y >= 0 then id else
if y >= -10 then y else
if -y >= id then id else
if -32*y <= id then y else
if simpleencode_len y <= simpleencode_len id then y else id
(* ===== types *)
let some_type = bool_ty
let id2type = Dynarray.make 0 some_type
let type2id = Hashtbl.create 16384
let typeid2compress id =
compress (Dynarray.length id2type) id
let type2compress ty =
typeid2compress (Hashtbl.find type2id ty)
let rec dedup_trace_type_core ty =
try
let id = Hashtbl.find type2id ty in
Dynarray.get id2type id,id
with Not_found -> begin
begin
match ty with
| Tyvar name ->
assert(String.length name > 0);
trace 'a' [] name
| Tyapp(name,args) ->
let args = map (snd o dedup_trace_type_core) args in
assert(String.length name > 0);
trace 'b' ((length args)::(map typeid2compress args)) name
end;
let id = Dynarray.length id2type in
Dynarray.add_last id2type ty;
Hashtbl.add type2id ty id;
ty,id
end
let dedup_trace_type_calls = ref 0
let dedup_trace_type_time = ref 0.0
let dedup_trace_type tm =
dedup_trace_type_time := !dedup_trace_type_time -. Sys.time();
let result = dedup_trace_type_core tm in
dedup_trace_type_time := !dedup_trace_type_time +. Sys.time();
dedup_trace_type_calls := !dedup_trace_type_calls + 1;
result
(* ===== terms *)
let some_constant = mk_const("=",[])
let id2term = Dynarray.make 0 some_constant
let term2id = Hashtbl.create 16384
let termid2compress id =
compress (Dynarray.length id2term) id
let term2compress tm =
termid2compress (Hashtbl.find term2id tm)
let rec dedup_trace_term_core tm =
try
let id = Hashtbl.find term2id tm in
Dynarray.get id2term id,id
with Not_found -> begin
begin
match tm with
| Const(s,ty) ->
let _,tyid = dedup_trace_type ty in
assert(String.length s > 0);
trace 'c' [typeid2compress tyid] s
| Var(s,ty) ->
let _,tyid = dedup_trace_type ty in
assert(String.length s > 0);
trace 'd' [typeid2compress tyid] s
| Comb(f,x) ->
let _,fid = dedup_trace_term_core f in
let _,xid = dedup_trace_term_core x in
let _,tyid = dedup_trace_type(type_of tm) in
trace 'e' [typeid2compress tyid;termid2compress fid;termid2compress xid] ""
| Abs(v,b) ->
let _,vid = dedup_trace_term_core v in
let _,bid = dedup_trace_term_core b in
let _,tyid = dedup_trace_type(type_of tm) in
trace 'f' [typeid2compress tyid;termid2compress vid;termid2compress bid] ""
end;
let id = Dynarray.length id2term in
Dynarray.add_last id2term tm;
Hashtbl.add term2id tm id;
tm,id
end
let dedup_trace_term_calls = ref 0
let dedup_trace_term_time = ref 0.0
let dedup_trace_term tm =
dedup_trace_term_time := !dedup_trace_term_time -. Sys.time();
let result = dedup_trace_term_core tm in
dedup_trace_term_time := !dedup_trace_term_time +. Sys.time();
dedup_trace_term_calls := !dedup_trace_term_calls + 1;
result
(* ===== theorems *)
let some_thm = orig_REFL some_constant
let id2thm = Dynarray.make 0 some_thm
let thm2id = Hashtbl.create 16384
let knownthm th = Hashtbl.mem thm2id th
let thmid2compress id =
compress (Dynarray.length id2thm) id
let thm2compress th =
thmid2compress (Hashtbl.find thm2id th)
let dedup_trace_thm_calls = ref 0
let dedup_trace_thm_time = ref 0.0
let dedup_trace_thm_core th =
try
let id = Hashtbl.find thm2id th in
Dynarray.get id2thm id,id
with Not_found -> begin
let h,c = dest_thm th in
let hc = h@[c] in
dedup_trace_thm_time := !dedup_trace_thm_time +. Sys.time();
let hc = map (snd o dedup_trace_term) hc in
dedup_trace_thm_time := !dedup_trace_thm_time -. Sys.time();
trace 't' (map termid2compress hc) "";
let id = Dynarray.length id2thm in
Dynarray.add_last id2thm th;
Hashtbl.add thm2id th id;
th,id
end
let dedup_trace_thm th =
dedup_trace_thm_time := !dedup_trace_thm_time -. Sys.time();
let result = dedup_trace_thm_core th in
dedup_trace_thm_time := !dedup_trace_thm_time +. Sys.time();
dedup_trace_thm_calls := !dedup_trace_thm_calls + 1;
if !dedup_trace_thm_calls mod 100000 = 0 then begin
Printf.printf "Holtrace_save so far: %d -> %d thms %.2f s, %d -> %d terms %.2f s, %d -> %d types %.2f s\n%!"
!dedup_trace_thm_calls (Dynarray.length id2thm) !dedup_trace_thm_time
!dedup_trace_term_calls (Dynarray.length id2term) !dedup_trace_term_time
!dedup_trace_type_calls (Dynarray.length id2type) !dedup_trace_type_time
end;
result
let dedup_trace_thm_noid =
fst o dedup_trace_thm
(* ===== inferences *)
let oneterm tag inf tm =
if !tracing then
let tm,tmid = dedup_trace_term tm in
let result = inf tm in
if knownthm result then result else begin
trace tag [termid2compress tmid] "";
dedup_trace_thm_noid result
end
else inf tm
let trace_BETA = oneterm 'B' orig_BETA
let trace_REFL = oneterm 'R' orig_REFL
let trace_ASSUME = oneterm 'U' orig_ASSUME
let trace_new_axiom = oneterm 'X' orig_new_axiom
let twothms tag inf th TH =
if !tracing then
let th,thid = dedup_trace_thm th in
let TH,THid = dedup_trace_thm TH in
let result = inf th TH in
if knownthm result then result else begin
trace tag (map thmid2compress [thid;THid]) "";
dedup_trace_thm_noid result
end
else inf th TH
let trace_MK_COMB(th,TH) = twothms 'E' (fun th TH -> orig_MK_COMB(th,TH)) th TH
let trace_EQ_MP = twothms 'P' orig_EQ_MP
let trace_TRANS = twothms 'V' orig_TRANS
let trace_DEDUCT_ANTISYM_RULE = twothms 'Z' orig_DEDUCT_ANTISYM_RULE
let trace_ABS tm th =
if !tracing then
let tm,tmid = dedup_trace_term tm in
let th,thid = dedup_trace_thm th in
let result = orig_ABS tm th in
if knownthm result then result else begin
trace 'F' [termid2compress tmid;thmid2compress thid] "";
dedup_trace_thm_noid result
end
else orig_ABS tm th
let trace_INST tmlist th =
if !tracing then
let th,thid = dedup_trace_thm th in
let dedup_trace_termpair(w,v) = fst(dedup_trace_term w),fst(dedup_trace_term v) in
let tmlist = map dedup_trace_termpair tmlist in
let result = orig_INST tmlist th in
if knownthm result then result else begin
let listlist = map (fun(w,v) -> [v;w]) tmlist in
let flat = List.flatten listlist in
trace 'S' ((thmid2compress thid)::(map term2compress flat)) "";
dedup_trace_thm_noid result
end
else orig_INST tmlist th
let trace_INST_TYPE tylist th =
if !tracing then
let th,thid = dedup_trace_thm th in
let dedup_trace_typepair(z,y) = fst(dedup_trace_type z),fst(dedup_trace_type y) in
let tylist = map dedup_trace_typepair tylist in
let result = orig_INST_TYPE tylist th in
if knownthm result then result else begin
let listlist = map (fun(z,y) -> [y;z]) tylist in
let flat = List.flatten listlist in
trace 'T' ((thmid2compress thid)::(map type2compress flat)) "";
dedup_trace_thm_noid result
end
else orig_INST_TYPE tylist th
let trace_new_basic_definition tm =
if !tracing then
let tm,tmid = dedup_trace_term tm in
let result = orig_new_basic_definition tm in
if knownthm result then result else begin
let c,cdef = dest_eq tm in
let cname,_ = dest_var c in
assert(String.length cname > 0);
trace 'C' [termid2compress tmid] cname;
dedup_trace_thm_noid result
end
else orig_new_basic_definition tm
let trace_new_basic_type_definition tyname (absname,repname) th =
if !tracing then
let th,thid = dedup_trace_thm th in
let ath,rth = orig_new_basic_type_definition tyname (absname,repname) th in
if knownthm ath && knownthm rth then ath,rth else begin
let repprop,witness = dest_comb(concl th) in
let reptype = type_of witness in
let reptype,reptypeid = dedup_trace_type reptype in
let repprop,reppropid = dedup_trace_term repprop in
let tyvars = sort (<=) (type_vars_in_term repprop) in
let tyvarnames = map dest_vartype tyvars in
let words = tyname::absname::repname::tyvarnames in
let text = "]" ^ (String.concat "]" words) ^ "]" in
let compressed = [typeid2compress reptypeid;termid2compress reppropid;thmid2compress thid] in
trace 'A' compressed text;
let ath = dedup_trace_thm_noid ath in
trace 'D' [-1] "";
let rth = dedup_trace_thm_noid rth in
ath,rth
end
else orig_new_basic_type_definition tyname (absname,repname) th
(* ===== theorem names *)
let names_saved_already = Hashtbl.create 100
let trace_nameonethm(name,th) =
if Hashtbl.mem names_saved_already name then
() (* just ignore (whether old th or not) *)
else if knownthm th then begin
assert(String.length(name) > 0);
trace '/' [thm2compress th] name;
Hashtbl.add names_saved_already name true
end else
failwith "unknown theorem; was HOLTrace running?"
let trace_namethm l = List.iter trace_nameonethm (List.rev l)
(* ===== switch *)
let REFL = if enabled then trace_REFL else orig_REFL
let TRANS = if enabled then trace_TRANS else orig_TRANS
let MK_COMB = if enabled then trace_MK_COMB else orig_MK_COMB
let ABS = if enabled then trace_ABS else orig_ABS
let BETA = if enabled then trace_BETA else orig_BETA
let ASSUME = if enabled then trace_ASSUME else orig_ASSUME
let EQ_MP = if enabled then trace_EQ_MP else orig_EQ_MP
let DEDUCT_ANTISYM_RULE = if enabled then trace_DEDUCT_ANTISYM_RULE else orig_DEDUCT_ANTISYM_RULE
let INST_TYPE = if enabled then trace_INST_TYPE else orig_INST_TYPE
let INST = if enabled then trace_INST else orig_INST
let new_axiom = if enabled then trace_new_axiom else orig_new_axiom
let new_basic_definition = if enabled then trace_new_basic_definition else orig_new_basic_definition
let almost_new_basic_type_definition = if enabled then trace_new_basic_type_definition else orig_new_basic_type_definition
let holtrace_nameonethm = if enabled then trace_nameonethm else ignore
let holtrace_namethm = if enabled then trace_namethm else ignore
let holtrace_stop = if enabled then trace_stop else ignore
(* ===== even if tracing is disabled, save typedef theorems *)
let typedef_thms = ref ([]:thm list)
let new_basic_type_definition tyname (absname,repname) th =
let ath,rth = almost_new_basic_type_definition tyname (absname,repname) th in
typedef_thms := rth::ath::!typedef_thms;
ath,rth
let holtrace_typedef_thms() = !typedef_thms
end
include Holtrace_save
Printf.printf("===== finished loading holtrace_save\n");;