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