src/HOL/Import/hol4rews.ML
author haftmann
Fri Oct 20 10:44:33 2006 +0200 (2006-10-20)
changeset 21056 2cfe839e8d58
parent 20897 3f8d2834b2c4
child 21546 268b6bed0cc8
permissions -rw-r--r--
Symtab.foldl replaced by Symtab.fold
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
wenzelm@20897
   264
fun add_hol4_rewrite (Context.Theory thy, th) =
skalberg@14516
   265
    let
skalberg@14516
   266
	val _ = message "Adding HOL4 rewrite"
haftmann@21056
   267
	val th1 = th RS HOL.eq_reflection
skalberg@14516
   268
	val current_rews = HOL4Rewrites.get thy
wenzelm@18921
   269
	val new_rews = insert Thm.eq_thm th1 current_rews
skalberg@14516
   270
	val updated_thy  = HOL4Rewrites.put new_rews thy
skalberg@14516
   271
    in
wenzelm@18728
   272
	(Context.Theory updated_thy,th1)
wenzelm@20897
   273
    end
haftmann@21056
   274
  | add_hol4_rewrite (context, th) = (context, th RS HOL.eq_reflection);
skalberg@14516
   275
skalberg@14516
   276
fun ignore_hol4 bthy bthm thy =
skalberg@14516
   277
    let
skalberg@14516
   278
	val _ = message ("Ignoring " ^ bthy ^ "." ^ bthm)
skalberg@14516
   279
	val curmaps = HOL4Maps.get thy
wenzelm@17412
   280
	val newmaps = StringPair.update_new ((bthy,bthm),NONE) curmaps
skalberg@14516
   281
	val upd_thy = HOL4Maps.put newmaps thy
skalberg@14516
   282
    in
skalberg@14516
   283
	upd_thy
skalberg@14516
   284
    end
skalberg@14516
   285
skalberg@14516
   286
val opt_get_output_thy = #2 o HOL4Dump.get
skalberg@14516
   287
skalberg@14516
   288
fun get_output_thy thy =
skalberg@14516
   289
    case #2 (HOL4Dump.get thy) of
skalberg@14516
   290
	"" => error "No theory file being output"
skalberg@14516
   291
      | thyname => thyname
skalberg@14516
   292
skalberg@14516
   293
val get_output_dir = #1 o HOL4Dump.get
skalberg@14516
   294
skalberg@14516
   295
fun add_hol4_move bef aft thy =
skalberg@14516
   296
    let
skalberg@14516
   297
	val curmoves = HOL4Moves.get thy
wenzelm@17412
   298
	val newmoves = Symtab.update_new (bef, aft) curmoves
skalberg@14516
   299
    in
skalberg@14516
   300
	HOL4Moves.put newmoves thy
skalberg@14516
   301
    end
skalberg@14516
   302
skalberg@14516
   303
fun get_hol4_move bef thy =
wenzelm@17412
   304
  Symtab.lookup (HOL4Moves.get thy) bef
skalberg@14516
   305
skalberg@14516
   306
fun follow_name thmname thy =
skalberg@14516
   307
    let
skalberg@14516
   308
	val moves = HOL4Moves.get thy
skalberg@14516
   309
	fun F thmname =
wenzelm@17412
   310
	    case Symtab.lookup moves thmname of
skalberg@15531
   311
		SOME name => F name
skalberg@15531
   312
	      | NONE => thmname
skalberg@14516
   313
    in
skalberg@14516
   314
	F thmname
skalberg@14516
   315
    end
skalberg@14516
   316
skalberg@14516
   317
fun add_hol4_cmove bef aft thy =
skalberg@14516
   318
    let
skalberg@14516
   319
	val curmoves = HOL4CMoves.get thy
wenzelm@17412
   320
	val newmoves = Symtab.update_new (bef, aft) curmoves
skalberg@14516
   321
    in
