src/Pure/System/isabelle_process.ML
author wenzelm
Tue Jun 06 13:13:25 2017 +0200 (2017-06-06)
changeset 66020 a31760eee09d
parent 65448 9bc3b57c1fa7
child 68025 7fb7a6366a40
permissions -rw-r--r--
discontinued obsolete print mode;
     1 (*  Title:      Pure/System/isabelle_process.ML
     2     Author:     Makarius
     3 
     4 Isabelle process wrapper.
     5 *)
     6 
     7 signature ISABELLE_PROCESS =
     8 sig
     9   val is_active: unit -> bool
    10   val protocol_command: string -> (string list -> unit) -> unit
    11   val reset_tracing: Document_ID.exec -> unit
    12   val crashes: exn list Synchronized.var
    13   val init_protocol: string -> unit
    14   val init_options: unit -> unit
    15   val init_options_interactive: unit -> unit
    16 end;
    17 
    18 structure Isabelle_Process: ISABELLE_PROCESS =
    19 struct
    20 
    21 (* print mode *)
    22 
    23 val isabelle_processN = "isabelle_process";
    24 
    25 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
    26 
    27 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    28 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    29 
    30 
    31 (* protocol commands *)
    32 
    33 local
    34 
    35 val commands =
    36   Synchronized.var "Isabelle_Process.commands"
    37     (Symtab.empty: (string list -> unit) Symtab.table);
    38 
    39 in
    40 
    41 fun protocol_command name cmd =
    42   Synchronized.change commands (fn cmds =>
    43    (if not (Symtab.defined cmds name) then ()
    44     else warning ("Redefining Isabelle protocol command " ^ quote name);
    45     Symtab.update (name, cmd) cmds));
    46 
    47 fun run_command name args =
    48   (case Symtab.lookup (Synchronized.value commands) name of
    49     NONE => error ("Undefined Isabelle protocol command " ^ quote name)
    50   | SOME cmd =>
    51       (Runtime.exn_trace_system (fn () => cmd args)
    52         handle _ (*sic!*) => error ("Isabelle protocol command failure: " ^ quote name)));
    53 
    54 end;
    55 
    56 
    57 (* restricted tracing messages *)
    58 
    59 val tracing_messages =
    60   Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
    61 
    62 fun reset_tracing exec_id =
    63   Synchronized.change tracing_messages (Inttab.delete_safe exec_id);
    64 
    65 fun update_tracing () =
    66   (case Position.parse_id (Position.thread_data ()) of
    67     NONE => ()
    68   | SOME exec_id =>
    69       let
    70         val ok =
    71           Synchronized.change_result tracing_messages (fn tab =>
    72             let
    73               val n = the_default 0 (Inttab.lookup tab exec_id) + 1;
    74               val ok = n <= Options.default_int "editor_tracing_messages";
    75             in (ok, Inttab.update (exec_id, n) tab) end);
    76       in
    77         if ok then ()
    78         else
    79           let
    80             val (text, promise) = Active.dialog_text ();
    81             val _ =
    82               writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
    83                 text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
    84             val m = Value.parse_int (Future.join promise)
    85               handle Fail _ => error "Stopped";
    86           in
    87             Synchronized.change tracing_messages
    88               (Inttab.map_default (exec_id, 0) (fn k => k - m))
    89           end
    90       end);
    91 
    92 
    93 (* output channels *)
    94 
    95 val serial_props = Markup.serial_properties o serial;
    96 
    97 fun init_channels channel =
    98   let
    99     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   100     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   101 
   102     val msg_channel = Message_Channel.make channel;
   103 
   104     fun message name props body =
   105       Message_Channel.send msg_channel (Message_Channel.message name props body);
   106 
   107     fun standard_message props name body =
   108       if forall (fn s => s = "") body then ()
   109       else
   110         let
   111           val props' =
   112             (case (Properties.defined props Markup.idN, Position.get_id (Position.thread_data ())) of
   113               (false, SOME id') => props @ [(Markup.idN, id')]
   114             | _ => props);
   115         in message name props' body end;
   116   in
   117     Private_Output.status_fn := standard_message [] Markup.statusN;
   118     Private_Output.report_fn := standard_message [] Markup.reportN;
   119     Private_Output.result_fn :=
   120       (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s);
   121     Private_Output.writeln_fn := (fn s => standard_message (serial_props ()) Markup.writelnN s);
   122     Private_Output.state_fn := (fn s => standard_message (serial_props ()) Markup.stateN s);
   123     Private_Output.information_fn :=
   124       (fn s => standard_message (serial_props ()) Markup.informationN s);
   125     Private_Output.tracing_fn :=
   126       (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s));
   127     Private_Output.warning_fn := (fn s => standard_message (serial_props ()) Markup.warningN s);
   128     Private_Output.legacy_fn := (fn s => standard_message (serial_props ()) Markup.legacyN s);
   129     Private_Output.error_message_fn :=
   130       (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s);
   131     Private_Output.system_message_fn := message Markup.systemN [];
   132     Private_Output.protocol_message_fn := message Markup.protocolN;
   133 
   134     Session.init_protocol_handlers ();
   135     message Markup.initN [] [Session.welcome ()];
   136     msg_channel
   137   end;
   138 
   139 
   140 (* protocol loop -- uninterruptible *)
   141 
   142 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   143 
   144 local
   145 
   146 fun recover crash =
   147   (Synchronized.change crashes (cons crash);
   148     Output.physical_stderr
   149       "Recovered from Isabelle process crash -- see also Isabelle_Process.crashes\n");
   150 
   151 fun read_chunk channel len =
   152   let
   153     val n =
   154       (case Int.fromString len of
   155         SOME n => n
   156       | NONE => error ("Isabelle process: malformed header " ^ quote len));
   157     val chunk = System_Channel.inputN channel n;
   158     val i = size chunk;
   159   in
   160     if i <> n then
   161       error ("Isabelle process: bad chunk (unexpected EOF after " ^
   162         string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
   163     else chunk
   164   end;
   165 
   166 fun read_command channel =
   167   System_Channel.input_line channel
   168   |> Option.map (fn line => map (read_chunk channel) (space_explode "," line));
   169 
   170 in
   171 
   172 fun loop channel =
   173   let
   174     val continue =
   175       (case read_command channel of
   176         NONE => false
   177       | SOME [] => (Output.system_message "Isabelle process: no input"; true)
   178       | SOME (name :: args) => (run_command name args; true))
   179       handle exn => (Runtime.exn_system_message exn handle crash => recover crash; true);
   180   in
   181     if continue then loop channel
   182     else (Future.shutdown (); Execution.reset (); ())
   183   end;
   184 
   185 end;
   186 
   187 
   188 (* init protocol *)
   189 
   190 val default_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN];
   191 val default_modes2 = [isabelle_processN, Pretty.symbolicN];
   192 
   193 val init_protocol = Thread_Attributes.uninterruptible (fn _ => fn socket =>
   194   let
   195     val _ = SHA1.test_samples ()
   196       handle exn as Fail msg => (Output.physical_stderr (msg ^ "\n"); Exn.reraise exn);
   197     val _ = Output.physical_stderr Symbol.STX;
   198 
   199     val _ = Context.put_generic_context NONE;
   200     val _ =
   201       Unsynchronized.change print_mode
   202         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   203 
   204     val channel = System_Channel.rendezvous socket;
   205     val msg_channel = init_channels channel;
   206     val _ = loop channel;
   207     val _ = Message_Channel.shutdown msg_channel;
   208     val _ = Private_Output.init_channels ();
   209 
   210     val _ = print_mode := [];
   211   in () end);
   212 
   213 
   214 (* init options *)
   215 
   216 fun init_options () =
   217  (ML_Print_Depth.set_print_depth (Options.default_int "ML_print_depth");
   218   Future.ML_statistics := Options.default_bool "ML_statistics";
   219   Multithreading.trace := Options.default_int "threads_trace";
   220   Multithreading.max_threads_update (Options.default_int "threads");
   221   Goal.parallel_proofs := Options.default_int "parallel_proofs";
   222   let val proofs = Options.default_int "record_proofs"
   223   in if proofs < 0 then () else Proofterm.proofs := proofs end;
   224   Printer.show_markup_default := false);
   225 
   226 fun init_options_interactive () =
   227  (init_options ();
   228   Goal.parallel_proofs := (if Options.default_int "parallel_proofs" > 0 then 3 else 0);
   229   Printer.show_markup_default := true);
   230 
   231 end;