src/Pure/PIDE/document.ML
author wenzelm
Thu Jan 27 12:24:00 2011 +0100 (2011-01-27)
changeset 41634 28d94383249c
parent 41630 a7a93df23664
child 41672 2f70b1ddd09f
permissions -rw-r--r--
cancel document execution before editing, to improve reactivity on systems with few cores;
wenzelm@38150
     1
(*  Title:      Pure/PIDE/document.ML
wenzelm@38150
     2
    Author:     Makarius
wenzelm@38150
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@38418
     5
list of commands, associated with asynchronous execution process.
wenzelm@38150
     6
*)
wenzelm@38150
     7
wenzelm@38150
     8
signature DOCUMENT =
wenzelm@38150
     9
sig
wenzelm@38363
    10
  type id = int
wenzelm@38373
    11
  type version_id = id
wenzelm@38363
    12
  type command_id = id
wenzelm@38373
    13
  type exec_id = id
wenzelm@38363
    14
  val no_id: id
wenzelm@38419
    15
  val new_id: unit -> id
wenzelm@38363
    16
  val parse_id: string -> id
wenzelm@38363
    17
  val print_id: id -> string
wenzelm@38448
    18
  type edit = string * ((command_id option * command_id option) list) option
wenzelm@38418
    19
  type state
wenzelm@38418
    20
  val init_state: state
wenzelm@41634
    21
  val cancel: state -> unit
wenzelm@38418
    22
  val define_command: command_id -> string -> state -> state
wenzelm@38418
    23
  val edit: version_id -> version_id -> edit list -> state -> (command_id * exec_id) list * state
wenzelm@38418
    24
  val execute: version_id -> state -> state
wenzelm@38150
    25
end;
wenzelm@38150
    26
wenzelm@38150
    27
structure Document: DOCUMENT =
wenzelm@38150
    28
struct
wenzelm@38150
    29
wenzelm@38150
    30
(* unique identifiers *)
wenzelm@38150
    31
wenzelm@38363
    32
type id = int;
wenzelm@38373
    33
type version_id = id;
wenzelm@38363
    34
type command_id = id;
wenzelm@38373
    35
type exec_id = id;
wenzelm@38355
    36
wenzelm@38355
    37
val no_id = 0;
wenzelm@40449
    38
val new_id = Synchronized.counter ();
wenzelm@38418
    39
wenzelm@38414
    40
val parse_id = Markup.parse_int;
wenzelm@38414
    41
val print_id = Markup.print_int;
wenzelm@38150
    42
wenzelm@38418
    43
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
wenzelm@38418
    44
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
wenzelm@38150
    45
wenzelm@38418
    46
wenzelm@38418
    47
wenzelm@38418
    48
(** document structure **)
wenzelm@38418
    49
wenzelm@38448
    50
structure Entries = Linear_Set(type key = command_id val ord = int_ord);
wenzelm@38448
    51
wenzelm@38448
    52
abstype node = Node of exec_id option Entries.T  (*command entries with excecutions*)
wenzelm@38418
    53
  and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    54
with
wenzelm@38418
    55
wenzelm@38448
    56
val empty_node = Node Entries.empty;
wenzelm@38448
    57
val empty_version = Version Graph.empty;
wenzelm@38418
    58
wenzelm@38448
    59
fun fold_entries start f (Node entries) = Entries.fold start f entries;
wenzelm@38448
    60
fun first_entry start P (Node entries) = Entries.get_first start P entries;
wenzelm@38418
    61
wenzelm@38418
    62
wenzelm@38448
    63
(* node edits and associated executions *)
wenzelm@38150
    64
wenzelm@38150
    65
type edit =
wenzelm@38448
    66
  string *
wenzelm@38448
    67
  (*NONE: remove node, SOME: insert/remove commands*)
wenzelm@38448
    68
  ((command_id option * command_id option) list) option;
wenzelm@38448
    69
wenzelm@38448
    70
fun the_entry (Node entries) id =
wenzelm@38448
    71
  (case Entries.lookup entries id of
wenzelm@38448
    72
    NONE => err_undef "command entry" id
wenzelm@38448
    73
  | SOME entry => entry);
wenzelm@38150
    74
wenzelm@38448
    75
fun update_entry (id, exec_id) (Node entries) =
wenzelm@38448
    76
  Node (Entries.update (id, SOME exec_id) entries);
wenzelm@38418
    77
wenzelm@38448
    78
