src/Pure/PIDE/command.ML
author wenzelm
Sat Mar 09 23:57:07 2019 +0100 (2 months ago ago)
changeset 70067 0cb8753bdb50
parent 70064 f8293bf510a0
child 70068 b9985133805d
permissions -rw-r--r--
clarified signature;
     1 (*  Title:      Pure/PIDE/command.ML
     2     Author:     Makarius
     3 
     4 Prover command execution: read -- eval -- print.
     5 *)
     6 
     7 signature COMMAND =
     8 sig
     9   type blob = (string * (SHA1.digest * string list) option) Exn.result
    10   val read_file: Path.T -> Position.T -> Path.T -> Token.file
    11   val read_thy: Toplevel.state -> theory
    12   val read: Keyword.keywords -> theory -> Path.T-> (unit -> theory) ->
    13     blob list * int -> Token.T list -> Toplevel.transition
    14   type eval
    15   val eval_command_id: eval -> Document_ID.command
    16   val eval_exec_id: eval -> Document_ID.exec
    17   val eval_eq: eval * eval -> bool
    18   val eval_running: eval -> bool
    19   val eval_finished: eval -> bool
    20   val eval_result_command: eval -> Toplevel.transition
    21   val eval_result_state: eval -> Toplevel.state
    22   val eval: Keyword.keywords -> Path.T -> (unit -> theory) ->
    23     blob list * int -> Document_ID.command -> Token.T list -> eval -> eval
    24   type print
    25   type print_fn = Toplevel.transition -> Toplevel.state -> unit
    26   val print0: {pri: int, print_fn: print_fn} -> eval -> print
    27   val print: bool -> (string * string list) list -> Keyword.keywords -> string ->
    28     eval -> print list -> print list option
    29   val parallel_print: print -> bool
    30   type print_function =
    31     {keywords: Keyword.keywords, command_name: string, args: string list, exec_id: Document_ID.exec} ->
    32       {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option
    33   val print_function: string -> print_function -> unit
    34   val no_print_function: string -> unit
    35   type exec = eval * print list
    36   val init_exec: theory option -> exec
    37   val no_exec: exec
    38   val exec_ids: exec option -> Document_ID.exec list
    39   val exec: Document_ID.execution -> exec -> unit
    40   val exec_parallel_prints: Document_ID.execution -> Future.task list -> exec -> exec option
    41 end;
    42 
    43 structure Command: COMMAND =
    44 struct
    45 
    46 (** main phases of execution **)
    47 
    48 fun task_context group f =
    49   f
    50   |> Future.interruptible_task
    51   |> Future.task_context "Command.run_process" group;
    52 
    53 
    54 (* read *)
    55 
    56 type blob =
    57   (string * (SHA1.digest * string list) option) Exn.result;  (*file node name, digest, lines*)
    58 
    59 fun read_file_node file_node master_dir pos src_path =
    60   let
    61     val _ = Position.report pos Markup.language_path;
    62     val _ =
    63       (case try Url.explode file_node of
    64         NONE => ()
    65       | SOME (Url.File _) => ()
    66       | _ =>
    67           error ("Prover cannot load remote file " ^
    68             Markup.markup (Markup.path file_node) (quote file_node)));
    69     val full_path = File.check_file (File.full_path master_dir src_path);
    70     val text = File.read full_path;
    71     val lines = split_lines text;
    72     val digest = SHA1.digest text;
    73     val file_pos = Position.copy_id pos (Path.position full_path);
    74   in {src_path = src_path, lines = lines, digest = digest, pos = file_pos} end
    75   handle ERROR msg => error (msg ^ Position.here pos);
    76 
    77 val read_file = read_file_node "";
    78 
    79 local
    80 
    81 fun blob_file src_path lines digest file_node =
    82   let
    83     val file_pos =
    84       Position.file file_node |>
    85       (case Position.get_id (Position.thread_data ()) of
    86         NONE => I
    87       | SOME exec_id => Position.put_id exec_id);
    88   in {src_path = src_path, lines = lines, digest = digest, pos = file_pos} end
    89 
    90 fun resolve_files keywords master_dir (blobs, blobs_index) toks =
    91   (case Outer_Syntax.parse_spans toks of
    92     [Command_Span.Span (Command_Span.Command_Span (cmd, _), _)] =>
    93       (case try (nth toks) blobs_index of
    94         SOME tok =>
    95           let
    96             val pos = Token.pos_of tok;
    97             val path = Path.explode (Token.content_of tok)
    98               handle ERROR msg => error (msg ^ Position.here pos);
    99             fun make_file src_path (Exn.Res (file_node, NONE)) =
   100                   Exn.interruptible_capture (fn () =>
   101                     read_file_node file_node master_dir pos src_path) ()
   102               | make_file src_path (Exn.Res (file_node, SOME (digest, lines))) =
   103                   (Position.report pos Markup.language_path;
   104                     Exn.Res (blob_file src_path lines digest file_node))
   105               | make_file _ (Exn.Exn e) = Exn.Exn e;
   106             val src_paths = Keyword.command_files keywords cmd path;
   107             val files =
   108               if null blobs then
   109                 map2 make_file src_paths (map (K (Exn.Res ("", NONE))) src_paths)
   110               else if length src_paths = length blobs then
   111                 map2 make_file src_paths blobs
   112               else error ("Misalignment of inlined files" ^ Position.here pos);
   113           in
   114             toks |> map_index (fn (i, tok) =>
   115               if i = blobs_index then Token.put_files files tok else tok)
   116           end
   117       | NONE => toks)
   118   | _ => toks);
   119 
   120 fun reports_of_token keywords tok =
   121   let
   122     val malformed_symbols =
   123       Input.source_explode (Token.input_of tok)
   124       |> map_filter (fn (sym, pos) =>
   125           if Symbol.is_malformed sym
   126           then SOME ((pos, Markup.bad ()), "Malformed symbolic character") else NONE);
   127     val is_malformed = Token.is_error tok orelse not (null malformed_symbols);
   128     val reports = Token.reports keywords tok @ Token.completion_report tok @ malformed_symbols;
   129   in (is_malformed, reports) end;
   130 
   131 in
   132 
   133 fun read_thy st = Toplevel.theory_of st
   134   handle Toplevel.UNDEF => Pure_Syn.bootstrap_thy;
   135 
   136 fun read keywords thy master_dir init blobs_info span =
   137   let
   138     val command_reports = Outer_Syntax.command_reports thy;
   139 
   140     val core_range = Token.range_of (drop_suffix Token.is_ignored span);
   141     val pos =
   142       (case find_first Token.is_command span of
   143         SOME tok => Token.pos_of tok
   144       | NONE => #1 core_range);
   145 
   146     val token_reports = map (reports_of_token keywords) span;
   147     val _ = Position.reports_text (maps #2 token_reports @ maps command_reports span);
   148 
   149     val verbatim =
   150       span |> map_filter (fn tok =>
   151         if Token.kind_of tok = Token.Verbatim then SOME (Token.pos_of tok) else NONE);
   152     val _ =
   153       if null verbatim then ()
   154       else legacy_feature ("Old-style {* verbatim *} token -- use \<open>cartouche\<close> instead" ^
   155         Position.here_list verbatim);
   156   in
   157     if exists #1 token_reports then Toplevel.malformed pos "Malformed command syntax"
   158     else
   159       (case Outer_Syntax.parse_tokens thy (resolve_files keywords master_dir blobs_info span) of
   160         [tr] => Toplevel.modify_init init tr
   161       | [] => Toplevel.ignored (#1 (Token.range_of span))
   162       | _ => Toplevel.malformed (#1 core_range) "Exactly one command expected")
   163       handle ERROR msg => Toplevel.malformed (#1 core_range) msg
   164   end;
   165 
   166 end;
   167 
   168 
   169 (* eval *)
   170 
   171 type eval_state = {failed: bool, command: Toplevel.transition, state: Toplevel.state};
   172 
   173 fun init_eval_state opt_thy =
   174  {failed = false,
   175   command = Toplevel.empty,
   176   state =
   177     (case opt_thy of
   178       NONE => Toplevel.init_toplevel ()
   179     | SOME thy => Toplevel.theory_toplevel thy)};
   180 
   181 datatype eval =
   182   Eval of
   183     {command_id: Document_ID.command, exec_id: Document_ID.exec, eval_process: eval_state lazy};
   184 
   185 fun eval_command_id (Eval {command_id, ...}) = command_id;
   186 
   187 fun eval_exec_id (Eval {exec_id, ...}) = exec_id;
   188 val eval_eq = op = o apply2 eval_exec_id;
   189 
   190 val eval_running = Execution.is_running_exec o eval_exec_id;
   191 fun eval_finished (Eval {eval_process, ...}) = Lazy.is_finished eval_process;
   192 
   193 fun eval_result (Eval {eval_process, ...}) =
   194   Exn.release (Lazy.finished_result eval_process);
   195 
   196 val eval_result_command = #command o eval_result;
   197 val eval_result_state = #state o eval_result;
   198 
   199 local
   200 
   201 fun reset_state keywords tr st0 = Toplevel.setmp_thread_position tr (fn () =>
   202   let
   203     val name = Toplevel.name_of tr;
   204     val res =
   205       if Keyword.is_theory_body keywords name then Toplevel.reset_theory st0
   206       else if Keyword.is_proof keywords name then Toplevel.reset_proof st0
   207       else if Keyword.is_theory_end keywords name then
   208         (case Toplevel.reset_notepad st0 of
   209           NONE => Toplevel.reset_theory st0
   210         | some => some)
   211       else NONE;
   212   in
   213     (case res of
   214       NONE => st0
   215     | SOME st => (Output.error_message (Toplevel.type_error tr ^ " -- using reset state"); st))
   216   end) ();
   217 
   218 fun run keywords int tr st =
   219   if Future.proofs_enabled 1 andalso Keyword.is_diag keywords (Toplevel.name_of tr) then
   220     (Execution.fork {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
   221       (fn () => Toplevel.command_exception int tr st); ([], SOME st))
   222   else Toplevel.command_errors int tr st;
   223 
   224 fun check_token_comments ctxt tok =
   225   (Thy_Output.check_comments ctxt (Input.source_explode (Token.input_of tok)); [])
   226     handle exn =>
   227       if Exn.is_interrupt exn then Exn.reraise exn
   228       else Runtime.exn_messages exn;
   229 
   230 fun check_span_comments ctxt span tr =
   231   Toplevel.setmp_thread_position tr (fn () => maps (check_token_comments ctxt) span) ();
   232 
   233 fun report tr m =
   234   Toplevel.setmp_thread_position tr (fn () => Output.report [Markup.markup_only m]) ();
   235 
   236 fun status tr m =
   237   Toplevel.setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
   238 
   239 fun command_indent tr st =
   240   (case try Toplevel.proof_of st of
   241     SOME prf =>
   242       let val keywords = Thy_Header.get_keywords (Proof.theory_of prf) in
   243         if Keyword.command_kind keywords (Toplevel.name_of tr) = SOME Keyword.prf_script then
   244           (case try Proof.goal prf of
   245             SOME {goal, ...} =>
   246               let val n = Thm.nprems_of goal
   247               in if n > 1 then report tr (Markup.command_indent (n - 1)) else () end
   248           | NONE => ())
   249         else ()
   250       end
   251   | NONE => ());
   252 
   253 
   254 fun eval_state keywords span tr ({state, ...}: eval_state) =
   255   let
   256     val _ = Thread_Attributes.expose_interrupt ();
   257 
   258     val st = reset_state keywords tr state;
   259 
   260     val _ = command_indent tr st;
   261     val _ = status tr Markup.running;
   262     val (errs1, result) = run keywords true tr st;
   263     val errs2 =
   264       (case result of
   265         NONE => []
   266       | SOME st' =>
   267           (case try Toplevel.presentation_context st' of
   268             NONE => []
   269           | SOME ctxt' => check_span_comments ctxt' span tr));
   270     val errs = errs1 @ errs2;
   271     val _ = List.app (Future.error_message (Toplevel.pos_of tr)) errs;
   272   in
   273     (case result of
   274       NONE =>
   275         let
   276           val _ = status tr Markup.failed;
   277           val _ = status tr Markup.finished;
   278           val _ = if null errs then (status tr Markup.canceled; Exn.interrupt ()) else ();
   279         in {failed = true, command = tr, state = st} end
   280     | SOME st' =>
   281         let
   282           val _ =
   283             if Keyword.is_theory_end keywords (Toplevel.name_of tr) andalso
   284               can (Toplevel.end_theory Position.none) st'
   285             then status tr Markup.finalized else ();
   286           val _ = status tr Markup.finished;
   287         in {failed = false, command = tr, state = st'} end)
   288   end;
   289 
   290 in
   291 
   292 fun eval keywords master_dir init blobs_info command_id span eval0 =
   293   let
   294     val exec_id = Document_ID.make ();
   295     fun process () =
   296       let
   297         val eval_state0 = eval_result eval0;
   298         val thy = read_thy (#state eval_state0);
   299         val tr =
   300           Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
   301             (fn () =>
   302               read keywords thy master_dir init blobs_info span |> Toplevel.exec_id exec_id) ();
   303       in eval_state keywords span tr eval_state0 end;
   304   in
   305     Eval {command_id = command_id, exec_id = exec_id,
   306       eval_process = Lazy.lazy_name "Command.eval" process}
   307   end;
   308 
   309 end;
   310 
   311 
   312 (* print *)
   313 
   314 datatype print = Print of
   315  {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
   316   exec_id: Document_ID.exec, print_process: unit lazy};
   317 
   318 fun print_exec_id (Print {exec_id, ...}) = exec_id;
   319 val print_eq = op = o apply2 print_exec_id;
   320 
   321 type print_fn = Toplevel.transition -> Toplevel.state -> unit;
   322 
   323 type print_function =
   324   {keywords: Keyword.keywords, command_name: string, args: string list, exec_id: Document_ID.exec} ->
   325     {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
   326 
   327 local
   328 
   329 val print_functions =
   330   Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
   331 
   332 fun print_error tr opt_context e =
   333   (Toplevel.setmp_thread_position tr o Runtime.controlled_execution opt_context) e ()
   334     handle exn =>
   335       if Exn.is_interrupt exn then Exn.reraise exn
   336       else List.app (Future.error_message (Toplevel.pos_of tr)) (Runtime.exn_messages exn);
   337 
   338 fun print_finished (Print {print_process, ...}) = Lazy.is_finished print_process;
   339 
   340 fun print_persistent (Print {persistent, ...}) = persistent;
   341 
   342 val overlay_ord = prod_ord string_ord (list_ord string_ord);
   343 
   344 fun make_print (name, args) {delay, pri, persistent, strict, print_fn} eval =
   345   let
   346     val exec_id = Document_ID.make ();
   347     fun process () =
   348       let
   349         val {failed, command, state = st', ...} = eval_result eval;
   350         val tr = Toplevel.exec_id exec_id command;
   351         val opt_context = try Toplevel.generic_theory_of st';
   352       in
   353         if failed andalso strict then ()
   354         else print_error tr opt_context (fn () => print_fn tr st')
   355       end;
   356   in
   357     Print {
   358       name = name, args = args, delay = delay, pri = pri, persistent = persistent,
   359       exec_id = exec_id, print_process = Lazy.lazy_name "Command.print" process}
   360   end;
   361 
   362 fun bad_print name_args exn =
   363   make_print name_args {delay = NONE, pri = 0, persistent = false,
   364     strict = false, print_fn = fn _ => fn _ => Exn.reraise exn};
   365 
   366 in
   367 
   368 fun print0 {pri, print_fn} =
   369   make_print ("", [serial_string ()])
   370     {delay = NONE, pri = pri, persistent = true, strict = true, print_fn = print_fn};
   371 
   372 fun print command_visible command_overlays keywords command_name eval old_prints =
   373   let
   374     val print_functions = Synchronized.value print_functions;
   375 
   376     fun new_print (name, args) get_pr =
   377       let
   378         val params =
   379          {keywords = keywords,
   380           command_name = command_name,
   381           args = args,
   382           exec_id = eval_exec_id eval};
   383       in
   384         (case Exn.capture (Runtime.controlled_execution NONE get_pr) params of
   385           Exn.Res NONE => NONE
   386         | Exn.Res (SOME pr) => SOME (make_print (name, args) pr eval)
   387         | Exn.Exn exn => SOME (bad_print (name, args) exn eval))
   388       end;
   389 
   390     fun get_print (name, args) =
   391       (case find_first (fn Print print => (#name print, #args print) = (name, args)) old_prints of
   392         NONE =>
   393           (case AList.lookup (op =) print_functions name of
   394             NONE =>
   395               SOME (bad_print (name, args) (ERROR ("Missing print function " ^ quote name)) eval)
   396           | SOME get_pr => new_print (name, args) get_pr)
   397       | some => some);
   398 
   399     val retained_prints =
   400       filter (fn print => print_finished print andalso print_persistent print) old_prints;
   401     val visible_prints =
   402       if command_visible then
   403         fold (fn (name, _) => cons (name, [])) print_functions command_overlays
   404         |> sort_distinct overlay_ord
   405         |> map_filter get_print
   406       else [];
   407     val new_prints = visible_prints @ retained_prints;
   408   in
   409     if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
   410   end;
   411 
   412 fun parallel_print (Print {pri, ...}) =
   413   pri <= 0 orelse (Future.enabled () andalso Options.default_bool "parallel_print");
   414 
   415 fun print_function name f =
   416   Synchronized.change print_functions (fn funs =>
   417    (if name = "" then error "Unnamed print function" else ();
   418     if not (AList.defined (op =) funs name) then ()
   419     else warning ("Redefining command print function: " ^ quote name);
   420     AList.update (op =) (name, f) funs));
   421 
   422 fun no_print_function name =
   423   Synchronized.change print_functions (filter_out (equal name o #1));
   424 
   425 end;
   426 
   427 val _ =
   428   print_function "Execution.print"
   429     (fn {args, exec_id, ...} =>
   430       if null args then
   431         SOME {delay = NONE, pri = Task_Queue.urgent_pri + 2, persistent = false, strict = false,
   432           print_fn = fn _ => fn _ => Execution.fork_prints exec_id}
   433       else NONE);
   434 
   435 val _ =
   436   print_function "print_state"
   437     (fn {keywords, command_name, ...} =>
   438       if Options.default_bool "editor_output_state" andalso Keyword.is_printed keywords command_name
   439       then
   440         SOME {delay = NONE, pri = Task_Queue.urgent_pri + 1, persistent = false, strict = false,
   441           print_fn = fn _ => fn st =>
   442             if Toplevel.is_proof st then Output.state (Toplevel.string_of_state st)
   443             else ()}
   444       else NONE);
   445 
   446 
   447 (* combined execution *)
   448 
   449 type exec = eval * print list;
   450 
   451 fun init_exec opt_thy : exec =
   452   (Eval
   453     {command_id = Document_ID.none, exec_id = Document_ID.none,
   454       eval_process = Lazy.value (init_eval_state opt_thy)}, []);
   455 
   456 val no_exec = init_exec NONE;
   457 
   458 fun exec_ids NONE = []
   459   | exec_ids (SOME (eval, prints)) = eval_exec_id eval :: map print_exec_id prints;
   460 
   461 local
   462 
   463 fun run_process execution_id exec_id process =
   464   let val group = Future.worker_subgroup () in
   465     if Execution.running execution_id exec_id [group] then
   466       ignore (task_context group (fn () => Lazy.force_result {strict = true} process) ())
   467     else ()
   468   end;
   469 
   470 fun ignore_process process =
   471   Lazy.is_running process orelse Lazy.is_finished process;
   472 
   473 fun run_eval execution_id (Eval {exec_id, eval_process, ...}) =
   474   if Lazy.is_finished eval_process then ()
   475   else run_process execution_id exec_id eval_process;
   476 
   477 fun fork_print execution_id deps (Print {name, delay, pri, exec_id, print_process, ...}) =
   478   let
   479     val group = Future.worker_subgroup ();
   480     fun fork () =
   481       ignore ((singleton o Future.forks)
   482         {name = name, group = SOME group, deps = deps, pri = pri, interrupts = true}
   483         (fn () =>
   484           if ignore_process print_process then ()
   485           else run_process execution_id exec_id print_process));
   486   in
   487     (case delay of
   488       NONE => fork ()
   489     | SOME d => ignore (Event_Timer.request {physical = true} (Time.now () + d) fork))
   490   end;
   491 
   492 fun run_print execution_id (print as Print {exec_id, print_process, ...}) =
   493   if ignore_process print_process then ()
   494   else if parallel_print print then fork_print execution_id [] print
   495   else run_process execution_id exec_id print_process;
   496 
   497 in
   498 
   499 fun exec execution_id (eval, prints) =
   500   (run_eval execution_id eval; List.app (run_print execution_id) prints);
   501 
   502 fun exec_parallel_prints execution_id deps (exec as (Eval {eval_process, ...}, prints)) =
   503   if Lazy.is_finished eval_process
   504   then (List.app (fork_print execution_id deps) prints; NONE)
   505   else SOME exec;
   506 
   507 end;
   508 
   509 end;