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