Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 10,868 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
(* ========================================================================= *)
(* Tooling for the generation of the ProofTrace dataset.                     *)
(* ========================================================================= *)

#load "unix.cma";;
#load "str.cma";;

(* ------------------------------------------------------------------------- *)
(* Marshalling of term to AST-like.                                          *)
(* ------------------------------------------------------------------------- *)

let type_string ty =
  let rec args_str args =
    match args with
      [] -> ""
    | ty::tail -> Printf.sprintf "[%s]%s"
                                 (type_str ty) (args_str tail)
  and type_str ty =
    match ty with
      Tyvar(v) -> Printf.sprintf "v[%s]"
                                 (String.escaped v)
    | Tyapp(c,args) -> Printf.sprintf "c[%s][%s]"
                                      (String.escaped c) (args_str args)
  in (type_str ty)

let rec term_string tm =
  match tm with
    Var(v,ty) -> Printf.sprintf "v(%s)(%s)"
                                (String.escaped v) (type_string ty)
  | Const(c,ty) -> Printf.sprintf "c(%s)(%s)"
                                  (String.escaped c) (type_string ty)
  | Comb(t1,t2) -> Printf.sprintf "C(%s)(%s)"
                                  (term_string t1) (term_string t2)
  | Abs(t1,t2) -> Printf.sprintf "A(%s)(%s)"
                                 (term_string t1) (term_string t2)

(* ------------------------------------------------------------------------- *)
(* Marshalling of proof to JSON parts.                                       *)
(* ------------------------------------------------------------------------- *)

let rec inst_string insts =
  match insts with
    [] -> ""
  | (t1,t2)::[] -> Printf.sprintf "[\"%s\", \"%s\"]"
                                  (term_string t2)
                                  (term_string t1)
  | (t1,t2)::tail -> Printf.sprintf "[\"%s\", \"%s\"], %s"
                                    (term_string t2)
                                    (term_string t1)
                                    (inst_string tail)

let rec instt_string insts =
  match insts with
    [] -> ""
  | (t1,t2)::[] -> Printf.sprintf "[\"%s\", \"%s\"]"
                                  (type_string t2)
                                  (type_string t1)
  | (t1,t2)::tail -> Printf.sprintf "[\"%s\", \"%s\"], %s"
                                    (type_string t2)
                                    (type_string t1)
                                    (instt_string tail)

let proof_index proof =
  let Proof(idx,_,_) = proof in idx

let proof_content_string content =
  match content with
    Prefl(tm) -> Printf.sprintf "[\"REFL\", \"%s\"]"
                                (term_string tm)
  | Ptrans(p1,p2) -> Printf.sprintf "[\"TRANS\", %d, %d]"
                                    (proof_index p1)
                                    (proof_index p2)
  | Pmkcomb(p1,p2) -> Printf.sprintf "[\"MK_COMB\", %d, %d]"
                                     (proof_index p1)
                                     (proof_index p2)
  | Pabs(p1,tm) -> Printf.sprintf "[\"ABS\", %d, \"%s\"]"
                                  (proof_index p1)
                                  (term_string tm)
  | Pbeta(tm) -> Printf.sprintf "[\"BETA\", \"%s\"]"
                                (term_string tm)
  | Passume(tm) -> Printf.sprintf "[\"ASSUME\", \"%s\"]"
                                  (term_string tm)
  | Peqmp(p1,p2) -> Printf.sprintf "[\"EQ_MP\", %d, %d]"
                                   (proof_index p1)
                                   (proof_index p2)
  | Pdeduct(p1,p2) -> Printf.sprintf "[\"DEDUCT_ANTISYM_RULE\", %d, %d]"
                                     (proof_index p1)
                                     (proof_index p2)
  | Pinst(p1,insts) -> Printf.sprintf "[\"INST\", %d, [%s]]"
                                      (proof_index p1)
                                      (inst_string insts)
  | Pinstt(p1,insts) -> Printf.sprintf "[\"INST_TYPE\", %d, [%s]]"
                                       (proof_index p1)
                                       (instt_string insts)
  | Paxiom(tm) -> Printf.sprintf "[\"AXIOM\", \"%s\"]"
                                 (term_string tm)
  | Pdef(tm,name,ty) -> Printf.sprintf "[\"DEFINITION\", \"%s\", \"%s\"]"
                                       (term_string tm)
                                       (String.escaped name)
  | Pdeft(p1,tm,name,ty) -> Printf.sprintf
                              "[\"TYPE_DEFINITION\", %d, \"%s\", \"%s\"]"
                              (proof_index p1)
                              (term_string tm)
                              (String.escaped name)


let proof_string proof =
  let Proof(idx,thm,content) = proof in
  Printf.sprintf "{\"id\": %d, \"pr\": %s}"
                 idx
                 (proof_content_string content);;

(* ------------------------------------------------------------------------- *)
(* Marshalling of thm to JSON.                                               *)
(* ------------------------------------------------------------------------- *)

let thm_string th =
  let asl,tm = dest_thm th in
  let rec asl_string asl =
    match asl with
      [] -> ""
    | tm::[] -> Printf.sprintf "\"%s\"" (term_string tm)
    | tm::tail -> Printf.sprintf "\"%s\", %s"
                                 (term_string tm)
                                 (asl_string tail)
  in Printf.sprintf "{\"hy\": [%s], \"cc\": \"%s\"}"
                    (asl_string asl)
                    (term_string tm)

let theorem_string proof =
  let Proof(idx,thm,content) = proof in
  Printf.sprintf "{\"id\": %d, \"th\": %s}"
                 idx
                 (thm_string thm);;