skalberg@14516
   322
	HOL4CMoves.put newmoves thy
skalberg@14516
   323
    end
skalberg@14516
   324
skalberg@14516
   325
fun get_hol4_cmove bef thy =
wenzelm@17412
   326
  Symtab.lookup (HOL4CMoves.get thy) bef
skalberg@14516
   327
skalberg@14516
   328
fun follow_cname thmname thy =
skalberg@14516
   329
    let
skalberg@14516
   330
	val moves = HOL4CMoves.get thy
skalberg@14516
   331
	fun F thmname =
wenzelm@17412
   332
	    case Symtab.lookup moves thmname of
skalberg@15531
   333
		SOME name => F name
skalberg@15531
   334
	      | NONE => thmname
skalberg@14516
   335
    in
skalberg@14516
   336
	F thmname
skalberg@14516
   337
    end
skalberg@14516
   338
skalberg@14516
   339
fun add_hol4_mapping bthy bthm isathm thy =
obua@17644
   340
    let	
obua@17644
   341
       (* val _ = writeln ("Before follow_name: "^isathm)  *)
obua@17644
   342
      val isathm = follow_name isathm thy
obua@17644
   343
       (* val _ = writeln ("Adding theorem map: " ^ bthy ^ "." ^ bthm ^ " --> " ^ isathm)*)
skalberg@14516
   344
	val curmaps = HOL4Maps.get thy
wenzelm@17412
   345
	val newmaps = StringPair.update_new ((bthy,bthm),SOME isathm) curmaps
skalberg@14516
   346
	val upd_thy = HOL4Maps.put newmaps thy
skalberg@14516
   347
    in
skalberg@14516
   348
	upd_thy
skalberg@14516
   349
    end;
skalberg@14516
   350
skalberg@14516
   351
fun get_hol4_type_mapping bthy tycon thy =
skalberg@14516
   352
    let
skalberg@14516
   353
	val maps = HOL4TypeMaps.get thy
skalberg@14516
   354
    in
wenzelm@17412
   355
	StringPair.lookup maps (bthy,tycon)
skalberg@14516
   356
    end
skalberg@14516
   357
skalberg@14516
   358
fun get_hol4_mapping bthy bthm thy =
skalberg@14516
   359
    let
skalberg@14516
   360
	val curmaps = HOL4Maps.get thy
skalberg@14516
   361
    in
wenzelm@17412
   362
	StringPair.lookup curmaps (bthy,bthm)
skalberg@14516
   363
    end;
skalberg@14516
   364
skalberg@14516
   365
fun add_hol4_const_mapping bthy bconst internal isaconst thy =
skalberg@14516
   366
    let
skalberg@14516
   367
	val thy = case opt_get_output_thy thy of
skalberg@14516
   368
		      "" => thy
skalberg@14516
   369
		    | output_thy => if internal
skalberg@14516
   370
				    then add_hol4_cmove (Sign.full_name (sign_of thy) bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy
skalberg@14516
   371
				    else thy
skalberg@14516
   372
	val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
skalberg@14516
   373
	val curmaps = HOL4ConstMaps.get thy
wenzelm@17412
   374
	val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,NONE)) curmaps
skalberg@14516
   375
	val upd_thy = HOL4ConstMaps.put newmaps thy
skalberg@14516
   376
    in
skalberg@14516
   377
	upd_thy
skalberg@14516
   378
    end;
skalberg@14516
   379
skalberg@14516
   380
fun add_hol4_const_renaming bthy bconst newname thy =
skalberg@14516
   381
    let
skalberg@14516
   382
	val currens = HOL4Rename.get thy
skalberg@14516
   383
	val _ = message ("Adding renaming " ^ bthy ^ "." ^ bconst ^ " -> " ^ newname)
wenzelm@17412
   384
	val newrens = StringPair.update_new ((bthy,bconst),newname) currens
