src/Pure/Isar/isar.ML
author wenzelm
Sun Jul 20 23:06:58 2008 +0200 (2008-07-20 ago)
changeset 27662 34e7236443f3
parent 27622 86608f598e9f
child 28300 111fc1879250
permissions -rw-r--r--
tty loop: do not report status;
wenzelm@26605
     1
(*  Title:      Pure/Isar/isar.ML
wenzelm@26605
     2
    ID:         $Id$
wenzelm@26605
     3
    Author:     Makarius
wenzelm@26605
     4
wenzelm@26605
     5
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm@26605
     6
*)
wenzelm@26605
     7
wenzelm@26605
     8
signature ISAR =
wenzelm@26605
     9
sig
wenzelm@27616
    10
  type id = string
wenzelm@27616
    11
  val no_id: id
wenzelm@27616
    12
  val create_command: Toplevel.transition -> id
wenzelm@27530
    13
  val init_point: unit -> unit
wenzelm@26605
    14
  val state: unit -> Toplevel.state
wenzelm@26605
    15
  val context: unit -> Proof.context
wenzelm@26605
    16
  val goal: unit -> thm
wenzelm@27533
    17
  val print: unit -> unit
wenzelm@27616
    18
  val exn: unit -> (exn * string) option
wenzelm@26605
    19
  val >> : Toplevel.transition -> bool
wenzelm@26605
    20
  val >>> : Toplevel.transition list -> unit
wenzelm@27529
    21
  val linear_undo: int -> unit
wenzelm@27524
    22
  val undo: int -> unit
wenzelm@27530
    23
  val kill: unit -> unit
wenzelm@27530
    24
  val kill_proof: unit -> unit
wenzelm@26605
    25
  val crashes: exn list ref
wenzelm@26643
    26
  val toplevel_loop: {init: bool, welcome: bool, sync: bool, secure: bool} -> unit
wenzelm@26605
    27
  val loop: unit -> unit
wenzelm@26606
    28
  val main: unit -> unit
wenzelm@27616
    29
  val insert_command: id -> id -> unit
wenzelm@27616
    30
  val remove_command: id -> unit
wenzelm@26605
    31
end;
wenzelm@26605
    32
wenzelm@26605
    33
structure Isar: ISAR =
wenzelm@26605
    34
struct
wenzelm@26605
    35
wenzelm@27432
    36
wenzelm@27428
    37
(** individual toplevel commands **)
wenzelm@27428
    38
wenzelm@27428
    39
(* unique identification *)
wenzelm@27428
    40
wenzelm@27428
    41
type id = string;
wenzelm@27428
    42
val no_id : id = "";
wenzelm@27428
    43
wenzelm@27428
    44
fun identify tr =
wenzelm@27428
    45
  (case Toplevel.get_id tr of
wenzelm@27428
    46
    SOME id => (id, tr)
wenzelm@27428
    47
  | NONE =>
wenzelm@27620
    48
      let val id =
wenzelm@27620
    49
        if ! Toplevel.debug then "isabelle:" ^ Toplevel.name_of tr ^ serial_string ()
wenzelm@27620
    50
        else "isabelle:" ^ serial_string ()
wenzelm@27428
    51
      in (id, Toplevel.put_id id tr) end);
wenzelm@27428
    52
wenzelm@27428
    53
wenzelm@27438
    54
(* command category *)
wenzelm@27438
    55
wenzelm@27524
    56
datatype category = Empty | Theory | Proof | Diag | Control;
wenzelm@27428
    57
wenzelm@27438
    58
fun category_of tr =
wenzelm@27438
    59
  let val name = Toplevel.name_of tr in
wenzelm@27438
    60
    if name = "" then Empty
wenzelm@27524
    61
    else if OuterKeyword.is_theory name then Theory
wenzelm@27524
    62
    else if OuterKeyword.is_proof name then Proof
wenzelm@27524
    63
    else if OuterKeyword.is_diag name then Diag
wenzelm@27524
    64
    else Control
wenzelm@27438
    65
  end;
wenzelm@27428
    66
wenzelm@27524
    67
val is_theory = fn Theory => true | _ => false;
wenzelm@27524
    68
val is_proper = fn Theory => true | Proof => true | _ => false;
wenzelm@27620
    69
val is_regular = fn Control => false | _ => true;
wenzelm@27524
    70
wenzelm@27428
    71
wenzelm@27616
    72
(* command status *)
wenzelm@27428
    73
wenzelm@27438
    74
datatype status =
wenzelm@27616
    75
  Unprocessed |
wenzelm@27616
    76
  Running |
