src/HOL/Import/import_syntax.ML
author obua
Mon Sep 26 02:27:14 2005 +0200 (2005-09-26)
changeset 17644 bd59bfd4bf37
parent 17370 754b7fcff03e
child 19064 bf19cc5a7899
permissions -rw-r--r--
fixed disambiguation problem
skalberg@14620
     1
(*  Title:      HOL/Import/import_syntax.ML
skalberg@14620
     2
    ID:         $Id$
skalberg@14620
     3
    Author:     Sebastian Skalberg (TU Muenchen)
skalberg@14620
     4
*)
skalberg@14620
     5
skalberg@14516
     6
signature HOL4_IMPORT_SYNTAX =
skalberg@14516
     7
sig
skalberg@14516
     8
    type token = OuterSyntax.token
skalberg@14516
     9
skalberg@14516
    10
    val import_segment: token list -> (theory -> theory) * token list
skalberg@14516
    11
    val import_theory : token list -> (theory -> theory) * token list
skalberg@14516
    12
    val end_import    : token list -> (theory -> theory) * token list
skalberg@14516
    13
skalberg@14516
    14
    val setup_theory  : token list -> (theory -> theory) * token list
skalberg@14516
    15
    val end_setup     : token list -> (theory -> theory) * token list
skalberg@14516
    16
skalberg@14516
    17
    val thm_maps      : token list -> (theory -> theory) * token list
skalberg@14516
    18
    val ignore_thms   : token list -> (theory -> theory) * token list
skalberg@14516
    19
    val type_maps     : token list -> (theory -> theory) * token list
skalberg@14516
    20
    val def_maps      : token list -> (theory -> theory) * token list
skalberg@14516
    21
    val const_maps    : token list -> (theory -> theory) * token list
skalberg@14516
    22
    val const_moves   : token list -> (theory -> theory) * token list
skalberg@14516
    23
    val const_renames : token list -> (theory -> theory) * token list
skalberg@14516
    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
type token = OuterSyntax.token
skalberg@14516
    32
wenzelm@17057
    33
local structure P = OuterParse and K = OuterKeyword in
skalberg@14516
    34
skalberg@14516
    35
(* Parsers *)
skalberg@14516
    36
skalberg@14516
    37
val import_segment = P.name >> set_import_segment
skalberg@14516
    38
skalberg@14516
    39
val import_theory = P.name >> (fn thyname =>
skalberg@14516
    40
			       fn thy =>
skalberg@14516
    41
				  thy |> set_generating_thy thyname
skalberg@14516
    42
				      |> Theory.add_path thyname
skalberg@14516
    43
				      |> add_dump (";setup_theory " ^ thyname))
skalberg@14516
    44
skalberg@14518
    45
fun end_import toks =
skalberg@14518
    46
    Scan.succeed
skalberg@14518
    47
	(fn thy =>
skalberg@14518
    48
	    let
skalberg@14518
    49
		val thyname = get_generating_thy thy
skalberg@14518
    50
		val segname = get_import_segment thy
skalberg@14518
    51
		val (int_thms,facts) = Replay.setup_int_thms thyname thy
skalberg@15570
    52
		val thmnames = List.filter (not o should_ignore thyname thy) facts
obua@17644
    53
               (* val _ = set show_types
obua@17644
    54
                val _ = set show_sorts*)
skalberg@14518
    55
	    in
skalberg@14518
    56
		thy |> clear_import_thy
skalberg@14518
    57
		    |> set_segment thyname segname
skalberg@14518
    58
		    |> set_used_names facts
skalberg@14518
    59
		    |> Replay.import_thms thyname int_thms thmnames
skalberg@14518
    60
		    |> clear_used_names
skalberg@14518
    61
		    |> export_hol4_pending
skalberg@14518
    62
		    |> Theory.parent_path
skalberg@14518
    63
		    |> dump_import_thy thyname
skalberg@14518
    64
		    |> add_dump ";end_setup"
skalberg@14518
    65
	    end) toks
skalberg@14516
    66
skalberg@14516
    67
val ignore_thms = Scan.repeat1 P.name
skalberg@14516
    68
			       >> (fn ignored =>
skalberg@14516
    69
				   fn thy =>
skalberg@14516
    70
				      let
skalberg@14516
    71
					  val thyname = get_import_thy thy
skalberg@14516
    72
				      in
skalberg@15570
    73
					  Library.foldl (fn (thy,thmname) => ignore_hol4 thyname thmname thy) (thy,ignored)
skalberg@14516
    74
				      end)
