src/Pure/PIDE/document.ML
author wenzelm
Thu Nov 11 19:58:07 2010 +0100 (2010-11-11)
changeset 40481 da2c56aaaa68
parent 40449 9c390868d255
child 40520 77a7b0a7d4b1
permissions -rw-r--r--
more precise treatment of deleted nodes;
     1 (*  Title:      Pure/PIDE/document.ML
     2     Author:     Makarius
     3 
     4 Document as collection of named nodes, each consisting of an editable
     5 list of commands, associated with asynchronous execution process.
     6 *)
     7 
     8 signature DOCUMENT =
     9 sig
    10   type id = int
    11   type version_id = id
    12   type command_id = id
    13   type exec_id = id
    14   val no_id: id
    15   val new_id: unit -> id
    16   val parse_id: string -> id
    17   val print_id: id -> string
    18   type edit = string * ((command_id option * command_id option) list) option
    19   type state
    20   val init_state: state
    21   val define_command: command_id -> string -> state -> state
    22   val edit: version_id -> version_id -> edit list -> state -> (command_id * exec_id) list * state
    23   val execute: version_id -> state -> state
    24 end;
    25 
    26 structure Document: DOCUMENT =
    27 struct
    28 
    29 (* unique identifiers *)
    30 
    31 type id = int;
    32 type version_id = id;
    33 type command_id = id;
    34 type exec_id = id;
    35 
    36 val no_id = 0;
    37 val new_id = Synchronized.counter ();
    38 
    39 val parse_id = Markup.parse_int;
    40 val print_id = Markup.print_int;
    41 
    42 fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
    43 fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
    44 
    45 
    46 
    47 (** document structure **)
    48 
    49 structure Entries = Linear_Set(type key = command_id val ord = int_ord);
    50 
    51 abstype node = Node of exec_id option Entries.T  (*command entries with excecutions*)
    52   and version = Version of node Graph.T  (*development graph wrt. static imports*)
    53 with
    54 
    55 val empty_node = Node Entries.empty;
    56 val empty_version = Version Graph.empty;
    57 
    58 fun fold_entries start f (Node entries) = Entries.fold start f entries;
    59 fun first_entry start P (Node entries) = Entries.get_first start P entries;
    60 
    61 
    62 (* node edits and associated executions *)
    63 
    64 type edit =
    65   string *
    66   (*NONE: remove node, SOME: insert/remove commands*)
    67   ((command_id option * command_id option) list) option;
    68 
    69 fun the_entry (Node entries) id =
    70   (case Entries.lookup entries id of
    71     NONE => err_undef "command entry" id
    72   | SOME entry => entry);
    73 
    74 fun update_entry (id, exec_id) (Node entries) =
    75   Node (Entries.update (id, SOME exec_id) entries);
    76 
    77 fun reset_after id entries =
    78   (case Entries.get_after entries id of
    79     NONE => entries
    80   | SOME next => Entries.update (next, NONE) entries);
    81 
    82 fun edit_node (hook, SOME id2) (Node entries) =
    83       Node (Entries.insert_after hook (id2, NONE) entries)
    84   | edit_node (hook, NONE) (Node entries) =
    85       Node (entries |> Entries.delete_after hook |> reset_after hook);
    86 
    87 
    88 (* version operations *)
    89 
    90 fun nodes_of (Version nodes) = nodes;
    91 val node_names_of = Graph.keys o nodes_of;
    92 
    93 fun get_node version name = Graph.get_node (nodes_of version) name
    94   handle Graph.UNDEF _ => empty_node;
    95 
    96 fun edit_nodes (name, SOME edits) (Version nodes) =
    97       Version (nodes
    98         |> Graph.default_node (name, empty_node)
    99         |> Graph.map_node name (fold edit_node edits))
   100   | edit_nodes (name, NONE) (Version nodes) =
   101       Version (perhaps (try (Graph.del_node name)) nodes);
   102 
   103 fun put_node name node (Version nodes) =
   104   Version (nodes
   105     |> Graph.default_node (name, empty_node)
   106     |> Graph.map_node name (K node));
   107 
   108 end;
   109 
   110 
   111 
   112 (** global state -- document structure and execution process **)
   113 
   114 abstype state = State of
   115  {versions: version Inttab.table,  (*version_id -> document content*)
   116   commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
   117   execs: (bool * Toplevel.state) lazy Inttab.table,  (*exec_id -> execution process*)
   118   execution: unit future list}  (*global execution process*)
   119 with
   120 
   121 fun make_state (versions, commands, execs, execution) =
   122   State {versions = versions, commands = commands, execs = execs, execution = execution};
   123 
   124 fun map_state f (State {versions, commands, execs, execution}) =
   125   make_state (f (versions, commands, execs, execution));
   126 
   127 val init_state =
   128   make_state (Inttab.make [(no_id, empty_version)],
   129     Inttab.make [(no_id, Future.value Toplevel.empty)],
   130     Inttab.make [(no_id, Lazy.value (true, Toplevel.toplevel))],
   131     []);
   132 
   133 
   134 (* document versions *)
   135 
   136 fun define_version (id: version_id) version =
   137   map_state (fn (versions, commands, execs, execution) =>
   138     let val versions' = Inttab.update_new (id, version) versions
   139       handle Inttab.DUP dup => err_dup "document version" dup
   140     in (versions', commands, execs, execution) end);
   141 
   142 fun the_version (State {versions, ...}) (id: version_id) =
   143   (case Inttab.lookup versions id of
   144     NONE => err_undef "document version" id
   145   | SOME version => version);
   146 
   147 
   148 (* commands *)
   149 
   150 fun define_command (id: command_id) text =
   151   map_state (fn (versions, commands, execs, execution) =>
   152     let
   153       val id_string = print_id id;
   154       val tr = Future.fork_pri 2 (fn () =>
   155         Position.setmp_thread_data (Position.id_only id_string)
   156           (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
   157       val commands' =
   158         Inttab.update_new (id, tr) commands
   159           handle Inttab.DUP dup => err_dup "command" dup;
   160     in (versions, commands', execs, execution) end);
   161 
   162 fun the_command (State {commands, ...}) (id: command_id) =
   163   (case Inttab.lookup commands id of
   164     NONE => err_undef "command" id
   165   | SOME tr => tr);
   166 
   167 fun join_commands (State {commands, ...}) =
   168   Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
   169 
   170 
   171 (* command executions *)
   172 
   173 fun define_exec (id: exec_id) exec =
   174   map_state (fn (versions, commands, execs, execution) =>
   175     let val execs' = Inttab.update_new (id, exec) execs
   176       handle Inttab.DUP dup => err_dup "command execution" dup
   177     in (versions, commands, execs', execution) end);
   178 
   179 fun the_exec (State {execs, ...}) (id: exec_id) =
   180   (case Inttab.lookup execs id of
   181     NONE => err_undef "command execution" id
   182   | SOME exec => exec);
   183 
   184 end;
   185 
   186 
   187 
   188 (* toplevel transactions *)
   189 
   190 local
   191 
   192 fun timing tr t = Toplevel.status tr (Markup.timing t);
   193 
   194 fun proof_status tr st =
   195   (case try Toplevel.proof_of st of
   196     SOME prf => Toplevel.status tr (Proof.status_markup prf)
   197   | NONE => ());
   198 
   199 fun async_state tr st =
   200   if Toplevel.print_of tr then
   201     ignore
   202       (Future.fork_group (Task_Queue.new_group NONE)
   203         (fn () =>
   204           Toplevel.setmp_thread_position tr
   205             (fn () => Toplevel.print_state false st) ()))
   206   else ();
   207 
   208 fun run int tr st =
   209   (case Toplevel.transition int tr st of
   210     SOME (st', NONE) => ([], SOME st')
   211   | SOME (_, SOME exn_info) =>
   212       (case ML_Compiler.exn_messages (Runtime.EXCURSION_FAIL exn_info) of
   213         [] => Exn.interrupt ()
   214       | errs => (errs, NONE))
   215   | NONE => ([ML_Compiler.exn_message Runtime.TERMINATE], NONE));
   216 
   217 in
   218 
   219 fun run_command thy_name tr st =
   220   (case
   221       (case Toplevel.init_of tr of
   222         SOME name => Exn.capture (fn () => Thy_Header.consistent_name thy_name name) ()
   223       | NONE => Exn.Result ()) of
   224     Exn.Result () =>
   225       let
   226         val int = is_some (Toplevel.init_of tr);
   227         val start = start_timing ();
   228         val (errs, result) = run int tr st;
   229         val _ = timing tr (end_timing start);
   230         val _ = List.app (Toplevel.error_msg tr) errs;
   231         val res =
   232           (case result of
   233             NONE => (Toplevel.status tr Markup.failed; (false, st))
   234           | SOME st' =>
   235              (Toplevel.status tr Markup.finished;
   236               proof_status tr st';
   237               if int then () else async_state tr st';
   238               (true, st')));
   239       in res end
   240   | Exn.Exn exn =>
   241       if Exn.is_interrupt exn then reraise exn
   242       else
   243        (Toplevel.error_msg tr (ML_Compiler.exn_message exn);
   244         Toplevel.status tr Markup.failed;
   245         (false, Toplevel.toplevel)));
   246 
   247 end;
   248 
   249 
   250 
   251 
   252 (** editing **)
   253 
   254 (* edit *)
   255 
   256 local
   257 
   258 fun is_changed node' ((_, id), exec) =
   259   (case try (the_entry node') id of
   260     NONE => true
   261   | SOME exec' => exec' <> exec);
   262 
   263 fun new_exec name (id: command_id) (exec_id, updates, state) =
   264   let
   265     val exec = the_exec state exec_id;
   266     val exec_id' = new_id ();
   267     val future_tr = the_command state id;
   268     val exec' =
   269       Lazy.lazy (fn () =>
   270         let
   271           val st = #2 (Lazy.force exec);
   272           val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join future_tr);
   273         in run_command name exec_tr st end);
   274     val state' = define_exec exec_id' exec' state;
   275   in (exec_id', (id, exec_id') :: updates, state') end;
   276 
   277 in
   278 
   279 fun edit (old_id: version_id) (new_id: version_id) edits state =
   280   let
   281     val old_version = the_version state old_id;
   282     val new_version = fold edit_nodes edits old_version;
   283 
   284     fun update_node name (rev_updates, version, st) =
   285       let val node = get_node version name in
   286         (case first_entry NONE (is_changed (get_node old_version name)) node of
   287           NONE => (rev_updates, version, st)
   288         | SOME ((prev, id), _) =>
   289             let
   290               val prev_exec =
   291                 (case prev of
   292                   NONE => no_id
   293                 | SOME prev_id => the_default no_id (the_entry node prev_id));
   294               val (_, rev_upds, st') =
   295                 fold_entries (SOME id) (new_exec name o #2 o #1) node (prev_exec, [], st);
   296               val node' = fold update_entry rev_upds node;
   297             in (rev_upds @ rev_updates, put_node name node' version, st') end)
   298       end;
   299 
   300     (* FIXME proper node deps *)
   301     val (rev_updates, new_version', state') =
   302       fold update_node (node_names_of new_version) ([], new_version, state);
   303     val state'' = define_version new_id new_version' state';
   304 
   305     val _ = join_commands state'';  (* FIXME async!? *)
   306   in (rev rev_updates, state'') end;
   307 
   308 end;
   309 
   310 
   311 (* execute *)
   312 
   313 fun execute version_id state =
   314   state |> map_state (fn (versions, commands, execs, execution) =>
   315     let
   316       val version = the_version state version_id;
   317 
   318       fun force_exec NONE = ()
   319         | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
   320 
   321       val _ = List.app Future.cancel execution;
   322       fun await_cancellation () =
   323         uninterruptible (fn _ => fn () => Future.join_results execution) ();
   324 
   325       val execution' = (* FIXME proper node deps *)
   326         node_names_of version |> map (fn name =>
   327           Future.fork_pri 1 (fn () =>
   328             (await_cancellation ();
   329               fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
   330                 (get_node version name) ())));
   331     in (versions, commands, execs, execution') end);
   332 
   333 end;
   334