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