src/HOL/Import/import_syntax.ML
author wenzelm
Mon May 17 15:11:25 2010 +0200 (2010-05-17)
changeset 36959 f5417836dbea
parent 33317 b4534348b8fd
child 36960 01594f816e3a
permissions -rw-r--r--
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
eliminated slightly odd alias structure T;
skalberg@14620
     1
(*  Title:      HOL/Import/import_syntax.ML
skalberg@14620
     2
    Author:     Sebastian Skalberg (TU Muenchen)
skalberg@14620
     3
*)
skalberg@14620
     4
skalberg@14516
     5
signature HOL4_IMPORT_SYNTAX =
skalberg@14516
     6
sig
wenzelm@36959
     7
    val import_segment: (theory -> theory) parser
wenzelm@36959
     8
    val import_theory : (theory -> theory) parser
wenzelm@36959
     9
    val end_import    : (theory -> theory) parser
wenzelm@36959
    10
                        
wenzelm@36959
    11
    val setup_theory  : (theory -> theory) parser
wenzelm@36959
    12
    val end_setup     : (theory -> theory) parser
wenzelm@36959
    13
                        
wenzelm@36959
    14
    val thm_maps      : (theory -> theory) parser
wenzelm@36959
    15
    val ignore_thms   : (theory -> theory) parser
wenzelm@36959
    16
    val type_maps     : (theory -> theory) parser
wenzelm@36959
    17
    val def_maps      : (theory -> theory) parser
wenzelm@36959
    18
    val const_maps    : (theory -> theory) parser
wenzelm@36959
    19
    val const_moves   : (theory -> theory) parser
wenzelm@36959
    20
    val const_renames : (theory -> theory) parser
skalberg@14516
    21
wenzelm@36959
    22
    val skip_import_dir : (theory -> theory) parser
wenzelm@36959
    23
    val skip_import     : (theory -> theory) parser
obua@19064
    24
skalberg@14516
    25
    val setup        : unit -> unit
skalberg@14516
    26
end
skalberg@14516
    27
skalberg@14516
    28
structure HOL4ImportSyntax :> HOL4_IMPORT_SYNTAX =
skalberg@14516
    29
struct
skalberg@14516
    30
wenzelm@17057
    31
local structure P = OuterParse and K = OuterKeyword in
skalberg@14516
    32
skalberg@14516
    33
(* Parsers *)
skalberg@14516
    34
skalberg@14516
    35
val import_segment = P.name >> set_import_segment
skalberg@14516
    36
obua@19064
    37
skalberg@14516
    38
val import_theory = P.name >> (fn thyname =>
wenzelm@32960
    39
                               fn thy =>
wenzelm@32960
    40
                                  thy |> set_generating_thy thyname
wenzelm@32960
    41
                                      |> Sign.add_path thyname
wenzelm@32960
    42
                                      |> add_dump (";setup_theory " ^ thyname))
skalberg@14516
    43
obua@19064
    44
fun do_skip_import_dir s = (ImportRecorder.set_skip_import_dir (SOME s); I)
wenzelm@36959
    45
val skip_import_dir = P.string >> do_skip_import_dir
obua@19064
    46
obua@19064
    47
val lower = String.map Char.toLower
obua@19064
    48
fun do_skip_import s = (ImportRecorder.set_skip_import (case lower s of "on" => true | "off" => false | _ => Scan.fail ()); I)
wenzelm@36959
    49
val skip_import = P.short_ident >> do_skip_import
obua@19064
    50
skalberg@14518
    51
fun end_import toks =
skalberg@14518
    52
    Scan.succeed
