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