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