skalberg@14516
    75
skalberg@14516
    76
val thm_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname)
skalberg@14516
    77
			    >> (fn thmmaps =>
skalberg@14516
    78
				fn thy =>
skalberg@14516
    79
				   let
skalberg@14516
    80
				       val thyname = get_import_thy thy
skalberg@14516
    81
				   in
skalberg@15570
    82
				       Library.foldl (fn (thy,(hol,isa)) => add_hol4_mapping thyname hol isa thy) (thy,thmmaps)
skalberg@14516
    83
				   end)
skalberg@14516
    84
skalberg@14516
    85
val type_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname)
skalberg@14516
    86
			     >> (fn typmaps =>
skalberg@14516
    87
				 fn thy =>
skalberg@14516
    88
				    let
skalberg@14516
    89
					val thyname = get_import_thy thy
skalberg@14516
    90
				    in
skalberg@15570
    91
					Library.foldl (fn (thy,(hol,isa)) => add_hol4_type_mapping thyname hol false isa thy) (thy,typmaps)
skalberg@14516
    92
				    end)
skalberg@14516
    93
skalberg@14516
    94
val def_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname)
skalberg@14516
    95
			    >> (fn defmaps =>
skalberg@14516
    96
				fn thy =>
skalberg@14516
    97
				   let
skalberg@14516
    98
				       val thyname = get_import_thy thy
skalberg@14516
    99
				   in
skalberg@15570
   100
				       Library.foldl (fn (thy,(hol,isa)) => add_defmap thyname hol isa thy) (thy,defmaps)
skalberg@14516
   101
				   end)
skalberg@14516
   102
skalberg@14516
   103
val const_renames = Scan.repeat1 (P.name --| P.$$$ ">" -- P.name)
skalberg@14516
   104
				 >> (fn renames =>
skalberg@14516
   105
				     fn thy =>
skalberg@14516
   106
					let
skalberg@14516
   107
					    val thyname = get_import_thy thy
skalberg@14516
   108
					in
skalberg@15570
   109
					    Library.foldl (fn (thy,(hol,isa)) => add_hol4_const_renaming thyname hol isa thy) (thy,renames)
skalberg@14516
   110
					end)
skalberg@14516
   111
																      
skalberg@14516
   112
val const_maps = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
skalberg@14516
   113
			      >> (fn constmaps =>
skalberg@14516
   114
				  fn thy =>
skalberg@14516
   115
				     let
skalberg@14516
   116
					 val thyname = get_import_thy thy
skalberg@14516
   117
				     in
skalberg@15570
   118
					 Library.foldl (fn (thy,((hol,isa),NONE)) => add_hol4_const_mapping thyname hol false isa thy
skalberg@15531
   119
						 | (thy,((hol,isa),SOME ty)) => add_hol4_const_wt_mapping thyname hol false isa (typ_of (read_ctyp (sign_of thy) ty)) thy) (thy,constmaps)
skalberg@14516
   120
				     end)
skalberg@14516
   121
skalberg@14516
   122
