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