skalberg@14516
   385
	val upd_thy = HOL4Rename.put newrens thy
skalberg@14516
   386
    in
skalberg@14516
   387
	upd_thy
skalberg@14516
   388
    end;
skalberg@14516
   389
skalberg@14516
   390
fun get_hol4_const_renaming bthy bconst thy =
skalberg@14516
   391
    let
skalberg@14516
   392
	val currens = HOL4Rename.get thy
skalberg@14516
   393
    in
wenzelm@17412
   394
	StringPair.lookup currens (bthy,bconst)
skalberg@14516
   395
    end;
skalberg@14516
   396
skalberg@14516
   397
fun get_hol4_const_mapping bthy bconst thy =
skalberg@14516
   398
    let
skalberg@14516
   399
	val bconst = case get_hol4_const_renaming bthy bconst thy of
skalberg@15531
   400
			 SOME name => name
skalberg@15531
   401
		       | NONE => bconst
skalberg@14516
   402
	val maps = HOL4ConstMaps.get thy
skalberg@14516
   403
    in
wenzelm@17412
   404
	StringPair.lookup maps (bthy,bconst)
skalberg@14516
   405
    end
skalberg@14516
   406
skalberg@14516
   407
fun add_hol4_const_wt_mapping bthy bconst internal isaconst typ thy =
skalberg@14516
   408
    let
skalberg@14516
   409
	val thy = case opt_get_output_thy thy of
skalberg@14516
   410
		      "" => thy
skalberg@14516
   411
		    | output_thy => if internal
skalberg@14516
   412
				    then add_hol4_cmove (Sign.full_name (sign_of thy) bconst) (output_thy ^ "." ^ bthy ^ "." ^ bconst) thy
skalberg@14516
   413
				    else thy
skalberg@14516
   414
	val _ = message ("Adding cmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
skalberg@14516
   415
	val curmaps = HOL4ConstMaps.get thy
wenzelm@17412
   416
	val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst,SOME typ)) curmaps
skalberg@14516
   417
	val upd_thy = HOL4ConstMaps.put newmaps thy
skalberg@14516
   418
    in
skalberg@14516
   419
	upd_thy
skalberg@14516
   420
    end;
skalberg@14516
   421
skalberg@14516
   422
fun add_hol4_type_mapping bthy bconst internal isaconst thy =
skalberg@14516
   423
    let
skalberg@14516
   424
	val curmaps = HOL4TypeMaps.get thy
obua@17322
   425
	val _ = writeln ("Adding tmap " ^ bthy ^ "." ^ bconst ^ " -> " ^ isaconst ^ (if internal then " (*)" else ""))
wenzelm@17412
   426
	val newmaps = StringPair.update_new ((bthy,bconst),(internal,isaconst)) curmaps