wenzelm@32960
    53
        (fn thy =>
wenzelm@32960
    54
            let
wenzelm@32960
    55
                val thyname = get_generating_thy thy
wenzelm@32960
    56
                val segname = get_import_segment thy
wenzelm@32960
    57
                val (int_thms,facts) = Replay.setup_int_thms thyname thy
wenzelm@33317
    58
                val thmnames = filter_out (should_ignore thyname thy) facts
wenzelm@32960
    59
                fun replay thy = 
wenzelm@32960
    60
                    let
wenzelm@32960
    61
                        val _ = ImportRecorder.load_history thyname
wenzelm@32960
    62
                        val thy = Replay.import_thms thyname int_thms thmnames thy
wenzelm@32960
    63
                            handle x => (ImportRecorder.save_history thyname; raise x)
wenzelm@32960
    64
                        val _ = ImportRecorder.save_history thyname
wenzelm@32960
    65
                        val _ = ImportRecorder.clear_history ()
wenzelm@32960
    66
                    in
wenzelm@32960
    67
                        thy
wenzelm@32960
    68
                    end                                 
wenzelm@32960
    69
            in
wenzelm@32960
    70
                thy |> clear_import_thy
wenzelm@32960
    71
                    |> set_segment thyname segname
wenzelm@32960
    72
                    |> set_used_names facts
wenzelm@32960
    73
                    |> replay 
wenzelm@32960
    74
                    |> clear_used_names
wenzelm@32960
    75
                    |> export_hol4_pending
wenzelm@32960
    76
                    |> Sign.parent_path
wenzelm@32960
    77
                    |> dump_import_thy thyname
wenzelm@32960
    78
                    |> add_dump ";end_setup"
wenzelm@32960
    79
            end) toks
skalberg@14516
    80
skalberg@14516
    81
val ignore_thms = Scan.repeat1 P.name
wenzelm@32960
    82
                               >> (fn ignored =>
wenzelm@32960
    83
                                   fn thy =>
wenzelm@32960
    84
                                      let
wenzelm@32960
    85
                                          val thyname = get_import_thy thy
wenzelm@32960
    86
                                      in
wenzelm@32960
    87
                                          Library.foldl (fn (thy,thmname) => ignore_hol4 thyname thmname thy) (thy,ignored)
wenzelm@32960
    88
                                      end)
skalberg@14516
    89
skalberg@14516
    90
val thm_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname)
wenzelm@32960
    91
                            >> (fn thmmaps =>
wenzelm@32960
    92
                                fn thy =>
wenzelm@32960
    93
                                   let
wenzelm@32960
    94
                                       val thyname = get_import_thy thy
wenzelm@32960
    95
                                   in
wenzelm@32960
    96
                                       Library.foldl (fn (thy,(hol,isa)) => add_hol4_mapping thyname hol isa thy) (thy,thmmaps)
wenzelm@32960
    97
                                   end)
skalberg@14516
    98
skalberg@14516
    99
val type_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname)
wenzelm@32960
   100
                             >> (fn typmaps =>
wenzelm@32960
   101
                                 fn thy =>
wenzelm@32960
   102
                                    let
wenzelm@32960
   103
                                        val thyname = get_import_thy thy
wenzelm@32960
   104
                                    in
wenzelm@32960
   105
                                        Library.foldl (fn (thy,(hol,isa)) => add_hol4_type_mapping thyname hol false isa thy) (thy,typmaps)
wenzelm@32960
   106
                                    end)
skalberg@14516
   107
skalberg@14516
   108
val def_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname)
wenzelm@32960
   109
                            >> (fn defmaps =>
wenzelm@32960
   110
                                fn thy =>
wenzelm@32960
   111
                                   let
wenzelm@32960
   112
                                       val thyname = get_import_thy thy
wenzelm@32960
   113
                                   in
wenzelm@32960
   114
                                       Library.foldl (fn (thy,(hol,isa)) => add_defmap thyname hol isa thy) (thy,defmaps)
wenzelm@32960
   115
                                   end)
skalberg@14516
   116
skalberg@14516
   117
