src/HOL/Import/hol4rews.ML
author wenzelm
Wed Jan 12 15:15:51 2011 +0100 (2011-01-12)
changeset 41522 42d13d00ccfb
parent 39557 fe5722fce758
child 42224 578a51fae383
permissions -rw-r--r--
more FIXMEs concerning bad catch-all exception handlers;
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@41522
   322
               (* FIXME avoid handle x *)
wenzelm@17412
   323
               handle x => let val (internal, isaconst') = the (StringPair.lookup curmaps (bthy, bconst)) in
obua@17322
   324
                      warning ("couldn't map type "^bthy^"."^bconst^" to "^isaconst^": already mapped to "^isaconst'); raise x end
obua@17322
   325
        val upd_thy = HOL4TypeMaps.put newmaps thy
skalberg@14516
   326
    in
wenzelm@32960
   327
        upd_thy
skalberg@14516
   328
    end;
skalberg@14516
   329
skalberg@14516
   330
fun add_hol4_pending bthy bthm hth thy =
skalberg@14516
   331
    let
wenzelm@32960
   332
        val thmname = Sign.full_bname thy bthm
wenzelm@32960
   333
        val _ = message ("Add pending " ^ bthy ^ "." ^ bthm)
wenzelm@32960
   334
        val curpend = HOL4Pending.get thy
wenzelm@32960
   335
        val newpend = StringPair.update_new ((bthy,bthm),hth) curpend
wenzelm@32960
   336
        val upd_thy = HOL4Pending.put newpend thy
wenzelm@32960
   337
        val thy' = case opt_get_output_thy upd_thy of
wenzelm@32960
   338
                       "" => add_hol4_mapping bthy bthm thmname upd_thy
wenzelm@32960
   339
                     | output_thy =>
wenzelm@32960
   340
                       let
wenzelm@32960
   341
                           val new_thmname = output_thy ^ "." ^ bthy ^ "." ^ bthm
wenzelm@32960
   342
                       in
wenzelm@32960
   343
                           upd_thy |> add_hol4_move thmname new_thmname
wenzelm@32960
   344
                                   |> add_hol4_mapping bthy bthm new_thmname
wenzelm@32960
   345
                       end
skalberg@14516
   346
    in
wenzelm@32960
   347
        thy'
skalberg@14516
   348
    end;
skalberg@14516
   349
skalberg@14516
   350
fun get_hol4_theorem thyname thmname thy =
haftmann@21056
   351
  let
haftmann@21056
   352
    val isathms = HOL4Thms.get thy
haftmann@21056
   353
  in
haftmann@21056
   354
    StringPair.lookup isathms (thyname,thmname)
haftmann@21056
   355
  end;
skalberg@14516
   356
haftmann@21056
   357
fun add_hol4_theorem thyname thmname hth =
haftmann@21056
   358
  let
haftmann@21056
   359
    val _ = message ("Adding HOL4 theorem " ^ thyname ^ "." ^ thmname)
haftmann@21056
   360
  in
haftmann@21056
   361
    HOL4Thms.map (StringPair.update_new ((thyname, thmname), hth))
haftmann@21056
   362
  end;
skalberg@14516
   363
skalberg@14516
   364
fun export_hol4_pending thy =
haftmann@21056
   365
  let
haftmann@21056
   366
    val rews = HOL4Rewrites.get thy;
haftmann@21056
   367
    val pending = HOL4Pending.get thy;
haftmann@21056
   368
    fun process ((bthy,bthm), hth as (_,thm)) thy =
haftmann@21056
   369
      let
haftmann@21056
   370
        val thm1 = rewrite_rule (map (Thm.transfer thy) rews) (Thm.transfer thy thm);
wenzelm@35021
   371
        val thm2 = Drule.export_without_context thm1;
haftmann@21056
   372
      in
haftmann@21056
   373
        thy
wenzelm@39557
   374
        |> Global_Theory.store_thm (Binding.name bthm, thm2)
haftmann@21056
   375
        |> snd
haftmann@21056
   376
        |> add_hol4_theorem bthy bthm hth
haftmann@21056
   377
      end;
haftmann@21056
   378
  in
haftmann@21056
   379
    thy
haftmann@21056
   380
    |> StringPair.fold process pending
haftmann@21056
   381
    |> HOL4Pending.put StringPair.empty
haftmann@21056
   382
  end;
skalberg@14516
   383
skalberg@14516
   384
fun setup_dump (dir,thyname) thy =
skalberg@15647
   385
    HOL4Dump.put (dir,thyname,["(* AUTOMATICALLY GENERATED, DO NOT EDIT! *)"]) thy
skalberg@14516
   386
skalberg@14516
   387
fun add_dump str thy =
skalberg@14516
   388
    let
wenzelm@32960
   389
        val (dir,thyname,curdump) = HOL4Dump.get thy
skalberg@14516
   390
    in
wenzelm@32960
   391
        HOL4Dump.put (dir,thyname,str::curdump) thy
skalberg@14516
   392
    end
skalberg@14516
   393
skalberg@14516
   394
fun flush_dump thy =
skalberg@14516
   395
    let
wenzelm@32960
   396
        val (dir,thyname,dumpdata) = HOL4Dump.get thy
wenzelm@32960
   397
        val os = TextIO.openOut (OS.Path.joinDirFile {dir=dir,
wenzelm@32960
   398
                                                      file=thyname ^ ".thy"})
wenzelm@32960
   399
        val _  = app (fn s => TextIO.output(os,s ^ "\n\n")) (rev dumpdata)
wenzelm@32960
   400
        val  _ = TextIO.closeOut os
skalberg@14516
   401
    in
wenzelm@32960
   402
        HOL4Dump.put ("","",[]) thy
skalberg@14516
   403
    end
skalberg@14516
   404
skalberg@14516
   405
fun set_generating_thy thyname thy =
skalberg@14516
   406
    case HOL4DefThy.get thy of
wenzelm@32960
   407
        NoImport => HOL4DefThy.put (Generating thyname) thy
skalberg@14516
   408
      | _ => error "Import already in progess"
skalberg@14516
   409
skalberg@14516
   410
fun set_replaying_thy thyname thy =
skalberg@14516
   411
    case HOL4DefThy.get thy of
wenzelm@32960
   412
        NoImport => HOL4DefThy.put (Replaying thyname) thy
skalberg@14516
   413
      | _ => error "Import already in progess"
skalberg@14516
   414
skalberg@14516
   415
fun clear_import_thy thy =
skalberg@14516
   416
    case HOL4DefThy.get thy of
wenzelm@32960
   417
        NoImport => error "No import in progress"
skalberg@14516
   418
      | _ => HOL4DefThy.put NoImport thy
skalberg@14516
   419
skalberg@14516
   420
fun get_generating_thy thy =
skalberg@14516
   421
    case HOL4DefThy.get thy of
wenzelm@32960
   422
        Generating thyname => thyname
skalberg@14516
   423
      | _ => error "No theory being generated"
skalberg@14516
   424
skalberg@14516
   425
fun get_replaying_thy thy =
skalberg@14516
   426
    case HOL4DefThy.get thy of
wenzelm@32960
   427
        Replaying thyname => thyname
skalberg@14516
   428
      | _ => error "No theory being replayed"
skalberg@14516
   429
skalberg@14516
   430
fun get_import_thy thy =
skalberg@14516
   431
    case HOL4DefThy.get thy of
wenzelm@32960
   432
        Replaying thyname => thyname
skalberg@14516
   433
      | Generating thyname => thyname
skalberg@14516
   434
      | _ => error "No theory being imported"
skalberg@14516
   435
skalberg@14516
   436
fun should_ignore thyname thy thmname =
skalberg@14516
   437
    case get_hol4_mapping thyname thmname thy of
wenzelm@32960
   438
        SOME NONE => true 
skalberg@14516
   439
      | _ => false
skalberg@14516
   440
skalberg@14516
   441
val trans_string =
skalberg@14516
   442
    let
wenzelm@32960
   443
        fun quote s = "\"" ^ s ^ "\""
wenzelm@32960
   444
        fun F [] = []
wenzelm@32960
   445
          | F (#"\\" :: cs) = patch #"\\" cs
wenzelm@32960
   446
          | F (#"\"" :: cs) = patch #"\"" cs
wenzelm@32960
   447
          | F (c     :: cs) = c :: F cs
wenzelm@32960
   448
        and patch c rest = #"\\" :: c :: F rest
skalberg@14516
   449
    in
wenzelm@32960
   450
        quote o String.implode o F o String.explode
skalberg@14516
   451
    end
skalberg@14516
   452
skalberg@14516
   453
fun dump_import_thy thyname thy =
skalberg@14516
   454
    let
wenzelm@32960
   455
        val output_dir = get_output_dir thy
wenzelm@32960
   456
        val output_thy = get_output_thy thy
wenzelm@32960
   457
        val input_thy = Context.theory_name thy
wenzelm@32960
   458
        val import_segment = get_import_segment thy
wenzelm@32960
   459
        val os = TextIO.openOut (OS.Path.joinDirFile {dir=output_dir,
wenzelm@32960
   460
                                                      file=thyname ^ ".imp"})
wenzelm@32960
   461
        fun out s = TextIO.output(os,s)
haftmann@21056
   462
    val (ignored, mapped) = StringPair.fold
haftmann@21056
   463
      (fn ((bthy, bthm), v) => fn (ign, map) =>
haftmann@21056
   464
        if bthy = thyname
haftmann@21056
   465
        then case v
haftmann@21056
   466
         of NONE => (bthm :: ign, map)
haftmann@21056
   467
          | SOME w => (ign, (bthm, w) :: map)
haftmann@21056
   468
        else (ign, map)) (HOL4Maps.get thy) ([],[]);
haftmann@21056
   469
    fun mk init = StringPair.fold
haftmann@21056
   470
      (fn ((bthy, bthm), v) => if bthy = thyname then cons (bthm, v) else I) init [];
haftmann@21056
   471
    val constmaps = mk (HOL4ConstMaps.get thy);
haftmann@21056
   472
    val constrenames = mk (HOL4Rename.get thy);
haftmann@21056
   473
    val typemaps = mk (HOL4TypeMaps.get thy);
haftmann@21056
   474
    val defmaps = mk (HOL4DefMaps.get thy);
wenzelm@32960
   475
        fun new_name internal isa =
wenzelm@32960
   476
            if internal
wenzelm@32960
   477
            then
wenzelm@32960
   478
                let
wenzelm@32960
   479
                    val paths = Long_Name.explode isa
haftmann@33955
   480
                    val i = drop (length paths - 2) paths
wenzelm@32960
   481
                in
wenzelm@32960
   482
                    case i of
wenzelm@32960
   483
                        [seg,con] => output_thy ^ "." ^ seg ^ "." ^ con
wenzelm@32960
   484
                      | _ => error "hol4rews.dump internal error"
wenzelm@32960
   485
                end
wenzelm@32960
   486
            else
wenzelm@32960
   487
                isa
skalberg@14516
   488
wenzelm@32960
   489
        val _ = out "import\n\n"
skalberg@14516
   490
wenzelm@32960
   491
        val _ = out ("import_segment " ^ trans_string import_segment ^ "\n\n")
wenzelm@32960
   492
        val _ = if null defmaps
wenzelm@32960
   493
                then ()
wenzelm@32960
   494
                else out "def_maps"
wenzelm@32960
   495
        val _ = app (fn (hol,isa) =>
wenzelm@32960
   496
                        out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string isa))) defmaps
wenzelm@32960
   497
        val _ = if null defmaps
wenzelm@32960
   498
                then ()
wenzelm@32960
   499
                else out "\n\n"
skalberg@14516
   500
wenzelm@32960
   501
        val _ = if null typemaps
wenzelm@32960
   502
                then ()
wenzelm@32960
   503
                else out "type_maps"
wenzelm@32960
   504
        val _ = app (fn (hol,(internal,isa)) =>
wenzelm@32960
   505
                        out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string (new_name internal isa)))) typemaps