fun reset_after id entries =
wenzelm@38448
    79
  (case Entries.get_after entries id of
wenzelm@38448
    80
    NONE => entries
wenzelm@38448
    81
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
    82
wenzelm@38448
    83
fun edit_node (hook, SOME id2) (Node entries) =
wenzelm@38448
    84
      Node (Entries.insert_after hook (id2, NONE) entries)
wenzelm@38448
    85
  | edit_node (hook, NONE) (Node entries) =
wenzelm@38448
    86
      Node (entries |> Entries.delete_after hook |> reset_after hook);
wenzelm@38418
    87
wenzelm@38418
    88
wenzelm@38418
    89
(* version operations *)
wenzelm@38418
    90
wenzelm@38418
    91
fun nodes_of (Version nodes) = nodes;
wenzelm@38418
    92
val node_names_of = Graph.keys o nodes_of;
wenzelm@38418
    93
wenzelm@38448
    94
fun get_node version name = Graph.get_node (nodes_of version) name
wenzelm@38448
    95
  handle Graph.UNDEF _ => empty_node;
wenzelm@38448
    96
wenzelm@38418
    97
fun edit_nodes (name, SOME edits) (Version nodes) =
wenzelm@38418
    98
      Version (nodes
wenzelm@38418
    99
        |> Graph.default_node (name, empty_node)
wenzelm@38418
   100
        |> Graph.map_node name (fold edit_node edits))
wenzelm@38448
   101
  | edit_nodes (name, NONE) (Version nodes) =
wenzelm@40481
   102
      Version (perhaps (try (Graph.del_node name)) nodes);
wenzelm@38418
   103
wenzelm@38418
   104
fun put_node name node (Version nodes) =
wenzelm@40481
   105
  Version (nodes
wenzelm@40481
   106
    |> Graph.default_node (name, empty_node)
wenzelm@40481
   107
    |> Graph.map_node name (K node));
wenzelm@38418
   108
wenzelm@38150
   109
end;
wenzelm@38150
   110
wenzelm@38418
   111
wenzelm@38418
   112
wenzelm@38418
   113
(** global state -- document structure and execution process **)
wenzelm@38418
   114
wenzelm@38418
   115
abstype state = State of
wenzelm@40398
   116
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@38421
   117
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
wenzelm@40398
   118
  execs: (bool * Toplevel.state) lazy Inttab.table,  (*exec_id -> execution process*)
wenzelm@40398
   119
  execution: unit future list}  (*global execution process*)
wenzelm@38418
   120
with
wenzelm@38418
   121
wenzelm@38418
   122
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   123
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   124
wenzelm@38418
   125
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   126
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   127
wenzelm@38418
   128
val init_state =
wenzelm@38418
   129
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38449
   130
    Inttab.make [(no_id, Future.value Toplevel.empty)],
wenzelm@40398
   131
    Inttab.make [(no_id, Lazy.value (true, Toplevel.toplevel))],
wenzelm@38418
   132
    []);
wenzelm@38418
   133
wenzelm@38418
   134
wenzelm@38418
   135
(* document versions *)
wenzelm@38418
   136
wenzelm@38418
   137