wenzelm@17412
   427
               handle x => let val (internal, isaconst') = the (StringPair.lookup curmaps (bthy, bconst)) in
obua@17322
   428
                      warning ("couldn't map type "^bthy^"."^bconst^" to "^isaconst^": already mapped to "^isaconst'); raise x end
obua@17322
   429
        val upd_thy = HOL4TypeMaps.put newmaps thy
skalberg@14516
   430
    in
skalberg@14516
   431
	upd_thy
skalberg@14516
   432
    end;
skalberg@14516
   433
skalberg@14516
   434
fun add_hol4_pending bthy bthm hth thy =
skalberg@14516
   435
    let
skalberg@14516
   436
	val thmname = Sign.full_name (sign_of thy) bthm
skalberg@14516
   437
	val _ = message ("Add pending " ^ bthy ^ "." ^ bthm)
skalberg@14516
   438
	val curpend = HOL4Pending.get thy
wenzelm@17412
   439
	val newpend = StringPair.update_new ((bthy,bthm),hth) curpend
skalberg@14516
   440
	val upd_thy = HOL4Pending.put newpend thy
skalberg@14516
   441
	val thy' = case opt_get_output_thy upd_thy of
skalberg@14516
   442
		       "" => add_hol4_mapping bthy bthm thmname upd_thy
skalberg@14516
   443
		     | output_thy =>
skalberg@14516
   444
		       let
skalberg@14516
   445
			   val new_thmname = output_thy ^ "." ^ bthy ^ "." ^ bthm
skalberg@14516
   446
		       in
skalberg@14516
   447
			   upd_thy |> add_hol4_move thmname new_thmname
skalberg@14516
   448
				   |> add_hol4_mapping bthy bthm new_thmname
skalberg@14516
   449
		       end
skalberg@14516
   450
    in
skalberg@14516
   451
	thy'
skalberg@14516
   452
    end;
skalberg@14516
   453
skalberg@14516
   454
fun get_hol4_theorem thyname thmname thy =
haftmann@21056
   455
  let
haftmann@21056
   456
    val isathms = HOL4Thms.get thy
haftmann@21056
   457
  in
haftmann@21056
   458
    StringPair.lookup isathms (thyname,thmname)
haftmann@21056
   459
  end;
skalberg@14516
   460
haftmann@21056
   461
fun add_hol4_theorem thyname thmname hth =
haftmann@21056
   462
  let
haftmann@21056
   463
    val _ = message ("Adding HOL4 theorem " ^ thyname ^ "." ^ thmname)
haftmann@21056
   464
  in
haftmann@21056
   465
    HOL4Thms.map (StringPair.update_new ((thyname, thmname), hth))
haftmann@21056
   466
  end;
skalberg@14516
   467
skalberg@14516
   468
fun export_hol4_pending thy =
haftmann@21056
   469
  let
haftmann@21056
   470
    val rews = HOL4Rewrites.get thy;
haftmann@21056
   471
    val pending = HOL4Pending.get thy;
haftmann@21056
   472
    fun process ((bthy,bthm), hth as (_,thm)) thy =
haftmann@21056
   473
      let
haftmann@21056
   474
        val thm1 = rewrite_rule (map (Thm.transfer thy) rews) (Thm.transfer thy thm);
haftmann@21056
   475
        val thm2 = standard thm1;
haftmann@21056
   476
      in
haftmann@21056
   477
        thy
haftmann@21056
   478
        |> PureThy.store_thm ((bthm, thm2), [])
haftmann@21056
   479
        |> snd
haftmann@21056
   480
        |> add_hol4_theorem bthy bthm hth
haftmann@21056
   481
      end;
haftmann@21056
   482
  in
haftmann@21056
   483
    thy
haftmann@21056
   484
    |> StringPair.fold process pending
haftmann@21056
   485
    |> HOL4Pending.put StringPair.empty
haftmann@21056
   486
  end;
skalberg@14516
   487
skalberg@14516
   488
fun setup_dump (dir,thyname) thy =
skalberg@15647
   489
    HOL4Dump.put (dir,thyname,["(* AUTOMATICALLY GENERATED, DO NOT EDIT! *)"]) thy
skalberg@14516
   490
skalberg@14516
   491
fun add_dump str thy =
skalberg@14516
   492
    let
skalberg@14516
   493
	val (dir,thyname,curdump) = HOL4Dump.get thy
skalberg@14516
   494
    in
skalberg@14516
   495
	HOL4Dump.put (dir,thyname,str::curdump) thy
skalberg@14516
   496
    end
skalberg@14516
   497
skalberg@14516
   498
fun flush_dump thy =
skalberg@14516
   499
    let
skalberg@14516
   500
	val (dir,thyname,dumpdata) = HOL4Dump.get thy
skalberg@14516
   501
	val os = TextIO.openOut (OS.Path.joinDirFile {dir=dir,
skalberg@14516
   502
						      file=thyname ^ ".thy"})
skalberg@14516
   503
	val _  = app (fn s => TextIO.output(os,s ^ "\n\n")) (rev dumpdata)
skalberg@14516
   504
	val  _ = TextIO.closeOut os
skalberg@14516
   505
    in
skalberg@14516
   506
	HOL4Dump.put ("","",[]) thy
skalberg@14516
   507
    end
skalberg@14516
   508
skalberg@14516
   509
fun set_generating_thy thyname thy =
skalberg@14516
   510
    case HOL4DefThy.get thy of
skalberg@14516
   511
	NoImport => HOL4DefThy.put (Generating thyname) thy
skalberg@14516
   512
      | _ => error "Import already in progess"
skalberg@14516
   513
skalberg@14516
   514
fun set_replaying_thy thyname thy =
skalberg@14516
   515
    case HOL4DefThy.get thy of
skalberg@14516
   516
	NoImport => HOL4DefThy.put (Replaying thyname) thy
skalberg@14516
   517
      | _ => error "Import already in progess"
skalberg@14516
   518
skalberg@14516
   519
fun clear_import_thy thy =
skalberg@14516
   520
    case HOL4DefThy.get thy of
skalberg@14516
   521
	NoImport => error "No import in progress"
skalberg@14516
   522
      | _ => HOL4DefThy.put NoImport thy
skalberg@14516
   523
skalberg@14516
   524
fun get_generating_thy thy =
skalberg@14516
   525
    case HOL4DefThy.get thy of
skalberg@14516
   526
	Generating thyname => thyname
skalberg@14516
   527
      | _ => error "No theory being generated"
skalberg@14516
   528
skalberg@14516
   529
fun get_replaying_thy thy =
skalberg@14516
   530
    case HOL4DefThy.get thy of
skalberg@14516
   531
	Replaying thyname => thyname
skalberg@14516
   532
      | _ => error "No theory being replayed"
skalberg@14516
   533
skalberg@14516
   534
fun get_import_thy thy =
skalberg@14516
   535
    case HOL4DefThy.get thy of
skalberg@14516
   536
	Replaying thyname => thyname
skalberg@14516
   537
      | Generating thyname => thyname
skalberg@14516
   538
      | _ => error "No theory being imported"
skalberg@14516
   539
skalberg@14516
   540
fun should_ignore thyname thy thmname =
skalberg@14516
   541
    case get_hol4_mapping thyname thmname thy of
skalberg@15531
   542
	SOME NONE => true 
skalberg@14516
   543
      | _ => false
skalberg@14516
   544
skalberg@14516
   545
val trans_string =
skalberg@14516
   546
    let
skalberg@14516
   547
	fun quote s = "\"" ^ s ^ "\""
skalberg@14516
   548
	fun F [] = []
skalberg@14516
   549
	  | F (#"\\" :: cs) = patch #"\\" cs
skalberg@14516
   550
	  | F (#"\"" :: cs) = patch #"\"" cs
skalberg@14516
   551
	  | F (c     :: cs) = c :: F cs
skalberg@14516
   552
	and patch c rest = #"\\" :: c :: F rest
skalberg@14516
   553
    in
skalberg@14516
   554
	quote o String.implode o F o String.explode
skalberg@14516
   555
    end
skalberg@14516
   556
skalberg@14516
   557
fun dump_import_thy thyname thy =
skalberg@14516
   558
    let
skalberg@14516
   559
	val output_dir = get_output_dir thy
skalberg@14516
   560
	val output_thy = get_output_thy thy
obua@17607
   561
	val input_thy = Context.theory_name thy
skalberg@14516
   562
	val import_segment = get_import_segment thy
skalberg@14516
   563
	val os = TextIO.openOut (OS.Path.joinDirFile {dir=output_dir,
skalberg@14516
   564
						      file=thyname ^ ".imp"})
skalberg@14516
   565
	fun out s = TextIO.output(os,s)
haftmann@21056
   566
    val (ignored, mapped) = StringPair.fold
haftmann@21056
   567
      (fn ((bthy, bthm), v) => fn (ign, map) =>
haftmann@21056
   568
        if bthy = thyname
haftmann@21056
   569
        then case v
haftmann@21056
   570
         of NONE => (bthm :: ign, map)
haftmann@21056
   571
          | SOME w => (ign, (bthm, w) :: map)
haftmann@21056
   572
        else (ign, map)) (HOL4Maps.get thy) ([],[]);
haftmann@21056
   573
    fun mk init = StringPair.fold
haftmann@21056
   574
      (fn ((bthy, bthm), v) => if bthy = thyname then cons (bthm, v) else I) init [];
haftmann@21056
   575
    val constmaps = mk (HOL4ConstMaps.get thy);
haftmann@21056
   576
    val constrenames = mk (HOL4Rename.get thy);
haftmann@21056
   577
    val typemaps = mk (HOL4TypeMaps.get thy);
haftmann@21056
   578
    val defmaps = mk (HOL4DefMaps.get thy);
skalberg@14516
   579
	fun new_name internal isa =
skalberg@14516
   580
	    if internal
skalberg@14516
   581
	    then
skalberg@14516
   582
		let
skalberg@14516
   583
		    val paths = NameSpace.unpack isa
skalberg@15570
   584
		    val i = Library.drop(length paths - 2,paths)
skalberg@14516
   585
		in
skalberg@14516
   586
		    case i of
skalberg@14516
   587
			[seg,con] => output_thy ^ "." ^ seg ^ "." ^ con
skalberg@14516
   588
		      | _ => error "hol4rews.dump internal error"
skalberg@14516
   589
		end
skalberg@14516
   590
	    else
skalberg@14516
   591
		isa
skalberg@14516
   592
skalberg@14516
   593
	val _ = out "import\n\n"
skalberg@14516
   594
skalberg@14516
   595
	val _ = out ("import_segment " ^ trans_string import_segment ^ "\n\n")
skalberg@14516
   596
	val _ = if null defmaps
skalberg@14516
   597
		then ()
skalberg@14516
   598
		else out "def_maps"
skalberg@14516
   599
	val _ = app (fn (hol,isa) =>
skalberg@14516
   600
			out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string isa))) defmaps
skalberg@14516
   601
	val _ = if null defmaps
skalberg@14516
   602
		then ()
skalberg@14516
   603
		else out "\n\n"
skalberg@14516
   604
skalberg@14516
   605
	val _ = if null typemaps
skalberg@14516
   606
		then ()
skalberg@14516
   607
		else out "type_maps"
skalberg@14516
   608
	val _ = app (fn (hol,(internal,isa)) =>
skalberg@14516
   609
			out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string (new_name internal isa)))) typemaps
