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