fun define_version (id: version_id) version =
wenzelm@38418
   138
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   139
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   140
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   141
    in (versions', commands, execs, execution) end);
wenzelm@38418
   142
wenzelm@38418
   143
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   144
  (case Inttab.lookup versions id of
wenzelm@38418
   145
    NONE => err_undef "document version" id
wenzelm@38418
   146
  | SOME version => version);
wenzelm@38418
   147
wenzelm@38418
   148
wenzelm@38418
   149
(* commands *)
wenzelm@38418
   150
wenzelm@38418
   151
fun define_command (id: command_id) text =
wenzelm@38418
   152
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   153
    let
wenzelm@38418
   154
      val id_string = print_id id;
wenzelm@38421
   155
      val tr = Future.fork_pri 2 (fn () =>
wenzelm@38418
   156
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@38421
   157
          (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
wenzelm@38418
   158
      val commands' =
wenzelm@38421
   159
        Inttab.update_new (id, tr) commands
wenzelm@38418
   160
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   161
    in (versions, commands', execs, execution) end);
wenzelm@38418
   162
wenzelm@38418
   163
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   164
  (case Inttab.lookup commands id of
wenzelm@38418
   165
    NONE => err_undef "command" id
wenzelm@38418
   166
  | SOME tr => tr);
wenzelm@38418
   167
wenzelm@38421
   168
fun join_commands (State {commands, ...}) =
wenzelm@38421
   169
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
wenzelm@38421
   170
wenzelm@38418
   171
wenzelm@38418
   172
(* command executions *)
wenzelm@38418
   173
wenzelm@38418
   174
fun define_exec (id: exec_id) exec =
wenzelm@38418
   175
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   176
    let val execs' = Inttab.update_new (id, exec) execs
wenzelm@38418
   177
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   178
    in (versions, commands, execs', execution) end);
wenzelm@38418
   179
wenzelm@38418
   180
fun the_exec (State {execs, ...}) (id: exec_id) =
wenzelm@38418
   181
  (case Inttab.lookup execs id of
wenzelm@38418
   182
    NONE => err_undef "command execution" id
wenzelm@38418
   183
  | SOME exec => exec);
wenzelm@38418
   184
wenzelm@41634
   185
wenzelm@41634
   186
(* document execution *)
wenzelm@41634
   187
wenzelm@41634
   188
fun cancel (State {execution, ...}) =
wenzelm@41634
   189
  List.app Future.cancel execution;
wenzelm@41634
   190
wenzelm@41634
   191
fun await_cancellation (State {execution, ...}) =
wenzelm@41634
   192
  ignore (Future.join_results execution);
wenzelm@41634
   193
wenzelm@38418
   194
end;
wenzelm@38418
   195
wenzelm@38418
   196
wenzelm@38418
   197
wenzelm@38888
   198
(* toplevel transactions *)
wenzelm@38888
   199
wenzelm@38888
   200
local
wenzelm@38888
   201
wenzelm@40391
   202
fun timing tr t = Toplevel.status tr (Markup.timing t);
wenzelm@40391
   203
wenzelm@38888
   204
fun proof_status tr st =
wenzelm@38888
   205
  (case try Toplevel.proof_of st of
wenzelm@38888
   206
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   207
  | NONE => ());
wenzelm@38888
   208
wenzelm@38888
   209
fun async_state tr st =
wenzelm@40534
   210
  ignore
wenzelm@40534
   211
    (Future.fork_group (Task_Queue.new_group NONE)
wenzelm@40534
   212
      (fn () =>
wenzelm@40534
   213
        Toplevel.setmp_thread_position tr
wenzelm@40534
   214
          (fn () => Toplevel.print_state false st) ()));
wenzelm@38888
   215
wenzelm@40390
   216
fun run int tr st =
wenzelm@40390
   217
  (case Toplevel.transition int tr st of
wenzelm@40390
   218
    SOME (st', NONE) => ([], SOME st')
wenzelm@40390
   219
  | SOME (_, SOME exn_info) =>
wenzelm@40390
   220
      (case ML_Compiler.exn_messages (Runtime.EXCURSION_FAIL exn_info) of
wenzelm@40390
   221
        [] => Exn.interrupt ()
wenzelm@40390
   222
      | errs => (errs, NONE))
wenzelm@40390
   223
  | NONE => ([ML_Compiler.exn_message Runtime.TERMINATE], NONE));
wenzelm@40390
   224
wenzelm@38888
   225
in
wenzelm@38888
   226
wenzelm@41537
   227
fun run_command thy_name raw_tr st =
wenzelm@38888
   228
  (case
wenzelm@41537
   229
      (case Toplevel.init_of raw_tr of
wenzelm@41537
   230
        SOME name => Exn.capture (fn () =>
wenzelm@41537
   231
          let
wenzelm@41537
   232
            val path = Path.explode thy_name;
wenzelm@41537
   233
            val _ = Thy_Header.consistent_name (Path.implode (Path.base path)) name;
wenzelm@41537
   234
          in Toplevel.modify_master (SOME (Path.dir path)) raw_tr end) ()
wenzelm@41537
   235
      | NONE => Exn.Result raw_tr) of
wenzelm@41537
   236
    Exn.Result tr =>
wenzelm@38888
   237
      let
wenzelm@40534
   238
        val is_init = is_some (Toplevel.init_of tr);
wenzelm@40534
   239
        val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@40534
   240
        val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@40534
   241
wenzelm@40391
   242
        val start = start_timing ();
wenzelm@40534
   243
        val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@40391
   244
        val _ = timing tr (end_timing start);
wenzelm@38888
   245
        val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@40398
   246
        val res =
wenzelm@38888
   247
          (case result of
wenzelm@40398
   248
            NONE => (Toplevel.status tr Markup.failed; (false, st))
wenzelm@38888
   249
          | SOME st' =>
wenzelm@38888
   250
             (Toplevel.status tr Markup.finished;
wenzelm@38888
   251
              proof_status tr st';
wenzelm@40534
   252
              if do_print then async_state tr st' else ();
wenzelm@40398
   253
              (true, st')));
wenzelm@40398
   254
      in res end
wenzelm@38888
   255
  | Exn.Exn exn =>
wenzelm@39238
   256
      if Exn.is_interrupt exn then reraise exn
wenzelm@39238
   257
      else
wenzelm@41537
   258
       (Toplevel.error_msg raw_tr (ML_Compiler.exn_message exn);
wenzelm@41537
   259
        Toplevel.status raw_tr Markup.failed;
wenzelm@40398
   260
        (false, Toplevel.toplevel)));
wenzelm@38888
   261
wenzelm@38888
   262
end;
wenzelm@38888
   263
wenzelm@38888
   264
wenzelm@38888
   265
wenzelm@38888
   266
wenzelm@38418
   267
(** editing **)
wenzelm@38418
   268
wenzelm@38418
   269
(* edit *)
wenzelm@38418
   270
wenzelm@38418
   271
local
wenzelm@38418
   272
wenzelm@38448
   273
fun is_changed node' ((_, id), exec) =
wenzelm@38418
   274
  (case try (the_entry node') id of
wenzelm@38418
   275
    NONE => true
wenzelm@38448
   276
  | SOME exec' => exec' <> exec);
wenzelm@38418
   277
wenzelm@38418
   278
fun new_exec name (id: command_id) (exec_id, updates, state) =
wenzelm@38418
   279
  let
wenzelm@38418
   280
    val exec = the_exec state exec_id;
wenzelm@38419
   281
    val exec_id' = new_id ();
wenzelm@38449
   282
    val future_tr = the_command state id;
wenzelm@38418
   283
    val exec' =
wenzelm@38418
   284
      Lazy.lazy (fn () =>
wenzelm@40398
   285
        let
wenzelm@40398
   286
          val st = #2 (Lazy.force exec);
wenzelm@40398
   287
          val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join future_tr);
wenzelm@40398
   288
        in run_command name exec_tr st end);
wenzelm@38418
   289
    val state' = define_exec exec_id' exec' state;
wenzelm@38418
   290
  in (exec_id', (id, exec_id') :: updates, state') end;
wenzelm@38418
   291
wenzelm@38418
   292
in
wenzelm@38418
   293
wenzelm@38418
   294
fun edit (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   295
  let
wenzelm@38418
   296
    val old_version = the_version state old_id;
wenzelm@41629
   297
    val _ = Time.now ();  (* FIXME odd workaround *)
wenzelm@38418
   298
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   299
wenzelm@38418
   300
    fun update_node name (rev_updates, version, st) =
wenzelm@38448
   301
      let val node = get_node version name in
wenzelm@38448
   302
        (case first_entry NONE (is_changed (get_node old_version name)) node of
wenzelm@38418
   303
          NONE => (rev_updates, version, st)
wenzelm@38448
   304
        | SOME ((prev, id), _) =>
wenzelm@38418
   305
            let
wenzelm@38448
   306
              val prev_exec =
wenzelm@38448
   307
                (case prev of
wenzelm@38448
   308
                  NONE => no_id
wenzelm@38448
   309
                | SOME prev_id => the_default no_id (the_entry node prev_id));
wenzelm@38418
   310
              val (_, rev_upds, st') =
wenzelm@38448
   311
                fold_entries (SOME id) (new_exec name o #2 o #1) node (prev_exec, [], st);
wenzelm@38448
   312
              val node' = fold update_entry rev_upds node;
wenzelm@38418
   313
            in (rev_upds @ rev_updates, put_node name node' version, st') end)
wenzelm@38418
   314
      end;
wenzelm@38418
   315
wenzelm@38418
   316
    (* FIXME proper node deps *)
wenzelm@38418
   317
    val (rev_updates, new_version', state') =
wenzelm@38418
   318
      fold update_node (node_names_of new_version) ([], new_version, state);
wenzelm@38418
   319
    val state'' = define_version new_id new_version' state';
wenzelm@38421
   320
wenzelm@38421
   321
    val _ = join_commands state'';  (* FIXME async!? *)
wenzelm@38418
   322
  in (rev rev_updates, state'') end;
wenzelm@38418
   323
wenzelm@38418
   324
end;
wenzelm@38418
   325
wenzelm@38418
   326
wenzelm@38418
   327
(* execute *)
wenzelm@38418
   328
wenzelm@38418
   329
fun execute version_id state =
wenzelm@38418
   330
  state |> map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   331
    let
wenzelm@38418
   332
      val version = the_version state version_id;
wenzelm@38418
   333
wenzelm@38418
   334
      fun force_exec NONE = ()
wenzelm@38418
   335
        | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
wenzelm@38418
   336
wenzelm@41634
   337
      val _ = cancel state;
wenzelm@38418
   338
wenzelm@38418
   339
      val execution' = (* FIXME proper node deps *)
wenzelm@41630
   340
        [Future.fork_pri 1 (fn () =>
wenzelm@41630
   341
          let
wenzelm@41634
   342
            val _ = await_cancellation state;
wenzelm@41630
   343
            val _ =
wenzelm@41630
   344
              node_names_of version |> List.app (fn name =>
wenzelm@41630
   345
                fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
wenzelm@41630
   346
                    (get_node version name) ());
wenzelm@41630
   347
          in () end)];
wenzelm@40520
   348
wenzelm@41634
   349
      val _ = await_cancellation state;  (* FIXME async!? *)
wenzelm@40520
   350
wenzelm@38418
   351
    in (versions, commands, execs, execution') end);
wenzelm@38418
   352
wenzelm@38418
   353
end;
wenzelm@38418
   354