val const_moves = Scan.repeat1 (P.name --| P.$$$ ">" -- P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
skalberg@14516
   123
			       >> (fn constmaps =>
skalberg@14516
   124
				   fn thy =>
skalberg@14516
   125
				      let
skalberg@14516
   126
					  val thyname = get_import_thy thy
skalberg@14516
   127
				      in
skalberg@15570
   128
					  Library.foldl (fn (thy,((hol,isa),NONE)) => add_hol4_const_mapping thyname hol true isa thy
skalberg@15531
   129
						  | (thy,((hol,isa),SOME ty)) => add_hol4_const_wt_mapping thyname hol true isa (typ_of (read_ctyp (sign_of thy) ty)) thy) (thy,constmaps)
skalberg@14516
   130
				      end)
skalberg@14516
   131
skalberg@14516
   132
fun import_thy s =
skalberg@14516
   133
    let
skalberg@14516
   134
	val is = TextIO.openIn(s ^ ".imp")
skalberg@14516
   135
	val inp = TextIO.inputAll is
skalberg@14516
   136
	val _ = TextIO.closeIn is
skalberg@14516
   137
	val orig_source = Source.of_string inp
skalberg@14516
   138
	val symb_source = Symbol.source false orig_source
skalberg@14516
   139
	val lexes = ref (OuterLex.make_lexicon ["import_segment","ignore_thms","import","end",">","::","const_maps","const_moves","thm_maps","const_renames","type_maps","def_maps"],
skalberg@14516
   140
			 Scan.empty_lexicon)
skalberg@14516
   141
	val get_lexes = fn () => !lexes
skalberg@14516
   142
	val token_source = OuterLex.source false get_lexes (Position.line 1) symb_source
skalberg@15570
   143
	val token_list = filter_out (OuterLex.is_kind OuterLex.Space) (Source.exhaust token_source)
skalberg@14516
   144
	val import_segmentP = OuterParse.$$$ "import_segment" |-- import_segment
skalberg@14516
   145
	val type_mapsP = OuterParse.$$$ "type_maps" |-- type_maps
skalberg@14516
   146
	val def_mapsP = OuterParse.$$$ "def_maps" |-- def_maps
skalberg@14516
   147
	val const_mapsP = OuterParse.$$$ "const_maps" |-- const_maps
skalberg@14516
   148
	val const_movesP = OuterParse.$$$ "const_moves" |-- const_moves
skalberg@14516
   149
	val const_renamesP = OuterParse.$$$ "const_renames" |-- const_renames
skalberg@14516
   150
	val ignore_thmsP = OuterParse.$$$ "ignore_thms" |-- ignore_thms
skalberg@14516
   151
	val thm_mapsP = OuterParse.$$$ "thm_maps" |-- thm_maps
skalberg@14516
   152
	val parser = type_mapsP || def_mapsP || const_mapsP || const_movesP || const_renamesP || thm_mapsP || ignore_thmsP || import_segmentP
skalberg@14516
   153
	val importP = OuterParse.$$$ "import" |-- Scan.repeat parser --| OuterParse.$$$ "end"
skalberg@14516
   154
	fun apply [] thy = thy
skalberg@14516
   155
	  | apply (f::fs) thy = apply fs (f thy)
skalberg@14516
   156
    in
skalberg@14516
   157
	apply (set_replaying_thy s::Theory.add_path s::(fst (importP token_list)))
skalberg@14516
   158
    end
skalberg@14516
   159
skalberg@14620
   160
fun create_int_thms thyname thy =
wenzelm@17370
   161
    if ! quick_and_dirty
skalberg@14627
   162
    then thy
skalberg@14627
   163
    else
skalberg@14627
   164
	case ImportData.get thy of
skalberg@15531
   165
	    NONE => ImportData.put (SOME (fst (Replay.setup_int_thms thyname thy))) thy
skalberg@15531
   166
	  | SOME _ => error "Import data not closed - forgotten an end_setup, mayhap?"
skalberg@14620
   167
skalberg@14620
   168
fun clear_int_thms thy =
wenzelm@17370
   169
    if ! quick_and_dirty
skalberg@14627
   170
    then thy
skalberg@14627
   171
    else
skalberg@14627
   172
	case ImportData.get thy of
skalberg@15531
   173
	    NONE => error "Import data already cleared - forgotten a setup_theory?"
skalberg@15531
   174
	  | SOME _ => ImportData.put NONE thy
skalberg@14620
   175
skalberg@14516
   176
val setup_theory = P.name
skalberg@14516
   177
		       >>
skalberg@14516
   178
		       (fn thyname =>
skalberg@14516
   179
			fn thy =>
skalberg@14620
   180
			   (case HOL4DefThy.get thy of
skalberg@14620
   181
				NoImport => thy
skalberg@14620
   182
			      | Generating _ => error "Currently generating a theory!"
skalberg@14620
   183
			      | Replaying _ => thy |> clear_import_thy)
skalberg@14620
   184
			       |> import_thy thyname
skalberg@14620
   185
			       |> create_int_thms thyname)
skalberg@14516
   186
skalberg@14518
   187
fun end_setup toks =
skalberg@14518
   188
    Scan.succeed
skalberg@14518
   189
	(fn thy =>
skalberg@14518
   190
	    let
skalberg@14518
   191
		val thyname = get_import_thy thy
skalberg@14518
   192
		val segname = get_import_segment thy
skalberg@14518
   193
	    in
skalberg@14518
   194
		thy |> set_segment thyname segname
skalberg@14518
   195
		    |> clear_import_thy
skalberg@14620
   196
		    |> clear_int_thms
skalberg@14518
   197
		    |> Theory.parent_path
skalberg@14518
   198
	    end) toks
skalberg@14516
   199
skalberg@14516
   200
val set_dump  = P.string -- P.string   >> setup_dump
skalberg@14518
   201
fun fl_dump toks  = Scan.succeed flush_dump toks
skalberg@14516
   202
val append_dump = (P.verbatim || P.string) >> add_dump
skalberg@14516
   203
skalberg@14516
   204
val parsers = 
skalberg@14516
   205
  [OuterSyntax.command "import_segment"
skalberg@14516
   206
		       "Set import segment name"
skalberg@14516
   207
		       K.thy_decl (import_segment >> Toplevel.theory),
skalberg@14516
   208
   OuterSyntax.command "import_theory"
skalberg@14516
   209
		       "Set default HOL4 theory name"
skalberg@14516
   210
		       K.thy_decl (import_theory >> Toplevel.theory),
skalberg@14516
   211
   OuterSyntax.command "end_import"
skalberg@14516
   212
		       "End HOL4 import"
skalberg@14516
   213
		       K.thy_decl (end_import >> Toplevel.theory),
skalberg@14516
   214
   OuterSyntax.command "setup_theory"
skalberg@14516
   215
		       "Setup HOL4 theory replaying"
skalberg@14516
   216
		       K.thy_decl (setup_theory >> Toplevel.theory),
skalberg@14516
   217
   OuterSyntax.command "end_setup"
skalberg@14516
   218
		       "End HOL4 setup"
skalberg@14516
   219
		       K.thy_decl (end_setup >> Toplevel.theory),
skalberg@14516
   220
   OuterSyntax.command "setup_dump"
skalberg@14516
   221
		       "Setup the dump file name"
skalberg@14516
   222
		       K.thy_decl (set_dump >> Toplevel.theory),
skalberg@14516
   223
   OuterSyntax.command "append_dump"
skalberg@14516
   224
		       "Add text to dump file"
skalberg@14516
   225
		       K.thy_decl (append_dump >> Toplevel.theory),
skalberg@14516
   226
   OuterSyntax.command "flush_dump"
skalberg@14516
   227
		       "Write the dump to file"
skalberg@14516
   228
		       K.thy_decl (fl_dump >> Toplevel.theory),
skalberg@14516
   229
   OuterSyntax.command "ignore_thms"
skalberg@14516
   230
		       "Theorems to ignore in next HOL4 theory import"
skalberg@14516
   231
		       K.thy_decl (ignore_thms >> Toplevel.theory),
skalberg@14516
   232
   OuterSyntax.command "type_maps"
skalberg@14516
   233
		       "Map HOL4 type names to existing Isabelle/HOL type names"
skalberg@14516
   234
		       K.thy_decl (type_maps >> Toplevel.theory),
skalberg@14516
   235
   OuterSyntax.command "def_maps"
skalberg@14516
   236
		       "Map HOL4 constant names to their primitive definitions"
skalberg@14516
   237
		       K.thy_decl (def_maps >> Toplevel.theory),
skalberg@14516
   238
   OuterSyntax.command "thm_maps"
skalberg@14516
   239
		       "Map HOL4 theorem names to existing Isabelle/HOL theorem names"
skalberg@14516
   240
		       K.thy_decl (thm_maps >> Toplevel.theory),
skalberg@14516
   241
   OuterSyntax.command "const_renames"
skalberg@14516
   242
		       "Rename HOL4 const names"
skalberg@14516
   243
		       K.thy_decl (const_renames >> Toplevel.theory),
skalberg@14516
   244
   OuterSyntax.command "const_moves"
skalberg@14516
   245
		       "Rename HOL4 const names to other HOL4 constants"
skalberg@14516
   246
		       K.thy_decl (const_moves >> Toplevel.theory),
skalberg@14516
   247
   OuterSyntax.command "const_maps"
skalberg@14516
   248
		       "Map HOL4 const names to existing Isabelle/HOL const names"
skalberg@14516
   249
		       K.thy_decl (const_maps >> Toplevel.theory)]
skalberg@14516
   250
skalberg@14516
   251
fun setup () = (OuterSyntax.add_keywords[">"];
skalberg@14516
   252
		OuterSyntax.add_parsers parsers)
skalberg@14516
   253
skalberg@14516
   254
end
skalberg@14516
   255
skalberg@14516
   256
end