src/Pure/System/isabelle_process.ML
author wenzelm
Wed, 10 Jul 2013 22:04:57 +0200
changeset 52582 31467a4b1466
parent 52580 36aa39694ab4
child 52583 0a7240d88e09
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30173
eabece26b89b moved isabelle_process.ML, isabelle_process.scala, isar.ML, session.ML to Pure/System/ (together with associated Isar commands);
wenzelm
parents: 29522
diff changeset
     1
(*  Title:      Pure/System/isabelle_process.ML
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     3
38445
ba9ea6b9b75c simplified internal message format: dropped special Symbol.STX header;
wenzelm
parents: 38270
diff changeset
     4
Isabelle process wrapper, based on private fifos for maximum
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
     5
robustness and performance, or local socket for maximum portability.
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39513
diff changeset
     6
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39513
diff changeset
     7
Startup phases:
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
     8
  - raw Posix process startup with uncontrolled output on stdout/stderr
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
     9
  - stderr \002: ML running
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    10
  - stdin/stdout/stderr freely available (raw ML loop)
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    11
  - protocol thread initialization
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    12
  - rendezvous on system channel
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
    13
  - message INIT: channels ready
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    14
*)
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    15
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    16
signature ISABELLE_PROCESS =
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    17
sig
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    18
  val is_active: unit -> bool
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    19
  val protocol_command: string -> (string list -> unit) -> unit
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    20
  val reset_tracing: unit -> unit
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    21
  val crashes: exn list Synchronized.var
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
    22
  val init_fifos: string -> string -> unit
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 44988
diff changeset
    23
  val init_socket: string -> unit
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    24
end;
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    25
31797
203d5e61e3bc renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 31384
diff changeset
    26
structure Isabelle_Process: ISABELLE_PROCESS =
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    27
struct
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    28
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    29
(* print mode *)
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    30
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    31
val isabelle_processN = "isabelle_process";
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    32
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    33
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    34
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    35
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
28036
wenzelm
parents: 27986
diff changeset
    36
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    37
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    38
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    39
(* protocol commands *)
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    40
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    41
local
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    42
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    43
val commands =
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    44
  Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    45
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    46
in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    47
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    48
fun protocol_command name cmd =
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    49
  Synchronized.change commands (fn cmds =>
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    50
   (if not (Symtab.defined cmds name) then ()
52582
31467a4b1466 tuned signature;
wenzelm
parents: 52580
diff changeset
    51
    else warning ("Redefining Isabelle protocol command " ^ quote name);
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    52
    Symtab.update (name, cmd) cmds));
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    53
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    54
fun run_command name args =
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    55
  (case Symtab.lookup (Synchronized.value commands) name of
52582
31467a4b1466 tuned signature;
wenzelm
parents: 52580
diff changeset
    56
    NONE => error ("Undefined Isabelle protocol command " ^ quote name)
46119
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    57
  | SOME cmd =>
0d7172a7672c tuned signature -- emphasize special nature of protocol commands;
wenzelm
parents: 45666
diff changeset
    58
      (Runtime.debugging cmd args handle exn =>
52582
31467a4b1466 tuned signature;
wenzelm
parents: 52580
diff changeset
    59
        error ("Isabelle protocol command failure: " ^ quote name ^ "\n" ^
48056
396749e9daaf tuned message;
wenzelm
parents: 47410
diff changeset
    60
          ML_Compiler.exn_message exn)));
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    61
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    62
end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    63
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    64
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    65
(* restricted tracing messages *)
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    66
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    67
val tracing_messages =
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    68
  Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    69
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    70
fun reset_tracing () =
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    71
  Synchronized.change tracing_messages (K Inttab.empty);
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    72
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    73
fun update_tracing () =
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50715
diff changeset
    74
  (case Position.parse_id (Position.thread_data ()) of
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    75
    NONE => ()
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    76
  | SOME id =>
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    77
      let
52560
wenzelm
parents: 52559
diff changeset
    78
        val ok =
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    79
          Synchronized.change_result tracing_messages (fn tab =>
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    80
            let
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50715
diff changeset
    81
              val n = the_default 0 (Inttab.lookup tab id) + 1;
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    82
              val ok = n <= Options.default_int "editor_tracing_messages";
52560
wenzelm
parents: 52559
diff changeset
    83
            in (ok, Inttab.update (id, n) tab) end);
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    84
      in
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    85
        if ok then ()
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    86
        else
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    87
          let
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    88
            val (text, promise) = Active.dialog_text ();
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    89
            val _ =
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    90
              writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
51044
890f502f0e89 more generous tracing limit, which is relevant for applications where this occurs routinely (e.g. HO unification trace);
wenzelm
parents: 50911
diff changeset
    91
                text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    92
            val m = Markup.parse_int (Future.join promise)
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    93
              handle Fail _ => error "Stopped";
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    94
          in
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    95
            Synchronized.change tracing_messages
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50715
diff changeset
    96
              (Inttab.map_default (id, 0) (fn k => k - m))
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    97
          end
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    98
      end);
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    99
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
   100
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   101
(* message channels *)
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   102
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   103
local
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
   104
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   105
fun chunk s = [string_of_int (size s), "\n", s];
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   106
fun flush channel = ignore (try System_Channel.flush channel);
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
   107
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   108
datatype target =
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   109
  Channel of System_Channel.T |
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   110
  Mailbox of (string list * bool) Mailbox.T;
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   111
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   112
fun message do_flush target name raw_props body =
43771
fc524449f511 allow empty body for raw_message -- important for Invoke_Scala;
wenzelm
parents: 43746
diff changeset
   113
  let
