src/Pure/Tools/print_operation.ML
author wenzelm
Wed, 13 Nov 2019 18:30:43 +0100
changeset 71111 cd166c3904dd
parent 70991 f9f7c34b7dd4
child 72156 065dcd80293e
permissions -rw-r--r--
more robust: system ssh service is required for Phabricator ssh service;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Tools/print_operation.ML
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     3
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     4
Print operations as asynchronous query.
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     5
*)
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     6
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     7
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     8
signature PRINT_OPERATION =
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
     9
sig
57604
30885e25c6de support multiple selected print operations instead of slightly odd "menu";
wenzelm
parents: 57415
diff changeset
    10
  val register: string -> string -> (Toplevel.state -> Pretty.T list) -> unit
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    11
end;
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    12
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    13
structure Print_Operation: PRINT_OPERATION =
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    14
struct
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    15
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    16
(* maintain print operations *)
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    17
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    18
local
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    19
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    20
val print_operations =
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    21
  Synchronized.var "print_operations"
57604
30885e25c6de support multiple selected print operations instead of slightly odd "menu";
wenzelm
parents: 57415
diff changeset
    22
    ([]: (string * (string * (Toplevel.state -> Pretty.T list))) list);
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    23
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    24
fun report () =
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    25
  Output.try_protocol_message Markup.print_operations
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 64677
diff changeset
    26
    (Synchronized.value print_operations
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 64677
diff changeset
    27
      |> map (fn (x, (y, _)) => (x, y)) |> rev
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 64677
diff changeset
    28
      |> let open XML.Encode in list (pair string string) end);
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    29
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    30
val _ = Isabelle_Process.protocol_command "print_operations" (fn [] => report ());
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    31
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    32
val _ = Session.protocol_handler "isabelle.Print_Operation$Handler";
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    33
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    34
in
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    35
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    36
fun register name description pr =
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    37
 (Synchronized.change print_operations (fn tab =>
56867
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    38
   (if not (AList.defined (op =) tab name) then ()
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    39
    else warning ("Redefining print operation: " ^ quote name);
56867
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    40
    AList.update (op =) (name, (description, pr)) tab));
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    41
  report ());
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    42
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    43
val _ =
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: 57605
diff changeset
    44
  Query_Operation.register {name = "print_operation", pri = Task_Queue.urgent_pri}
61223
dfccf6c06201 clarified markup;
wenzelm
parents: 61212
diff changeset
    45
    (fn {state, args, writeln_result, ...} =>
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: 57605
diff changeset
    46
      let
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57605
diff changeset
    47
        val _ = Toplevel.context_of state handle Toplevel.UNDEF => error "Unknown context";
64677
8dc24130e8fe more uniform treatment of "bad" like other messages (with serial number);
wenzelm
parents: 61223
diff changeset
    48
        fun err s = Pretty.mark_str (Markup.bad (), s);
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: 57605
diff changeset
    49
        fun print name =
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57605
diff changeset
    50
          (case AList.lookup (op =) (Synchronized.value print_operations) name of
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57605
diff changeset
    51
            SOME (_, pr) => (pr state handle Toplevel.UNDEF => [err "Unknown context"])
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57605
diff changeset
    52
          | NONE => [err ("Unknown print operation: " ^ quote name)]);
61223
dfccf6c06201 clarified markup;
wenzelm
parents: 61212
diff changeset
    53
      in writeln_result (Pretty.string_of (Pretty.chunks (maps print args))) end);
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    54
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    55
end;
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    56
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    57
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    58
(* common print operations *)
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    59
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    60
val _ =
57604
30885e25c6de support multiple selected print operations instead of slightly odd "menu";
wenzelm
parents: 57415
diff changeset
    61
  register "context" "context of local theory target" Toplevel.pretty_context;
56867
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    62
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    63
val _ =
57605
8e0a7eaffe47 tuned messages;
wenzelm
parents: 57604
diff changeset
    64
  register "cases" "cases of proof context"
8e0a7eaffe47 tuned messages;
wenzelm
parents: 57604
diff changeset
    65
    (Proof_Context.pretty_cases o Toplevel.context_of);
56867
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    66
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    67
val _ =
56868
b5fb264d53ba clarified print operations for "terms" and "theorems";
wenzelm
parents: 56867
diff changeset
    68
  register "terms" "term bindings of proof context"
57604
30885e25c6de support multiple selected print operations instead of slightly odd "menu";
wenzelm
parents: 57415
diff changeset
    69
    (Proof_Context.pretty_term_bindings o Toplevel.context_of);
56867
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    70
224109105008 more print operations;
wenzelm
parents: 56864
diff changeset
    71
val _ =
56868
b5fb264d53ba clarified print operations for "terms" and "theorems";
wenzelm
parents: 56867
diff changeset
    72
  register "theorems" "theorems of local theory or proof context"
57605
8e0a7eaffe47 tuned messages;
wenzelm
parents: 57604
diff changeset
    73
    (Isar_Cmd.pretty_theorems false);
56864
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    74
0446c7ac2e32 support print operations as asynchronous query;
wenzelm
parents:
diff changeset
    75
end;