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