src/Pure/PIDE/command.ML
author wenzelm
Sun Mar 10 14:19:30 2019 +0100 (4 months ago ago)
changeset 70070 be04e9a053a7
parent 70068 b9985133805d
child 70072 def3ec9cdb7e
permissions -rw-r--r--
markup and document markers for some meta data from "Dublin Core Metadata Element Set";
     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     let
   221       val (tr1, tr2) = Toplevel.fork_presentation tr;
   222       val _ =
   223         Execution.fork {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
   224           (fn () => Toplevel.command_exception int tr1 st);
   225     in Toplevel.command_errors int tr2 st end
   226   else Toplevel.command_errors int tr st;
   227 
   228 fun check_token_comments ctxt tok =
   229   (Thy_Output.check_comments ctxt (Input.source_explode (Token.input_of tok)); [])
   230     handle exn =>
   231       if Exn.is_interrupt exn then Exn.reraise exn
   232       else Runtime.exn_messages exn;
   233 
   234 fun check_span_comments ctxt span tr =
   235   Toplevel.setmp_thread_position tr (fn () => maps (check_token_comments ctxt) span) ();
   236 
   237 fun report tr m =
   238   Toplevel.setmp_thread_position tr (fn () => Output.report [Markup.markup_only m]) ();
   239 
   240 fun status tr m =
   241   Toplevel.setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
   242 
   243 fun command_indent tr st =
   244   (case try Toplevel.proof_of st of
   245     SOME prf =>
   246       let val keywords = Thy_Header.get_keywords (Proof.theory_of prf) in
   247         if Keyword.command_kind keywords (Toplevel.name_of tr) = SOME Keyword.prf_script then
   248           (case try Proof.goal prf of
   249             SOME {goal, ...} =>
   250               let val n = Thm.nprems_of goal
   251               in if n > 1 then report tr (Markup.command_indent (n - 1)) else () end
   252           | NONE => ())
   253         else ()
   254       end
   255   | NONE => ());
   256 
   257 
   258 fun eval_state keywords span tr ({state, ...}: eval_state) =
   259   let
   260     val _ = Thread_Attributes.expose_interrupt ();
   261 
   262     val st = reset_state keywords tr state;
   263 
   264     val _ = command_indent tr st;
   265     val _ = status tr Markup.running;
   266     val (errs1, result) = run keywords true tr st;
   267     val errs2 =
   268       (case result of
   269         NONE => []
   270       | SOME st' =>
   271           (case try Toplevel.presentation_context st' of
   272             NONE => []
   273           | SOME ctxt' => check_span_comments ctxt' span tr));
   274     val errs = errs1 @ errs2;
   275     val _ = List.app (Future.error_message (Toplevel.pos_of tr)) errs;
   276   in
   277     (case result of
   278       NONE =>
   279         let
   280           val _ = status tr Markup.failed;
   281           val _ = status tr Markup.finished;
   282           val _ = if null errs then (status tr Markup.canceled; Exn.interrupt ()) else ();
   283         in {failed = true, command = tr, state = st} end
   284     | SOME st' =>
   285         let
   286           val _ =
   287             if Keyword.is_theory_end keywords (Toplevel.name_of tr) andalso
   288               can (Toplevel.end_theory Position.none) st'
   289             then status tr Markup.finalized else ();
   290           val _ = status tr Markup.finished;
   291         in {failed = false, command = tr, state = st'} end)
   292   end;
   293 
   294 in
   295 
   296 fun eval keywords master_dir init blobs_info command_id span eval0 =
   297   let
   298     val exec_id = Document_ID.make ();
   299     fun process () =
   300       let
   301         val eval_state0 = eval_result eval0;
   302         val thy = read_thy (#state eval_state0);
   303         val tr =
   304           Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
   305             (fn () =>
   306               read keywords thy master_dir init blobs_info span |> Toplevel.exec_id exec_id) ();
   307       in eval_state keywords span tr eval_state0 end;
   308   in
   309     Eval {command_id = command_id, exec_id = exec_id,
   310       eval_process = Lazy.lazy_name "Command.eval" process}
   311   end;
   312 
   313 end;
   314 
   315 
   316 (* print *)
   317 
   318 datatype print = Print of
   319  {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
   320   exec_id: Document_ID.exec, print_process: unit lazy};
   321 
   322 fun print_exec_id (Print {exec_id, ...}) = exec_id;
   323 val print_eq = op = o apply2 print_exec_id;
   324 
   325 type print_fn = Toplevel.transition -> Toplevel.state -> unit;
   326 
   327 type print_function =
   328   {keywords: Keyword.keywords, command_name: string, args: string list, exec_id: Document_ID.exec} ->
   329     {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
   330 
   331 local
   332 
   333 val print_functions =
   334   Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
   335 
   336 fun print_error tr opt_context e =
   337   (Toplevel.setmp_thread_position tr o Runtime.controlled_execution opt_context) e ()
   338     handle exn =>
   339       if Exn.is_interrupt exn then Exn.reraise exn
   340       else List.app (Future.error_message (Toplevel.pos_of tr)) (Runtime.exn_messages exn);
   341 
   342 fun print_finished (Print {print_process, ...}) = Lazy.is_finished print_process;
   343 
   344 fun print_persistent (Print {persistent, ...}) = persistent;
   345 
   346 val overlay_ord = prod_ord string_ord (list_ord string_ord);
   347 
   348 fun make_print (name, args) {delay, pri, persistent, strict, print_fn} eval =
   349   let
   350     val exec_id = Document_ID.make ();
   351     fun process () =
   352       let
   353         val {failed, command, state = st', ...} = eval_result eval;
   354         val tr = Toplevel.exec_id exec_id command;
   355         val opt_context = try Toplevel.generic_theory_of st';
   356       in
   357         if failed andalso strict then ()
   358         else print_error tr opt_context (fn () => print_fn tr st')
   359       end;
   360   in
   361     Print {
   362       name = name, args = args, delay = delay, pri = pri, persistent = persistent,
   363       exec_id = exec_id, print_process = Lazy.lazy_name "Command.print" process}
   364   end;
   365 
   366 fun bad_print name_args exn =
   367   make_print name_args {delay = NONE, pri = 0, persistent = false,
   368     strict = false, print_fn = fn _ => fn _ => Exn.reraise exn};
   369 
   370 in
   371 
   372 fun print0 {pri, print_fn} =
   373   make_print ("", [serial_string ()])
   374     {delay = NONE, pri = pri, persistent = true, strict = true, print_fn = print_fn};
   375 
   376 fun print command_visible command_overlays keywords command_name eval old_prints =
   377   let
   378     val print_functions = Synchronized.value print_functions;
   379 
   380     fun new_print (name, args) get_pr =
   381       let
   382         val params =
   383          {keywords = keywords,
   384           command_name = command_name,
   385           args = args,
   386           exec_id = eval_exec_id eval};
   387       in
   388         (case Exn.capture (Runtime.controlled_execution NONE get_pr) params of
   389           Exn.Res NONE => NONE
   390         | Exn.Res (SOME pr) => SOME (make_print (name, args) pr eval)
   391         | Exn.Exn exn => SOME (bad_print (name, args) exn eval))
   392       end;
   393 
   394     fun get_print (name, args) =
   395       (case find_first (fn Print print => (#name print, #args print) = (name, args)) old_prints of
   396         NONE =>
   397           (case AList.lookup (op =) print_functions name of
   398             NONE =>
   399               SOME (bad_print (name, args) (ERROR ("Missing print function " ^ quote name)) eval)
   400           | SOME get_pr => new_print (name, args) get_pr)
   401       | some => some);
   402 
   403     val retained_prints =
   404       filter (fn print => print_finished print andalso print_persistent print) old_prints;
   405     val visible_prints =
   406       if command_visible then
   407         fold (fn (name, _) => cons (name, [])) print_functions command_overlays
   408         |> sort_distinct overlay_ord
   409         |> map_filter get_print
   410       else [];
   411     val new_prints = visible_prints @ retained_prints;
   412   in
   413     if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
   414   end;
   415 
   416 fun parallel_print (Print {pri, ...}) =
   417   pri <= 0 orelse (Future.enabled () andalso Options.default_bool "parallel_print");
   418 
   419 fun print_function name f =
   420   Synchronized.change print_functions (fn funs =>
   421    (if name = "" then error "Unnamed print function" else ();
   422     if not (AList.defined (op =) funs name) then ()
   423     else warning ("Redefining command print function: " ^ quote name);
   424     AList.update (op =) (name, f) funs));
   425 
   426 fun no_print_function name =
   427   Synchronized.change print_functions (filter_out (equal name o #1));
   428 
   429 end;
   430 
   431 val _ =
   432   print_function "Execution.print"
   433     (fn {args, exec_id, ...} =>
   434       if null args then
   435         SOME {delay = NONE, pri = Task_Queue.urgent_pri + 2, persistent = false, strict = false,
   436           print_fn = fn _ => fn _ => Execution.fork_prints exec_id}
   437       else NONE);
   438 
   439 val _ =
   440   print_function "print_state"
   441     (fn {keywords, command_name, ...} =>
   442       if Options.default_bool "editor_output_state" andalso Keyword.is_printed keywords command_name
   443       then
   444         SOME {delay = NONE, pri = Task_Queue.urgent_pri + 1, persistent = false, strict = false,
   445           print_fn = fn _ => fn st =>
   446             if Toplevel.is_proof st then Output.state (Toplevel.string_of_state st)
   447             else ()}
   448       else NONE);
   449 
   450 
   451 (* combined execution *)
   452 
   453 type exec = eval * print list;
   454 
   455 fun init_exec opt_thy : exec =
   456   (Eval
   457     {command_id = Document_ID.none, exec_id = Document_ID.none,
   458       eval_process = Lazy.value (init_eval_state opt_thy)}, []);
   459 
   460 val no_exec = init_exec NONE;
   461 
   462 fun exec_ids NONE = []
   463   | exec_ids (SOME (eval, prints)) = eval_exec_id eval :: map print_exec_id prints;
   464 
   465 local
   466 
   467 fun run_process execution_id exec_id process =
   468   let val group = Future.worker_subgroup () in
   469     if Execution.running execution_id exec_id [group] then
   470       ignore (task_context group (fn () => Lazy.force_result {strict = true} process) ())
   471     else ()
   472   end;
   473 
   474 fun ignore_process process =
   475   Lazy.is_running process orelse Lazy.is_finished process;
   476 
   477 fun run_eval execution_id (Eval {exec_id, eval_process, ...}) =
   478   if Lazy.is_finished eval_process then ()
   479   else run_process execution_id exec_id eval_process;
   480 
   481 fun fork_print execution_id deps (Print {name, delay, pri, exec_id, print_process, ...}) =
   482   let
   483     val group = Future.worker_subgroup ();
   484     fun fork () =
   485       ignore ((singleton o Future.forks)
   486         {name = name, group = SOME group, deps = deps, pri = pri, interrupts = true}
   487         (fn () =>
   488           if ignore_process print_process then ()
   489           else run_process execution_id exec_id print_process));
   490   in
   491     (case delay of
   492       NONE => fork ()
   493     | SOME d => ignore (Event_Timer.request {physical = true} (Time.now () + d) fork))
   494   end;
   495 
   496 fun run_print execution_id (print as Print {exec_id, print_process, ...}) =
   497   if ignore_process print_process then ()
   498   else if parallel_print print then fork_print execution_id [] print
   499   else run_process execution_id exec_id print_process;
   500 
   501 in
   502 
   503 fun exec execution_id (eval, prints) =
   504   (run_eval execution_id eval; List.app (run_print execution_id) prints);
   505 
   506 fun exec_parallel_prints execution_id deps (exec as (Eval {eval_process, ...}, prints)) =
   507   if Lazy.is_finished eval_process
   508   then (List.app (fork_print execution_id deps) prints; NONE)
   509   else SOME exec;
   510 
   511 end;
   512 
   513 end;