src/Pure/Isar/outer_syntax.ML
author wenzelm
Thu Jul 01 17:24:14 1999 +0200 (1999-07-01 ago)
changeset 6868 27ba88d57399
parent 6860 8dc6a1e6fa13
child 7026 69724548fad1
permissions -rw-r--r--
added prf_asm;
wenzelm@5829
     1
(*  Title:      Pure/Isar/outer_syntax.ML
wenzelm@5829
     2
    ID:         $Id$
wenzelm@5829
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5829
     4
wenzelm@5829
     5
The global Isabelle/Isar outer syntax.
wenzelm@6247
     6
wenzelm@6247
     7
TODO:
wenzelm@6247
     8
  - cleanup;
wenzelm@6247
     9
  - avoid string constants;
wenzelm@5829
    10
*)
wenzelm@5829
    11
wenzelm@5829
    12
signature BASIC_OUTER_SYNTAX =
wenzelm@5829
    13
sig
wenzelm@5829
    14
  val main: unit -> unit
wenzelm@5883
    15
  val loop: unit -> unit
wenzelm@6860
    16
  val sync_main: unit -> unit
wenzelm@6860
    17
  val sync_loop: unit -> unit
wenzelm@5829
    18
  val help: unit -> unit
wenzelm@5829
    19
end;
wenzelm@5829
    20
wenzelm@5829
    21
signature OUTER_SYNTAX =
wenzelm@5829
    22
sig
wenzelm@5829
    23
  include BASIC_OUTER_SYNTAX
wenzelm@6722
    24
  structure Keyword:
wenzelm@6722
    25
    sig
wenzelm@6722
    26
      val control: string
wenzelm@6722
    27
      val diag: string
wenzelm@6722
    28
      val thy_begin: string
wenzelm@6722
    29
      val thy_end: string
wenzelm@6722
    30
      val thy_heading: string
wenzelm@6722
    31
      val thy_decl: string
wenzelm@6722
    32
      val thy_goal: string
wenzelm@6722
    33
      val qed: string
wenzelm@6733
    34
      val qed_block: string
wenzelm@6722
    35
      val prf_goal: string
wenzelm@6722
    36
      val prf_block: string
wenzelm@6722
    37
      val prf_chain: string
wenzelm@6722
    38
      val prf_decl: string
wenzelm@6868
    39
      val prf_asm: string
wenzelm@6722
    40
      val prf_script: string
wenzelm@6722
    41
      val kinds: string list
wenzelm@6722
    42
    end
wenzelm@5829
    43
  type token
wenzelm@5829
    44
  type parser
wenzelm@6722
    45
  val command: string -> string -> string ->
wenzelm@6373
    46
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@6722
    47
  val improper_command: string -> string -> string ->
wenzelm@6373
    48
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@6722
    49
  val dest_syntax: unit -> string list * (string * string) list
wenzelm@5883
    50
  val print_outer_syntax: unit -> unit
wenzelm@5829
    51
  val add_keywords: string list -> unit
wenzelm@5829
    52
  val add_parsers: parser list -> unit
wenzelm@6247
    53
  val theory_header: token list -> (string * string list * (string * bool) list) * token list
wenzelm@6199
    54
  val deps_thy: string -> bool -> Path.T -> string list * Path.T list
wenzelm@6199
    55
  val load_thy: string -> bool -> bool -> Path.T -> unit
wenzelm@6860
    56
  val isar: bool -> Toplevel.isar
wenzelm@5829
    57
end;
wenzelm@5829
    58
wenzelm@5829
    59
structure OuterSyntax: OUTER_SYNTAX =
wenzelm@5829
    60
struct
wenzelm@5829
    61
wenzelm@6860
    62
structure P = OuterParse;
wenzelm@6860
    63
wenzelm@5829
    64
wenzelm@5829
    65
(** outer syntax **)
wenzelm@5829
    66
wenzelm@6722
    67
(* command keyword classification *)
wenzelm@6722
    68
wenzelm@6722
    69
structure Keyword =
wenzelm@6722
    70
struct
wenzelm@6722
    71
  val control = "control";
wenzelm@6722
    72
  val diag = "diag";
wenzelm@6722
    73
  val thy_begin = "theory-begin";
