src/Pure/System/isar.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32786 f1ac4b515af9
child 33223 d27956b4d3b4
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
     1 (*  Title:      Pure/System/isar.ML
     2     Author:     Makarius
     3 
     4 The global Isabelle/Isar state and main 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 context: unit -> Proof.context
    13   val goal: unit -> thm
    14   val print: unit -> unit
    15   val >> : Toplevel.transition -> bool
    16   val >>> : Toplevel.transition list -> unit
    17   val linear_undo: int -> unit
    18   val undo: int -> unit
    19   val kill: unit -> unit
    20   val kill_proof: unit -> unit
    21   val crashes: exn list Unsynchronized.ref
    22   val toplevel_loop: {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 () = NAMED_CRITICAL "Isar" (fn () => ! global_state);
    51 
    52 fun exn () = NAMED_CRITICAL "Isar" (fn () => ! global_exn);
    53 fun set_exn exn =  NAMED_CRITICAL "Isar" (fn () => global_exn := exn);
    54 
    55 end;
    56 
    57 
    58 fun init () = edit_history 1 (K (K (Toplevel.toplevel, [])));
    59 
    60 fun context () = Toplevel.context_of (state ())
    61   handle Toplevel.UNDEF => error "Unknown context";
    62 
    63 fun goal () = #2 (#2 (Proof.get_goal (Toplevel.proof_of (state ()))))
    64   handle Toplevel.UNDEF => error "No goal present";
    65 
    66 fun print () = Toplevel.print_state false (state ());
    67 
    68 
    69 (* history navigation *)
    70 
    71 local
    72 
    73 fun find_and_undo _ [] = error "Undo history exhausted"
    74   | find_and_undo which ((prev, tr) :: hist) =
    75       ((case Toplevel.init_of tr of SOME name => ThyInfo.kill_thy name | NONE => ());
    76         if which (Toplevel.name_of tr) then (prev, hist) else find_and_undo which hist);
    77 
    78 in
    79 
    80 fun linear_undo n = edit_history n (K (find_and_undo (K true)));
    81 
    82 fun undo n = edit_history n (fn st => fn hist =>
    83   find_and_undo (if Toplevel.is_proof st then K true else OuterKeyword.is_theory) hist);
    84 
    85 fun kill () = edit_history 1 (fn st => fn hist =>
    86   find_and_undo
    87     (if Toplevel.is_proof st then OuterKeyword.is_theory else OuterKeyword.is_theory_begin) hist);
    88 
    89 fun kill_proof () = edit_history 1 (fn st => fn hist =>
    90   if Toplevel.is_proof st then find_and_undo OuterKeyword.is_theory hist
    91   else raise Toplevel.UNDEF);
    92 
    93 end;
    94 
    95 
    96 (* interactive state transformations *)
    97 
    98 fun op >> tr =
    99   (case Toplevel.transition true tr (state ()) of
   100     NONE => false
   101   | SOME (_, SOME err) => (set_exn (SOME err); Toplevel.error_msg tr err; true)
   102   | SOME (st', NONE) =>
   103       let
   104         val name = Toplevel.name_of tr;
   105         val _ = if OuterKeyword.is_theory_begin name then init () else ();
   106         val _ =
   107           if OuterKeyword.is_regular name
   108           then edit_history 1 (fn st => fn hist => (st', (st, tr) :: hist)) else ();
   109       in true end);
   110 
   111 fun op >>> [] = ()
   112   | op >>> (tr :: trs) = if op >> tr then op >>> trs else ();
   113 
   114 
   115 (* toplevel loop *)
   116 
   117 val crashes = Unsynchronized.ref ([]: exn list);
   118 
   119 local
   120 
   121 fun raw_loop secure src =
   122   let
   123     fun check_secure () =
   124       (if secure then warning "Secure loop -- cannot exit to ML" else (); secure);
   125   in
   126     (case Source.get_single (Source.set_prompt Source.default_prompt src) of
   127       NONE => if secure then quit () else ()
   128     | SOME (tr, src') => if op >> tr orelse check_secure () then raw_loop secure src' else ())
   129     handle exn =>
   130       (Output.error_msg (ML_Compiler.exn_message exn)
   131         handle crash =>
   132           (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
   133             warning "Recovering from Isar toplevel crash -- see also Isar.crashes");
   134           raw_loop secure src)
   135   end;
   136 
   137 in
   138 
   139 fun toplevel_loop {init = do_init, welcome, sync, secure} =
   140  (Context.set_thread_data NONE;
   141   Secure.open_unsynchronized ();
   142   if do_init then init () else ();
   143   if welcome then writeln (Session.welcome ()) else ();
   144   uninterruptible (fn _ => fn () => raw_loop secure (OuterSyntax.isar sync)) ());
   145 
   146 end;
   147 
   148 fun loop () =
   149   toplevel_loop {init = false, welcome = false, sync = false, secure = Secure.is_secure ()};
   150 
   151 fun main () =
   152   toplevel_loop {init = true, welcome = true, sync = false, secure = Secure.is_secure ()};
   153 
   154 
   155 
   156 (** command syntax **)
   157 
   158 local
   159 
   160 structure P = OuterParse and K = OuterKeyword;
   161 val op >> = Scan.>>;
   162 
   163 in
   164 
   165 (* global history *)
   166 
   167 val _ =
   168   OuterSyntax.improper_command "init_toplevel" "init toplevel point-of-interest" K.control
   169     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative init));
   170 
   171 val _ =
   172   OuterSyntax.improper_command "linear_undo" "undo commands" K.control
   173     (Scan.optional P.nat 1 >>
   174       (fn n => Toplevel.no_timing o Toplevel.imperative (fn () => linear_undo n)));
   175 
   176 val _ =
   177   OuterSyntax.improper_command "undo" "undo commands (skipping closed proofs)" K.control
   178     (Scan.optional P.nat 1 >>
   179       (fn n => Toplevel.no_timing o Toplevel.imperative (fn () => undo n)));
   180 
   181 val _ =
   182   OuterSyntax.improper_command "undos_proof" "undo commands (skipping closed proofs)" K.control
   183     (Scan.optional P.nat 1 >> (fn n => Toplevel.no_timing o
   184       Toplevel.keep (fn state =>
   185         if Toplevel.is_proof state then (undo n; print ()) else raise Toplevel.UNDEF)));
   186 
   187 val _ =
   188   OuterSyntax.improper_command "cannot_undo" "partial undo -- Proof General legacy" K.control
   189     (P.name >>
   190       (fn "end" => Toplevel.no_timing o Toplevel.imperative (fn () => undo 1)
   191         | txt => Toplevel.imperative (fn () => error ("Cannot undo " ^ quote txt))));
   192 
   193 val _ =
   194   OuterSyntax.improper_command "kill" "kill partial proof or theory development" K.control
   195     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
   196 
   197 end;
   198 
   199 end;