skalberg@14516
   610
	val _ = if null typemaps
skalberg@14516
   611
		then ()
skalberg@14516
   612
		else out "\n\n"
skalberg@14516
   613
skalberg@14516
   614
	val _ = if null constmaps
skalberg@14516
   615
		then ()
skalberg@14516
   616
		else out "const_maps"
skalberg@14516
   617
	val _ = app (fn (hol,(internal,isa,opt_ty)) =>
skalberg@14516
   618
			(out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string (follow_cname isa thy)));
skalberg@14516
   619
			 case opt_ty of
haftmann@21056
   620
			     SOME ty => out (" :: \"" ^ (string_of_ctyp (ctyp_of thy ty)) ^ "\"")
skalberg@15531
   621
			   | NONE => ())) constmaps
skalberg@14516
   622
	val _ = if null constmaps
skalberg@14516
   623
		then ()
skalberg@14516
   624
		else out "\n\n"
skalberg@14516
   625
skalberg@14516
   626
	val _ = if null constrenames
skalberg@14516
   627
		then ()
skalberg@14516
   628
		else out "const_renames"
skalberg@14516
   629
	val _ = app (fn (old,new) =>
skalberg@14516
   630
			out ("\n  " ^ (trans_string old) ^ " > " ^ (trans_string new))) constrenames