(* ------------------------------------------------------------------------- *)
(* Proofs and Theorems trace dumping.                                        *)
(* ------------------------------------------------------------------------- *)

let dump_proofs filename =
  let foutc = open_out filename in
  (do_list (fun p -> Printf.fprintf foutc
                                    "%s\n"
                                    (proof_string p)) (proofs());
   flush foutc;
   close_out foutc)
;;

let dump_theorems filename =
  let foutc = open_out filename in
  (do_list (fun p -> Printf.fprintf foutc
                     "%s\n"
                     (theorem_string p)) (proofs());
   flush foutc;
   close_out foutc)
;;

(* ------------------------------------------------------------------------- *)
(* Theorem names extraction (inspired by HolStep, but non-destructive).      *)
(* ------------------------------------------------------------------------- *)

let PROVE_1_RE = Str.regexp (String.concat "" (
  "\\(let\\|and\\)[ \n\t]*"::
  "\\([a-zA-Z0-9_-]+\\)[ \n\t]*"::
  "=[ \n\t]*"::
  "\\(prove\\|"::
  "prove_by_refinement\\|"::
  "new_definition\\|"::
  "new_basic_definition\\|"::
  "new_axiom\\|"::
  "new_infix_definition\\|"::
  "INT_OF_REAL_THM\\|"::
  "define_finite_type\\|"::
  "TAUT\\|"::
  "INT_ARITH\\|"::
  "new_recursive_definition\\)"::
  []
))

let PROVE_2_RE = Str.regexp (String.concat "" (
  "\\(let\\|and\\)[ \n\t]*"::
  "\\([a-zA-Z0-9_-]+\\)[ \n\t]*,[ \n\t]*"::
  "\\([a-zA-Z0-9_-]+\\)[ \n\t]*"::
  "=[ \n\t]*"::
  "\\(define_type\\|"::
  "(CONJ_PAIR o prove)\\)"::
  []
))

let PROVE_3_RE = Str.regexp (String.concat "" (
  "\\(let\\|and\\)[ \n\t]*"::
  "\\([a-zA-Z0-9_-]+\\)[ \n\t]*,[ \n\t]*"::
  "\\([a-zA-Z0-9_-]+\\)[ \n\t]*,[ \n\t]*"::
  "\\([a-zA-Z0-9_-]+\\)[ \n\t]*"::
  "=[ \n\t]*"::
  "\\(new_inductive_definition\\)"::
  []
))

let source_files() =
  let select str = Str.string_match (Str.regexp ".*\\.[hm]l$") str 0 in
  let rec walk acc = function
  | [] -> (acc)
  | dir::tail ->
      let contents = Array.to_list (Sys.readdir dir) in
      let contents = List.rev_map (Filename.concat dir) contents in
      let dirs, files =
        List.fold_left (fun (dirs,files) f ->
                          match Sys.is_directory f with
                          | false -> (dirs, f::files)  (* Regular file *)
                          | true -> (f::dirs, files)  (* Directory *)
        ) ([],[]) contents in
      let matched = List.filter (select) files in
        walk (matched @ acc) (dirs @ tail)
  in walk [] [Sys.getcwd()]
;;

let load_file f =
  let ic = open_in f in
  let n = in_channel_length ic in
  let s = Bytes.create n in
  really_input ic s 0 n;
  close_in ic;
  (s)

let extract_prove_1 f =
  let content = Bytes.to_string(load_file f) in
  let rec search acc start =
    try
      let _ = Str.search_forward PROVE_1_RE content start in
      let matches = (Str.matched_group 2 content)::[] in
      search (matches @ acc) (Str.match_end())
    with e -> (acc)
  in search [] 0
;;

let extract_prove_2 f =
  let content = Bytes.to_string(load_file f) in
  let rec search acc start =
    try
      let _ = Str.search_forward PROVE_2_RE content start in
      let matches = (Str.matched_group 2 content)::
                    (Str.matched_group 3 content)::
                    [] in
      search (matches @ acc) (Str.match_end())
    with e -> (acc)
  in search [] 0
;;

let extract_prove_3 f =
  let content = Bytes.to_string(load_file f) in
  let rec search acc start =
    try
      let _ = Str.search_forward PROVE_3_RE content start in
      let matches = (Str.matched_group 2 content)::
                    (Str.matched_group 3 content)::
                    (Str.matched_group 4 content)::
                    [] in
      search (matches @ acc) (Str.match_end())
    with e -> (acc)
  in search [] 0
;;

(* ------------------------------------------------------------------------- *)
(* Names trace dumping (:see_no_evil)                                        *)
(* ------------------------------------------------------------------------- *)

let eval code =
  let as_buf = Lexing.from_string code in
  let parsed = !Toploop.parse_toplevel_phrase as_buf in
  ignore (Toploop.execute_phrase true Format.std_formatter parsed)

let _CODE_GEN name = Printf.sprintf
                       "_IDX := proof_index (proof_of %s);;"
                       name
let _IDX = ref (0)

let dump_names filename =
  let foutc = open_out filename in
  let acc = ref ([]) in
  (do_list (fun f -> acc := !acc @
                     (extract_prove_1 f) @
                     (extract_prove_2 f) @
                     (extract_prove_3 f)) (source_files());
   acc := List.sort_uniq compare !acc;
   do_list (fun name ->
               try
                 eval (_CODE_GEN name);
                 Printf.fprintf foutc
                                "{\"id\": %d, \"nm\": \"%s\"}\n"
                                !_IDX name;
               with _ -> ()
            ) (!acc);
   flush foutc;
   close_out foutc)
;;