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