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