wenzelm@32960
   506
        val _ = if null typemaps
wenzelm@32960
   507
                then ()
wenzelm@32960
   508
                else out "\n\n"
skalberg@14516
   509
wenzelm@32960
   510
        val _ = if null constmaps
wenzelm@32960
   511
                then ()
wenzelm@32960
   512
                else out "const_maps"
wenzelm@32960
   513
        val _ = app (fn (hol,(internal,isa,opt_ty)) =>
wenzelm@32960
   514
                        (out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string (follow_cname isa thy)));
wenzelm@32960
   515
                         case opt_ty of
wenzelm@32960
   516
                             SOME ty => out (" :: \"" ^ Syntax.string_of_typ_global thy ty ^ "\"")
wenzelm@32960
   517
                           | NONE => ())) constmaps
wenzelm@32960
   518
        val _ = if null constmaps
wenzelm@32960
   519
                then ()
wenzelm@32960
   520
                else out "\n\n"
skalberg@14516
   521
wenzelm@32960
   522
        val _ = if null constrenames
wenzelm@32960
   523
                then ()
wenzelm@32960
   524
                else out "const_renames"
wenzelm@32960
   525
        val _ = app (fn (old,new) =>
wenzelm@32960
   526
                        out ("\n  " ^ (trans_string old) ^ " > " ^ (trans_string new))) constrenames