wenzelm@6722
    74
  val thy_end = "theory-end";
wenzelm@6722
    75
  val thy_heading = "theory-heading";
wenzelm@6722
    76
  val thy_decl = "theory-decl";
wenzelm@6722
    77
  val thy_goal = "theory-goal";
wenzelm@6722
    78
  val qed = "qed";
wenzelm@6733
    79
  val qed_block = "qed-block";
wenzelm@6722
    80
  val prf_goal = "proof-goal";
wenzelm@6722
    81
  val prf_block = "proof-block";
wenzelm@6722
    82
  val prf_chain = "proof-chain";
wenzelm@6722
    83
  val prf_decl = "proof-decl";
wenzelm@6868
    84
  val prf_asm = "proof-asm";
wenzelm@6722
    85
  val prf_script = "proof-script";
wenzelm@6722
    86
wenzelm@6722
    87
  val kinds = [control, diag, thy_begin, thy_end, thy_heading, thy_decl, thy_goal, qed,
wenzelm@6868
    88
    qed_block, prf_goal, prf_block, prf_chain, prf_decl, prf_asm, prf_script];
wenzelm@6722
    89
end;
wenzelm@6722
    90
wenzelm@6722
    91
wenzelm@5829
    92
(* parsers *)
wenzelm@5829
    93
wenzelm@5829
    94
type token = OuterLex.token;
wenzelm@5829
    95
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
wenzelm@5829
    96
wenzelm@5829
    97
datatype parser =
wenzelm@6722
    98
  Parser of string * (string * string) * bool * parser_fn;
wenzelm@5829
    99
wenzelm@6722
   100
fun parser int_only name comment kind parse = Parser (name, (comment, kind), int_only, parse);
wenzelm@5829
   101
wenzelm@5829
   102
wenzelm@5829
   103
(* parse command *)
wenzelm@5829
   104
wenzelm@6860
   105
local
wenzelm@6199
   106
wenzelm@5829
   107
fun command_name cmd =
wenzelm@6860
   108
  P.group "command"
wenzelm@6860
   109
    (P.position (Scan.one (OuterLex.keyword_pred (is_some o cmd)) >> OuterLex.val_of));
wenzelm@5829
   110
wenzelm@5829
   111
fun command_body cmd (name, _) =
wenzelm@5829
   112
  let val (int_only, parse) = the (cmd name)
wenzelm@6860
   113
  in P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) end;
wenzelm@6860
   114
wenzelm@6860
   115
fun terminator false = Scan.succeed ()
wenzelm@6860
   116
  | terminator true = P.group "terminator" (Scan.option P.sync -- P.$$$ ";" >> K ());
wenzelm@6860
   117
wenzelm@6860
   118
in
wenzelm@5829
   119
wenzelm@6860
   120
fun command term cmd =
wenzelm@6860
   121
  P.$$$ ";" >> K None ||
wenzelm@6860
   122
  P.sync >> K None ||
wenzelm@6860
   123
  (command_name cmd :-- command_body cmd) --| terminator term
wenzelm@6860
   124
    >> (fn ((name, pos), (int_only, f)) =>
wenzelm@6860
   125
      Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
   126
        Toplevel.interactive int_only |> f));
wenzelm@5829
   127
wenzelm@6199
   128
end;
wenzelm@6199
   129
wenzelm@5829
   130
wenzelm@5829
   131
wenzelm@5829
   132
(** global syntax state **)
wenzelm@5829
   133
wenzelm@5829
   134
val global_lexicon = ref Scan.empty_lexicon;
wenzelm@6722
   135
val global_parsers = ref (Symtab.empty: ((string * string) * (bool * parser_fn)) Symtab.table);
wenzelm@5952
   136
wenzelm@5829
   137
wenzelm@5829
   138
(* print syntax *)
wenzelm@5829
   139
wenzelm@6722
   140
fun dest_syntax () =
wenzelm@6722
   141
  (map implode (Scan.dest_lexicon (! global_lexicon)),
wenzelm@6722
   142
    map (fn (name, ((_, kind), _)) => (name, kind)) (Symtab.dest (! global_parsers)));
wenzelm@6722
   143
wenzelm@5829
   144
