src/Pure/System/isar_document.ML
author wenzelm
Sun Aug 15 14:18:52 2010 +0200 (2010-08-15)
changeset 38417 b8922ae21111
parent 38414 49f1f657adc2
child 38418 9a7af64d71bb
permissions -rw-r--r--
renamed class Document to Document.Version etc.;
renamed Change.prev to Change.previous, and Change.document to Change.current;
tuned;
wenzelm@38412
     1
(*  Title:      Pure/System/isar_document.ML
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@38412
     4
Interactive Isar documents, which are structured as follows:
wenzelm@38412
     5
wenzelm@38412
     6
  - history: tree of documents (i.e. changes without merge)
wenzelm@38412
     7
  - document: graph of nodes (cf. theory files)
wenzelm@38412
     8
  - node: linear set of commands, potentially with proof structure
wenzelm@38412
     9
*)
wenzelm@38412
    10
wenzelm@38412
    11
structure Isar_Document: sig end =
wenzelm@38412
    12
struct
wenzelm@38412
    13
wenzelm@38412
    14
(* unique identifiers *)
wenzelm@38412
    15
wenzelm@38412
    16
local
wenzelm@38412
    17
  val id_count = Synchronized.var "id" 0;
wenzelm@38412
    18
in
wenzelm@38412
    19
  fun create_id () =
wenzelm@38412
    20
    Synchronized.change_result id_count
wenzelm@38412
    21
      (fn i =>
wenzelm@38412
    22
        let val i' = i + 1
wenzelm@38412
    23
        in (i', i') end);
wenzelm@38412
    24
end;
wenzelm@38412
    25
wenzelm@38412
    26
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ Document.print_id id);
wenzelm@38412
    27
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ Document.print_id id);
wenzelm@38412
    28
wenzelm@38412
    29
wenzelm@38417
    30
(** document versions **)
wenzelm@38412
    31
wenzelm@38412
    32
datatype entry = Entry of {next: Document.command_id option, exec: Document.exec_id option};
wenzelm@38412
    33
type node = entry Inttab.table; (*unique command entries indexed by command_id, start with no_id*)
wenzelm@38417
    34
type version = node Graph.T;   (*development graph via static imports*)
wenzelm@38412
    35
wenzelm@38412
    36
wenzelm@38412
    37
(* command entries *)
wenzelm@38412
    38
wenzelm@38412
    39
fun make_entry next exec = Entry {next = next, exec = exec};
wenzelm@38412
    40
wenzelm@38412
    41
fun the_entry (node: node) (id: Document.command_id) =
wenzelm@38412
    42
  (case Inttab.lookup node id of
wenzelm@38412
    43
    NONE => err_undef "command entry" id
wenzelm@38412
    44
  | SOME (Entry entry) => entry);
wenzelm@38412
    45
wenzelm@38412
    46
fun put_entry (id: Document.command_id, entry: entry) = Inttab.update (id, entry);
wenzelm@38412
    47
wenzelm@38412
    48
fun put_entry_exec (id: Document.command_id) exec (node: node) =
wenzelm@38412
    49
  let val {next, ...} = the_entry node id
wenzelm@38412
    50
  in put_entry (id, make_entry next exec) node end;
wenzelm@38412
    51
wenzelm@38412
    52
fun reset_entry_exec id = put_entry_exec id NONE;
wenzelm@38412
    53
fun set_entry_exec (id, exec_id) = put_entry_exec id (SOME exec_id);
wenzelm@38412
    54
wenzelm@38412
    55
wenzelm@38412
    56
(* iterate entries *)
wenzelm@38412
    57
wenzelm@38412
    58
fun fold_entries id0 f (node: node) =
wenzelm@38412
    59
  let
wenzelm@38412
    60
    fun apply NONE x = x
wenzelm@38412
    61
      | apply (SOME id) x =
wenzelm@38412
    62
          let val entry = the_entry node id
