src/Pure/System/isar.ML
author wenzelm
Mon Aug 23 16:53:22 2010 +0200 (2010-08-23)
changeset 38639 f642faca303e
parent 38272 dc53026c6350
child 38799 712cb964d113
permissions -rw-r--r--
main session actor as independent thread, to avoid starvation via regular worker pool;
tuned;
     1 (*  Title:      Pure/System/isar.ML
     2     Author:     Makarius
     3 
     4 Global state of the raw Isar read-eval-print loop.
     5 *)
     6 
     7 signature ISAR =
     8 sig
     9   val init: unit -> unit
    10   val exn: unit -> (exn * string) option
    11   val state: unit -> Toplevel.state
    12   val goal: unit -> {context: Proof.context, facts: thm list, goal: thm}
    13   val print: unit -> unit
    14   val >> : Toplevel.transition -> bool
    15   val >>> : Toplevel.transition list -> unit
    16   val linear_undo: int -> unit
    17   val undo: int -> unit
    18   val kill: unit -> unit
    19   val kill_proof: unit -> unit
    20   val crashes: exn list Unsynchronized.ref
    21   val toplevel_loop: TextIO.instream ->
    22     {init: bool, welcome: bool, sync: bool, secure: bool} -> unit
    23   val loop: unit -> unit
    24   val main: unit -> unit
    25 end;
    26 
    27 structure Isar: ISAR =
    28 struct
    29 
    30 
    31 (** TTY model -- SINGLE-THREADED! **)
    32 
    33 (* the global state *)
    34 
    35 type history = (Toplevel.state * Toplevel.transition) list;
    36   (*previous state, state transition -- regular commands only*)
    37 
    38 local
    39   val global_history = Unsynchronized.ref ([]: history);
    40   val global_state = Unsynchronized.ref Toplevel.toplevel;
    41   val global_exn = Unsynchronized.ref (NONE: (exn * string) option);
    42 in
    43 
    44 fun edit_history count f = NAMED_CRITICAL "Isar" (fn () =>
    45   let
    46     fun edit 0 (st, hist) = (global_history := hist; global_state := st; global_exn := NONE)
    47       | edit n (st, hist) = edit (n - 1) (f st hist);
    48   in edit count (! global_state, ! global_history) end);
    49 
    50 fun state () = ! global_state;
    51 
    52 fun exn () = ! global_exn;
    53 fun set_exn exn =  global_exn := exn;
    54 
    55 end;
    56 
    57 
    58 fun init () = edit_history 1 (K (K (Toplevel.toplevel, [])));
    59 
    60 fun goal () = Proof.goal (Toplevel.proof_of (state ()))
    61   handle Toplevel.UNDEF => error "No goal present";
    62 
    63 fun print () = Toplevel.print_state false (state ());
    64 
    65 
    66 (* history navigation *)
    67 
    68 local
    69 
    70 fun find_and_undo _ [] = error "Undo history exhausted"
    71   | find_and_undo which ((prev, tr) :: hist) =
    72       if which (Toplevel.name_of tr) then (prev, hist) else find_and_undo which hist;
    73 
    74 in
    75 
    76 fun linear_undo n = edit_history n (K (find_and_undo (K true)));
    77 
    78 fun undo n = edit_history n (fn st => fn hist =>
    79   find_and_undo (if Toplevel.is_proof st then K true else Keyword.is_theory) hist);
    80 
    81 fun kill () = edit_history 1 (fn st => fn hist =>
    82   find_and_undo
    83     (if Toplevel.is_proof st then Keyword.is_theory else Keyword.is_theory_begin) hist);
    84 
    85 fun kill_proof () = edit_history 1 (fn st => fn hist =>
    86   if Toplevel.is_proof st then find_and_undo Keyword.is_theory hist
    87   else raise Toplevel.UNDEF);
    88 
    89 end;
    90 
    91 
    92 (* interactive state transformations *)
    93 
    94 fun op >> tr =
    95   (case Toplevel.transition true tr (state ()) of
    96     NONE => false
    97   | SOME (_, SOME err) => (set_exn (SOME err); Toplevel.error_msg tr err; true)
    98   | SOME (st', NONE) =>
    99       let
   100         val name = Toplevel.name_of tr;
   101         val _ = if Keyword.is_theory_begin name then init () else ();
   102         val _ =
   103           if Keyword.is_regular name
   104           then edit_history 1 (fn st => fn hist => (st', (st, tr) :: hist)) else ();
   105       in true end);
   106 
   107 fun op >>> [] = ()
   108   | op >>> (tr :: trs) = if op >> tr then op >>> trs else ();
   109 
   110 
   111 (* toplevel loop *)
   112 
   113 val crashes = Unsynchronized.ref ([]: exn list);
   114 
   115 local
   116 
   117 fun raw_loop secure src =
   118   let
   119     fun check_secure () =
   120       (if secure then warning "Secure loop -- cannot exit to ML" else (); secure);
   121   in
   122     (case Source.get_single (Source.set_prompt Source.default_prompt src) of
   123       NONE => if secure then quit () else ()
   124     | SOME (tr, src') => if op >> tr orelse check_secure () then raw_loop secure src' else ())
   125     handle exn =>
   126       (Output.error_msg (ML_Compiler.exn_message exn)
   127         handle crash =>
   128           (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
   129             warning "Recovering from Isar toplevel crash -- see also Isar.crashes");
   130         raw_loop secure src)
   131   end;
   132 
   133 in
   134 
   135 fun toplevel_loop in_stream {init = do_init, welcome, sync, secure} =
   136  (Context.set_thread_data NONE;
   137   Secure.Isar_setup ();
   138   if do_init then init () else ();
   139   if welcome then writeln (Session.welcome ()) else ();
   140   uninterruptible (fn _ => fn () => raw_loop secure (Outer_Syntax.isar in_stream sync)) ());
   141 
   142 end;
   143 
   144 fun loop () =
   145   toplevel_loop TextIO.stdIn
   146     {init = false, welcome = false, sync = false, secure = Secure.is_secure ()};
   147 
   148 fun main () =
   149   toplevel_loop TextIO.stdIn
   150     {init = true, welcome = true, sync = false, secure = Secure.is_secure ()};
   151 
   152 
   153 
   154 (** command syntax **)
   155 
   156 local
   157 
   158 val op >> = Scan.>>;
   159 
   160 in
   161 
   162 (* global history *)
   163 
   164 val _ =
   165   Outer_Syntax.improper_command "init_toplevel" "init toplevel point-of-interest" Keyword.control
   166     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative init));
   167 
   168 val _ =
   169   Outer_Syntax.improper_command "linear_undo" "undo commands" Keyword.control
   170     (Scan.optional Parse.nat 1 >>
   171       (fn n => Toplevel.no_timing o Toplevel.imperative (fn () => linear_undo n)));
   172 
   173 val _ =
   174   Outer_Syntax.improper_command "undo" "undo commands (skipping closed proofs)" Keyword.control
   175     (Scan.optional Parse.nat 1 >>
   176       (fn n => Toplevel.no_timing o Toplevel.imperative (fn () => undo n)));
   177 
   178 val _ =
   179   Outer_Syntax.improper_command "undos_proof" "undo commands (skipping closed proofs)"
   180     Keyword.control
   181     (Scan.optional Parse.nat 1 >> (fn n => Toplevel.no_timing o
   182       Toplevel.keep (fn state =>
   183         if Toplevel.is_proof state then (undo n; print ()) else raise Toplevel.UNDEF)));
   184 
   185 val _ =
   186   Outer_Syntax.improper_command "cannot_undo" "partial undo -- Proof General legacy"
   187     Keyword.control
   188     (Parse.name >>
   189       (fn "end" => Toplevel.no_timing o Toplevel.imperative (fn () => undo 1)
   190         | txt => Toplevel.imperative (fn () => error ("Cannot undo " ^ quote txt))));
   191 
   192 val _ =
   193   Outer_Syntax.improper_command "kill" "kill partial proof or theory development" Keyword.control
   194     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
   195 
   196 end;
   197 
   198 end;