43772
c825594fd0c1 clarified YXML.embed_controls -- this is idempotent and cannot be nested;
wenzelm
parents: 43771
diff changeset
   114
    val robust_props = map (pairself YXML.embed_controls) raw_props;
49677
c4e2762a265c more direct message header: eliminated historic encoding via single letter;
wenzelm
parents: 49661
diff changeset
   115
    val header = YXML.string_of (XML.Elem ((name, robust_props), []));
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   116
    val msg = chunk header @ chunk body;
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   117
  in
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   118
    (case target of
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   119
      Channel channel => (List.app (fn s => System_Channel.output channel s) msg; flush channel)
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   120
    | Mailbox mbox => Mailbox.send mbox (msg, do_flush))
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   121
  end;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   122
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   123
fun message_output mbox channel =
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   124
  let
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   125
    fun loop receive =
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   126
      (case receive mbox of
44731
8f7b3a89fc15 flush after Output.raw_message (and init message) for reduced latency of important protocol events;
wenzelm
parents: 44389
diff changeset
   127
        SOME (msg, do_flush) =>
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   128
         (List.app (fn s => System_Channel.output channel s) msg;
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   129
          if do_flush then flush channel else ();
40301
bf39a257b3d3 simplified some time constants;
wenzelm
parents: 40134
diff changeset
   130
          loop (Mailbox.receive_timeout (seconds 0.02)))
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   131
      | NONE => (flush channel; loop (SOME o Mailbox.receive)));
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   132
  in fn () => loop (SOME o Mailbox.receive) end;
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   133
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   134
in
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   135
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
   136
fun init_channels channel =
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   137
  let
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   138
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   139
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   140
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   141
    val target =
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   142
      if Multithreading.available then
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   143
        let
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   144
          val mbox = Mailbox.create ();
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   145
          val _ = Simple_Thread.fork false (message_output mbox channel);
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   146
        in Mailbox mbox end
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   147
      else Channel channel;
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   148
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   149
    fun standard_message opt_serial name body =
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   150
      if body = "" then ()
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   151
      else
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   152
        message false target name
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   153
          ((case opt_serial of SOME i => cons (Markup.serialN, Markup.print_int i) | _ => I)
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   154
            (Position.properties_of (Position.thread_data ()))) body;
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   155
  in
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   156
    Output.Private_Hooks.status_fn := standard_message NONE Markup.statusN;
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   157
    Output.Private_Hooks.report_fn := standard_message NONE Markup.reportN;
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   158
    Output.Private_Hooks.result_fn := (fn (i, s) => standard_message (SOME i) Markup.resultN s);
