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