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