Skip to content

Commit d8b4732

Browse files
authored
Generate the proofs of all the theorems of an HOL-Light file in a single Lambdapi file (#183)
other changes: - Xlib: concat and remove takes a list of filenames in argument
1 parent 5255980 commit d8b4732

9 files changed

Lines changed: 173 additions & 67 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
99

1010
- command nbp to print the number of (useful) proofs
1111
- command files to print theorem statements following the file structuration in HOL-Light
12+
- command thms to print theorems (named or not) proved in a file
13+
- command merge to generate a single lp file for the proofs of all the theorems (named or not) proved in a file
1214
- renamings to handle the Multivariate library
1315
- test/Sig_mappings_N.v and test/Sig_With_N.v: axiomatizations of mappings_N.v and With_N.v respectively
1416

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ b.sig: signature (types, constants, axioms, definitions)
309309

310310
b.thm: map from theorem indexes having a name to their name
311311

312-
b.thp: map every useful theorem index to its name and position (similar to f.thm but with position)
312+
b.thp: map every useful theorem index to its position in b.prf and the lp filename where its proof is translated
313313

314314
f.nbp: number of proof steps
315315

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
TODO
22
----
33

4+
- xnames.ml: define string_of_file as an iterator
5+
46
- replace variables like _1718 by better names
57

68
- always share closed subterms

main.ml

Lines changed: 129 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ hol2dk $base.(dk|lp)
7878
hol2dk $base.(dk|lp) $thm_id
7979
generate $base.(dk|lp) but with theorem index $thm_id only (for debug)
8080
81-
Multi-threaded lp file generation by having a file for each named theorem
82-
-------------------------------------------------------------------------
81+
Multi-threaded lp file generation with a file for each named theorem
82+
--------------------------------------------------------------------
8383
8484
hol2dk config hollight_file.ml root_path [coq_module_or_file ...] [mappings.lp] [mappings.mk]
8585
create files and links to the files generated by hol2dk dump
@@ -91,6 +91,10 @@ hol2dk split $base
9191
generate $base.thp and the files $thm.sti, $thm.pos and $thm.use
9292
for each theorem $thm
9393
94+
hol2dk merge $base [$path/]$file.(dk|lp)
95+
generate a single lp file for the proofs of all the theorems (named or not)
96+
proved in [$path/]$file.(dk|lp)
97+
9498
hol2dk theorem $base $thm.lp
9599
generate the lp proof of the theorem $thm
96100
@@ -148,7 +152,10 @@ hol2dk print use $base $x
148152
print the contents of $base.use for theorem index $x
149153
150154
hol2dk print $base.thm
151-
print the contents of $base.thm
155+
print the contents of $base.thm (named theorems with their indexes)
156+
157+
hol2dk thms $base [$path/]$file.(ml|hl)
158+
print the list of theorems (named or not) proved in [$path/]$file.(ml|hl)
152159
153160
hol2dk stat $base [$thm]
154161
print statistics on proof steps
@@ -308,6 +315,24 @@ let print_env_var n =
308315

309316
let wrong_nb_args() = err "wrong number of arguments\n"; 1;;
310317

318+
(* compute the minimum and maximum theorem indexes in f *)
319+
let thid_range_of_file b f =
320+
let min_id = ref max_int and max_id = ref min_int in
321+
let map_thid_name = read_val (b^".thm") in
322+
let map_name_thid = (* inverse of map_thid_name *)
323+
MapInt.fold (fun k n map -> MapStr.add n k map) map_thid_name MapStr.empty
324+
in
325+
List.iter
326+
(fun n ->
327+
try
328+
let k = MapStr.find n map_name_thid in
329+
min_id := min !min_id k;
330+
max_id := max !max_id k
331+
with Not_found -> ())
332+
(thms_of_file f);
333+
!min_id, !max_id, map_thid_name
334+
;;
335+
311336
let rec log_command l =
312337
print_string "\nhol2dk";
313338
List.iter (fun s -> print_char ' '; print_string s) l;
@@ -337,12 +362,16 @@ and command = function
337362
| "--max-abbrev-size"::k::args ->
338363
Xlp.max_abbrev_part_size := integer k; command args
339364

365+
| ["--max-abbrev-size"] -> wrong_nb_args()
366+
340367
| "--max-proof-size"::k::args ->
341368
Xlp.max_proof_part_size := integer k; command args
342369

370+
| ["--max-proof-size"] -> wrong_nb_args()
371+
343372
| "--root-path"::arg::args -> Xlp.root_path := arg; command args
344373

345-
| ["--root-path"] -> err "missing root path\n"; 1
374+
| ["--root-path"] -> wrong_nb_args()
346375

347376
| s::_ when String.starts_with ~prefix:"--" s ->
348377
err "unknown option \"%s\"\n" s; 1
@@ -887,8 +916,9 @@ and command = function
887916

888917
| "axm"::_ -> wrong_nb_args()
889918

890-
(* Called in Makefile to generate a file f.lp with, for each named
891-
theorem in f.ml, a declaration "symbol thm_name : type". *)
919+
(* Called in Makefile to generate, for each file n required by f, a
920+
file n.lp with a declaration "symbol thm_name : type" for each
921+
named theorem in n.ml. *)
892922
| ["files";f] ->
893923
let dk = is_dk f in
894924
let f = Filename.chop_extension f in
@@ -1047,6 +1077,98 @@ and command = function
10471077

10481078
| "theorem"::_ -> wrong_nb_args()
10491079

1080+
(* List theorems proved in file f. *)
1081+
| ["thms";b;f] ->
1082+
let min_id, max_id, map_thid_name = thid_range_of_file b f in
1083+
let map,_,_ = MapInt.split (max_id + 1) (read_val (b^".thp")) in
1084+
let _,_,map = MapInt.split (min_id - 1) map in
1085+
MapInt.iter
1086+
(fun k _ ->
1087+
try log "%s\n" (MapInt.find k map_thid_name)
1088+
with Not_found -> log "thm%d\n" k)
1089+
map;
1090+
0
1091+
1092+
| "thms"::_ -> wrong_nb_args()
1093+
1094+
(* Generate p.lp and its associated files, where
1095+
p=chop_extension(basename f), with all the theorems (named or
1096+
not) proved in f. *)
1097+
| ["merge";b;f] ->
1098+
let p = Filename.chop_extension (Filename.basename f) in
1099+
if Sys.file_exists (p^".lp") then
1100+
begin
1101+
err "\"%s.lp\" already exists. If you generated it using \"hol2dk merge\", you cannot regenerate it again unless you remove it and do \"hol2dk split\" again.\n" p;
1102+
exit 1
1103+
end;
1104+
(* update b.thp and set Xproof.map_thid_pos for export *)
1105+
let min_id, max_id, _ = thid_range_of_file b f
1106+
and thm_names = ref SetStr.empty
1107+
and map_thid_name = ref MapInt.empty in
1108+
Xproof.map_thid_pos :=
1109+
MapInt.mapi (fun k ((name,pos) as v) ->
1110+
if min_id <= k && k <= max_id && name <> p then
1111+
begin
1112+
thm_names := SetStr.add name !thm_names;
1113+
map_thid_name := MapInt.add k name !map_thid_name;
1114+
(p,pos)
1115+
end
1116+
else v)
1117+
(read_val (b^".thp"));
1118+
write_val (b^".thp") !Xproof.map_thid_pos;
1119+
(* generate proof steps *)
1120+
read_sig b;
1121+
init_proof_reading b;
1122+
let deps = ref SetStr.empty in
1123+
let gen oc_spec n =
1124+
read_pos n;
1125+
read_use n;
1126+
the_start_idx := read_val (n^".sti");
1127+
Xlib.remove [n^".pos";n^".use";n^".sti";n^".nbp"];
1128+
(* part of Xlp.export_theorem_proof b n; *)
1129+
let thid = !the_start_idx + Array.length !prf_pos - 1 in
1130+
Xlp.export_proofs_in_interval n !the_start_idx thid;
1131+
Xlib.rename (n^part !Xlp.proof_part^"_proofs.lp") (n^"_proofs.lp");
1132+
(* record external dependencies *)
1133+
for i = 1 to !Xlp.proof_part do
1134+
deps := SetStr.fold
1135+
(fun n acc ->
1136+
if SetStr.mem n !thm_names then acc else SetStr.add n acc)
1137+
(Hashtbl.find Xlp.htbl_thm_deps i)
1138+
!deps
1139+
done;
1140+
(* generate corresponding spec *)
1141+
Xlp.theorem_as_axiom false oc_spec thid (proof_at thid)
1142+
in
1143+
Xlp.export (p^"_spec") [b^"_types";b^"_terms"]
1144+
(fun oc_spec -> SetStr.iter (gen oc_spec) !thm_names);
1145+
close_in !Xproof.ic_prf;
1146+
(* merge all proof files into [n_proofs.lp]. The order of proof
1147+
files is important. *)
1148+
let proof_files =
1149+
List.map (fun (_,n) -> n^"_proofs.lp")
1150+
(MapInt.bindings !map_thid_name) in
1151+
Xlib.concat proof_files (p^"_proofs.lp");
1152+
Xlib.remove proof_files;
1153+
Xlp.export_term_abbrevs_in_one_file b p;
1154+
write_val (p^".typ") !Xlp.map_typ_abbrev;
1155+
(* export deps *)
1156+
let iter_deps f =
1157+
f (b^"_types");
1158+
f (b^"_terms");
1159+
f (b^"_axioms");
1160+
f (b^"_type_abbrevs");
1161+
if !use_sharing then f (p^"_subterm_abbrevs");
1162+
f (p^"_term_abbrevs");
1163+
SetStr.iter (Xlp.spec f) !deps
1164+
in
1165+
Xlp.create_file_with_deps (p^"_deps") p iter_deps (fun _ -> ());
1166+
Xlib.concat [p^"_deps.lp";p^"_proofs.lp"] (p^".lp");
1167+
Xlib.remove [p^"_deps.lp";p^"_proofs.lp"];
1168+
0
1169+
1170+
| "merge"::_ -> wrong_nb_args()
1171+
10501172
(* Merge all maps in typ files into a single map, give a unique
10511173
index to every entry in the obtained map, generate
10521174
b^"_type_abbrevs.lp" and a sed file for every typ file. *)
@@ -1152,9 +1274,7 @@ and command = function
11521274
List.map (fun s -> b^"_"^s^".dk")
11531275
(if r = All then deps @ ["theorems"] else deps)
11541276
in
1155-
exit
1156-
(Sys.command
1157-
("cat theory_hol.dk "^String.concat " " infiles^" > "^b^".dk"))
1277+
Xlib.concat ("theory_hol.dk"::infiles) (b^".dk")
11581278
end
11591279
else
11601280
begin

test.mk

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
.PHONY: default
44
default: test1 test2 test3 test4 test5
55

6+
test%:
7+
mkdir -p output$*
8+
$(MAKE) -C output$* -f ../test.mk do-test$*
9+
610
clean:
711
-rm -rf output1 output2 output3 output4 output5
812

@@ -12,49 +16,29 @@ config:
1216

1317
# single dk
1418

15-
.PHONY: test1
16-
test%:
17-
mkdir -p output$*
18-
$(MAKE) -C output$* -f ../test.mk do-test$*
19-
20-
.PHONY: do-test1
19+
.PHONY: test1 do-test1
2120
do-test1: config
2221
hol2dk hol_upto_arith.dk
2322
dk check hol_upto_arith.dk
2423

2524
# single lp
2625

27-
.PHONY: test2
28-
test2:
29-
mkdir -p output2
30-
$(MAKE) -C output2 -f ../test.mk do-test2
31-
32-
.PHONY: do-test2
26+
.PHONY: test2 do-test2
3327
do-test2: config
3428
hol2dk hol_upto_arith.lp
3529
lambdapi check -v0 -w -c hol_upto_arith.lp
3630

3731
# multi dk
3832

39-
.PHONY: test3
40-
test3:
41-
mkdir -p output3
42-
$(MAKE) -C output3 -f ../test.mk do-test3
43-
44-
.PHONY: do-test3
33+
.PHONY: test3 do-test3
4534
do-test3: config
4635
hol2dk mk 3 hol_upto_arith
4736
$(MAKE) -f hol_upto_arith.mk dk
4837
dk check hol_upto_arith.dk
4938

5039
# multi lp with mk
5140

52-
.PHONY: test4
53-
test4:
54-
mkdir -p output4
55-
$(MAKE) -C output1 -f ../test.mk do-test4
56-
57-
.PHONY: do-test4
41+
.PHONY: test4 do-test4
5842
do-test4: config
5943
hol2dk mk 3 hol_upto_arith
6044
$(MAKE) -f hol_upto_arith.mk lp
@@ -64,12 +48,7 @@ do-test4: config
6448

6549
# multi lp with split
6650

67-
.PHONY: test5
68-
test5:
69-
mkdir -p output5
70-
$(MAKE) -C output5 -f ../test.mk do-test5
71-
72-
.PHONY: do-test5
51+
.PHONY: test5 do-test5
7352
do-test5: config
7453
$(MAKE) split
7554
$(MAKE) lp

xlib.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ let percent k n = (100 * k) / n;;
4545
(****************************************************************************)
4646

4747
let command s =
48-
if Sys.command s <> 0 then (log "Error: \"%s\" failed.\n" s; exit 1);;
48+
match Sys.command s with
49+
| 0 -> ()
50+
| n -> log "Error: \"%s\" failed.\n" s; exit n
51+
;;
4952

5053
(****************************************************************************)
5154
(* Functions on files. *)
@@ -61,11 +64,14 @@ let create_file_bin n f = let oc = log_open_out_bin n in f oc; close_out oc;;
6164

6265
let read_file_bin n f = let ic = log_open_in_bin n in f ic; close_in ic;;
6366

64-
let concat f1 f2 f3 =
65-
log "generate %s ...\n%!" f3; command ("cat "^f1^" "^f2^" > "^f3)
67+
let concat fs out =
68+
log "generate %s ...\n%!" out;
69+
match fs with
70+
| [] -> command ("touch "^out)
71+
| _ -> command ("cat "^String.concat " " fs^" > "^out)
6672
;;
6773

68-
let remove f = command ("rm -f "^f);;
74+
let remove fs = command ("rm -f "^String.concat " " fs);;
6975

7076
let copy f1 f2 = log "generate %s ...\n%!" f2; command ("cp -f "^f1^" "^f2);;
7177

xlp.ml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,16 @@ let export_subterm_abbrevs b n =
752752
[export_term_abbrevs_in_one_file b n] generates
753753
[n^"_term_abbrevs.lp"] and [n^"_term_abbrevs.typ"]. *)
754754
let export_term_abbrevs_in_one_file b n =
755-
let deps = [b^"_types";b^"_type_abbrevs";b^"_terms"] in
756-
export (n^"_term_abbrevs")
757-
(if !use_sharing then deps @ [n^"_subterm_abbrevs"] else deps)
758-
decl_term_abbrevs;
759-
if !use_sharing then export_subterm_abbrevs b n;
755+
begin
756+
if !use_sharing then
757+
let deps =
758+
[b^"_types";b^"_type_abbrevs";b^"_terms";n^"_subterm_abbrevs"] in
759+
export (n^"_term_abbrevs") deps decl_term_abbrevs;
760+
export_subterm_abbrevs b n
761+
else
762+
let deps = [b^"_types";b^"_type_abbrevs";b^"_terms"] in
763+
export (n^"_term_abbrevs") deps decl_term_abbrevs
764+
end;
760765
write_val (n^"_term_abbrevs.typ") !map_typ_abbrev
761766
;;
762767

