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