wenzelm@27616
    77
  Failed of exn * string |
wenzelm@27616
    78
  Finished of Toplevel.state;
wenzelm@27616
    79
wenzelm@27616
    80
fun status_markup Unprocessed = Markup.unprocessed
wenzelm@27616
    81
  | status_markup Running = Markup.running
wenzelm@27616
    82
  | status_markup (Failed _) = Markup.failed
wenzelm@27616
    83
  | status_markup (Finished _) = Markup.finished;
wenzelm@27616
    84
wenzelm@27620
    85
fun run int tr state =
wenzelm@27620
    86
  (case Toplevel.transition int tr state of
wenzelm@27620
    87
    NONE => NONE
wenzelm@27620
    88
  | SOME (_, SOME err) => (Toplevel.error_msg tr err; SOME (Failed err))
wenzelm@27620
    89
  | SOME (state', NONE) => SOME (Finished state'));
wenzelm@27620
    90
wenzelm@27616
    91
wenzelm@27616
    92
(* datatype command *)
wenzelm@27438
    93
wenzelm@27428
    94
datatype command = Command of
wenzelm@27501
    95
 {category: category,
wenzelm@27438
    96
  transition: Toplevel.transition,
wenzelm@27428
    97
  status: status};
wenzelm@27428
    98
wenzelm@27501
    99
fun make_command (category, transition, status) =
wenzelm@27501
   100
  Command {category = category, transition = transition, status = status};
wenzelm@27438
   101
wenzelm@27438
   102
val empty_command =
wenzelm@27616
   103
  make_command (Empty, Toplevel.empty, Finished Toplevel.toplevel);
wenzelm@27428
   104
wenzelm@27501
   105
fun map_command f (Command {category, transition, status}) =
wenzelm@27501
   106
  make_command (f (category, transition, status));
wenzelm@27428
   107
wenzelm@27501
   108
fun map_status f = map_command (fn (category, transition, status) =>
wenzelm@27501
   109
  (category, transition, f status));
wenzelm@27428
   110
wenzelm@26605
   111
wenzelm@27501
   112
(* global collection of identified commands *)
wenzelm@27428
   113
wenzelm@27518
   114
fun err_dup id = sys_error ("Duplicate command " ^ quote id);
wenzelm@27518
   115
fun err_undef id = sys_error ("Unknown command " ^ quote id);
wenzelm@27518
   116
wenzelm@27616
   117
local val global_commands = ref (Graph.empty: command Graph.T) in
wenzelm@27428
   118
wenzelm@27501
   119
fun change_commands f = NAMED_CRITICAL "Isar" (fn () => change global_commands f)
wenzelm@27616
   120
  handle Graph.DUP bad => err_dup bad | Graph.UNDEF bad => err_undef bad;
wenzelm@27616
   121
wenzelm@27616
   122
fun get_commands () = NAMED_CRITICAL "Isar" (fn () => ! global_commands);
wenzelm@27501
   123
wenzelm@27616
   124
end;
wenzelm@27616
   125
wenzelm@27620
   126
fun add_edge (id1, id2) =
wenzelm@27620
   127
  if id1 = no_id orelse id2 = no_id then I else Graph.add_edge (id1, id2);
wenzelm@27620
   128
wenzelm@27616
   129
wenzelm@27616
   130
fun init_commands () = change_commands (K Graph.empty);
wenzelm@27428
   131
wenzelm@27428
   132
fun the_command id =
wenzelm@27524
   133
  let val Command cmd =
wenzelm@27524
   134
    if id = no_id then empty_command
wenzelm@27616
   135
    else (Graph.get_node (get_commands ()) id handle Graph.UNDEF bad => err_undef bad)
wenzelm@27524
   136
  in cmd end;
wenzelm@27518
   137
wenzelm@27518
   138
fun prev_command id =
wenzelm@27620
   139
  if id = no_id then no_id
wenzelm@27518
   140
  else
wenzelm@27616
   141
    (case Graph.imm_preds (get_commands ()) id handle Graph.UNDEF bad => err_undef bad of
wenzelm@27620
   142
      [] => no_id
wenzelm@27620
   143
    | [prev] => prev
wenzelm@27518
   144
    | _ => sys_error ("Non-linear command dependency " ^ quote id));
wenzelm@27501
   145
wenzelm@27616
   146
fun next_commands id =
wenzelm@27616
   147
  if id = no_id then []
wenzelm@27616
   148
  else Graph.imm_succs (get_commands ()) id handle Graph.UNDEF bad => err_undef bad;
wenzelm@27616
   149
wenzelm@27616
   150