skalberg@14516
   631
	val _ = if null constrenames
skalberg@14516
   632
		then ()
skalberg@14516
   633
		else out "\n\n"
skalberg@14516
   634
obua@17607
   635
	fun gen2replay in_thy out_thy s = 
obua@17607
   636
	    let
obua@17607
   637
		val ss = NameSpace.unpack s
obua@17607
   638
	    in
obua@17607
   639
		if (hd ss = in_thy) then 
obua@17607
   640
		    NameSpace.pack (out_thy::(tl ss))
obua@17607
   641
		else
obua@17607
   642
		    s
obua@17607
   643
	    end 
obua@17607
   644
skalberg@14516
   645
	val _ = if null mapped
skalberg@14516
   646
		then ()
skalberg@14516
   647
		else out "thm_maps"
obua@17607
   648
	val _ = app (fn (hol,isa) => out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string (gen2replay input_thy output_thy isa)))) mapped
skalberg@14516
   649
	val _ = if null mapped 
skalberg@14516
   650
		then ()
skalberg@14516
   651
		else out "\n\n"
skalberg@14516
   652
skalberg@14516
   653
	val _ = if null ignored
skalberg@14516
   654
		then ()
skalberg@14516
   655
		else out "ignore_thms"
skalberg@14516
   656
	val _ = app (fn ign => out ("\n  " ^ (trans_string ign))) ignored
