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