fun descendant_commands ids =
wenzelm@27620
   151
  Graph.all_succs (get_commands ()) (distinct (op =) (filter_out (fn id => id = no_id) ids))
wenzelm@27616
   152
    handle Graph.UNDEF bad => err_undef bad;
wenzelm@27616
   153
wenzelm@27616
   154
wenzelm@27616
   155
(* maintain status *)
wenzelm@27616
   156
wenzelm@27616
   157
fun report_status markup id = Toplevel.status (#transition (the_command id)) markup;
wenzelm@27616
   158
wenzelm@27662
   159
fun update_status status id = change_commands (Graph.map_node id (map_status (K status)));
wenzelm@27662
   160
wenzelm@27662
   161
fun report_update_status status id =
wenzelm@27662
   162
  change_commands (Graph.map_node id (map_status (fn old_status =>
wenzelm@27662
   163
    let val markup = status_markup status
wenzelm@27662
   164
    in if markup <> status_markup old_status then report_status markup id else (); status end)));
wenzelm@27501
   165
wenzelm@26605
   166
wenzelm@27616
   167
(* create and dispose commands *)
wenzelm@27524
   168
wenzelm@27616
   169
fun create_command raw_tr =
wenzelm@27616
   170
  let
wenzelm@27616
   171
    val (id, tr) = identify raw_tr;
wenzelm@27616
   172
    val cmd = make_command (category_of tr, tr, Unprocessed);
wenzelm@27616
   173
    val _ = change_commands (Graph.new_node (id, cmd));
wenzelm@27616
   174
  in id end;
wenzelm@27616
   175
wenzelm@27620
   176
fun dispose_commands ids =
wenzelm@27616
   177
  let
wenzelm@27620
   178
    val desc = descendant_commands ids;
wenzelm@27616
   179
    val _ = List.app (report_status Markup.disposed) desc;
wenzelm@27616
   180
    val _ = change_commands (Graph.del_nodes desc);
wenzelm@27616
   181
  in () end;
wenzelm@27606
   182
wenzelm@27428
   183
wenzelm@27616
   184
(* final state *)
wenzelm@27501
   185
wenzelm@27616
   186
fun the_state id =
wenzelm@27616
   187
  (case the_command id of
wenzelm@27616
   188
    {status = Finished state, ...} => state
wenzelm@27616
   189
  | {transition, ...} => error ("Unfinished command " ^ Toplevel.str_of transition));
wenzelm@27501
   190
wenzelm@27428
   191
wenzelm@27428
   192
wenzelm@27616
   193
(** TTY model -- single-threaded **)
wenzelm@27428
   194
wenzelm@27428
   195
(* global point *)
wenzelm@27428
   196
wenzelm@27428
   197
local val global_point = ref no_id in
wenzelm@27428
   198
wenzelm@27428
   199
fun change_point f = NAMED_CRITICAL "Isar" (fn () => change global_point f);
wenzelm@27524
   200
fun point () = NAMED_CRITICAL "Isar" (fn () => ! global_point);
wenzelm@27428
   201
wenzelm@27501
   202
end;
wenzelm@27501
   203
wenzelm@27616
   204
wenzelm@27524
   205
fun set_point id = change_point (K id);
wenzelm@27530
   206
fun init_point () = set_point no_id;
wenzelm@27524
   207
wenzelm@27616
   208
fun point_state () = NAMED_CRITICAL "Isar" (fn () =>
wenzelm@27616
   209
  let val id = point () in (id, the_state id) end);
wenzelm@27501
   210
wenzelm@27616
   211
fun state () = #2 (point_state ());
wenzelm@26605
   212
wenzelm@26605
   213
fun context () =
wenzelm@26605
   214
  Toplevel.context_of (state ())
wenzelm@26605
   215
    handle Toplevel.UNDEF => error "Unknown context";
wenzelm@26605
   216
wenzelm@26605
   217
fun goal () =
wenzelm@26605
   218
  #2 (#2 (Proof.get_goal (Toplevel.proof_of (state ()))))
wenzelm@26605
   219
    handle Toplevel.UNDEF => error "No goal present";
wenzelm@26605
   220
wenzelm@27533
   221
fun print () = Toplevel.print_state false (state ());
wenzelm@27533
   222
wenzelm@26605
   223
wenzelm@27616
   224
(* global failure status *)
wenzelm@27616
   225
wenzelm@27616
   226
local val global_exn = ref (NONE: (exn * string) option) in
wenzelm@27616
   227
wenzelm@27616
   228
fun set_exn err = global_exn := err;
wenzelm@27616
   229
fun exn () = ! global_exn;
wenzelm@27616
   230
wenzelm@27616
   231
end;
wenzelm@27616
   232
wenzelm@27616
   233
wenzelm@27616
   234
(* interactive state transformations *)
wenzelm@26605
   235
wenzelm@26605
   236
nonfix >> >>>;
wenzelm@26605
   237
wenzelm@27428
   238
fun >> raw_tr =
wenzelm@27428
   239
  let
wenzelm@27616
   240
    val id = create_command raw_tr;
wenzelm@27616
   241
    val {category, transition = tr, ...} = the_command id;
wenzelm@27616
   242
    val (prev, prev_state) = point_state ();
wenzelm@27620
   243
    val _ =
wenzelm@27620
   244
      if is_regular category
wenzelm@27620
   245
      then (dispose_commands (next_commands prev); change_commands (add_edge (prev, id)))
wenzelm@27620
   246
      else ();
wenzelm@27428
   247
  in
wenzelm@27620
   248
    (case run true tr prev_state of
wenzelm@27620
   249
      NONE => false
wenzelm@27620
   250
    | SOME (status as Failed err) => (update_status status id; set_exn (SOME err); true)
wenzelm@27620
   251
    | SOME status =>
wenzelm@27620
   252
       (update_status status id; set_exn NONE;
wenzelm@27620
   253
        if is_regular category then set_point id else ();
wenzelm@27616
   254
        true))
wenzelm@27428
   255
  end;
wenzelm@26605
   256
wenzelm@26605
   257
fun >>> [] = ()
wenzelm@26605
   258
  | >>> (tr :: trs) = if >> tr then >>> trs else ();
wenzelm@26605
   259
wenzelm@26605
   260
wenzelm@27530
   261
(* implicit navigation wrt. proper commands *)
wenzelm@27524
   262
wenzelm@27524
   263
local
wenzelm@27524
   264
wenzelm@27524
   265
fun err_undo () = error "Undo history exhausted";
wenzelm@27524
   266
wenzelm@27530
   267
fun find_category which id =
wenzelm@27524
   268
  (case #category (the_command id) of
wenzelm@27524
   269
    Empty => err_undo ()
wenzelm@27620
   270
  | category => if which category then id else find_category which (prev_command id));
wenzelm@27530
   271
wenzelm@27530
   272
fun find_begin_theory id =
wenzelm@27530
   273
  if id = no_id then err_undo ()
wenzelm@27530
   274
  else if is_some (Toplevel.init_of (#transition (the_command id))) then id
wenzelm@27620
   275
  else find_begin_theory (prev_command id);
wenzelm@27524
   276
wenzelm@27529
   277
fun undo_command id =
wenzelm@27530
   278
  (case Toplevel.init_of (#transition (the_command id)) of
wenzelm@27620
   279
    SOME name => prev_command id before ThyInfo.kill_thy name
wenzelm@27620
   280
  | NONE => prev_command id);
wenzelm@27529
   281
wenzelm@27524
   282
in
wenzelm@27524
   283
wenzelm@27530
   284
fun linear_undo n = change_point (funpow n (fn id => undo_command (find_category is_proper id)));
wenzelm@27530
   285
wenzelm@27530
   286
fun undo n = change_point (funpow n (fn id => undo_command
wenzelm@27530
   287
  (find_category (if Toplevel.is_proof (the_state id) then is_proper else is_theory) id)));
wenzelm@27530
   288
wenzelm@27530
   289
fun kill () = change_point (fn id => undo_command
wenzelm@27530
   290
  (if Toplevel.is_proof (the_state id) then find_category is_theory id else find_begin_theory id));
wenzelm@27530
   291
wenzelm@27530
   292
fun kill_proof () = change_point (fn id =>
wenzelm@27530
   293
  if Toplevel.is_proof (the_state id) then undo_command (find_category is_theory id)
wenzelm@27530
   294
  else raise Toplevel.UNDEF);
wenzelm@27524
   295
wenzelm@27524
   296
end;
wenzelm@27524
   297
wenzelm@27524
   298
wenzelm@26606
   299
(* toplevel loop *)
wenzelm@26605
   300
wenzelm@26605
   301
val crashes = ref ([]: exn list);
wenzelm@26605
   302
wenzelm@26605
   303
local
wenzelm@26605
   304
wenzelm@26605
   305
fun raw_loop secure src =
wenzelm@26605
   306
  let
wenzelm@26605
   307
    fun check_secure () =
wenzelm@26605
   308
      (if secure then warning "Secure loop -- cannot exit to ML" else (); secure);
wenzelm@27524
   309
    val prev = point ();
wenzelm@27524
   310
    val prev_name = Toplevel.name_of (#transition (the_command prev));
wenzelm@27428
   311
    val prompt_markup =
wenzelm@27524
   312
      prev <> no_id ? Markup.markup
wenzelm@27524
   313
        (Markup.properties [(Markup.idN, prev), (Markup.nameN, prev_name)] Markup.prompt);
wenzelm@26605
   314
  in
wenzelm@27428
   315
    (case Source.get_single (Source.set_prompt (prompt_markup Source.default_prompt) src) of
wenzelm@26606
   316
      NONE => if secure then quit () else ()
wenzelm@26606
   317
    | SOME (tr, src') => if >> tr orelse check_secure () then raw_loop secure src' else ())
wenzelm@26605
   318
    handle exn => (Output.error_msg (Toplevel.exn_message exn) handle crash =>
wenzelm@26605
   319
      (CRITICAL (fn () => change crashes (cons crash));
wenzelm@26605
   320
        warning "Recovering after Isar toplevel crash -- see also Isar.crashes");
wenzelm@26605
   321
      raw_loop secure src)
wenzelm@26605
   322
  end;
wenzelm@26605
   323
wenzelm@26605
   324
in
wenzelm@26605
   325
wenzelm@27428
   326
fun toplevel_loop {init, welcome, sync, secure} =
wenzelm@26605
   327
 (Context.set_thread_data NONE;
wenzelm@27530
   328
  if init then (init_point (); init_commands ()) else ();
wenzelm@26643
   329
  if welcome then writeln (Session.welcome ()) else ();
wenzelm@26606
   330
  uninterruptible (fn _ => fn () => raw_loop secure (OuterSyntax.isar sync)) ());
wenzelm@26605
   331
wenzelm@26605
   332
end;
wenzelm@26605
   333
wenzelm@26643
   334
fun loop () =
wenzelm@26643
   335
  toplevel_loop {init = false, welcome = false, sync = false, secure = Secure.is_secure ()};
wenzelm@27528
   336
wenzelm@26643
   337
fun main () =
wenzelm@26643
   338
  toplevel_loop {init = true, welcome = true, sync = false, secure = Secure.is_secure ()};
wenzelm@26605
   339
wenzelm@27616
   340
wenzelm@27616
   341
wenzelm@27616
   342
(** editor model **)
wenzelm@27616
   343
wenzelm@27620
   344
(* run commands *)
wenzelm@27616
   345
wenzelm@27620
   346
fun try_run id =
wenzelm@27620
   347
  (case try the_state (prev_command id) of
wenzelm@27620
   348
    NONE => ()
wenzelm@27620
   349
  | SOME state =>
wenzelm@27622
   350
      (case run true (#transition (the_command id)) state of
wenzelm@27620
   351
        NONE => ()
wenzelm@27662
   352
      | SOME status => report_update_status status id));
wenzelm@27620
   353
      
wenzelm@27620
   354
fun rerun_commands ids =
wenzelm@27662
   355
  (List.app (report_update_status Unprocessed) ids; List.app try_run ids);
wenzelm@27616
   356
wenzelm@27616
   357
wenzelm@27616
   358
(* modify document *)
wenzelm@27616
   359
wenzelm@27616
   360
fun insert_command prev id = NAMED_CRITICAL "Isar" (fn () =>
wenzelm@27616
   361
  let
wenzelm@27616
   362
    val nexts = next_commands prev;
wenzelm@27616
   363
    val _ = change_commands
wenzelm@27620
   364
     (fold (fn next => Graph.del_edge (prev, next)) nexts #> add_edge (prev, id) #>
wenzelm@27620
   365
      fold (fn next => Graph.add_edge (id, next)) nexts);
wenzelm@27620
   366
  in descendant_commands [id] end) |> rerun_commands;
wenzelm@27616
   367
wenzelm@27616
   368
fun remove_command id = NAMED_CRITICAL "Isar" (fn () =>
wenzelm@27616
   369
  let
wenzelm@27620
   370
    val prev = prev_command id;
wenzelm@27616
   371
    val nexts = next_commands id;
wenzelm@27620
   372
    val _ = change_commands
wenzelm@27620
   373
     (fold (fn next => Graph.del_edge (id, next)) nexts #>
wenzelm@27620
   374
      fold (fn next => add_edge (prev, next)) nexts);
wenzelm@27620
   375
  in descendant_commands nexts end) |> rerun_commands;
wenzelm@27616
   376
wenzelm@26605
   377
end;
wenzelm@26605
   378