src/Pure/System/isabelle_process.ML
author wenzelm
Tue Jul 05 21:53:59 2011 +0200 (2011-07-05 ago)
changeset 43671 a250b092ac66
parent 42897 6bc8a6dcb3e0
child 43673 29eb1cd29961
permissions -rw-r--r--
hard-wired print mode "xsymbols" increases chance that "iff" in HOL will print symbolic arrow;
     1 (*  Title:      Pure/System/isabelle_process.ML
     2     Author:     Makarius
     3 
     4 Isabelle process wrapper, based on private fifos for maximum
     5 robustness and performance.
     6 
     7 Startup phases:
     8   . raw Posix process startup with uncontrolled output on stdout/stderr
     9   . stdout \002: ML running
    10   .. stdin/stdout/stderr freely available (raw ML loop)
    11   .. protocol thread initialization
    12   ... switch to in_fifo/out_fifo channels (rendezvous via open)
    13   ... out_fifo INIT(pid): channels ready
    14   ... out_fifo STATUS(keywords)
    15   ... out_fifo READY: main loop ready
    16 *)
    17 
    18 signature ISABELLE_PROCESS =
    19 sig
    20   val is_active: unit -> bool
    21   val add_command: string -> (string list -> unit) -> unit
    22   val command: string -> string list -> unit
    23   val crashes: exn list Unsynchronized.ref
    24   val init: string -> string -> unit
    25 end;
    26 
    27 structure Isabelle_Process: ISABELLE_PROCESS =
    28 struct
    29 
    30 (* print mode *)
    31 
    32 val isabelle_processN = "isabelle_process";
    33 
    34 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
    35 
    36 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    37 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    38 
    39 
    40 (* commands *)
    41 
    42 local
    43 
    44 val global_commands = Unsynchronized.ref (Symtab.empty: (string list -> unit) Symtab.table);
    45 
    46 in
    47 
    48 fun add_command name cmd = CRITICAL (fn () =>
    49   Unsynchronized.change global_commands (fn cmds =>
    50    (if not (Symtab.defined cmds name) then ()
    51     else warning ("Redefining Isabelle process command " ^ quote name);
    52     Symtab.update (name, cmd) cmds)));
    53 
    54 fun command name args =
    55   (case Symtab.lookup (! global_commands) name of
    56     NONE => error ("Undefined Isabelle process command " ^ quote name)
    57   | SOME cmd => cmd args);
    58 
    59 end;
    60 
    61 
    62 (* message channels *)
    63 
    64 local
    65 
    66 fun chunk s = [string_of_int (size s), "\n", s];
    67 
    68 fun message _ _ _ "" = ()
    69   | message mbox ch raw_props body =
    70       let
    71         val robust_props = map (pairself YXML.escape_controls) raw_props;
    72         val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    73       in Mailbox.send mbox (chunk header @ chunk body) end;
    74 
    75 fun standard_message mbox with_serial ch body =
    76   message mbox ch
    77     ((if with_serial then cons (Markup.serialN, serial_string ()) else I)
    78       (Position.properties_of (Position.thread_data ()))) body;
    79 
    80 fun message_output mbox out_stream =
    81   let
    82     fun loop receive =
    83       (case receive mbox of
    84         SOME msg =>
    85          (List.app (fn s => TextIO.output (out_stream, s)) msg;
    86           loop (Mailbox.receive_timeout (seconds 0.02)))
    87       | NONE => (try TextIO.flushOut out_stream; loop (SOME o Mailbox.receive)));
    88   in fn () => loop (SOME o Mailbox.receive) end;
    89 
    90 in
    91 
    92 fun setup_channels in_fifo out_fifo =
    93   let
    94     (*rendezvous*)
    95     val in_stream = TextIO.openIn in_fifo;
    96     val out_stream = TextIO.openOut out_fifo;
    97 
    98     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
    99     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   100 
   101     val mbox = Mailbox.create () : string list Mailbox.T;
   102     val _ = Simple_Thread.fork false (message_output mbox out_stream);
   103   in
   104     Output.Private_Hooks.status_fn := standard_message mbox false "B";
   105     Output.Private_Hooks.report_fn := standard_message mbox false "C";
   106     Output.Private_Hooks.writeln_fn := standard_message mbox true "D";
   107     Output.Private_Hooks.tracing_fn := standard_message mbox true "E";
   108     Output.Private_Hooks.warning_fn := standard_message mbox true "F";
   109     Output.Private_Hooks.error_fn := standard_message mbox true "G";
   110     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   111     Output.Private_Hooks.prompt_fn := ignore;
   112     message mbox "A" [] (Session.welcome ());
   113     in_stream
   114   end;
   115 
   116 end;
   117 
   118 
   119 (* protocol loop -- uninterruptible *)
   120 
   121 val crashes = Unsynchronized.ref ([]: exn list);
   122 
   123 local
   124 
   125 fun recover crash =
   126   (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
   127     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   128 
   129 fun read_chunk stream len =
   130   let
   131     val n =
   132       (case Int.fromString len of
   133         SOME n => n
   134       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   135     val chunk = TextIO.inputN (stream, n);
   136     val m = size chunk;
   137   in
   138     if m = n then chunk
   139     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   140   end;
   141 
   142 fun read_command stream =
   143   (case TextIO.inputLine stream of
   144     NONE => raise Runtime.TERMINATE
   145   | SOME line => map (read_chunk stream) (space_explode "," line));
   146 
   147 fun run_command name args =
   148   Runtime.debugging (command name) args
   149     handle exn =>
   150       error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
   151 
   152 in
   153 
   154 fun loop stream =
   155   let val continue =
   156     (case read_command stream of
   157       [] => (Output.error_msg "Isabelle process: no input"; true)
   158     | name :: args => (run_command name args; true))
   159     handle Runtime.TERMINATE => false
   160       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   161   in if continue then loop stream else () end;
   162 
   163 end;
   164 
   165 
   166 (* init *)
   167 
   168 fun init in_fifo out_fifo = ignore (Simple_Thread.fork false (fn () =>
   169   let
   170     val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
   171     val _ = Output.raw_stdout Symbol.STX;
   172 
   173     val _ = quick_and_dirty := true;
   174     val _ = Goal.parallel_proofs := 0;
   175     val _ = Context.set_thread_data NONE;
   176     val _ =
   177       Unsynchronized.change print_mode
   178         (fold (update op =)
   179           [Symbol.xsymbolsN, isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
   180 
   181     val in_stream = setup_channels in_fifo out_fifo;
   182     val _ = Keyword.status ();
   183     val _ = Output.status (Markup.markup Markup.ready "process ready");
   184   in loop in_stream end));
   185 
   186 end;
   187