wenzelm@32960
   527
        val _ = if null constrenames
wenzelm@32960
   528
                then ()
wenzelm@32960
   529
                else out "\n\n"
skalberg@14516
   530
wenzelm@32960
   531
        fun gen2replay in_thy out_thy s = 
wenzelm@32960
   532
            let
wenzelm@32960
   533
                val ss = Long_Name.explode s
wenzelm@32960
   534
            in
wenzelm@32960
   535
                if (hd ss = in_thy) then 
wenzelm@32960
   536
                    Long_Name.implode (out_thy::(tl ss))
wenzelm@32960
   537
                else
wenzelm@32960
   538
                    s
wenzelm@32960
   539
            end 
obua@17607
   540
wenzelm@32960
   541
        val _ = if null mapped
wenzelm@32960
   542
                then ()
wenzelm@32960
   543
                else out "thm_maps"
wenzelm@32960
   544
        val _ = app (fn (hol,isa) => out ("\n  " ^ (trans_string hol) ^ " > " ^ (trans_string (gen2replay input_thy output_thy isa)))) mapped
wenzelm@32960
   545
        val _ = if null mapped 
wenzelm@32960
   546
                then ()
wenzelm@32960
   547
                else out "\n\n"
skalberg@14516
   548
wenzelm@32960
   549
        val _ = if null ignored
