src/Pure/PIDE/document.ML
author wenzelm
Tue Jan 25 21:26:25 2011 +0100 (2011-01-25)
changeset 41630 a7a93df23664
parent 41629 5490dc4d999d
child 41634 28d94383249c
permissions -rw-r--r--
singleton (sequential) execution, to avoid race conditions in theory loader state (e.g. when multiple independent theories import the same theory);
     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   ignore
   201     (Future.fork_group (Task_Queue.new_group NONE)
   202       (fn () =>
   203         Toplevel.setmp_thread_position tr
   204           (fn () => Toplevel.print_state false st) ()));
   205 
   206 fun run int tr st =
   207   (case Toplevel.transition int tr st of
   208     SOME (st', NONE) => ([], SOME st')
   209   | SOME (_, SOME exn_info) =>
   210       (case ML_Compiler.exn_messages (Runtime.EXCURSION_FAIL exn_info) of
   211         [] => Exn.interrupt ()
   212       | errs => (errs, NONE))
   213   | NONE => ([ML_Compiler.exn_message Runtime.TERMINATE], NONE));
   214 
   215 in
   216 
   217 fun run_command thy_name raw_tr st =
   218   (case
   219       (case Toplevel.init_of raw_tr of
   220         SOME name => Exn.capture (fn () =>
   221           let
   222             val path = Path.explode thy_name;
   223             val _ = Thy_Header.consistent_name (Path.implode (Path.base path)) name;
   224           in Toplevel.modify_master (SOME (Path.dir path)) raw_tr end) ()
   225       | NONE => Exn.Result raw_tr) of
   226     Exn.Result tr =>
   227       let
   228         val is_init = is_some (Toplevel.init_of tr);
   229         val is_proof = Keyword.is_proof (Toplevel.name_of tr);
   230         val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
   231 
   232         val start = start_timing ();
   233         val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
   234         val _ = timing tr (end_timing start);
   235         val _ = List.app (Toplevel.error_msg tr) errs;
   236         val res =
   237           (case result of
   238             NONE => (Toplevel.status tr Markup.failed; (false, st))
   239           | SOME st' =>
   240              (Toplevel.status tr Markup.finished;
   241               proof_status tr st';
   242               if do_print then async_state tr st' else ();
   243               (true, st')));
   244       in res end
   245   | Exn.Exn exn =>
   246       if Exn.is_interrupt exn then reraise exn
   247       else
   248        (Toplevel.error_msg raw_tr (ML_Compiler.exn_message exn);
   249         Toplevel.status raw_tr Markup.failed;
   250         (false, Toplevel.toplevel)));
   251 
   252 end;
   253 
   254 
   255 
   256 
   257 (** editing **)
   258 
   259 (* edit *)
   260 
   261 local
   262 
   263 fun is_changed node' ((_, id), exec) =
   264   (case try (the_entry node') id of
   265     NONE => true
   266   | SOME exec' => exec' <> exec);
   267 
   268 fun new_exec name (id: command_id) (exec_id, updates, state) =
   269   let
   270     val exec = the_exec state exec_id;
   271     val exec_id' = new_id ();
   272     val future_tr = the_command state id;
   273     val exec' =
   274       Lazy.lazy (fn () =>
   275         let
   276           val st = #2 (Lazy.force exec);
   277           val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join future_tr);
   278         in run_command name exec_tr st end);
   279     val state' = define_exec exec_id' exec' state;
   280   in (exec_id', (id, exec_id') :: updates, state') end;
   281 
   282 in
   283 
   284 fun edit (old_id: version_id) (new_id: version_id) edits state =
   285   let
   286     val old_version = the_version state old_id;
   287     val _ = Time.now ();  (* FIXME odd workaround *)
   288     val new_version = fold edit_nodes edits old_version;
   289 
   290     fun update_node name (rev_updates, version, st) =
   291       let val node = get_node version name in
   292         (case first_entry NONE (is_changed (get_node old_version name)) node of
   293           NONE => (rev_updates, version, st)
   294         | SOME ((prev, id), _) =>
   295             let
   296               val prev_exec =
   297                 (case prev of
   298                   NONE => no_id
   299                 | SOME prev_id => the_default no_id (the_entry node prev_id));
   300               val (_, rev_upds, st') =
   301                 fold_entries (SOME id) (new_exec name o #2 o #1) node (prev_exec, [], st);
   302               val node' = fold update_entry rev_upds node;
   303             in (rev_upds @ rev_updates, put_node name node' version, st') end)
   304       end;
   305 
   306     (* FIXME proper node deps *)
   307     val (rev_updates, new_version', state') =
   308       fold update_node (node_names_of new_version) ([], new_version, state);
   309     val state'' = define_version new_id new_version' state';
   310 
   311     val _ = join_commands state'';  (* FIXME async!? *)
   312   in (rev rev_updates, state'') end;
   313 
   314 end;
   315 
   316 
   317 (* execute *)
   318 
   319 fun execute version_id state =
   320   state |> map_state (fn (versions, commands, execs, execution) =>
   321     let
   322       val version = the_version state version_id;
   323 
   324       fun force_exec NONE = ()
   325         | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
   326 
   327       val _ = List.app Future.cancel execution;
   328       fun await_cancellation () = Future.join_results execution;
   329 
   330       val execution' = (* FIXME proper node deps *)
   331         [Future.fork_pri 1 (fn () =>
   332           let
   333             val _ = await_cancellation ();
   334             val _ =
   335               node_names_of version |> List.app (fn name =>
   336                 fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
   337                     (get_node version name) ());
   338           in () end)];
   339 
   340       val _ = await_cancellation ();
   341 
   342     in (versions, commands, execs, execution') end);
   343 
   344 end;
   345