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