wenzelm@32960
   550
                then ()
wenzelm@32960
   551
                else out "ignore_thms"
wenzelm@32960
   552
        val _ = app (fn ign => out ("\n  " ^ (trans_string ign))) ignored
wenzelm@32960
   553
        val _ = if null ignored
wenzelm@32960
   554
                then ()
wenzelm@32960
   555
                else out "\n\n"
skalberg@14516
   556
wenzelm@32960
   557
        val _ = out "end\n"
wenzelm@32960
   558
        val _ = TextIO.closeOut os
skalberg@14516
   559
    in
wenzelm@32960
   560
        thy
skalberg@14516
   561
    end
skalberg@14516
   562
skalberg@14516
   563
fun set_used_names names thy =
skalberg@14516
   564
    let
wenzelm@32960
   565
        val unames = HOL4UNames.get thy
skalberg@14516
   566
    in
wenzelm@32960
   567
        case unames of
wenzelm@32960
   568
            [] => HOL4UNames.put names thy
wenzelm@32960
   569
          | _ => error "hol4rews.set_used_names called on initialized data!"
skalberg@14516
   570
    end
skalberg@14516
   571
wenzelm@22846
   572
val clear_used_names = HOL4UNames.put [];
skalberg@14516
   573
skalberg@14516
   574
fun get_defmap thyname const thy =
skalberg@14516
   575
    let
wenzelm@32960
   576
        val maps = HOL4DefMaps.get thy
skalberg@14516
   577
    in
