1 (* Title: HOL/Import/hol4rews.ML
3 Author: Sebastian Skalberg (TU Muenchen)
6 structure StringPair = TableFun(type key = string * string val ord = prod_ord string_ord string_ord);
8 type holthm = (term * term) list * thm
10 datatype ImportStatus =
12 | Generating of string
15 structure HOL4DefThyArgs: THEORY_DATA_ARGS =
17 val name = "HOL4/import_status"
22 fun merge _ (NoImport,NoImport) = NoImport
23 | merge _ _ = (warning "Import status set during merge"; NoImport)
24 fun print sg import_status =
25 Pretty.writeln (Pretty.str (case import_status of NoImport => "No current import" | Generating thyname => ("Generating " ^ thyname) | Replaying thyname => ("Replaying " ^ thyname)))
28 structure HOL4DefThy = TheoryDataFun(HOL4DefThyArgs);
30 structure ImportSegmentArgs: THEORY_DATA_ARGS =
32 val name = "HOL4/import_segment"
37 fun merge _ ("",arg) = arg
38 | merge _ (arg,"") = arg
42 else error "Trying to merge two different import segments"
43 fun print sg import_segment =
44 Pretty.writeln (Pretty.str ("Import segment: " ^ import_segment))
47 structure ImportSegment = TheoryDataFun(ImportSegmentArgs);
49 val get_import_segment = ImportSegment.get
50 val set_import_segment = ImportSegment.put
52 structure HOL4UNamesArgs: THEORY_DATA_ARGS =
54 val name = "HOL4/used_names"
59 fun merge _ ([],[]) = []
60 | merge _ _ = error "Used names not empty during merge"
61 fun print sg used_names =
62 Pretty.writeln (Pretty.str "Printing of HOL4/used_names Not implemented")
65 structure HOL4UNames = TheoryDataFun(HOL4UNamesArgs);
67 structure HOL4DumpArgs: THEORY_DATA_ARGS =
69 val name = "HOL4/dump_data"
70 type T = string * string * string list
71 val empty = ("","",[])
74 fun merge _ (("","",[]),("","",[])) = ("","",[])
75 | merge _ _ = error "Dump data not empty during merge"
76 fun print sg dump_data =
77 Pretty.writeln (Pretty.str "Printing of HOL4/dump_data Not implemented")
80 structure HOL4Dump = TheoryDataFun(HOL4DumpArgs);
82 structure HOL4MovesArgs: THEORY_DATA_ARGS =
84 val name = "HOL4/moves"
85 type T = string Symtab.table
86 val empty = Symtab.empty
89 fun merge _ : T * T -> T = Symtab.merge (K true)
91 Pretty.writeln (Pretty.big_list "HOL4 moves:"
92 (Symtab.foldl (fn (xl,(bef,aft)) => (Pretty.str (bef ^ " --> " ^ aft)::xl)) ([],tab)))
95 structure HOL4Moves = TheoryDataFun(HOL4MovesArgs);
97 structure HOL4ImportsArgs: THEORY_DATA_ARGS =
99 val name = "HOL4/imports"
100 type T = string Symtab.table
101 val empty = Symtab.empty
104 fun merge _ : T * T -> T = Symtab.merge (K true)
106 Pretty.writeln (Pretty.big_list "HOL4 imports:"
107 (Symtab.foldl (fn (xl,(thyname,segname)) => (Pretty.str (thyname ^ " imported from segment " ^ segname)::xl)) ([],tab)))
110 structure HOL4Imports = TheoryDataFun(HOL4ImportsArgs);
112 fun get_segment2 thyname thy =
113 Symtab.lookup (HOL4Imports.get thy) thyname
115 fun set_segment thyname segname thy =
117 val imps = HOL4Imports.get thy
118 val imps' = Symtab.update_new (thyname,segname) imps
120 HOL4Imports.put imps' thy
123 structure HOL4CMovesArgs: THEORY_DATA_ARGS =
125 val name = "HOL4/constant_moves"
126 type T = string Symtab.table
127 val empty = Symtab.empty
130 fun merge _ : T * T -> T = Symtab.merge (K true)
132 Pretty.writeln (Pretty.big_list "HOL4 constant moves:"
133 (Symtab.foldl (fn (xl,(bef,aft)) => (Pretty.str (bef ^ " --> " ^ aft)::xl)) ([],tab)))
136 structure HOL4CMoves = TheoryDataFun(HOL4CMovesArgs);
138 structure HOL4MapsArgs: THEORY_DATA_ARGS =
140 val name = "HOL4/mappings"
141 type T = (string option) StringPair.table
142 val empty = StringPair.empty
145 fun merge _ : T * T -> T = StringPair.merge (K true)
147 Pretty.writeln (Pretty.big_list "HOL4 mappings:"
148 (StringPair.foldl (fn (xl,((bthy,bthm),isathm)) => (Pretty.str (bthy ^ "." ^ bthm ^ (case isathm of SOME th => " --> " ^ th | NONE => "IGNORED"))::xl)) ([],tab)))
151 structure HOL4Maps = TheoryDataFun(HOL4MapsArgs);
153 structure HOL4ThmsArgs: THEORY_DATA_ARGS =
155 val name = "HOL4/theorems"
156 type T = holthm StringPair.table
157 val empty = StringPair.empty
160 fun merge _ : T * T -> T = StringPair.merge (K true)
162 Pretty.writeln (Pretty.big_list "HOL4 mappings:"
163 (StringPair.foldl (fn (xl,((bthy,bthm),(_,thm))) => (Pretty.str (bthy ^ "." ^ bthm ^ ":")::(Display.pretty_thm thm)::xl)) ([],tab)))
166 structure HOL4Thms = TheoryDataFun(HOL4ThmsArgs);
168 structure HOL4ConstMapsArgs: THEORY_DATA_ARGS =
170 val name = "HOL4/constmappings"
171 type T = (bool * string * typ option) StringPair.table
172 val empty = StringPair.empty
175 fun merge _ : T * T -> T = StringPair.merge (K true)
177 Pretty.writeln (Pretty.big_list "HOL4 constant mappings:"
178 (StringPair.foldl (fn (xl,((bthy,bconst),(internal,isaconst,_))) => (Pretty.str (bthy ^ "." ^ bconst ^ " --> " ^ isaconst ^ (if internal then " (*)" else ""))::xl)) ([],tab)))
181 structure HOL4ConstMaps = TheoryDataFun(HOL4ConstMapsArgs);
183 structure HOL4RenameArgs: THEORY_DATA_ARGS =
185 val name = "HOL4/renamings"
186 type T = string StringPair.table
187 val empty = StringPair.empty
190 fun merge _ : T * T -> T = StringPair.merge (K true)
192 Pretty.writeln (Pretty.big_list "HOL4 constant renamings:"
193 (StringPair.foldl (fn (xl,((bthy,bconst),newname)) => (Pretty.str (bthy ^ "." ^ bconst ^ " --> " ^ newname)::xl)) ([],tab)))
196 structure HOL4Rename = TheoryDataFun(HOL4RenameArgs);
198 structure HOL4DefMapsArgs: THEORY_DATA_ARGS =
200 val name = "HOL4/def_maps"
201 type T = string StringPair.table
202 val empty = StringPair.empty
205 fun merge _ : T * T -> T = StringPair.merge (K true)
207 Pretty.writeln (Pretty.big_list "HOL4 constant definitions:"
208 (StringPair.foldl (fn (xl,((bthy,bconst),newname)) => (Pretty.str (bthy ^ "." ^ bconst ^ ": " ^ newname)::xl)) ([],tab)))
211 structure HOL4DefMaps = TheoryDataFun(HOL4DefMapsArgs);
213 structure HOL4TypeMapsArgs: THEORY_DATA_ARGS =
215 val name = "HOL4/typemappings"
216 type T = (bool * string) StringPair.table
217 val empty = StringPair.empty
220 fun merge _ : T * T -> T = StringPair.merge (K true)
222 Pretty.writeln (Pretty.big_list "HOL4 type mappings:"
223 (StringPair.foldl (fn (xl,((bthy,bconst),(internal,isaconst))) => (Pretty.str (bthy ^ "." ^ bconst ^ " --> " ^ isaconst ^ (if internal then " (*)" else ""))::xl)) ([],tab)))
226 structure HOL4TypeMaps = TheoryDataFun(HOL4TypeMapsArgs);
228 structure HOL4PendingArgs: THEORY_DATA_ARGS =
230 val name = "HOL4/pending"
231 type T = ((term * term) list * thm) StringPair.table
232 val empty = StringPair.empty
235 fun merge _ : T * T -> T = StringPair.merge (K true)
237 Pretty.writeln (Pretty.big_list "HOL4 pending theorems:"
238 (StringPair.foldl (fn (xl,((bthy,bthm),(_,th))) => (Pretty.chunks [Pretty.str (bthy ^ "." ^ bthm ^ ":"),Display.pretty_thm th]::xl)) ([],tab)))
241 structure HOL4Pending = TheoryDataFun(HOL4PendingArgs);
243 structure HOL4RewritesArgs: THEORY_DATA_ARGS =
245 val name = "HOL4/rewrites"
250 fun merge _ = Library.gen_union Thm.eq_thm
252 Pretty.writeln (Pretty.big_list "HOL4 rewrite rules:"
253 (map Display.pretty_thm thms))
256 structure HOL4Rewrites = TheoryDataFun(HOL4RewritesArgs);
258 val hol4_debug = ref false
259 fun message s = if !hol4_debug then writeln s else ()
261 fun add_hol4_rewrite (context, th) =
263 val thy = Context.the_theory context;
264 val _ = message "Adding HOL4 rewrite"
265 val th1 = th RS eq_reflection
266 val current_rews = HOL4Rewrites.get thy
267 val new_rews = insert Thm.eq_thm th1 current_rews
268 val updated_thy = HOL4Rewrites.put new_rews thy
270 (Context.Theory updated_thy,th1)
273 fun ignore_hol4 bthy bthm thy =
275 val _ = message ("Ignoring " ^ bthy ^ "." ^ bthm)
276 val curmaps = HOL4Maps.get thy
277 val newmaps = StringPair.update_new ((bthy,bthm),NONE) curmaps
278 val upd_thy = HOL4Maps.put newmaps thy
283 val opt_get_output_thy = #2 o HOL4Dump.get
285 fun get_output_thy thy =
286 case #2 (HOL4Dump.get thy) of
287 "" => error "No theory file being output"
290 val get_output_dir = #1 o HOL4Dump.get
292 fun add_hol4_move bef aft thy =
294 val curmoves = HOL4Moves.get thy
295 val newmoves = Symtab.update_new (bef, aft) curmoves
297 HOL4Moves.put newmoves thy
300 fun get_hol4_move bef thy =
301 Symtab.lookup (HOL4Moves.get thy) bef
303 fun follow_name thmname thy =
305 val moves = HOL4Moves.get thy
307 case Symtab.lookup moves thmname of
314 fun add_hol4_cmove bef aft thy =
316 val curmoves = HOL4CMoves.get thy
317 val newmoves = Symtab.update_new (bef, aft) curmoves
319 HOL4CMoves.put newmoves thy
322 fun get_hol4_cmove bef thy =
323 Symtab.lookup (HOL4CMoves.get thy) bef
325 fun follow_cname thmname thy =
327 val moves = HOL4CMoves.get thy
329 case Symtab.lookup moves thmname of
336 fun add_hol4_mapping bthy bthm isathm thy =
338 (* val _ = writeln ("Before follow_name: "^isathm) *)
339 val isathm = follow_name isathm thy
340 (* val _ = writeln ("Adding theorem map: " ^ bthy ^ "." ^ bthm ^ " --> " ^ isathm)*)
341 val curmaps = HOL4Maps.get thy
342 val newmaps = StringPair.update_new ((bthy,bthm),SOME isathm) curmaps
343 val upd_thy = HOL4Maps.put newmaps thy
348 fun get_hol4_type_mapping bthy tycon thy =
350 val maps = HOL4TypeMaps.get thy
352 StringPair.lookup maps (bthy,tycon)
355 fun get_hol4_mapping bthy bthm thy =
357 val curmaps = HOL4Maps.get thy
359 StringPair.lookup curmaps (bthy,bthm)
362 fun add_hol4_const_mapping bthy bconst internal isaconst thy =
364 val thy = case opt_get_output_thy thy of
366 | output_thy => if internal
367 then add_hol4_cmove (Sign.full_name (sign_of thy) bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy
369 val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
370 val curmaps = HOL4ConstMaps.get thy
371 val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,NONE)) curmaps
372 val upd_thy = HOL4ConstMaps.put newmaps thy
377 fun add_hol4_const_renaming bthy bconst newname thy =
379 val currens = HOL4Rename.get thy
380 val _ = message ("Adding renaming " ^ bthy ^ "." ^ bconst ^ " -> " ^ newname)
381 val newrens = StringPair.update_new ((bthy,bconst),newname) currens
382 val upd_thy = HOL4Rename.put newrens thy
387 fun get_hol4_const_renaming bthy bconst thy =
389 val currens = HOL4Rename.get thy
391 StringPair.lookup currens (bthy,bconst)
394 fun get_hol4_const_mapping bthy bconst thy =
396 val bconst = case get_hol4_const_renaming bthy bconst thy of
399 val maps = HOL4ConstMaps.get thy
401 StringPair.lookup maps (bthy,bconst)
404 fun add_hol4_const_wt_mapping bthy bconst internal isaconst typ thy =
406 val thy = case opt_get_output_thy thy of
408 | output_thy => if internal
409 then add_hol4_cmove (Sign.full_name (sign_of thy) bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy
411 val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
412 val curmaps = HOL4ConstMaps.get thy
413 val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,SOME typ)) curmaps
414 val upd_thy = HOL4ConstMaps.put newmaps thy
419 fun add_hol4_type_mapping bthy bconst internal isaconst thy =
421 val curmaps = HOL4TypeMaps.get thy
422 val _ = writeln ("Adding tmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
423 val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst)) curmaps
424 handle x => let val (internal, isaconst') = the (StringPair.lookup curmaps (bthy, bconst)) in
425 warning ("couldn't map type "^bthy^"."^bconst^" to "^isaconst^": already mapped to "^isaconst'); raise x end
426 val upd_thy = HOL4TypeMaps.put newmaps thy
431 fun add_hol4_pending bthy bthm hth thy =
433 val thmname = Sign.full_name (sign_of thy) bthm
434 val _ = message ("Add pending " ^ bthy ^ "." ^ bthm)
435 val curpend = HOL4Pending.get thy
436 val newpend = StringPair.update_new ((bthy,bthm),hth) curpend
437 val upd_thy = HOL4Pending.put newpend thy
438 val thy' = case opt_get_output_thy upd_thy of
439 "" => add_hol4_mapping bthy bthm thmname upd_thy
442 val new_thmname = output_thy ^ "." ^ bthy ^ "." ^ bthm
444 upd_thy |> add_hol4_move thmname new_thmname
445 |> add_hol4_mapping bthy bthm new_thmname
451 fun get_hol4_theorem thyname thmname thy =
453 val isathms = HOL4Thms.get thy
455 StringPair.lookup isathms (thyname,thmname)
458 fun add_hol4_theorem thyname thmname hth thy =
460 val _ = message ("Adding HOL4 theorem " ^ thyname ^ "." ^ thmname)
461 val isathms = HOL4Thms.get thy
462 val isathms' = StringPair.update_new ((thyname,thmname),hth) isathms
463 val thy' = HOL4Thms.put isathms' thy
468 fun export_hol4_pending thy =
470 val rews = HOL4Rewrites.get thy
471 val outthy = get_output_thy thy
472 fun process (thy,((bthy,bthm),hth as (_,thm))) =
475 val thm1 = rewrite_rule (map (Thm.transfer sg) rews) (Thm.transfer sg thm)
476 val thm2 = standard thm1
477 val thy2 = PureThy.store_thm ((bthm, thm2), []) thy |> snd
478 val thy5 = add_hol4_theorem bthy bthm hth thy2
483 val pending = HOL4Pending.get thy
484 val thy1 = StringPair.foldl process (thy,pending)
485 val thy2 = HOL4Pending.put (StringPair.empty) thy1
490 fun setup_dump (dir,thyname) thy =
491 HOL4Dump.put (dir,thyname,["(* AUTOMATICALLY GENERATED, DO NOT EDIT! *)"]) thy
493 fun add_dump str thy =
495 val (dir,thyname,curdump) = HOL4Dump.get thy
497 HOL4Dump.put (dir,thyname,str::curdump) thy
502 val (dir,thyname,dumpdata) = HOL4Dump.get thy
503 val os = TextIO.openOut (OS.Path.joinDirFile {dir=dir,
504 file=thyname ^ ".thy"})
505 val _ = app (fn s => TextIO.output(os,s ^ "\n\n")) (rev dumpdata)
506 val _ = TextIO.closeOut os
508 HOL4Dump.put ("","",[]) thy
511 fun set_generating_thy thyname thy =
512 case HOL4DefThy.get thy of
513 NoImport => HOL4DefThy.put (Generating thyname) thy
514 | _ => error "Import already in progess"
516 fun set_replaying_thy thyname thy =
517 case HOL4DefThy.get thy of
518 NoImport => HOL4DefThy.put (Replaying thyname) thy
519 | _ => error "Import already in progess"
521 fun clear_import_thy thy =
522 case HOL4DefThy.get thy of
523 NoImport => error "No import in progress"
524 | _ => HOL4DefThy.put NoImport thy
526 fun get_generating_thy thy =
527 case HOL4DefThy.get thy of
528 Generating thyname => thyname
529 | _ => error "No theory being generated"
531 fun get_replaying_thy thy =
532 case HOL4DefThy.get thy of
533 Replaying thyname => thyname
534 | _ => error "No theory being replayed"
536 fun get_import_thy thy =
537 case HOL4DefThy.get thy of
538 Replaying thyname => thyname
539 | Generating thyname => thyname
540 | _ => error "No theory being imported"
542 fun should_ignore thyname thy thmname =
543 case get_hol4_mapping thyname thmname thy of
549 fun quote s = "\"" ^ s ^ "\""
551 | F (#"\\" :: cs) = patch #"\\" cs
552 | F (#"\"" :: cs) = patch #"\"" cs
553 | F (c :: cs) = c :: F cs
554 and patch c rest = #"\\" :: c :: F rest
556 quote o String.implode o F o String.explode
559 fun dump_import_thy thyname thy =
561 val output_dir = get_output_dir thy
562 val output_thy = get_output_thy thy
563 val input_thy = Context.theory_name thy
564 val import_segment = get_import_segment thy
566 val os = TextIO.openOut (OS.Path.joinDirFile {dir=output_dir,
567 file=thyname ^ ".imp"})
568 fun out s = TextIO.output(os,s)
569 val (ignored,mapped) =
570 StringPair.foldl (fn ((ign,map),((bthy,bthm),v)) =>
573 NONE => (bthm::ign,map)
574 | SOME w => (ign,(bthm,w)::map))
576 (([],[]),HOL4Maps.get thy)
578 StringPair.foldl (fn (map,((bthy,bthm),v)) =>
582 ([],HOL4ConstMaps.get thy)
585 StringPair.foldl (fn (map,((bthy,bthm),v)) =>
589 ([],HOL4Rename.get thy)
592 StringPair.foldl (fn (map,((bthy,bthm),v)) =>
596 ([],HOL4TypeMaps.get thy)
599 StringPair.foldl (fn (map,((bthy,bthm),v)) =>
603 ([],HOL4DefMaps.get thy)
605 fun new_name internal isa =
609 val paths = NameSpace.unpack isa
610 val i = Library.drop(length paths - 2,paths)
613 [seg,con] => output_thy ^ "." ^ seg ^ "." ^ con
614 | _ => error "hol4rews.dump internal error"
619 val _ = out "import\n\n"
621 val _ = out ("import_segment " ^ trans_string import_segment ^ "\n\n")
622 val _ = if null defmaps
625 val _ = app (fn (hol,isa) =>
626 out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string isa))) defmaps
627 val _ = if null defmaps
631 val _ = if null typemaps
634 val _ = app (fn (hol,(internal,isa)) =>
635 out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (new_name internal isa)))) typemaps
636 val _ = if null typemaps
640 val _ = if null constmaps
642 else out "const_maps"
643 val _ = app (fn (hol,(internal,isa,opt_ty)) =>
644 (out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (follow_cname isa thy)));
646 SOME ty => out (" :: \"" ^ (string_of_ctyp (ctyp_of sg ty)) ^ "\"")
647 | NONE => ())) constmaps
648 val _ = if null constmaps
652 val _ = if null constrenames
654 else out "const_renames"
655 val _ = app (fn (old,new) =>
656 out ("\n " ^ (trans_string old) ^ " > " ^ (trans_string new))) constrenames
657 val _ = if null constrenames
661 fun gen2replay in_thy out_thy s =
663 val ss = NameSpace.unpack s
665 if (hd ss = in_thy) then
666 NameSpace.pack (out_thy::(tl ss))
671 val _ = if null mapped
674 val _ = app (fn (hol,isa) => out ("\n " ^ (trans_string hol) ^ " > " ^ (trans_string (gen2replay input_thy output_thy isa)))) mapped
675 val _ = if null mapped
679 val _ = if null ignored
681 else out "ignore_thms"
682 val _ = app (fn ign => out ("\n " ^ (trans_string ign))) ignored
683 val _ = if null ignored
688 val _ = TextIO.closeOut os
693 fun set_used_names names thy =
695 val unames = HOL4UNames.get thy
698 [] => HOL4UNames.put names thy
699 | _ => error "hol4rews.set_used_names called on initialized data!"
702 val clear_used_names = HOL4UNames.put HOL4UNamesArgs.empty
704 fun get_defmap thyname const thy =
706 val maps = HOL4DefMaps.get thy
708 StringPair.lookup maps (thyname,const)
711 fun add_defmap thyname const defname thy =
713 val _ = message ("Adding defmap " ^ thyname ^ "." ^ const ^ " --> " ^ defname)
714 val maps = HOL4DefMaps.get thy
715 val maps' = StringPair.update_new ((thyname,const),defname) maps
716 val thy' = HOL4DefMaps.put maps' thy
721 fun get_defname thyname name thy =
723 val maps = HOL4DefMaps.get thy
724 fun F dname = (dname,add_defmap thyname name dname thy)
726 case StringPair.lookup maps (thyname,name) of
727 SOME thmname => (thmname,thy)
730 val used = HOL4UNames.get thy
731 val defname = def_name name
732 val pdefname = name ^ "_primdef"
734 if not (defname mem used)
735 then F defname (* name_def *)
736 else if not (pdefname mem used)
737 then F pdefname (* name_primdef *)
738 else F (Name.variant used pdefname) (* last resort *)
743 fun handle_meta [x as Appl[Appl[Constant "_constrain", Constant "==", _],_,_]] = x
744 | handle_meta [x as Appl[Appl[Constant "_constrain", Constant "all", _],_]] = x
745 | handle_meta [x as Appl[Appl[Constant "_constrain", Constant "==>", _],_,_]] = x
746 | handle_meta [x] = Appl[Constant "Trueprop",x]
747 | handle_meta _ = error "hol4rews error: Trueprop not applied to single argument"
749 val smarter_trueprop_parsing = [("Trueprop",handle_meta)]
753 fun initial_maps thy =
754 thy |> add_hol4_type_mapping "min" "bool" false "bool"
755 |> add_hol4_type_mapping "min" "fun" false "fun"
756 |> add_hol4_type_mapping "min" "ind" false "Nat.ind"
757 |> add_hol4_const_mapping "min" "=" false "op ="
758 |> add_hol4_const_mapping "min" "==>" false "op -->"
759 |> add_hol4_const_mapping "min" "@" false "Hilbert_Choice.Eps"
768 HOL4ConstMaps.init #>
776 ImportSegment.init #>
778 Attrib.add_attributes
779 [("hol4rew", Attrib.no_args add_hol4_rewrite, "HOL4 rewrite rule")]