val const_renames = Scan.repeat1 (P.name --| P.$$$ ">" -- P.name)
wenzelm@32960
   118
                                 >> (fn renames =>
wenzelm@32960
   119
                                     fn thy =>
wenzelm@32960
   120
                                        let
wenzelm@32960
   121
                                            val thyname = get_import_thy thy
wenzelm@32960
   122
                                        in
wenzelm@32960
   123
                                            Library.foldl (fn (thy,(hol,isa)) => add_hol4_const_renaming thyname hol isa thy) (thy,renames)
wenzelm@32960
   124
                                        end)
wenzelm@32960
   125
                                                                                                                                      
skalberg@14516
   126
val const_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
wenzelm@32960
   127
                              >> (fn constmaps =>
wenzelm@32960
   128
                                  fn thy =>
wenzelm@32960
   129
                                     let
wenzelm@32960
   130
                                         val thyname = get_import_thy thy
wenzelm@32960
   131
                                     in
wenzelm@32960
   132
                                         Library.foldl (fn (thy,((hol,isa),NONE)) => add_hol4_const_mapping thyname hol false isa thy
wenzelm@32960
   133
                                                 | (thy,((hol,isa),SOME ty)) => add_hol4_const_wt_mapping thyname hol false isa (Syntax.read_typ_global thy ty) thy) (thy,constmaps)
wenzelm@32960
   134
                                     end)
skalberg@14516
   135
skalberg@14516
   136
val const_moves = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
wenzelm@32960
   137
                               >> (fn constmaps =>
wenzelm@32960
   138
                                   fn thy =>
wenzelm@32960
   139
                                      let
wenzelm@32960
   140
                                          val thyname = get_import_thy thy
wenzelm@32960
   141
                                      in
wenzelm@32960
   142
                                          Library.foldl (fn (thy,((hol,isa),NONE)) => add_hol4_const_mapping thyname hol true isa thy
wenzelm@32960
   143
                                                  | (thy,((hol,isa),SOME ty)) => add_hol4_const_wt_mapping thyname hol true isa (Syntax.read_typ_global thy ty) thy) (thy,constmaps)
wenzelm@32960
   144
                                      end)
skalberg@14516
   145
skalberg@14516
   146
fun import_thy s =
skalberg@14516
   147
    let
wenzelm@32960
   148
        val is = TextIO.openIn(s ^ ".imp")
wenzelm@32960
   149
        val inp = TextIO.inputAll is
wenzelm@32960
   150
        val _ = TextIO.closeIn is
wenzelm@32960
   151
        val orig_source = Source.of_string inp
wenzelm@32960
   152
        val symb_source = Symbol.source {do_recover = false} orig_source
wenzelm@32960
   153
        val lexes = Unsynchronized.ref
wenzelm@32960
   154
          (Scan.make_lexicon (map Symbol.explode ["import_segment","ignore_thms","import","end",">","::","const_maps","const_moves","thm_maps","const_renames","type_maps","def_maps"]),
wenzelm@32960
   155
                  Scan.empty_lexicon)
wenzelm@32960
   156
        val get_lexes = fn () => !lexes
wenzelm@36959
   157
        val token_source = Token.source {do_recover = NONE} get_lexes Position.start symb_source
wenzelm@36959
   158
        val token_list = filter_out (Token.is_kind Token.Space) (Source.exhaust token_source)
wenzelm@32960
   159
        val import_segmentP = OuterParse.$$$ "import_segment" |-- import_segment
wenzelm@32960
   160
        val type_mapsP = OuterParse.$$$ "type_maps" |-- type_maps
wenzelm@32960
   161
        val def_mapsP = OuterParse.$$$ "def_maps" |-- def_maps
wenzelm@32960
   162
        val const_mapsP = OuterParse.$$$ "const_maps" |-- const_maps
wenzelm@32960
   163
        val const_movesP = OuterParse.$$$ "const_moves" |-- const_moves
wenzelm@32960
   164
        val const_renamesP = OuterParse.$$$ "const_renames" |-- const_renames
wenzelm@32960
   165
        val ignore_thmsP = OuterParse.$$$ "ignore_thms" |-- ignore_thms
