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