wenzelm@32960
   578
        StringPair.lookup maps (thyname,const)
skalberg@14516
   579
    end
skalberg@14516
   580
skalberg@14516
   581
fun add_defmap thyname const defname thy =
skalberg@14516
   582
    let
wenzelm@32960
   583
        val _ = message ("Adding defmap " ^ thyname ^ "." ^ const ^ " --> " ^ defname)
wenzelm@32960
   584
        val maps = HOL4DefMaps.get thy
wenzelm@32960
   585
        val maps' = StringPair.update_new ((thyname,const),defname) maps
wenzelm@32960
   586
        val thy' = HOL4DefMaps.put maps' thy
skalberg@14516
   587
    in
wenzelm@32960
   588
        thy'
skalberg@14516
   589
    end
skalberg@14516
   590
skalberg@14516
   591
fun get_defname thyname name thy =
skalberg@14516
   592
    let
wenzelm@32960
   593
        val maps = HOL4DefMaps.get thy
wenzelm@32960
   594
        fun F dname = (dname,add_defmap thyname name dname thy)
skalberg@14516
   595
    in
wenzelm@32960
   596
        case StringPair.lookup maps (thyname,name) of
wenzelm@32960
   597
            SOME thmname => (thmname,thy)
wenzelm@32960
   598
          | NONE =>
wenzelm@32960
   599
            let
wenzelm@32960
   600
                val used = HOL4UNames.get thy
wenzelm@32960
   601
                val defname = Thm.def_name name
wenzelm@32960
   602
                val pdefname = name ^ "_primdef"
wenzelm@32960
   603
            in
haftmann@36692
   604
                if not (member (op =) used defname)
wenzelm@32960
   605
                then F defname                 (* name_def *)
haftmann@36692
   606
                else if not (member (op =) used pdefname)
wenzelm@32960
   607
                then F pdefname                (* name_primdef *)
wenzelm@32960
   608
                else F (Name.variant used pdefname) (* last resort *)
wenzelm@32960
   609
            end
skalberg@14516
   610
    end
skalberg@14516
   611
skalberg@14516
   612
local
wenzelm@35250
   613
    fun handle_meta [x as Appl[Appl[Constant "_constrain", Constant @{const_syntax "=="}, _],_,_]] = x
wenzelm@35250
   614
      | handle_meta [x as Appl[Appl[Constant "_constrain", Constant @{const_syntax all}, _],_]] = x
wenzelm@35250
   615
      | handle_meta [x as Appl[Appl[Constant "_constrain", Constant @{const_syntax "==>"}, _],_,_]] = x
haftmann@38556
   616
      | handle_meta [x] = Appl[Constant @{const_syntax Trueprop}, x]
skalberg@14518
   617
      | handle_meta _ = error "hol4rews error: Trueprop not applied to single argument"
skalberg@14516
   618
in
wenzelm@35250
   619
val smarter_trueprop_parsing = [(@{const_syntax Trueprop},handle_meta)]
skalberg@14516
   620
end
skalberg@14516
   621
skalberg@14516
   622
local
skalberg@14516
   623
    fun initial_maps thy =
haftmann@38551
   624
        thy |> add_hol4_type_mapping "min" "bool" false @{type_name bool}
wenzelm@32960
   625
            |> add_hol4_type_mapping "min" "fun" false "fun"
haftmann@38551
   626
            |> add_hol4_type_mapping "min" "ind" false @{type_name ind}
haftmann@38864
   627
            |> add_hol4_const_mapping "min" "=" false @{const_name HOL.eq}
haftmann@38786
   628
            |> add_hol4_const_mapping "min" "==>" false @{const_name HOL.implies}
haftmann@38551
   629
            |> add_hol4_const_mapping "min" "@" false @{const_name "Eps"}
skalberg@14516
   630
in
skalberg@14516
   631
val hol4_setup =
wenzelm@18708
   632
  initial_maps #>
wenzelm@30528
   633
  Attrib.setup @{binding hol4rew} (Scan.succeed add_hol4_rewrite) "HOL4 rewrite rule"
wenzelm@30528
   634
skalberg@14516
   635
end