src/Pure/PIDE/query_operation.ML
author wenzelm
Mon, 21 Sep 2015 16:55:37 +0200
changeset 61214 a00bee2dfbd1
parent 61209 7a421e7ef97c
child 61223 dfccf6c06201
permissions -rw-r--r--
tuned priority (like other query operations, e.g. "find_theorems");
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/PIDE/query_operation.ML
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     3
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     4
One-shot query operations via asynchronous print functions and temporary
57872
28e17057b545 tuned comments;
wenzelm
parents: 56333
diff changeset
     5
document overlays.
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     6
*)
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     7
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     8
signature QUERY_OPERATION =
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     9
sig
60610
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57872
diff changeset
    10
  val register: {name: string, pri: int} ->
52982
8e78bd316a53 clarified Query_Operation.register: avoid hard-wired parallel policy;
wenzelm
parents: 52953
diff changeset
    11
    ({state: Toplevel.state, args: string list, output_result: string -> unit} -> unit) -> unit
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    12
end;
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    13
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    14
structure Query_Operation: QUERY_OPERATION =
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    15
struct
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    16
60610
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57872
diff changeset
    17
fun register {name, pri} f =
61206
d5aeb401111a more specific name to reduce danger of clash with direct uses of plain Command.print_function;
wenzelm
parents: 60610
diff changeset
    18
  Command.print_function (name ^ "_query")
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    19
    (fn {args = instance :: args, ...} =>
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    20
      SOME {delay = NONE, pri = pri, persistent = false, strict = false,
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    21
        print_fn = fn _ => uninterruptible (fn restore_attributes => fn state =>
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    22
          let
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    23
            fun result s = Output.result [(Markup.instanceN, instance)] [s];
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    24
            fun status m = result (Markup.markup_only m);
61209
7a421e7ef97c tuned signature;
wenzelm
parents: 61208
diff changeset
    25
            fun output_result s = result (Markup.markup Markup.writeln s);
7a421e7ef97c tuned signature;
wenzelm
parents: 61208
diff changeset
    26
            fun toplevel_error exn = result (Markup.markup Markup.error (Runtime.exn_message exn));
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52865
diff changeset
    27
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    28
            val _ = status Markup.running;
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    29
            fun run () = f {state = state, args = args, output_result = output_result};
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    30
            val _ =
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    31
              (case Exn.capture (*sic!*) (restore_attributes run) () of
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    32
                Exn.Res () => ()
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    33
              | Exn.Exn exn => toplevel_error exn);
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    34
            val _ = status Markup.finished;
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    35
          in () end)}
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    36
    | _ => NONE);
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    37
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    38
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    39
(* print_state *)
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    40
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    41
val _ =
61214
a00bee2dfbd1 tuned priority (like other query operations, e.g. "find_theorems");
wenzelm
parents: 61209
diff changeset
    42
  register {name = "print_state", pri = Task_Queue.urgent_pri}
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    43
    (fn {state = st, output_result, ...} =>
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    44
      if Toplevel.is_proof st
61209
7a421e7ef97c tuned signature;
wenzelm
parents: 61208
diff changeset
    45
      then output_result (Markup.markup Markup.state (Toplevel.string_of_state st))
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents: 61206
diff changeset
    46
      else ());
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    47
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    48
end;