fun print_outer_syntax () =
wenzelm@5829
   145
  let
wenzelm@5829
   146
    val keywords = map implode (Scan.dest_lexicon (! global_lexicon));
wenzelm@6722
   147
    fun pretty_cmd (name, ((comment, _), _)) =
wenzelm@5829
   148
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5829
   149
    val (int_cmds, cmds) = partition (#1 o #2 o #2) (Symtab.dest (! global_parsers));
wenzelm@5829
   150
  in
wenzelm@6107
   151
    Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote keywords));
wenzelm@6095
   152
    Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
wenzelm@6095
   153
    Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
wenzelm@6095
   154
      (map pretty_cmd int_cmds))
wenzelm@5829
   155
  end;
wenzelm@5829
   156
wenzelm@5829
   157
wenzelm@5829
   158
(* augment syntax *)
wenzelm@5829
   159
wenzelm@5829
   160
fun add_keywords keywords =
wenzelm@5829
   161
  global_lexicon := Scan.extend_lexicon (! global_lexicon) (map Symbol.explode keywords);
wenzelm@5829
   162
wenzelm@5829
   163
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
wenzelm@5829
   164
 (if is_none (Symtab.lookup (tab, name)) then ()
wenzelm@5829
   165
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@5829
   166
  Symtab.update ((name, (comment, (int_only, parse))), tab));
wenzelm@5829
   167
wenzelm@5829
   168
fun add_parsers parsers =
wenzelm@5829
   169
  (global_parsers := foldl add_parser (! global_parsers, parsers);
wenzelm@5829
   170
    add_keywords (map (fn Parser (name, _, _, _) => name) parsers));
wenzelm@5829
   171
wenzelm@5829
   172
wenzelm@5829
   173
(* get current lexer / parser *)
wenzelm@5829
   174
wenzelm@5829
   175
(*Note: the syntax for files is statically determined at the very
wenzelm@5829
   176
  beginning; for interactive processing it may change dynamically.*)
wenzelm@5829
   177
wenzelm@5829
   178
fun get_lexicon () = ! global_lexicon;
wenzelm@5829
   179
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
wenzelm@5829
   180
wenzelm@5829
   181
wenzelm@5829
   182
wenzelm@5829
   183
(** read theory **)
wenzelm@5829
   184
wenzelm@6247
   185
(* theory keyword *)
wenzelm@6247
   186
wenzelm@6247
   187
val theoryN = "theory";
wenzelm@6247
   188
val theory_keyword = OuterParse.$$$ theoryN;
wenzelm@6247
   189
wenzelm@6247
   190
wenzelm@5829
   191
(* source *)
wenzelm@5829
   192
wenzelm@6860
   193
local
wenzelm@5829
   194
wenzelm@6860
   195
val no_terminator =
wenzelm@6860
   196
  Scan.unless (P.$$$ ";") (Scan.one (OuterLex.not_sync andf OuterLex.not_eof));
wenzelm@6860
   197
wenzelm@6860
   198
val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
wenzelm@6860
   199
wenzelm@6860
   200
in
wenzelm@5829
   201
wenzelm@6860
   202
fun source term do_recover cmd src =
wenzelm@5829
   203
  src
wenzelm@6860
   204
  |> Source.source OuterLex.stopper
wenzelm@6860
   205
    (Scan.bulk (fn xs => OuterParse.!!! (command term (cmd ())) xs))
wenzelm@6860
   206
    (if do_recover then Some recover else None)
wenzelm@5829
   207
  |> Source.mapfilter I;
wenzelm@5829
   208
wenzelm@6860
   209
end;
wenzelm@6860
   210
wenzelm@5829
   211
wenzelm@5829
   212
(* detect header *)
wenzelm@5829
   213
wenzelm@6199
   214
fun scan_header get_lexicon scan (src, pos) =
wenzelm@5829
   215
  src
wenzelm@5829
   216
  |> Symbol.source false
wenzelm@6199
   217
  |> OuterLex.source false get_lexicon pos
wenzelm@6199
   218
  |> Source.source OuterLex.stopper (Scan.single scan) None
wenzelm@5829
   219
  |> (fst o the o Source.get_single);
wenzelm@5829
   220
wenzelm@6247
   221
