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