skalberg@14516
   657
	val _ = if null ignored
skalberg@14516
   658
		then ()
skalberg@14516
   659
		else out "\n\n"
skalberg@14516
   660
skalberg@14516
   661
	val _ = out "end\n"
skalberg@14516
   662
	val _ = TextIO.closeOut os
skalberg@14516
   663
    in
skalberg@14516
   664
	thy
skalberg@14516
   665
    end
skalberg@14516
   666
skalberg@14516
   667
fun set_used_names names thy =
skalberg@14516
   668
    let
skalberg@14516
   669
	val unames = HOL4UNames.get thy
skalberg@14516
   670
    in
skalberg@14516
   671
	case unames of
skalberg@14516
   672
	    [] => HOL4UNames.put names thy
skalberg@14516
   673
	  | _ => error "hol4rews.set_used_names called on initialized data!"
skalberg@14516
   674
    end
skalberg@14516
   675
skalberg@14516
   676
val clear_used_names = HOL4UNames.put HOL4UNamesArgs.empty
skalberg@14516
   677
skalberg@14516
   678
fun get_defmap thyname const thy =
skalberg@14516
   679
    let
skalberg@14516
   680
	val maps = HOL4DefMaps.get thy
skalberg@14516
   681
    in
wenzelm@17412
   682
	StringPair.lookup maps (thyname,const)
skalberg@14516
   683
    end
skalberg@14516
   684
skalberg@14516
   685
fun add_defmap thyname const defname thy =
skalberg@14516
   686
    let
skalberg@14516
   687
	val _ = message ("Adding defmap " ^ thyname ^ "." ^ const ^ " --> " ^ defname)
skalberg@14516
   688
	val maps = HOL4DefMaps.get thy
wenzelm@17412
   689
	val maps' = StringPair.update_new ((thyname,const),defname) maps
skalberg@14516
   690
	val thy' = HOL4DefMaps.put maps' thy
skalberg@14516
   691
    in
skalberg@14516
   692
	thy'
skalberg@14516
   693
    end
skalberg@14516
   694
skalberg@14516
   695
fun get_defname thyname name thy =
skalberg@14516
   696
    let
skalberg@14516
   697
	val maps = HOL4DefMaps.get thy
skalberg@14516
   698
	fun F dname = (dname,add_defmap thyname name dname thy)