49677
c4e2762a265c more direct message header: eliminated historic encoding via single letter;
wenzelm
parents: 49661
diff changeset
   159
    Output.Private_Hooks.writeln_fn :=
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   160
      (fn s => standard_message (SOME (serial ())) Markup.writelnN s);
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
   161
    Output.Private_Hooks.tracing_fn :=
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   162
      (fn s => (update_tracing (); standard_message (SOME (serial ())) Markup.tracingN s));
49677
c4e2762a265c more direct message header: eliminated historic encoding via single letter;
wenzelm
parents: 49661
diff changeset
   163
    Output.Private_Hooks.warning_fn :=
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   164
      (fn s => standard_message (SOME (serial ())) Markup.warningN s);
49677
c4e2762a265c more direct message header: eliminated historic encoding via single letter;
wenzelm
parents: 49661
diff changeset
   165
    Output.Private_Hooks.error_fn :=
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   166
      (fn (i, s) => standard_message (SOME i) Markup.errorN s);
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   167
    Output.Private_Hooks.protocol_message_fn := message true target Markup.protocolN;
40133
b61d52de66f0 more explicit indication of Output.Private_Hooks (still public to accomodate clones of Proof General for now);
wenzelm
parents: 40132
diff changeset
   168
    Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
b61d52de66f0 more explicit indication of Output.Private_Hooks (still public to accomodate clones of Proof General for now);
wenzelm
parents: 40132
diff changeset
   169
    Output.Private_Hooks.prompt_fn := ignore;
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   170
    message true target Markup.initN [] (Session.welcome ())
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   171
  end;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   172
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   173
end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   174
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   175
39234
d76a2fd129b5 main command loops are supposed to be uninterruptible -- no special treatment here;
wenzelm
parents: 38871
diff changeset
   176
