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