val check_header_lexicon = Scan.make_lexicon [Symbol.explode theoryN];
wenzelm@5829
   222
wenzelm@6199
   223
fun is_old_theory src =
wenzelm@6247
   224
  is_none (scan_header (K check_header_lexicon) (Scan.option theory_keyword) src);
wenzelm@6199
   225
wenzelm@6199
   226
fun warn_theory_style path is_old =
wenzelm@6199
   227
  let
wenzelm@6199
   228
    val style = if is_old then "old" else "new";
wenzelm@6199
   229
    val _ = warning ("Assuming " ^ style ^ "-style theory format for " ^ quote (Path.pack path));
wenzelm@6199
   230
  in is_old end;
wenzelm@6199
   231
wenzelm@6199
   232
wenzelm@6199
   233
(* deps_thy --- inspect theory header *)
wenzelm@6199
   234
wenzelm@6247
   235
val header_lexicon =
wenzelm@6247
   236
  Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
wenzelm@6199
   237
wenzelm@6860
   238
local
wenzelm@6199
   239
wenzelm@6860
   240
val file_name = (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
wenzelm@6247
   241
wenzelm@6247
   242
val theory_head =
wenzelm@6860
   243
  (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
wenzelm@6860
   244
    Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [])
wenzelm@6247
   245
  >> (fn ((A, Bs), files) => (A, Bs, files));
wenzelm@6247
   246
wenzelm@6860
   247
in
wenzelm@6860
   248
wenzelm@6860
   249
val theory_header = theory_head --| (Scan.ahead P.eof || P.$$$ ":");
wenzelm@6860
   250
val only_header = theory_keyword |-- theory_head --| Scan.ahead P.eof;
wenzelm@6860
   251
val new_header = theory_keyword |-- P.!!! theory_header;
wenzelm@6199
   252
wenzelm@6199
   253
val old_header =
wenzelm@6860
   254
  P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))
wenzelm@6247
   255
  >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
wenzelm@6199
   256
wenzelm@6199
   257
end;
wenzelm@5829
   258
wenzelm@6199
   259
fun deps_thy name ml path =
wenzelm@6199
   260
  let
wenzelm@6641
   261
    val src = File.source path;
wenzelm@6199
   262
    val is_old = warn_theory_style path (is_old_theory src);
