discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
1 (* Title: HOL/Import/hol4rews.ML
2 Author: Sebastian Skalberg (TU Muenchen)
5 structure StringPair = Table(type key = string * string val ord = prod_ord string_ord string_ord);
7 type holthm = (term * term) list * thm
9 datatype ImportStatus =
11 | Generating of string
14 structure HOL4DefThy = Theory_Data
19 fun merge (NoImport, NoImport) = NoImport
20 | merge _ = (warning "Import status set during merge"; NoImport)
23 structure ImportSegment = Theory_Data
28 fun merge ("",arg) = arg
29 | merge (arg,"") = arg
33 else error "Trying to merge two different import segments"
36 val get_import_segment = ImportSegment.get
37 val set_import_segment = ImportSegment.put
39 structure HOL4UNames = Theory_Data
44 fun merge ([], []) = []
45 | merge _ = error "Used names not empty during merge"
48 structure HOL4Dump = Theory_Data
50 type T = string * string * string list
51 val empty = ("","",[])
53 fun merge (("","",[]),("","",[])) = ("","",[])
54 | merge _ = error "Dump data not empty during merge"
57 structure HOL4Moves = Theory_Data
59 type T = string Symtab.table
60 val empty = Symtab.empty
62 fun merge data = Symtab.merge (K true) data
65 structure HOL4Imports = Theory_Data
67 type T = string Symtab.table
68 val empty = Symtab.empty
70 fun merge data = Symtab.merge (K true) data
73 fun get_segment2 thyname thy =
74 Symtab.lookup (HOL4Imports.get thy) thyname
76 fun set_segment thyname segname thy =
78 val imps = HOL4Imports.get thy
79 val imps' = Symtab.update_new (thyname,segname) imps
81 HOL4Imports.put imps' thy
84 structure HOL4CMoves = Theory_Data
86 type T = string Symtab.table
87 val empty = Symtab.empty
89 fun merge data = Symtab.merge (K true) data
92 structure HOL4Maps = Theory_Data
94 type T = string option StringPair.table
95 val empty = StringPair.empty
97 fun merge data = StringPair.merge (K true) data
100 structure HOL4Thms = Theory_Data
102 type T = holthm StringPair.table
103 val empty = StringPair.empty
105 fun merge data = StringPair.merge (K true) data
108 structure HOL4ConstMaps = Theory_Data
110 type T = (bool * string * typ option) StringPair.table
111 val empty = StringPair.empty
113 fun merge data = StringPair.merge (K true) data
116 structure HOL4Rename = Theory_Data
118 type T = string StringPair.table
119 val empty = StringPair.empty
121 fun merge data = StringPair.merge (K true) data
124 structure HOL4DefMaps = Theory_Data
126 type T = string StringPair.table
127 val empty = StringPair.empty
129 fun merge data = StringPair.merge (K true) data
132 structure HOL4TypeMaps = Theory_Data
134 type T = (bool * string) StringPair.table
135 val empty = StringPair.empty
137 fun merge data = StringPair.merge (K true) data
140 structure HOL4Pending = Theory_Data
142 type T = ((term * term) list * thm) StringPair.table
143 val empty = StringPair.empty
145 fun merge data = StringPair.merge (K true) data
148 structure HOL4Rewrites = Theory_Data
153 val merge = Thm.merge_thms
156 val hol4_debug = Unsynchronized.ref false
157 fun message s = if !hol4_debug then writeln s else ()
159 fun add_hol4_rewrite (Context.Theory thy, th) =
161 val _ = message "Adding HOL4 rewrite"
162 val th1 = th RS @{thm eq_reflection}
163 val current_rews = HOL4Rewrites.get thy
164 val new_rews = insert Thm.eq_thm th1 current_rews
165 val updated_thy = HOL4Rewrites.put new_rews thy
167 (Context.Theory updated_thy,th1)
169 | add_hol4_rewrite (context, th) = (context, th RS eq_reflection);
171 fun ignore_hol4 bthy bthm thy =
173 val _ = message ("Ignoring " ^ bthy ^ "." ^ bthm)
174 val curmaps = HOL4Maps.get thy
175 val newmaps = StringPair.update_new ((bthy,bthm),NONE) curmaps
176 val upd_thy = HOL4Maps.put newmaps thy
181 val opt_get_output_thy = #2 o HOL4Dump.get
183 fun get_output_thy thy =
184 case #2 (HOL4Dump.get thy) of
185 "" => error "No theory file being output"
188 val get_output_dir = #1 o HOL4Dump.get
190 fun add_hol4_move bef aft thy =
192 val curmoves = HOL4Moves.get thy
193 val newmoves = Symtab.update_new (bef, aft) curmoves
195 HOL4Moves.put newmoves thy
198 fun get_hol4_move bef thy =
199 Symtab.lookup (HOL4Moves.get thy) bef
201 fun follow_name thmname thy =
203 val moves = HOL4Moves.get thy
205 case Symtab.lookup moves thmname of
212 fun add_hol4_cmove bef aft thy =
214 val curmoves = HOL4CMoves.get thy
215 val newmoves = Symtab.update_new (bef, aft) curmoves
217 HOL4CMoves.put newmoves thy
220 fun get_hol4_cmove bef thy =
221 Symtab.lookup (HOL4CMoves.get thy) bef
223 fun follow_cname thmname thy =
225 val moves = HOL4CMoves.get thy
227 case Symtab.lookup moves thmname of
234 fun add_hol4_mapping bthy bthm isathm thy =
236 (* val _ = writeln ("Before follow_name: "^isathm) *)
237 val isathm = follow_name isathm thy
238 (* val _ = writeln ("Adding theorem map: " ^ bthy ^ "." ^ bthm ^ " --> " ^ isathm)*)
239 val curmaps = HOL4Maps.get thy
240 val newmaps = StringPair.update_new ((bthy,bthm),SOME isathm) curmaps
241 val upd_thy = HOL4Maps.put newmaps thy
246 fun get_hol4_type_mapping bthy tycon thy =
248 val maps = HOL4TypeMaps.get thy
250 StringPair.lookup maps (bthy,tycon)
253 fun get_hol4_mapping bthy bthm thy =
255 val curmaps = HOL4Maps.get thy
257 StringPair.lookup curmaps (bthy,bthm)
260 fun add_hol4_const_mapping bthy bconst internal isaconst thy =
262 val thy = case opt_get_output_thy thy of
264 | output_thy => if internal
265 then add_hol4_cmove (Sign.full_bname thy bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy
267 val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
268 val curmaps = HOL4ConstMaps.get thy
269 val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,NONE)) curmaps
270 val upd_thy = HOL4ConstMaps.put newmaps thy
275 fun add_hol4_const_renaming bthy bconst newname thy =
277 val currens = HOL4Rename.get thy
278 val _ = message ("Adding renaming " ^ bthy ^ "." ^ bconst ^ " -> " ^ newname)
279 val newrens = StringPair.update_new ((bthy,bconst),newname) currens
280 val upd_thy = HOL4Rename.put newrens thy
285 fun get_hol4_const_renaming bthy bconst thy =
287 val currens = HOL4Rename.get thy
289 StringPair.lookup currens (bthy,bconst)
292 fun get_hol4_const_mapping bthy bconst thy =
294 val bconst = case get_hol4_const_renaming bthy bconst thy of
297 val maps = HOL4ConstMaps.get thy
299 StringPair.lookup maps (bthy,bconst)
302 fun add_hol4_const_wt_mapping bthy bconst internal isaconst typ thy =
304 val thy = case opt_get_output_thy thy of
306 | output_thy => if internal
307 then add_hol4_cmove (Sign.full_bname thy bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy
309 val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
310 val curmaps = HOL4ConstMaps.get thy
311 val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,SOME typ)) curmaps
312 val upd_thy = HOL4ConstMaps.put newmaps thy
317 fun add_hol4_type_mapping bthy bconst internal isaconst thy =
319 val curmaps = HOL4TypeMaps.get thy
320 val _ = writeln ("Adding tmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
321 val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst)) curmaps
322 (* FIXME avoid handle x *)
323 handle x => let val (internal, isaconst') = the (StringPair.lookup curmaps (bthy, bconst)) in
324 warning ("couldn't map type "^bthy^"."^bconst^" to "^isaconst^": already mapped to "^isaconst'); raise x end
325 val upd_thy = HOL4TypeMaps.put newmaps thy
330 fun add_hol4_pending bthy bthm hth thy =
332 val thmname = Sign.full_bname thy bthm
333 val _ = message ("Add pending " ^ bthy ^ "." ^ bthm)
334 val curpend = HOL4Pending.get thy
335 val newpend = StringPair.update_new ((bthy,bthm),hth) curpend
336 val upd_thy = HOL4Pending.put newpend thy
337 val thy' = case opt_get_output_thy upd_thy of
338 "" => add_hol4_mapping bthy bthm thmname upd_thy
341 val new_thmname = output_thy ^ "." ^ bthy ^ "." ^ bthm
343 upd_thy |> add_hol4_move thmname new_thmname
344 |> add_hol4_mapping bthy bthm new_thmname
350 fun get_hol4_theorem thyname thmname thy =
352 val isathms = HOL4Thms.get thy
354 StringPair.lookup isathms (thyname,thmname)
357 fun add_hol4_theorem thyname thmname hth =
359 val _ = message ("Adding HOL4 theorem " ^ thyname ^ "." ^ thmname)
361 HOL4Thms.map (StringPair.update_new ((thyname, thmname), hth))
364 fun export_hol4_pending thy =
366 val rews = HOL4Rewrites.get thy;
367 val pending = HOL4Pending.get thy;
368 fun process ((bthy,bthm), hth as (_,thm)) thy =
370 val thm1 = rewrite_rule (map (Thm.transfer thy) rews) (Thm.transfer thy thm);
371 val thm2 = Drule.export_without_context thm1;
374 |> Global_Theory.store_thm (Binding.name bthm, thm2)
376 |> add_hol4_theorem bthy bthm hth
380 |> StringPair.fold process pending
381 |> HOL4Pending.put StringPair.empty
384 fun setup_dump (dir,thyname) thy =
385 HOL4Dump.put (dir,thyname,["(* AUTOMATICALLY GENERATED, DO NOT EDIT! *)"]) thy
387 fun add_dump str thy =
389 val (dir,thyname,curdump) = HOL4Dump.get thy
391 HOL4Dump.put (dir,thyname,str::curdump) thy
396 val (dir,thyname,dumpdata) = HOL4Dump.get thy
397 val os = TextIO.openOut (OS.Path.joinDirFile {dir=dir,
398 file=thyname ^ ".thy"})
399 val _ = app (fn s => TextIO.output(os,s ^ "\n\n")) (rev dumpdata)
400 val _ = TextIO.closeOut os
402 HOL4Dump.put ("","",[]) thy
405 fun set_generating_thy thyname thy =
406 case HOL4DefThy.get thy of
407 NoImport => HOL4DefThy.put (Generating thyname) thy
408 | _ => error "Import already in progess"
410 fun set_replaying_thy thyname thy =
411 case HOL4DefThy.get thy of
412 NoImport => HOL4DefThy.put (Replaying thyname) thy
413 | _ => error "Import already in progess"
415 fun clear_import_thy thy =
416 case HOL4DefThy.get thy of
417 NoImport => error "No import in progress"
418 | _ => HOL4DefThy.put NoImport thy
420 fun get_generating_thy thy =
421 case HOL4DefThy.get thy of
422 Generating thyname => thyname
423 | _ => error "No theory being generated"
425 fun get_replaying_thy thy =
426 case HOL4DefThy.get thy of
427 Replaying thyname => thyname
428 | _ => error "No theory being replayed"
430 fun get_import_thy thy =
431 case HOL4DefThy.get thy of
432 Replaying thyname => thyname
433 | Generating thyname => thyname
434 | _ => error "No theory being imported"
436 fun should_ignore thyname thy thmname =
437 case get_hol4_mapping thyname thmname thy of
443 fun quote s = "\"" ^ s ^ "\""
445 | F (#"\\" :: cs) = patch #"\\" cs
446 | F (#"\"" :: cs) = patch #"\"" cs
447 | F (c :: cs) = c :: F cs
448 and patch c rest = #"\\" :: c :: F rest
450 quote o String.implode o F o String.explode
453 fun dump_import_thy thyname thy =
455 val output_dir = get_output_dir thy
456 val output_thy = get_output_thy thy
457 val input_thy = Context.theory_name thy
458 val import_segment = get_import_segment thy
459 val os = TextIO.openOut (OS.Path.joinDirFile {dir=output_dir,
460 file=thyname ^ ".imp"})
461 fun out s = TextIO.output(os,s)
462 val (ignored, mapped) = StringPair.fold
463 (fn ((bthy, bthm), v) => fn (ign, map) =>
466 of NONE => (bthm :: ign, map)
467 | SOME w => (ign, (bthm, w) :: map)
468 else (ign, map)) (HOL4Maps.get thy) ([],[]);
469 fun mk init = StringPair.fold
470 (fn ((bthy, bthm), v) => if bthy = thyname then cons (bthm, v) else I) init [];
471 val constmaps = mk (HOL4ConstMaps.get thy);
472 val constrenames = mk (HOL4Rename.get thy);
473 val typemaps = mk (HOL4TypeMaps.get thy);
474 val defmaps = mk (HOL4DefMaps.get thy);
475 fun new_name internal isa =
479 val paths = Long_Name.explode isa
480 val i = drop (length paths - 2) paths
483 [seg,con] => output_thy ^ "." ^ seg ^ "." ^ con
484 | _ => error "hol4rews.dump internal error"
489 val _ = out "import\n\n"
491 val _ = out ("import_segment " ^ trans_string import_segment ^ "\n\n")
492 val _ = if null defmaps
495 val _ = app (fn (hol,isa) =>
496 out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string isa))) defmaps
497 val _ = if null defmaps
501 val _ = if null typemaps
504 val _ = app (fn (hol,(internal,isa)) =>
505 out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (new_name internal isa)))) typemaps
506 val _ = if null typemaps
510 val _ = if null constmaps
512 else out "const_maps"
513 val _ = app (fn (hol,(internal,isa,opt_ty)) =>
514 (out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (follow_cname isa thy)));
516 SOME ty => out (" :: \"" ^ Syntax.string_of_typ_global thy ty ^ "\"")
517 | NONE => ())) constmaps
518 val _ = if null constmaps
522 val _ = if null constrenames
524 else out "const_renames"
525 val _ = app (fn (old,new) =>
526 out ("\n " ^ (trans_string old) ^ " > " ^ (trans_string new))) constrenames
527 val _ = if null constrenames
531 fun gen2replay in_thy out_thy s =
533 val ss = Long_Name.explode s
535 if (hd ss = in_thy) then
536 Long_Name.implode (out_thy::(tl ss))
541 val _ = if null mapped
544 val _ = app (fn (hol,isa) => out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (gen2replay input_thy output_thy isa)))) mapped
545 val _ = if null mapped
549 val _ = if null ignored
551 else out "ignore_thms"
552 val _ = app (fn ign => out ("\n " ^ (trans_string ign))) ignored
553 val _ = if null ignored
558 val _ = TextIO.closeOut os
563 fun set_used_names names thy =
565 val unames = HOL4UNames.get thy
568 [] => HOL4UNames.put names thy
569 | _ => error "hol4rews.set_used_names called on initialized data!"
572 val clear_used_names = HOL4UNames.put [];
574 fun get_defmap thyname const thy =
576 val maps = HOL4DefMaps.get thy
578 StringPair.lookup maps (thyname,const)
581 fun add_defmap thyname const defname thy =
583 val _ = message ("Adding defmap " ^ thyname ^ "." ^ const ^ " --> " ^ defname)
584 val maps = HOL4DefMaps.get thy
585 val maps' = StringPair.update_new ((thyname,const),defname) maps
586 val thy' = HOL4DefMaps.put maps' thy
591 fun get_defname thyname name thy =
593 val maps = HOL4DefMaps.get thy
594 fun F dname = (dname,add_defmap thyname name dname thy)
596 case StringPair.lookup maps (thyname,name) of
597 SOME thmname => (thmname,thy)
600 val used = HOL4UNames.get thy
601 val defname = Thm.def_name name
602 val pdefname = name ^ "_primdef"
604 if not (member (op =) used defname)
605 then F defname (* name_def *)
606 else if not (member (op =) used pdefname)
607 then F pdefname (* name_primdef *)
608 else F (Name.variant used pdefname) (* last resort *)
613 fun handle_meta [x as Ast.Appl [Ast.Appl [Ast.Constant "_constrain", Ast.Constant @{const_syntax "=="}, _],_,_]] = x
614 | handle_meta [x as Ast.Appl [Ast.Appl [Ast.Constant "_constrain", Ast.Constant @{const_syntax all}, _],_]] = x
615 | handle_meta [x as Ast.Appl [Ast.Appl [Ast.Constant "_constrain", Ast.Constant @{const_syntax "==>"}, _],_,_]] = x
616 | handle_meta [x] = Ast.Appl [Ast.Constant @{const_syntax Trueprop}, x]
617 | handle_meta _ = error "hol4rews error: Trueprop not applied to single argument"
619 val smarter_trueprop_parsing = [(@{const_syntax Trueprop},handle_meta)]
623 fun initial_maps thy =
624 thy |> add_hol4_type_mapping "min" "bool" false @{type_name bool}
625 |> add_hol4_type_mapping "min" "fun" false "fun"
626 |> add_hol4_type_mapping "min" "ind" false @{type_name ind}
627 |> add_hol4_const_mapping "min" "=" false @{const_name HOL.eq}
628 |> add_hol4_const_mapping "min" "==>" false @{const_name HOL.implies}
629 |> add_hol4_const_mapping "min" "@" false @{const_name "Eps"}
633 Attrib.setup @{binding hol4rew} (Scan.succeed add_hol4_rewrite) "HOL4 rewrite rule"