@@ -790,8 +795,8 @@ let export_theorem_term_abbrevs_part b n k =
790795
in
791796
create_file_with_deps (p^"_head") p iter_deps (fun _ -> ());
792797
(* generate [p^".lp"] *)
793-
concat (p^"_head.lp") (p^"_tail.lp") (p^".lp");
794-
remove (p^"_head.lp "^p^"_tail.lp")
798+
Xlib.concat [p^"_head.lp";p^"_tail.lp"] (p^".lp");
799+
Xlib.remove [p^"_head.lp";p^"_tail.lp"]
795800
;;
796801

797802
(****************************************************************************)
@@ -908,8 +913,8 @@ let export_theorem_deps b n =
908913
SetStr.iter (spec f) (Hashtbl.find htbl_thm_deps i);
909914
in
910915
create_file_with_deps (p^"_deps") p iter_deps (fun _ -> ());
911-
concat (p^"_deps.lp") (p^"_proofs.lp") (p^".lp");
912-
remove (p^"_deps.lp "^p^"_proofs.lp")
916+
Xlib.concat [p^"_deps.lp";p^"_proofs.lp"] (p^".lp");
917+
Xlib.remove [p^"_deps.lp";p^"_proofs.lp"]
913918
done
914919
;;
915920

@@ -1070,9 +1075,9 @@ let export_theorem_proof_part b n k =
10701075
in
10711076
create_file_with_deps (p^"_spec_deps") (p^"_spec") iter_deps (fun _ -> ());
10721077
(* generate [n^part(k)^".lp"] and [n^part(k)^"_spec.lp"] *)
1073-
concat (p^"_deps.lp") (p^"_body.lp") (p^".lp");
1074-
concat (p^"_spec_deps.lp") (p^"_spec_body.lp") (p^"_spec.lp");
1075-
remove (p^"_deps.lp "^p^"_body.lp "^p^"_spec_deps.lp "^p^"_spec_body.lp")
1078+
Xlib.concat [p^"_deps.lp";p^"_body.lp"] (p^".lp");
1079+
Xlib.concat [p^"_spec_deps.lp";p^"_spec_body.lp"] (p^"_spec.lp");
1080+
Xlib.remove [p^"_deps.lp";p^"_body.lp";p^"_spec_deps.lp";p^"_spec_body.lp"]
10761081
;;
10771082

10781083
(****************************************************************************)

0 commit comments

Comments
 (0)