wenzelm@32960
   166
        val thm_mapsP = OuterParse.$$$ "thm_maps" |-- thm_maps
wenzelm@32960
   167
        val parser = type_mapsP || def_mapsP || const_mapsP || const_movesP || const_renamesP || thm_mapsP || ignore_thmsP || import_segmentP
wenzelm@32960
   168
        val importP = OuterParse.$$$ "import" |-- Scan.repeat parser --| OuterParse.$$$ "end"
wenzelm@32960
   169
        fun apply [] thy = thy
wenzelm@32960
   170
          | apply (f::fs) thy = apply fs (f thy)
skalberg@14516
   171
    in
wenzelm@32960
   172
        apply (set_replaying_thy s::Sign.add_path s::(fst (importP token_list)))
skalberg@14516
   173
    end
skalberg@14516
   174
skalberg@14620
   175
fun create_int_thms thyname thy =
wenzelm@17370
   176
    if ! quick_and_dirty
skalberg@14627
   177
    then thy
skalberg@14627
   178
    else
wenzelm@32960
   179
        case ImportData.get thy of
wenzelm@32960
   180
            NONE => ImportData.put (SOME (fst (Replay.setup_int_thms thyname thy))) thy
wenzelm@32960
   181
          | SOME _ => error "Import data not closed - forgotten an end_setup, mayhap?"
skalberg@14620
   182
skalberg@14620
   183
fun clear_int_thms thy =
wenzelm@17370
   184
    if ! quick_and_dirty
skalberg@14627
   185
    then thy
skalberg@14627
   186
    else
wenzelm@32960
   187
        case ImportData.get thy of
wenzelm@32960
   188
            NONE => error "Import data already cleared - forgotten a setup_theory?"
wenzelm@32960
   189
          | SOME _ => ImportData.put NONE thy
skalberg@14620
   190
skalberg@14516
   191
val setup_theory = P.name
wenzelm@32960
   192
                       >>
wenzelm@32960
   193
                       (fn thyname =>
wenzelm@32960
   194
                        fn thy =>
wenzelm@32960
   195
                           (case HOL4DefThy.get thy of
wenzelm@32960
   196
                                NoImport => thy
wenzelm@32960
   197
                              | Generating _ => error "Currently generating a theory!"
wenzelm@32960
   198
                              | Replaying _ => thy |> clear_import_thy)
wenzelm@32960
   199
                               |> import_thy thyname
wenzelm@32960
   200
                               |> create_int_thms thyname)
skalberg@14516
   201
skalberg@14518
   202
fun end_setup toks =
skalberg@14518
   203
    Scan.succeed
wenzelm@32960
   204
        (fn thy =>
wenzelm@32960
   205
            let
wenzelm@32960
   206
                val thyname = get_import_thy thy
wenzelm@32960
   207
                val segname = get_import_segment thy
wenzelm@32960
   208
            in
wenzelm@32960
   209
                thy |> set_segment thyname segname
wenzelm@32960
   210
                    |> clear_import_thy
wenzelm@32960
   211
                    |> clear_int_thms
wenzelm@32960
   212
                    |> Sign.parent_path
wenzelm@32960
   213
            end) toks
skalberg@14516
   214
skalberg@14516
   215
val set_dump  = P.string -- P.string   >> setup_dump
skalberg@14518
   216
fun fl_dump toks  = Scan.succeed flush_dump toks
skalberg@14516
   217
val append_dump = (P.verbatim || P.string) >> add_dump
skalberg@14516
   218
wenzelm@24867
   219
