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