author | wenzelm |
Wed, 04 Apr 2007 00:11:03 +0200 | |
changeset 22578 | b0eb5652f210 |
parent 21858 | 05f57309170c |
child 22846 | fb79144af9a3 |
permissions | -rw-r--r-- |
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
1 |
(* Title: HOL/Import/hol4rews.ML |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
2 |
ID: $Id$ |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
3 |
Author: Sebastian Skalberg (TU Muenchen) |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
4 |
*) |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
5 |
|
14516 | 6 |
structure StringPair = TableFun(type key = string * string val ord = prod_ord string_ord string_ord); |
7 |
||
8 |
type holthm = (term * term) list * thm |
|
9 |
||
10 |
datatype ImportStatus = |
|
11 |
NoImport |
|
12 |
| Generating of string |
|
13 |
| Replaying of string |
|
14 |
||
15 |
structure HOL4DefThyArgs: THEORY_DATA_ARGS = |
|
16 |
struct |
|
17 |
val name = "HOL4/import_status" |
|
18 |
type T = ImportStatus |
|
19 |
val empty = NoImport |
|
20 |
val copy = I |
|
16424 | 21 |
val extend = I |
22 |
fun merge _ (NoImport,NoImport) = NoImport |
|
23 |
| merge _ _ = (warning "Import status set during merge"; NoImport) |
|
21056 | 24 |
fun print _ import_status = |
14516 | 25 |
Pretty.writeln (Pretty.str (case import_status of NoImport => "No current import" | Generating thyname => ("Generating " ^ thyname) | Replaying thyname => ("Replaying " ^ thyname))) |
26 |
end; |
|
27 |
||
28 |
structure HOL4DefThy = TheoryDataFun(HOL4DefThyArgs); |
|
29 |
||
30 |
structure ImportSegmentArgs: THEORY_DATA_ARGS = |
|
31 |
struct |
|
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
32 |
val name = "HOL4/import_segment" |
14516 | 33 |
type T = string |
34 |
val empty = "" |
|
35 |
val copy = I |
|
16424 | 36 |
val extend = I |
37 |
fun merge _ ("",arg) = arg |
|
38 |
| merge _ (arg,"") = arg |
|
39 |
| merge _ (s1,s2) = |
|
14516 | 40 |
if s1 = s2 |
41 |
then s1 |
|
42 |
else error "Trying to merge two different import segments" |
|
21056 | 43 |
fun print _ import_segment = |
14516 | 44 |
Pretty.writeln (Pretty.str ("Import segment: " ^ import_segment)) |
45 |
end; |
|
46 |
||
47 |
structure ImportSegment = TheoryDataFun(ImportSegmentArgs); |
|
48 |
||
49 |
val get_import_segment = ImportSegment.get |
|
50 |
val set_import_segment = ImportSegment.put |
|
51 |
||
52 |
structure HOL4UNamesArgs: THEORY_DATA_ARGS = |
|
53 |
struct |
|
54 |
val name = "HOL4/used_names" |
|
55 |
type T = string list |
|
56 |
val empty = [] |
|
57 |
val copy = I |
|
16424 | 58 |
val extend = I |
59 |
fun merge _ ([],[]) = [] |
|
60 |
| merge _ _ = error "Used names not empty during merge" |
|
21056 | 61 |
fun print _ used_names = |
14516 | 62 |
Pretty.writeln (Pretty.str "Printing of HOL4/used_names Not implemented") |
63 |
end; |
|
64 |
||
65 |
structure HOL4UNames = TheoryDataFun(HOL4UNamesArgs); |
|
66 |
||
67 |
structure HOL4DumpArgs: THEORY_DATA_ARGS = |
|
68 |
struct |
|
69 |
val name = "HOL4/dump_data" |
|
70 |
type T = string * string * string list |
|
71 |
val empty = ("","",[]) |
|
72 |
val copy = I |
|
16424 | 73 |
val extend = I |
74 |
fun merge _ (("","",[]),("","",[])) = ("","",[]) |
|
75 |
| merge _ _ = error "Dump data not empty during merge" |
|
21056 | 76 |
fun print _ dump_data = |
14516 | 77 |
Pretty.writeln (Pretty.str "Printing of HOL4/dump_data Not implemented") |
78 |
end; |
|
79 |
||
80 |
structure HOL4Dump = TheoryDataFun(HOL4DumpArgs); |
|
81 |
||
82 |
structure HOL4MovesArgs: THEORY_DATA_ARGS = |
|
83 |
struct |
|
84 |
val name = "HOL4/moves" |
|
85 |
type T = string Symtab.table |
|
86 |
val empty = Symtab.empty |
|
87 |
val copy = I |
|
16424 | 88 |
val extend = I |
89 |
fun merge _ : T * T -> T = Symtab.merge (K true) |
|
21056 | 90 |
fun print _ tab = |
91 |
Pretty.writeln (Pretty.big_list "HOL4 moves:" |
|
92 |
(Symtab.fold (fn (bef, aft) => fn xl => (Pretty.str (bef ^ " --> " ^ aft) :: xl)) tab [])) |
|
14516 | 93 |
end; |
94 |
||
95 |
structure HOL4Moves = TheoryDataFun(HOL4MovesArgs); |
|
96 |
||
97 |
structure HOL4ImportsArgs: THEORY_DATA_ARGS = |
|
98 |
struct |
|
99 |
val name = "HOL4/imports" |
|
100 |
type T = string Symtab.table |
|
101 |
val empty = Symtab.empty |
|
102 |
val copy = I |
|
16424 | 103 |
val extend = I |
104 |
fun merge _ : T * T -> T = Symtab.merge (K true) |
|
21056 | 105 |
fun print _ tab = |
106 |
Pretty.writeln (Pretty.big_list "HOL4 imports:" |
|
107 |
(Symtab.fold (fn (thyname, segname) => fn xl => (Pretty.str (thyname ^ " imported from segment " ^ segname) :: xl)) tab [])) |
|
14516 | 108 |
end; |
109 |
||
110 |
structure HOL4Imports = TheoryDataFun(HOL4ImportsArgs); |
|
111 |
||
112 |
fun get_segment2 thyname thy = |
|
17412 | 113 |
Symtab.lookup (HOL4Imports.get thy) thyname |
14516 | 114 |
|
115 |
fun set_segment thyname segname thy = |
|
116 |
let |
|
117 |
val imps = HOL4Imports.get thy |
|
17412 | 118 |
val imps' = Symtab.update_new (thyname,segname) imps |
14516 | 119 |
in |
120 |
HOL4Imports.put imps' thy |
|
121 |
end |
|
122 |
||
123 |
structure HOL4CMovesArgs: THEORY_DATA_ARGS = |
|
124 |
struct |
|
125 |
val name = "HOL4/constant_moves" |
|
126 |
type T = string Symtab.table |
|
127 |
val empty = Symtab.empty |
|
128 |
val copy = I |
|
16424 | 129 |
val extend = I |
130 |
fun merge _ : T * T -> T = Symtab.merge (K true) |
|
21056 | 131 |
fun print _ tab = |
132 |
Pretty.writeln (Pretty.big_list "HOL4 constant moves:" |
|
133 |
(Symtab.fold (fn (bef, aft) => fn xl => (Pretty.str (bef ^ " --> " ^ aft) :: xl)) tab [])) |
|
14516 | 134 |
end; |
135 |
||
136 |
structure HOL4CMoves = TheoryDataFun(HOL4CMovesArgs); |
|
137 |
||
138 |
structure HOL4MapsArgs: THEORY_DATA_ARGS = |
|
139 |
struct |
|
140 |
val name = "HOL4/mappings" |
|
141 |
type T = (string option) StringPair.table |
|
142 |
val empty = StringPair.empty |
|
143 |
val copy = I |
|
16424 | 144 |
val extend = I |
145 |
fun merge _ : T * T -> T = StringPair.merge (K true) |
|
21056 | 146 |
fun print _ tab = |
147 |
Pretty.writeln (Pretty.big_list "HOL4 mappings:" |
|
148 |
(StringPair.fold (fn ((bthy, bthm), isathm) => fn xl => |
|
149 |
(Pretty.str (bthy ^ "." ^ bthm ^ (case isathm of SOME th => " --> " ^ th | NONE => "IGNORED")) :: xl)) tab [])) |
|
14516 | 150 |
end; |
151 |
||
152 |
structure HOL4Maps = TheoryDataFun(HOL4MapsArgs); |
|
153 |
||
154 |
structure HOL4ThmsArgs: THEORY_DATA_ARGS = |
|
155 |
struct |
|
156 |
val name = "HOL4/theorems" |
|
157 |
type T = holthm StringPair.table |
|
158 |
val empty = StringPair.empty |
|
159 |
val copy = I |
|
16424 | 160 |
val extend = I |
161 |
fun merge _ : T * T -> T = StringPair.merge (K true) |
|
21056 | 162 |
fun print _ tab = |
163 |
Pretty.writeln (Pretty.big_list "HOL4 mappings:" |
|
164 |
(StringPair.fold (fn ((bthy, bthm), (_, thm)) => fn xl => (Pretty.str (bthy ^ "." ^ bthm ^ ":") :: Display.pretty_thm thm :: xl)) tab [])) |
|
14516 | 165 |
end; |
166 |
||
167 |
structure HOL4Thms = TheoryDataFun(HOL4ThmsArgs); |
|
168 |
||
169 |
structure HOL4ConstMapsArgs: THEORY_DATA_ARGS = |
|
170 |
struct |
|
171 |
val name = "HOL4/constmappings" |
|
172 |
type T = (bool * string * typ option) StringPair.table |
|
173 |
val empty = StringPair.empty |
|
174 |
val copy = I |
|
16424 | 175 |
val extend = I |
176 |
fun merge _ : T * T -> T = StringPair.merge (K true) |
|
21056 | 177 |
fun print _ tab = |
178 |
Pretty.writeln (Pretty.big_list "HOL4 constant mappings:" |
|
179 |
(StringPair.fold (fn ((bthy, bconst), (internal, isaconst, _)) => fn xl => |
|
180 |
(Pretty.str (bthy ^ "." ^ bconst ^ " --> " ^ isaconst ^ (if internal then " (*)" else "")) :: xl)) tab [])) |
|
14516 | 181 |
end; |
182 |
||
183 |
structure HOL4ConstMaps = TheoryDataFun(HOL4ConstMapsArgs); |
|
184 |
||
185 |
structure HOL4RenameArgs: THEORY_DATA_ARGS = |
|
186 |
struct |
|
187 |
val name = "HOL4/renamings" |
|
188 |
type T = string StringPair.table |
|
189 |
val empty = StringPair.empty |
|
190 |
val copy = I |
|
16424 | 191 |
val extend = I |
192 |
fun merge _ : T * T -> T = StringPair.merge (K true) |
|
21056 | 193 |
fun print _ tab = |
194 |
Pretty.writeln (Pretty.big_list "HOL4 constant renamings:" |
|
195 |
(StringPair.fold (fn ((bthy,bconst),newname) => fn xl => (Pretty.str (bthy ^ "." ^ bconst ^ " --> " ^ newname)::xl)) tab [])) |
|
14516 | 196 |
end; |
197 |
||
198 |
structure HOL4Rename = TheoryDataFun(HOL4RenameArgs); |
|
199 |
||
200 |
structure HOL4DefMapsArgs: THEORY_DATA_ARGS = |
|
201 |
struct |
|
202 |
val name = "HOL4/def_maps" |
|
203 |
type T = string StringPair.table |
|
204 |
val empty = StringPair.empty |
|
205 |
val copy = I |
|
16424 | 206 |
val extend = I |
207 |
fun merge _ : T * T -> T = StringPair.merge (K true) |
|
21056 | 208 |
fun print _ tab = |
209 |
Pretty.writeln (Pretty.big_list "HOL4 constant definitions:" |
|
210 |
(StringPair.fold (fn ((bthy,bconst),newname) => fn xl => (Pretty.str (bthy ^ "." ^ bconst ^ ": " ^ newname)::xl)) tab [])) |
|
14516 | 211 |
end; |
212 |
||
213 |
structure HOL4DefMaps = TheoryDataFun(HOL4DefMapsArgs); |
|
214 |
||
215 |
structure HOL4TypeMapsArgs: THEORY_DATA_ARGS = |
|
216 |
struct |
|
217 |
val name = "HOL4/typemappings" |
|
218 |
type T = (bool * string) StringPair.table |
|
219 |
val empty = StringPair.empty |
|
220 |
val copy = I |
|
16424 | 221 |
val extend = I |
222 |
fun merge _ : T * T -> T = StringPair.merge (K true) |
|
21056 | 223 |
fun print _ tab = |
224 |
Pretty.writeln (Pretty.big_list "HOL4 type mappings:" |
|
225 |
(StringPair.fold (fn ((bthy, bconst), (internal, isaconst)) => fn xl => |
|
226 |
(Pretty.str (bthy ^ "." ^ bconst ^ " --> " ^ isaconst ^ (if internal then " (*)" else "")) :: xl)) tab [])) |
|
14516 | 227 |
end; |
228 |
||
229 |
structure HOL4TypeMaps = TheoryDataFun(HOL4TypeMapsArgs); |
|
230 |
||
231 |
structure HOL4PendingArgs: THEORY_DATA_ARGS = |
|
232 |
struct |
|
233 |
val name = "HOL4/pending" |
|
234 |
type T = ((term * term) list * thm) StringPair.table |
|
235 |
val empty = StringPair.empty |
|
236 |
val copy = I |
|
16424 | 237 |
val extend = I |
238 |
fun merge _ : T * T -> T = StringPair.merge (K true) |
|
21056 | 239 |
fun print _ tab = |
14516 | 240 |
Pretty.writeln (Pretty.big_list "HOL4 pending theorems:" |
21056 | 241 |
(StringPair.fold (fn ((bthy,bthm),(_,th)) => fn xl => (Pretty.chunks [Pretty.str (bthy ^ "." ^ bthm ^ ":"),Display.pretty_thm th]::xl)) tab [])) |
14516 | 242 |
end; |
243 |
||
244 |
structure HOL4Pending = TheoryDataFun(HOL4PendingArgs); |
|
245 |
||
246 |
structure HOL4RewritesArgs: THEORY_DATA_ARGS = |
|
247 |
struct |
|
248 |
val name = "HOL4/rewrites" |
|
249 |
type T = thm list |
|
250 |
val empty = [] |
|
251 |
val copy = I |
|
16424 | 252 |
val extend = I |
253 |
fun merge _ = Library.gen_union Thm.eq_thm |
|
21056 | 254 |
fun print _ thms = |
14516 | 255 |
Pretty.writeln (Pretty.big_list "HOL4 rewrite rules:" |
256 |
(map Display.pretty_thm thms)) |
|
257 |
end; |
|
258 |
||
259 |
structure HOL4Rewrites = TheoryDataFun(HOL4RewritesArgs); |
|
260 |
||
261 |
val hol4_debug = ref false |
|
262 |
fun message s = if !hol4_debug then writeln s else () |
|
263 |
||
21546 | 264 |
local |
265 |
val eq_reflection = thm "eq_reflection" |
|
266 |
in |
|
20897 | 267 |
fun add_hol4_rewrite (Context.Theory thy, th) = |
14516 | 268 |
let |
269 |
val _ = message "Adding HOL4 rewrite" |
|
21546 | 270 |
val th1 = th RS eq_reflection |
14516 | 271 |
val current_rews = HOL4Rewrites.get thy |
18921 | 272 |
val new_rews = insert Thm.eq_thm th1 current_rews |
14516 | 273 |
val updated_thy = HOL4Rewrites.put new_rews thy |
274 |
in |
|
18728 | 275 |
(Context.Theory updated_thy,th1) |
20897 | 276 |
end |
21546 | 277 |
| add_hol4_rewrite (context, th) = (context, th RS eq_reflection); |
278 |
end |
|
14516 | 279 |
|
280 |
fun ignore_hol4 bthy bthm thy = |
|
281 |
let |
|
282 |
val _ = message ("Ignoring " ^ bthy ^ "." ^ bthm) |
|
283 |
val curmaps = HOL4Maps.get thy |
|
17412 | 284 |
val newmaps = StringPair.update_new ((bthy,bthm),NONE) curmaps |
14516 | 285 |
val upd_thy = HOL4Maps.put newmaps thy |
286 |
in |
|
287 |
upd_thy |
|
288 |
end |
|
289 |
||
290 |
val opt_get_output_thy = #2 o HOL4Dump.get |
|
291 |
||
292 |
fun get_output_thy thy = |
|
293 |
case #2 (HOL4Dump.get thy) of |
|
294 |
"" => error "No theory file being output" |
|
295 |
| thyname => thyname |
|
296 |
||
297 |
val get_output_dir = #1 o HOL4Dump.get |
|
298 |
||
299 |
fun add_hol4_move bef aft thy = |
|
300 |
let |
|
301 |
val curmoves = HOL4Moves.get thy |
|
17412 | 302 |
val newmoves = Symtab.update_new (bef, aft) curmoves |
14516 | 303 |
in |
304 |
HOL4Moves.put newmoves thy |
|
305 |
end |
|
306 |
||
307 |
fun get_hol4_move bef thy = |
|
17412 | 308 |
Symtab.lookup (HOL4Moves.get thy) bef |
14516 | 309 |
|
310 |
fun follow_name thmname thy = |
|
311 |
let |
|
312 |
val moves = HOL4Moves.get thy |
|
313 |
fun F thmname = |
|
17412 | 314 |
case Symtab.lookup moves thmname of |
15531 | 315 |
SOME name => F name |
316 |
| NONE => thmname |
|
14516 | 317 |
in |
318 |
F thmname |
|
319 |
end |
|
320 |
||
321 |
fun add_hol4_cmove bef aft thy = |
|
322 |
let |
|
323 |
val curmoves = HOL4CMoves.get thy |
|
17412 | 324 |
val newmoves = Symtab.update_new (bef, aft) curmoves |
14516 | 325 |
in |
326 |
HOL4CMoves.put newmoves thy |
|
327 |
end |
|
328 |
||
329 |
fun get_hol4_cmove bef thy = |
|
17412 | 330 |
Symtab.lookup (HOL4CMoves.get thy) bef |
14516 | 331 |
|
332 |
fun follow_cname thmname thy = |
|
333 |
let |
|
334 |
val moves = HOL4CMoves.get thy |
|
335 |
fun F thmname = |
|
17412 | 336 |
case Symtab.lookup moves thmname of |
15531 | 337 |
SOME name => F name |
338 |
| NONE => thmname |
|
14516 | 339 |
in |
340 |
F thmname |
|
341 |
end |
|
342 |
||
343 |
fun add_hol4_mapping bthy bthm isathm thy = |
|
17644 | 344 |
let |
345 |
(* val _ = writeln ("Before follow_name: "^isathm) *) |
|
346 |
val isathm = follow_name isathm thy |
|
347 |
(* val _ = writeln ("Adding theorem map: " ^ bthy ^ "." ^ bthm ^ " --> " ^ isathm)*) |
|
14516 | 348 |
val curmaps = HOL4Maps.get thy |
17412 | 349 |
val newmaps = StringPair.update_new ((bthy,bthm),SOME isathm) curmaps |
14516 | 350 |
val upd_thy = HOL4Maps.put newmaps thy |
351 |
in |
|
352 |
upd_thy |
|
353 |
end; |
|
354 |
||
355 |
fun get_hol4_type_mapping bthy tycon thy = |
|
356 |
let |
|
357 |
val maps = HOL4TypeMaps.get thy |
|
358 |
in |
|
17412 | 359 |
StringPair.lookup maps (bthy,tycon) |
14516 | 360 |
end |
361 |
||
362 |
fun get_hol4_mapping bthy bthm thy = |
|
363 |
let |
|
364 |
val curmaps = HOL4Maps.get thy |
|
365 |
in |
|
17412 | 366 |
StringPair.lookup curmaps (bthy,bthm) |
14516 | 367 |
end; |
368 |
||
369 |
fun add_hol4_const_mapping bthy bconst internal isaconst thy = |
|
370 |
let |
|
371 |
val thy = case opt_get_output_thy thy of |
|
372 |
"" => thy |
|
373 |
| output_thy => if internal |
|
22578 | 374 |
then add_hol4_cmove (Sign.full_name thy bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy |
14516 | 375 |
else thy |
376 |
val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else "")) |
|
377 |
val curmaps = HOL4ConstMaps.get thy |
|
17412 | 378 |
val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,NONE)) curmaps |
14516 | 379 |
val upd_thy = HOL4ConstMaps.put newmaps thy |
380 |
in |
|
381 |
upd_thy |
|
382 |
end; |
|
383 |
||
384 |
fun add_hol4_const_renaming bthy bconst newname thy = |
|
385 |
let |
|
386 |
val currens = HOL4Rename.get thy |
|
387 |
val _ = message ("Adding renaming " ^ bthy ^ "." ^ bconst ^ " -> " ^ newname) |
|
17412 | 388 |
val newrens = StringPair.update_new ((bthy,bconst),newname) currens |
14516 | 389 |
val upd_thy = HOL4Rename.put newrens thy |
390 |
in |
|
391 |
upd_thy |
|
392 |
end; |
|
393 |
||
394 |
fun get_hol4_const_renaming bthy bconst thy = |
|
395 |
let |
|
396 |
val currens = HOL4Rename.get thy |
|
397 |
in |
|
17412 | 398 |
StringPair.lookup currens (bthy,bconst) |
14516 | 399 |
end; |
400 |
||
401 |
fun get_hol4_const_mapping bthy bconst thy = |
|
402 |
let |
|
403 |
val bconst = case get_hol4_const_renaming bthy bconst thy of |
|
15531 | 404 |
SOME name => name |
405 |
| NONE => bconst |
|
14516 | 406 |
val maps = HOL4ConstMaps.get thy |
407 |
in |
|
17412 | 408 |
StringPair.lookup maps (bthy,bconst) |
14516 | 409 |
end |
410 |
||
411 |
fun add_hol4_const_wt_mapping bthy bconst internal isaconst typ thy = |
|
412 |
let |
|
413 |
val thy = case opt_get_output_thy thy of |
|
414 |
"" => thy |
|
415 |
| output_thy => if internal |
|
22578 | 416 |
then add_hol4_cmove (Sign.full_name thy bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy |
14516 | 417 |
else thy |
418 |
val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else "")) |
|
419 |
val curmaps = HOL4ConstMaps.get thy |
|
17412 | 420 |
val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,SOME typ)) curmaps |
14516 | 421 |
val upd_thy = HOL4ConstMaps.put newmaps thy |
422 |
in |
|
423 |
upd_thy |
|
424 |
end; |
|
425 |
||
426 |
fun add_hol4_type_mapping bthy bconst internal isaconst thy = |
|
427 |
let |
|
428 |
val curmaps = HOL4TypeMaps.get thy |
|
17322 | 429 |
val _ = writeln ("Adding tmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else "")) |
17412 | 430 |
val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst)) curmaps |
431 |
handle x => let val (internal, isaconst') = the (StringPair.lookup curmaps (bthy, bconst)) in |
|
17322 | 432 |
warning ("couldn't map type "^bthy^"."^bconst^" to "^isaconst^": already mapped to "^isaconst'); raise x end |
433 |
val upd_thy = HOL4TypeMaps.put newmaps thy |
|
14516 | 434 |
in |
435 |
upd_thy |
|
436 |
end; |
|
437 |
||
438 |
fun add_hol4_pending bthy bthm hth thy = |
|
439 |
let |
|
22578 | 440 |
val thmname = Sign.full_name thy bthm |
14516 | 441 |
val _ = message ("Add pending " ^ bthy ^ "." ^ bthm) |
442 |
val curpend = HOL4Pending.get thy |
|
17412 | 443 |
val newpend = StringPair.update_new ((bthy,bthm),hth) curpend |
14516 | 444 |
val upd_thy = HOL4Pending.put newpend thy |
445 |
val thy' = case opt_get_output_thy upd_thy of |
|
446 |
"" => add_hol4_mapping bthy bthm thmname upd_thy |
|
447 |
| output_thy => |
|
448 |
let |
|
449 |
val new_thmname = output_thy ^ "." ^ bthy ^ "." ^ bthm |
|
450 |
in |
|
451 |
upd_thy |> add_hol4_move thmname new_thmname |
|
452 |
|> add_hol4_mapping bthy bthm new_thmname |
|
453 |
end |
|
454 |
in |
|
455 |
thy' |
|
456 |
end; |
|
457 |
||
458 |
fun get_hol4_theorem thyname thmname thy = |
|
21056 | 459 |
let |
460 |
val isathms = HOL4Thms.get thy |
|
461 |
in |
|
462 |
StringPair.lookup isathms (thyname,thmname) |
|
463 |
end; |
|
14516 | 464 |
|
21056 | 465 |
fun add_hol4_theorem thyname thmname hth = |
466 |
let |
|
467 |
val _ = message ("Adding HOL4 theorem " ^ thyname ^ "." ^ thmname) |
|
468 |
in |
|
469 |
HOL4Thms.map (StringPair.update_new ((thyname, thmname), hth)) |
|
470 |
end; |
|
14516 | 471 |
|
472 |
fun export_hol4_pending thy = |
|
21056 | 473 |
let |
474 |
val rews = HOL4Rewrites.get thy; |
|
475 |
val pending = HOL4Pending.get thy; |
|
476 |
fun process ((bthy,bthm), hth as (_,thm)) thy = |
|
477 |
let |
|
478 |
val thm1 = rewrite_rule (map (Thm.transfer thy) rews) (Thm.transfer thy thm); |
|
479 |
val thm2 = standard thm1; |
|
480 |
in |
|
481 |
thy |
|
482 |
|> PureThy.store_thm ((bthm, thm2), []) |
|
483 |
|> snd |
|
484 |
|> add_hol4_theorem bthy bthm hth |
|
485 |
end; |
|
486 |
in |
|
487 |
thy |
|
488 |
|> StringPair.fold process pending |
|
489 |
|> HOL4Pending.put StringPair.empty |
|
490 |
end; |
|
14516 | 491 |
|
492 |
fun setup_dump (dir,thyname) thy = |
|
15647 | 493 |
HOL4Dump.put (dir,thyname,["(* AUTOMATICALLY GENERATED, DO NOT EDIT! *)"]) thy |
14516 | 494 |
|
495 |
fun add_dump str thy = |
|
496 |
let |
|
497 |
val (dir,thyname,curdump) = HOL4Dump.get thy |
|
498 |
in |
|
499 |
HOL4Dump.put (dir,thyname,str::curdump) thy |
|
500 |
end |
|
501 |
||
502 |
fun flush_dump thy = |
|
503 |
let |
|
504 |
val (dir,thyname,dumpdata) = HOL4Dump.get thy |
|
505 |
val os = TextIO.openOut (OS.Path.joinDirFile {dir=dir, |
|
506 |
file=thyname ^ ".thy"}) |
|
507 |
val _ = app (fn s => TextIO.output(os,s ^ "\n\n")) (rev dumpdata) |
|
508 |
val _ = TextIO.closeOut os |
|
509 |
in |
|
510 |
HOL4Dump.put ("","",[]) thy |
|
511 |
end |
|
512 |
||
513 |
fun set_generating_thy thyname thy = |
|
514 |
case HOL4DefThy.get thy of |
|
515 |
NoImport => HOL4DefThy.put (Generating thyname) thy |
|
516 |
| _ => error "Import already in progess" |
|
517 |
||
518 |
fun set_replaying_thy thyname thy = |
|
519 |
case HOL4DefThy.get thy of |
|
520 |
NoImport => HOL4DefThy.put (Replaying thyname) thy |
|
521 |
| _ => error "Import already in progess" |
|
522 |
||
523 |
fun clear_import_thy thy = |
|
524 |
case HOL4DefThy.get thy of |
|
525 |
NoImport => error "No import in progress" |
|
526 |
| _ => HOL4DefThy.put NoImport thy |
|
527 |
||
528 |
fun get_generating_thy thy = |
|
529 |
case HOL4DefThy.get thy of |
|
530 |
Generating thyname => thyname |
|
531 |
| _ => error "No theory being generated" |
|
532 |
||
533 |
fun get_replaying_thy thy = |
|
534 |
case HOL4DefThy.get thy of |
|
535 |
Replaying thyname => thyname |
|
536 |
| _ => error "No theory being replayed" |
|
537 |
||
538 |
fun get_import_thy thy = |
|
539 |
case HOL4DefThy.get thy of |
|
540 |
Replaying thyname => thyname |
|
541 |
| Generating thyname => thyname |
|
542 |
| _ => error "No theory being imported" |
|
543 |
||
544 |
fun should_ignore thyname thy thmname = |
|
545 |
case get_hol4_mapping thyname thmname thy of |
|
15531 | 546 |
SOME NONE => true |
14516 | 547 |
| _ => false |
548 |
||
549 |
val trans_string = |
|
550 |
let |
|
551 |
fun quote s = "\"" ^ s ^ "\"" |
|
552 |
fun F [] = [] |
|
553 |
| F (#"\\" :: cs) = patch #"\\" cs |
|
554 |
| F (#"\"" :: cs) = patch #"\"" cs |
|
555 |
| F (c :: cs) = c :: F cs |
|
556 |
and patch c rest = #"\\" :: c :: F rest |
|
557 |
in |
|
558 |
quote o String.implode o F o String.explode |
|
559 |
end |
|
560 |
||
561 |
fun dump_import_thy thyname thy = |
|
562 |
let |
|
563 |
val output_dir = get_output_dir thy |
|
564 |
val output_thy = get_output_thy thy |
|
17607
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
565 |
val input_thy = Context.theory_name thy |
14516 | 566 |
val import_segment = get_import_segment thy |
567 |
val os = TextIO.openOut (OS.Path.joinDirFile {dir=output_dir, |
|
568 |
file=thyname ^ ".imp"}) |
|
569 |
fun out s = TextIO.output(os,s) |
|
21056 | 570 |
val (ignored, mapped) = StringPair.fold |
571 |
(fn ((bthy, bthm), v) => fn (ign, map) => |
|
572 |
if bthy = thyname |
|
573 |
then case v |
|
574 |
of NONE => (bthm :: ign, map) |
|
575 |
| SOME w => (ign, (bthm, w) :: map) |
|
576 |
else (ign, map)) (HOL4Maps.get thy) ([],[]); |
|
577 |
fun mk init = StringPair.fold |
|
578 |
(fn ((bthy, bthm), v) => if bthy = thyname then cons (bthm, v) else I) init []; |
|
579 |
val constmaps = mk (HOL4ConstMaps.get thy); |
|
580 |
val constrenames = mk (HOL4Rename.get thy); |
|
581 |
val typemaps = mk (HOL4TypeMaps.get thy); |
|
582 |
val defmaps = mk (HOL4DefMaps.get thy); |
|
14516 | 583 |
fun new_name internal isa = |
584 |
if internal |
|
585 |
then |
|
586 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21546
diff
changeset
|
587 |
val paths = NameSpace.explode isa |
15570 | 588 |
val i = Library.drop(length paths - 2,paths) |
14516 | 589 |
in |
590 |
case i of |
|
591 |
[seg,con] => output_thy ^ "." ^ seg ^ "." ^ con |
|
592 |
| _ => error "hol4rews.dump internal error" |
|
593 |
end |
|
594 |
else |
|
595 |
isa |
|
596 |
||
597 |
val _ = out "import\n\n" |
|
598 |
||
599 |
val _ = out ("import_segment " ^ trans_string import_segment ^ "\n\n") |
|
600 |
val _ = if null defmaps |
|
601 |
then () |
|
602 |
else out "def_maps" |
|
603 |
val _ = app (fn (hol,isa) => |
|
604 |
out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string isa))) defmaps |
|
605 |
val _ = if null defmaps |
|
606 |
then () |
|
607 |
else out "\n\n" |
|
608 |
||
609 |
val _ = if null typemaps |
|
610 |
then () |
|
611 |
else out "type_maps" |
|
612 |
val _ = app (fn (hol,(internal,isa)) => |
|
613 |
out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (new_name internal isa)))) typemaps |
|
614 |
val _ = if null typemaps |
|
615 |
then () |
|
616 |
else out "\n\n" |
|
617 |
||
618 |
val _ = if null constmaps |
|
619 |
then () |
|
620 |
else out "const_maps" |
|
621 |
val _ = app (fn (hol,(internal,isa,opt_ty)) => |
|
622 |
(out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (follow_cname isa thy))); |
|
623 |
case opt_ty of |
|
21056 | 624 |
SOME ty => out (" :: \"" ^ (string_of_ctyp (ctyp_of thy ty)) ^ "\"") |
15531 | 625 |
| NONE => ())) constmaps |
14516 | 626 |
val _ = if null constmaps |
627 |
then () |
|
628 |
else out "\n\n" |
|
629 |
||
630 |
val _ = if null constrenames |
|
631 |
then () |
|
632 |
else out "const_renames" |
|
633 |
val _ = app (fn (old,new) => |
|
634 |
out ("\n " ^ (trans_string old) ^ " > " ^ (trans_string new))) constrenames |
|
635 |
val _ = if null constrenames |
|
636 |
then () |
|
637 |
else out "\n\n" |
|
638 |
||
17607
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
639 |
fun gen2replay in_thy out_thy s = |
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
640 |
let |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21546
diff
changeset
|
641 |
val ss = NameSpace.explode s |
17607
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
642 |
in |
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
643 |
if (hd ss = in_thy) then |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21546
diff
changeset
|
644 |
NameSpace.implode (out_thy::(tl ss)) |
17607
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
645 |
else |
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
646 |
s |
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
647 |
end |
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
648 |
|
14516 | 649 |
val _ = if null mapped |
650 |
then () |
|
651 |
else out "thm_maps" |
|
17607
7725da65f8e0
1) fixed bug in type_introduction: first stage uses different namespace than second stage
obua
parents:
17412
diff
changeset
|
652 |
val _ = app (fn (hol,isa) => out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (gen2replay input_thy output_thy isa)))) mapped |
14516 | 653 |
val _ = if null mapped |
654 |
then () |
|
655 |
else out "\n\n" |
|
656 |
||
657 |
val _ = if null ignored |
|
658 |
then () |
|
659 |
else out "ignore_thms" |
|
660 |
val _ = app (fn ign => out ("\n " ^ (trans_string ign))) ignored |
|
661 |
val _ = if null ignored |
|
662 |
then () |
|
663 |
else out "\n\n" |
|
664 |
||
665 |
val _ = out "end\n" |
|
666 |
val _ = TextIO.closeOut os |
|
667 |
in |
|
668 |
thy |
|
669 |
end |
|
670 |
||
671 |
fun set_used_names names thy = |
|
672 |
let |
|
673 |
val unames = HOL4UNames.get thy |
|
674 |
in |
|
675 |
case unames of |
|
676 |
[] => HOL4UNames.put names thy |
|
677 |
| _ => error "hol4rews.set_used_names called on initialized data!" |
|
678 |
end |
|
679 |
||
680 |
val clear_used_names = HOL4UNames.put HOL4UNamesArgs.empty |
|
681 |
||
682 |
fun get_defmap thyname const thy = |
|
683 |
let |
|
684 |
val maps = HOL4DefMaps.get thy |
|
685 |
in |
|
17412 | 686 |
StringPair.lookup maps (thyname,const) |
14516 | 687 |
end |
688 |
||
689 |
fun add_defmap thyname const defname thy = |
|
690 |
let |
|
691 |
val _ = message ("Adding defmap " ^ thyname ^ "." ^ const ^ " --> " ^ defname) |
|
692 |
val maps = HOL4DefMaps.get thy |
|
17412 | 693 |
val maps' = StringPair.update_new ((thyname,const),defname) maps |
14516 | 694 |
val thy' = HOL4DefMaps.put maps' thy |
695 |
in |
|
696 |
thy' |
|
697 |
end |
|
698 |
||
699 |
fun get_defname thyname name thy = |
|
700 |
let |
|
701 |
val maps = HOL4DefMaps.get thy |
|
702 |
fun F dname = (dname,add_defmap thyname name dname thy) |
|
703 |
in |
|
17412 | 704 |
case StringPair.lookup maps (thyname,name) of |
15531 | 705 |
SOME thmname => (thmname,thy) |
706 |
| NONE => |
|
14516 | 707 |
let |
708 |
val used = HOL4UNames.get thy |
|
709 |
val defname = def_name name |
|
710 |
val pdefname = name ^ "_primdef" |
|
711 |
in |
|
712 |
if not (defname mem used) |
|
713 |
then F defname (* name_def *) |
|
714 |
else if not (pdefname mem used) |
|
715 |
then F pdefname (* name_primdef *) |
|
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
18921
diff
changeset
|
716 |
else F (Name.variant used pdefname) (* last resort *) |
14516 | 717 |
end |
718 |
end |
|
719 |
||
720 |
local |
|
721 |
fun handle_meta [x as Appl[Appl[Constant "_constrain", Constant "==", _],_,_]] = x |
|
722 |
| handle_meta [x as Appl[Appl[Constant "_constrain", Constant "all", _],_]] = x |
|
723 |
| handle_meta [x as Appl[Appl[Constant "_constrain", Constant "==>", _],_,_]] = x |
|
724 |
| handle_meta [x] = Appl[Constant "Trueprop",x] |
|
14518
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
725 |
| handle_meta _ = error "hol4rews error: Trueprop not applied to single argument" |
14516 | 726 |
in |
727 |
val smarter_trueprop_parsing = [("Trueprop",handle_meta)] |
|
728 |
end |
|
729 |
||
730 |
local |
|
731 |
fun initial_maps thy = |
|
732 |
thy |> add_hol4_type_mapping "min" "bool" false "bool" |
|
733 |
|> add_hol4_type_mapping "min" "fun" false "fun" |
|
734 |
|> add_hol4_type_mapping "min" "ind" false "Nat.ind" |
|
735 |
|> add_hol4_const_mapping "min" "=" false "op =" |
|
736 |
|> add_hol4_const_mapping "min" "==>" false "op -->" |
|
737 |
|> add_hol4_const_mapping "min" "@" false "Hilbert_Choice.Eps" |
|
738 |
in |
|
739 |
val hol4_setup = |
|
18708 | 740 |
HOL4Rewrites.init #> |
741 |
HOL4Maps.init #> |
|
742 |
HOL4UNames.init #> |
|
743 |
HOL4DefMaps.init #> |
|
744 |
HOL4Moves.init #> |
|
745 |
HOL4CMoves.init #> |
|
746 |
HOL4ConstMaps.init #> |
|
747 |
HOL4Rename.init #> |
|
748 |
HOL4TypeMaps.init #> |
|
749 |
HOL4Pending.init #> |
|
750 |
HOL4Thms.init #> |
|
751 |
HOL4Dump.init #> |
|
752 |
HOL4DefThy.init #> |
|
753 |
HOL4Imports.init #> |
|
754 |
ImportSegment.init #> |
|
755 |
initial_maps #> |
|
756 |
Attrib.add_attributes |
|
18728 | 757 |
[("hol4rew", Attrib.no_args add_hol4_rewrite, "HOL4 rewrite rule")] |
14516 | 758 |
end |