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