wenzelm@6247
   263
    val (name', parents, files) =
wenzelm@6199
   264
      (*Note: old style headers dynamically depend on the current lexicon :-( *)
wenzelm@6199
   265
      if is_old then scan_header ThySyn.get_lexicon (Scan.error old_header) src
wenzelm@6247
   266
      else scan_header (K header_lexicon) (Scan.error new_header) src;
wenzelm@6199
   267
wenzelm@6199
   268
    val ml_path = ThyLoad.ml_path name;
wenzelm@6199
   269
    val ml_file = if not ml orelse is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
wenzelm@6199
   270
  in
wenzelm@6199
   271
    if name <> name' then
wenzelm@6199
   272
      error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
wenzelm@6247
   273
    else (parents, map (Path.unpack o #1) files @ ml_file)
wenzelm@6199
   274
  end;
wenzelm@6199
   275
wenzelm@6199
   276
wenzelm@6199
   277
(* load_thy --- read text (including header) *)
wenzelm@6199
   278
wenzelm@6247
   279
fun try_ml_file name ml time =
wenzelm@6199
   280
  let
wenzelm@6199
   281
    val path = ThyLoad.ml_path name;
wenzelm@6247
   282
    val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
wenzelm@6247
   283
    val tr_name = if time then "time_use" else "use";
wenzelm@6199
   284
  in
wenzelm@6199
   285
    if not ml orelse is_none (ThyLoad.check_file path) then ()
wenzelm@6247
   286
    else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
wenzelm@6199
   287
  end;
wenzelm@6199
   288
wenzelm@6199
   289
fun parse_thy (src, pos) =
wenzelm@6247
   290
  let
wenzelm@6247
   291
    val lex_src =
wenzelm@6247
   292
      src
wenzelm@6247
   293
      |> Symbol.source false
wenzelm@6247
   294
      |> OuterLex.source false (K (get_lexicon ())) pos;
wenzelm@6247
   295
    val only_head =
wenzelm@6247
   296
      lex_src
wenzelm@6247
   297
      |> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
wenzelm@6247
   298
      |> (fst o the o Source.get_single);
wenzelm@6247
   299
  in
wenzelm@6247
   300
    (case only_head of
wenzelm@6247
   301
      None =>
wenzelm@6247
   302
        lex_src
wenzelm@6860
   303
        |> source false false (K (get_parser ()))
wenzelm@6247
   304
        |> Source.exhaust
wenzelm@6247
   305
    | Some spec =>
wenzelm@6247
   306
        [Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
wenzelm@6247
   307
          Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
wenzelm@6247
   308
  end;
wenzelm@5829
   309
wenzelm@6247
   310
fun run_thy name path =
wenzelm@6641
   311
  let val (src, pos) = File.source path in
wenzelm@6332
   312
    Present.theory_source name src;
wenzelm@6247
   313
    if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
wenzelm@6247
   314
    else (Toplevel.excursion (parse_thy (src, pos))
wenzelm@6247
   315
      handle exn => error (Toplevel.exn_message exn))
wenzelm@6247
   316
  end;
wenzelm@6199
   317
wenzelm@6199
   318
fun load_thy name ml time path =
wenzelm@6247
   319
 (if time then
wenzelm@6247
   320
    timeit (fn () =>
wenzelm@6247
   321
     (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
wenzelm@6247
   322
      setmp Goals.proof_timing true (run_thy name) path;
wenzelm@6247
   323
      writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
wenzelm@6247
   324
  else run_thy name path;
wenzelm@6247
   325
  Context.context (ThyInfo.get_theory name);
wenzelm@6247
   326
  try_ml_file name ml time);
wenzelm@5829
   327
wenzelm@5829
   328
wenzelm@5829
   329
(* interactive source of state transformers *)
wenzelm@5829
   330
wenzelm@6860
   331
fun isar term =
wenzelm@5829
   332
  Source.tty
wenzelm@5829
   333
  |> Symbol.source true
wenzelm@5829
   334
  |> OuterLex.source true get_lexicon (Position.line_name 1 "stdin")
wenzelm@6860
   335
  |> source term true get_parser;
wenzelm@5829
   336
wenzelm@5829
   337
wenzelm@5829
   338
wenzelm@5829
   339
(** the read-eval-print loop **)
wenzelm@5829
   340
wenzelm@5923
   341
(* main loop *)
wenzelm@5923
   342
wenzelm@6860
   343
fun gen_loop term = (Context.reset_context (); Toplevel.loop (isar term));
wenzelm@5829
   344
wenzelm@6860
   345
fun gen_main term =
wenzelm@5829
   346
 (Toplevel.set_state Toplevel.toplevel;
wenzelm@5829
   347
  ml_prompts "ML> " "ML# ";
wenzelm@6199
   348
  writeln (Session.welcome ());
wenzelm@6860
   349
  gen_loop term);
wenzelm@6860
   350
wenzelm@6860
   351
fun main () = gen_main false;
wenzelm@6860
   352
fun loop () = gen_loop false;
wenzelm@6860
   353
fun sync_main () = gen_main true;
wenzelm@6860
   354
fun sync_loop () = gen_loop true;
wenzelm@5829
   355
wenzelm@5829
   356
wenzelm@5829
   357
(* help *)
wenzelm@5829
   358
wenzelm@5829
   359
fun help () =
wenzelm@5829
   360
  writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
wenzelm@5883
   361
    \invoke 'loop();' to enter the Isar loop.");
wenzelm@5829
   362
wenzelm@5829
   363
wenzelm@6373
   364
(*final declarations of this structure!*)
wenzelm@6685
   365
val command = parser false;
wenzelm@6685
   366
val improper_command = parser true;
wenzelm@6685
   367
wenzelm@6373
   368
wenzelm@5829
   369
end;
wenzelm@5829
   370
wenzelm@6199
   371
(*setup theory syntax dependent operations*)
wenzelm@6199
   372
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
wenzelm@6199
   373
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
wenzelm@6199
   374
structure ThyLoad: THY_LOAD = ThyLoad;
wenzelm@6199
   375
wenzelm@5829
   376
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   377
open BasicOuterSyntax;