fun setup () =
wenzelm@27353
   220
  (OuterKeyword.keyword ">";
wenzelm@24867
   221
   OuterSyntax.command "import_segment"
wenzelm@32960
   222
                       "Set import segment name"
wenzelm@32960
   223
                       K.thy_decl (import_segment >> Toplevel.theory);
skalberg@14516
   224
   OuterSyntax.command "import_theory"
wenzelm@32960
   225
                       "Set default HOL4 theory name"
wenzelm@32960
   226
                       K.thy_decl (import_theory >> Toplevel.theory);
skalberg@14516
   227
   OuterSyntax.command "end_import"
wenzelm@32960
   228
                       "End HOL4 import"
wenzelm@32960
   229
                       K.thy_decl (end_import >> Toplevel.theory);
obua@19064
   230
   OuterSyntax.command "skip_import_dir" 
obua@19064
   231
                       "Sets caching directory for skipping importing"
wenzelm@24867
   232
                       K.thy_decl (skip_import_dir >> Toplevel.theory);
obua@19064
   233
   OuterSyntax.command "skip_import" 
obua@19064
   234
                       "Switches skipping importing on or off"
wenzelm@32960
   235
                       K.thy_decl (skip_import >> Toplevel.theory);                   
skalberg@14516
   236
   OuterSyntax.command "setup_theory"
wenzelm@32960
   237
                       "Setup HOL4 theory replaying"
wenzelm@32960
   238
                       K.thy_decl (setup_theory >> Toplevel.theory);
skalberg@14516
   239
   OuterSyntax.command "end_setup"
wenzelm@32960
   240
                       "End HOL4 setup"
wenzelm@32960
   241
                       K.thy_decl (end_setup >> Toplevel.theory);
skalberg@14516
   242
   OuterSyntax.command "setup_dump"
wenzelm@32960
   243
                       "Setup the dump file name"
wenzelm@32960
   244
                       K.thy_decl (set_dump >> Toplevel.theory);
skalberg@14516
   245
   OuterSyntax.command "append_dump"
wenzelm@32960
   246
                       "Add text to dump file"
wenzelm@32960
   247
                       K.thy_decl (append_dump >> Toplevel.theory);
skalberg@14516
   248
   OuterSyntax.command "flush_dump"
wenzelm@32960
   249
                       "Write the dump to file"
wenzelm@32960
   250
                       K.thy_decl (fl_dump >> Toplevel.theory);
skalberg@14516
   251
   OuterSyntax.command "ignore_thms"
wenzelm@32960
   252
                       "Theorems to ignore in next HOL4 theory import"
wenzelm@32960
   253
                       K.thy_decl (ignore_thms >> Toplevel.theory);
skalberg@14516
   254
   OuterSyntax.command "type_maps"
wenzelm@32960
   255
                       "Map HOL4 type names to existing Isabelle/HOL type names"
wenzelm@32960
   256
                       K.thy_decl (type_maps >> Toplevel.theory);
skalberg@14516
   257
   OuterSyntax.command "def_maps"
wenzelm@32960
   258
                       "Map HOL4 constant names to their primitive definitions"
wenzelm@32960
   259
                       K.thy_decl (def_maps >> Toplevel.theory);
skalberg@14516
   260
   OuterSyntax.command "thm_maps"
wenzelm@32960
   261
                       "Map HOL4 theorem names to existing Isabelle/HOL theorem names"
wenzelm@32960
   262
                       K.thy_decl (thm_maps >> Toplevel.theory);
skalberg@14516
   263
   OuterSyntax.command "const_renames"
wenzelm@32960
   264
                       "Rename HOL4 const names"
wenzelm@32960
   265
                       K.thy_decl (const_renames >> Toplevel.theory);
skalberg@14516
   266
   OuterSyntax.command "const_moves"
wenzelm@32960
   267
                       "Rename HOL4 const names to other HOL4 constants"
wenzelm@32960
   268
                       K.thy_decl (const_moves >> Toplevel.theory);
skalberg@14516
   269
   OuterSyntax.command "const_maps"
wenzelm@32960
   270
                       "Map HOL4 const names to existing Isabelle/HOL const names"
wenzelm@32960
   271
                       K.thy_decl (const_maps >> Toplevel.theory));
skalberg@14516
   272
skalberg@14516
   273
end
skalberg@14516
   274
skalberg@14516
   275
end