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