wenzelm@38412
    63
          in apply (#next entry) (f (id, entry) x) end;
wenzelm@38412
    64
  in if Inttab.defined node id0 then apply (SOME id0) else I end;
wenzelm@38412
    65
wenzelm@38412
    66
fun first_entry P (node: node) =
wenzelm@38412
    67
  let
wenzelm@38412
    68
    fun first _ NONE = NONE
wenzelm@38412
    69
      | first prev (SOME id) =
wenzelm@38412
    70
          let val entry = the_entry node id
wenzelm@38412
    71
          in if P (id, entry) then SOME (prev, id, entry) else first (SOME id) (#next entry) end;
wenzelm@38412
    72
  in first NONE (SOME Document.no_id) end;
wenzelm@38412
    73
wenzelm@38412
    74
wenzelm@38412
    75
(* modify entries *)
wenzelm@38412
    76
wenzelm@38412
    77
fun insert_after (id: Document.command_id) (id2: Document.command_id) (node: node) =
wenzelm@38412
    78
  let val {next, exec} = the_entry node id in
wenzelm@38412
    79
    node
wenzelm@38412
    80
    |> put_entry (id, make_entry (SOME id2) exec)
wenzelm@38412
    81
    |> put_entry (id2, make_entry next NONE)
wenzelm@38412
    82
  end;
wenzelm@38412
    83
wenzelm@38412
    84
fun delete_after (id: Document.command_id) (node: node) =
wenzelm@38412
    85
  let val {next, exec} = the_entry node id in
wenzelm@38412
    86
    (case next of
wenzelm@38412
    87
      NONE => error ("No next entry to delete: " ^ Document.print_id id)
wenzelm@38412
    88
    | SOME id2 =>
wenzelm@38412
    89
        node |>
wenzelm@38412
    90
          (case #next (the_entry node id2) of
wenzelm@38412
    91
            NONE => put_entry (id, make_entry NONE exec)
wenzelm@38412
    92
          | SOME id3 => put_entry (id, make_entry (SOME id3) exec) #> reset_entry_exec id3))
wenzelm@38412
    93
  end;
wenzelm@38412
    94
wenzelm@38412
    95
wenzelm@38412
    96
(* node operations *)
wenzelm@38412
    97
wenzelm@38412
    98
val empty_node: node = Inttab.make [(Document.no_id, make_entry NONE (SOME Document.no_id))];
wenzelm@38412
    99
wenzelm@38417
   100
fun the_node (version: version) name =
wenzelm@38417
   101
  Graph.get_node version name handle Graph.UNDEF _ => empty_node;
wenzelm@38412
   102
wenzelm@38412
   103
fun edit_node (id, SOME id2) = insert_after id id2
wenzelm@38412
   104
  | edit_node (id, NONE) = delete_after id;
wenzelm@38412
   105
wenzelm@38412
   106
fun edit_nodes (name, SOME edits) =
wenzelm@38412
   107
      Graph.default_node (name, empty_node) #>
wenzelm@38412
   108
      Graph.map_node name (fold edit_node edits)
wenzelm@38412
   109
  | edit_nodes (name, NONE) = Graph.del_node name;
wenzelm@38412
   110
wenzelm@38412
   111
wenzelm@38412
   112
wenzelm@38412
   113
(** global configuration **)
wenzelm@38412
   114
wenzelm@38412
   115
(* command executions *)
wenzelm@38412
   116
wenzelm@38412
   117
local
wenzelm@38412
   118
wenzelm@38412
   119
val global_execs =
wenzelm@38412
   120
  Unsynchronized.ref (Inttab.make [(Document.no_id, Lazy.value (SOME Toplevel.toplevel))]);
wenzelm@38412
   121
wenzelm@38412
   122
in
wenzelm@38412
   123
wenzelm@38412
   124
fun define_exec (id: Document.exec_id) exec =
wenzelm@38412
   125
  NAMED_CRITICAL "Isar" (fn () =>
wenzelm@38412
   126
    Unsynchronized.change global_execs (Inttab.update_new (id, exec))
wenzelm@38412
   127
      handle Inttab.DUP dup => err_dup "exec" dup);
wenzelm@38412
   128
wenzelm@38412
   129
fun the_exec (id: Document.exec_id) =
wenzelm@38412
   130
  (case Inttab.lookup (! global_execs) id of
wenzelm@38412
   131
    NONE => err_undef "exec" id
wenzelm@38412
   132
  | SOME exec => exec);
wenzelm@38412
   133
wenzelm@38412
   134
end;
wenzelm@38412
   135
wenzelm@38412
   136
wenzelm@38412
   137
(* commands *)
wenzelm@38412
   138
wenzelm@38412
   139
local
wenzelm@38412
   140
wenzelm@38412
   141
val global_commands = Unsynchronized.ref (Inttab.make [(Document.no_id, Toplevel.empty)]);
wenzelm@38412
   142
wenzelm@38412
   143
in
wenzelm@38412
   144
wenzelm@38412
   145
fun define_command (id: Document.command_id) text =
wenzelm@38412
   146
  let
wenzelm@38412
   147
    val id_string = Document.print_id id;
wenzelm@38412
   148
    val tr =
wenzelm@38412
   149
      Position.setmp_thread_data (Position.id_only id_string) (fn () =>
wenzelm@38412
   150
        Outer_Syntax.prepare_command (Position.id id_string) text) ();
wenzelm@38412
   151
  in
wenzelm@38412
   152
    NAMED_CRITICAL "Isar" (fn () =>
wenzelm@38412
   153
      Unsynchronized.change global_commands (Inttab.update_new (id, Toplevel.put_id id_string tr))
wenzelm@38412
   154
        handle Inttab.DUP dup => err_dup "command" dup)
wenzelm@38412
   155
  end;
wenzelm@38412
   156
wenzelm@38412
   157
fun the_command (id: Document.command_id) =
wenzelm@38412
   158
  (case Inttab.lookup (! global_commands) id of
wenzelm@38412
   159
    NONE => err_undef "command" id
wenzelm@38412
   160
  | SOME tr => tr);
wenzelm@38412
   161
wenzelm@38412
   162
end;
wenzelm@38412
   163
wenzelm@38412
   164
wenzelm@38412
   165
(* document versions *)
wenzelm@38412
   166
wenzelm@38412
   167
local
wenzelm@38412
   168
wenzelm@38417
   169
val global_versions = Unsynchronized.ref (Inttab.make [(Document.no_id, Graph.empty: version)]);
wenzelm@38412
   170
wenzelm@38412
   171
in
wenzelm@38412
   172
wenzelm@38417
   173
fun define_version (id: Document.version_id) version =
wenzelm@38412
   174
  NAMED_CRITICAL "Isar" (fn () =>
wenzelm@38417
   175
    Unsynchronized.change global_versions (Inttab.update_new (id, version))
wenzelm@38417
   176
      handle Inttab.DUP dup => err_dup "version" dup);
wenzelm@38412
   177
wenzelm@38417
   178
fun the_version (id: Document.version_id) =
wenzelm@38417
   179
  (case Inttab.lookup (! global_versions) id of
wenzelm@38417
   180
    NONE => err_undef "version" id
wenzelm@38417
   181
  | SOME version => version);
wenzelm@38412
   182
wenzelm@38412
   183
end;
wenzelm@38412
   184
wenzelm@38412
   185
wenzelm@38412
   186
wenzelm@38412
   187
(** document editing **)
wenzelm@38412
   188
wenzelm@38412
   189
(* execution *)
wenzelm@38412
   190
wenzelm@38412
   191
local
wenzelm@38412
   192
wenzelm@38412
   193
val execution: unit future list Unsynchronized.ref = Unsynchronized.ref [];
wenzelm@38412
   194
wenzelm@38412
   195
fun force_exec NONE = ()
wenzelm@38412
   196
  | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec exec_id));
wenzelm@38412
   197
wenzelm@38412
   198
in
wenzelm@38412
   199
wenzelm@38417
   200
fun execute version =
wenzelm@38412
   201
  NAMED_CRITICAL "Isar" (fn () =>
wenzelm@38412
   202
    let
wenzelm@38412
   203
      val old_execution = ! execution;
wenzelm@38412
   204
      val _ = List.app Future.cancel old_execution;
wenzelm@38412
   205
      fun await_cancellation () = uninterruptible (K Future.join_results) old_execution;
wenzelm@38412
   206
      (* FIXME proper node deps *)
wenzelm@38417
   207
      val new_execution = Graph.keys version |> map (fn name =>
wenzelm@38412
   208
        Future.fork_pri 1 (fn () =>
wenzelm@38412
   209
          let
wenzelm@38412
   210
            val _ = await_cancellation ();
wenzelm@38412
   211
            val exec =
wenzelm@38412
   212
              fold_entries Document.no_id (fn (_, {exec, ...}) => fn () => force_exec exec)
wenzelm@38417
   213
                (the_node version name);
wenzelm@38412
   214
          in exec () end));
wenzelm@38412
   215
    in execution := new_execution end);
wenzelm@38412
   216
wenzelm@38412
   217
end;
wenzelm@38412
   218
wenzelm@38412
   219
wenzelm@38412
   220
(* editing *)
wenzelm@38412
   221
wenzelm@38412
   222
local
wenzelm@38412
   223
wenzelm@38412
   224
fun is_changed node' (id, {next = _, exec}) =
wenzelm@38412
   225
  (case try (the_entry node') id of
wenzelm@38412
   226
    NONE => true
wenzelm@38412
   227
  | SOME {next = _, exec = exec'} => exec' <> exec);
wenzelm@38412
   228
wenzelm@38412
   229
fun new_exec name (id: Document.command_id) (exec_id, updates) =
wenzelm@38412
   230
  let
wenzelm@38412
   231
    val exec = the_exec exec_id;
wenzelm@38412
   232
    val exec_id' = create_id ();
wenzelm@38412
   233
    val tr = Toplevel.put_id (Document.print_id exec_id') (the_command id);
wenzelm@38412
   234
    val exec' =
wenzelm@38412
   235
      Lazy.lazy (fn () =>
wenzelm@38412
   236
        (case Lazy.force exec of
wenzelm@38412
   237
          NONE => NONE
wenzelm@38412
   238
        | SOME st => Toplevel.run_command name tr st));
wenzelm@38412
   239
    val _ = define_exec exec_id' exec';
wenzelm@38412
   240
  in (exec_id', (id, exec_id') :: updates) end;
wenzelm@38412
   241
wenzelm@38412
   242
fun updates_status new_id updates =
wenzelm@38414
   243
  implode (map (fn (id, exec_id) => Markup.markup (Markup.edit id exec_id) "") updates)
wenzelm@38414
   244
  |> Markup.markup (Markup.assign new_id)
wenzelm@38414
   245
  |> Output.status;
wenzelm@38412
   246
wenzelm@38412
   247
in
wenzelm@38412
   248
wenzelm@38412
   249
fun edit_document (old_id: Document.version_id) (new_id: Document.version_id) edits =
wenzelm@38412
   250
  NAMED_CRITICAL "Isar" (fn () =>
wenzelm@38412
   251
    let
wenzelm@38417
   252
      val old_version = the_version old_id;
wenzelm@38417
   253
      val new_version = fold edit_nodes edits old_version;
wenzelm@38412
   254
wenzelm@38412
   255
      fun update_node name node =
wenzelm@38417
   256
        (case first_entry (is_changed (the_node old_version name)) node of
wenzelm@38412
   257
          NONE => ([], node)
wenzelm@38412
   258
        | SOME (prev, id, _) =>
wenzelm@38412
   259
            let
wenzelm@38412
   260
              val prev_exec_id = the (#exec (the_entry node (the prev)));
wenzelm@38412
   261
              val (_, updates) = fold_entries id (new_exec name o #1) node (prev_exec_id, []);
wenzelm@38412
   262
              val node' = fold set_entry_exec updates node;
wenzelm@38412
   263
            in (rev updates, node') end);
wenzelm@38412
   264
wenzelm@38412
   265
      (* FIXME proper node deps *)
wenzelm@38417
   266
      val (updatess, new_version') =
wenzelm@38417
   267
        (Graph.keys new_version, new_version)
wenzelm@38412
   268
          |-> fold_map (fn name => Graph.map_node_yield name (update_node name));
wenzelm@38412
   269
wenzelm@38417
   270
      val _ = define_version new_id new_version';
wenzelm@38412
   271
      val _ = updates_status new_id (flat updatess);
wenzelm@38417
   272
      val _ = execute new_version';
wenzelm@38412
   273
    in () end);
wenzelm@38412
   274
wenzelm@38412
   275
end;
wenzelm@38412
   276
wenzelm@38412
   277
wenzelm@38412
   278
wenzelm@38412
   279
(** Isabelle process commands **)
wenzelm@38412
   280
wenzelm@38412
   281
val _ =
wenzelm@38412
   282
  Isabelle_Process.add_command "Isar_Document.define_command"
wenzelm@38412
   283
    (fn [id, text] => define_command (Document.parse_id id) text);
wenzelm@38412
   284
wenzelm@38412
   285
val _ =
wenzelm@38417
   286
  Isabelle_Process.add_command "Isar_Document.edit_version"
wenzelm@38412
   287
    (fn [old_id, new_id, edits] =>
wenzelm@38412
   288
      edit_document (Document.parse_id old_id) (Document.parse_id new_id)
wenzelm@38412
   289
        (XML_Data.dest_list (XML_Data.dest_pair XML_Data.dest_string
wenzelm@38412
   290
            (XML_Data.dest_option (XML_Data.dest_list
wenzelm@38412
   291
                (XML_Data.dest_pair XML_Data.dest_int
wenzelm@38412
   292
                  (XML_Data.dest_option XML_Data.dest_int))))) (YXML.parse_body edits)));
wenzelm@38412
   293
wenzelm@38412
   294
end;
wenzelm@38412
   295