(* protocol loop -- uninterruptible *)
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   177
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
   178
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   179
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   180
local
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   181
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   182
fun recover crash =
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
   183
  (Synchronized.change crashes (cons crash);
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   184
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   185
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   186
fun read_chunk channel len =
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   187
  let
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   188
    val n =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   189
      (case Int.fromString len of
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   190
        SOME n => n
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   191
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   192
    val chunk = System_Channel.inputN channel n;
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   193
    val m = size chunk;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   194
  in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   195
    if m = n then chunk
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   196
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   197
  end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   198
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   199
fun read_command channel =
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   200
  (case System_Channel.input_line channel of
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   201
    NONE => raise Runtime.TERMINATE
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   202
  | SOME line => map (read_chunk channel) (space_explode "," line));
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   203
52559
ddaf277e0d8c more direct interleaving of eval/print and update/execution -- refrain from crude manipulation of max_threads;
wenzelm
parents: 52111
diff changeset
   204
fun worker_guest e =
ddaf277e0d8c more direct interleaving of eval/print and update/execution -- refrain from crude manipulation of max_threads;
wenzelm
parents: 52111
diff changeset
   205
  Future.worker_guest "Isabelle_Process.loop" (Future.new_group NONE) e ();
ddaf277e0d8c more direct interleaving of eval/print and update/execution -- refrain from crude manipulation of max_threads;
wenzelm
parents: 52111
diff changeset
   206
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   207
in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   208
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   209
fun loop channel =
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   210
  let val continue =
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   211
    (case read_command channel of
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   212
      [] => (Output.error_msg "Isabelle process: no input"; true)
52559
ddaf277e0d8c more direct interleaving of eval/print and update/execution -- refrain from crude manipulation of max_threads;
wenzelm
parents: 52111
diff changeset
   213
    | name :: args => (worker_guest (fn () => run_command name args); true))
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   214
    handle Runtime.TERMINATE => false
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   215
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   216
  in if continue then loop channel else () end;
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   217
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   218
end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   219
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   220
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   221
(* init *)
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   222
50715
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50698
diff changeset
   223
val default_modes1 =
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50698
diff changeset
   224
  [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50698
diff changeset
   225
val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49108
diff changeset
   226
52578
bd94e26e4388 retain main thread for protocol loop -- no access to raw ML toplevel;
wenzelm
parents: 52560
diff changeset
   227
val init = uninterruptible (fn _ => fn rendezvous =>
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   228
  let
46548
c54a4a22501c clarified initial process startup errors: recover image load failure message (cf. 2cb7e34f6096) and suppress accidental output from raw ML toplevel;
wenzelm
parents: 46121
diff changeset
   229
    val _ = Output.physical_stderr Symbol.STX;
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39513
diff changeset
   230
49661
ac48def96b69 enable show_markup by default (approx. double output size);
wenzelm
parents: 49647
diff changeset
   231
    val _ = Printer.show_markup_default := true;
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39513
diff changeset
   232
    val _ = Context.set_thread_data NONE;
43671
a250b092ac66 hard-wired print mode "xsymbols" increases chance that "iff" in HOL will print symbolic arrow;
wenzelm
parents: 42897
diff changeset
   233
    val _ =
a250b092ac66 hard-wired print mode "xsymbols" increases chance that "iff" in HOL will print symbolic arrow;
wenzelm
parents: 42897
diff changeset
   234
      Unsynchronized.change print_mode
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49108
diff changeset
   235
        (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39513
diff changeset
   236
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   237
    val channel = rendezvous ();
48712
6b7a9bcc0bae simplified process startup phases: INIT suffices for is_ready;
wenzelm
parents: 48710
diff changeset
   238
    val _ = init_channels channel;
52111
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 52105
diff changeset
   239
    val _ = Session.init_protocol_handlers ();
52578
bd94e26e4388 retain main thread for protocol loop -- no access to raw ML toplevel;
wenzelm
parents: 52560
diff changeset
   240
  in loop channel end);
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   241
45029
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   242
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
63144ea111f7 abstract System_Channel in ML (cf. Scala version);
wenzelm
parents: 45028
diff changeset
   243
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 44988
diff changeset
   244
50117
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   245
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   246
(* options *)
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   247
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   248
val _ =
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   249
  protocol_command "Isabelle_Process.options"
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   250
    (fn [options_yxml] =>
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   251
      let val options = Options.decode (YXML.parse_body options_yxml) in
51941
ead4248aef3b full default options for Isabelle_Process and Build;
wenzelm
parents: 51554
diff changeset
   252
        Options.set_default options;
50698
49621c755075 always enable Future.ML_statistics where this makes sense -- runtime overhead should be negligible;
wenzelm
parents: 50505
diff changeset
   253
        Future.ML_statistics := true;
50117
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   254
        Multithreading.trace := Options.int options "threads_trace";
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   255
        Multithreading.max_threads := Options.int options "threads";
51554
041bc3d31f23 separate option editor_skip_proofs, to avoid accidental change of preferences for skip_proofs, which would invalidate batch builds;
wenzelm
parents: 51553
diff changeset
   256
        Goal.skip_proofs := Options.bool options "editor_skip_proofs";
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
   257
        Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0)
50117
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   258
      end);
32755e357a51 update options via protocol;
wenzelm
parents: 49677
diff changeset
   259
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   260
end;
39528
c01d89d18ff0 refined Isabelle_Process startup: emit \002 before rendezvous on fifos, more robust treatment of startup failure with timeout, do not quit() after main loop;
wenzelm
parents: 39513
diff changeset
   261