skalberg@14516
   699
    in
wenzelm@17412
   700
	case StringPair.lookup maps (thyname,name) of
skalberg@15531
   701
	    SOME thmname => (thmname,thy)
skalberg@15531
   702
	  | NONE =>
skalberg@14516
   703
	    let
skalberg@14516
   704
		val used = HOL4UNames.get thy
skalberg@14516
   705
		val defname = def_name name
skalberg@14516
   706
		val pdefname = name ^ "_primdef"
skalberg@14516
   707
	    in
skalberg@14516
   708
		if not (defname mem used)
skalberg@14516
   709
		then F defname                 (* name_def *)
skalberg@14516
   710
		else if not (pdefname mem used)
skalberg@14516
   711
		then F pdefname                (* name_primdef *)
wenzelm@20071
   712
		else F (Name.variant used pdefname) (* last resort *)
skalberg@14516
   713
	    end
skalberg@14516
   714
    end
skalberg@14516
   715
skalberg@14516
   716
local
skalberg@14516
   717
    fun handle_meta [x as Appl[Appl[Constant "_constrain", Constant "==", _],_,_]] = x
skalberg@14516
   718
      | handle_meta [x as Appl[Appl[Constant "_constrain", Constant "all", _],_]] = x
skalberg@14516
   719
      | handle_meta [x as Appl[Appl[Constant "_constrain", Constant "==>", _],_,_]] = x
skalberg@14516
   720
      | handle_meta [x] = Appl[Constant "Trueprop",x]
skalberg@14518
   721
      | handle_meta _ = error "hol4rews error: Trueprop not applied to single argument"
skalberg@14516
   722
in
skalberg@14516
   723
val smarter_trueprop_parsing = [("Trueprop",handle_meta)]
skalberg@14516
   724
end
skalberg@14516
   725
skalberg@14516
   726
local
skalberg@14516
   727
    fun initial_maps thy =
skalberg@14516
   728
	thy |> add_hol4_type_mapping "min" "bool" false "bool"
skalberg@14516
   729
	    |> add_hol4_type_mapping "min" "fun" false "fun"
skalberg@14516
   730
	    |> add_hol4_type_mapping "min" "ind" false "Nat.ind"
skalberg@14516
   731
	    |> add_hol4_const_mapping "min" "=" false "op ="
skalberg@14516
   732
	    |> add_hol4_const_mapping "min" "==>" false "op -->"
skalberg@14516
   733
	    |> add_hol4_const_mapping "min" "@" false "Hilbert_Choice.Eps"
skalberg@14516
   734
in
skalberg@14516
   735
val hol4_setup =
wenzelm@18708
   736
  HOL4Rewrites.init #>
wenzelm@18708
   737
  HOL4Maps.init #>
wenzelm@18708
   738
  HOL4UNames.init #>
wenzelm@18708
   739
  HOL4DefMaps.init #>
wenzelm@18708
   740
  HOL4Moves.init #>
wenzelm@18708
   741
  HOL4CMoves.init #>
wenzelm@18708
   742
  HOL4ConstMaps.init #>
wenzelm@18708
   743
  HOL4Rename.init #>
wenzelm@18708
   744
  HOL4TypeMaps.init #>
wenzelm@18708
   745
  HOL4Pending.init #>
wenzelm@18708
   746
  HOL4Thms.init #>
wenzelm@18708
   747
  HOL4Dump.init #>
wenzelm@18708
   748
  HOL4DefThy.init #>
wenzelm@18708
   749
  HOL4Imports.init #>
wenzelm@18708
   750
  ImportSegment.init #>
wenzelm@18708
   751
  initial_maps #>
wenzelm@18708
   752
  Attrib.add_attributes
wenzelm@18728
   753
    [("hol4rew", Attrib.no_args add_hol4_rewrite, "HOL4 rewrite rule")]
skalberg@14516
   754
end