src/Pure/PIDE/protocol_command.ML
author wenzelm
Mon, 29 Aug 2022 19:26:27 +0200
changeset 76019 f3d8da992445
parent 75577 c51e1cef1eae
child 78725 3c02ad5a1586
permissions -rw-r--r--
provide cvc5-1.0.2 (inactive);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/PIDE/protocol_command.ML
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     3
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     4
Protocol commands.
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     5
*)
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     6
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     7
signature PROTOCOL_COMMAND =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     8
sig
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     9
  exception STOP of int
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    10
  val is_protocol_exn: exn -> bool
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    11
  val define_bytes: string -> (Bytes.T list -> unit) -> unit
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    12
  val define: string -> (string list -> unit) -> unit
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    13
  val run: string -> Bytes.T list -> unit
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    14
end;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    15
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    16
structure Protocol_Command: PROTOCOL_COMMAND =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    17
struct
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    18
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    19
exception STOP of int;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    20
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    21
val is_protocol_exn = fn STOP _ => true | _ => false;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    22
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    23
local
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    24
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    25
val commands =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    26
  Synchronized.var "Protocol_Command.commands"
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    27
    (Symtab.empty: (Bytes.T list -> unit) Symtab.table);
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    28
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    29
in
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    30
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    31
fun define_bytes name cmd =
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    32
  Synchronized.change commands (fn cmds =>
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    33
   (if not (Symtab.defined cmds name) then ()
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    34
    else warning ("Redefining Isabelle protocol command " ^ quote name);
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    35
    Symtab.update (name, cmd) cmds));
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    36
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    37
fun define name cmd = define_bytes name (map Bytes.content #> cmd);
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    38
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    39
fun run name args =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    40
  (case Symtab.lookup (Synchronized.value commands) name of
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    41
    NONE => error ("Undefined Isabelle protocol command " ^ quote name)
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    42
  | SOME cmd =>
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    43
      (Runtime.exn_trace_system (fn () => cmd args)
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    44
        handle exn =>
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    45
          if is_protocol_exn exn then Exn.reraise exn
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    46
          else error ("Isabelle protocol command failure: " ^ quote name)));
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    47
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    48
end;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    49
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    50
end;