src/Pure/PIDE/document.ML
changeset 47389 e8552cba702d
parent 47388 fe4b245af74c
child 47395 e6261a493f04
equal deleted inserted replaced
47388:fe4b245af74c 47389:e8552cba702d
   233 
   233 
   234 end;
   234 end;
   235 
   235 
   236 
   236 
   237 
   237 
   238 (** document state -- content structure and execution process **)
   238 (** main state -- document structure and execution process **)
   239 
   239 
   240 abstype state = State of
   240 abstype state = State of
   241  {versions: version Inttab.table,  (*version_id -> document content*)
   241  {versions: version Inttab.table,  (*version_id -> document content*)
   242   commands: (string * Token.T list lazy) Inttab.table,  (*command_id -> named span*)
   242   commands: (string * Token.T list lazy) Inttab.table,  (*command_id -> named span*)
   243   execution: version_id * Future.group * bool Unsynchronized.ref}  (*current execution process*)
   243   execution: version_id * Future.group * bool Unsynchronized.ref}  (*current execution process*)
   296 
   296 
   297 end;
   297 end;
   298 
   298 
   299 
   299 
   300 
   300 
   301 (** execute **)
   301 (** edit operations **)
       
   302 
       
   303 (* execute *)
       
   304 
       
   305 local
   302 
   306 
   303 fun finished_theory node =
   307 fun finished_theory node =
   304   (case Exn.capture Command.memo_result (get_result node) of
   308   (case Exn.capture Command.memo_result (get_result node) of
   305     Exn.Res (st, _) => can (Toplevel.end_theory Position.none) st
   309     Exn.Res (st, _) => can (Toplevel.end_theory Position.none) st
   306   | _ => false);
   310   | _ => false);
       
   311 
       
   312 in
   307 
   313 
   308 fun execute version_id state =
   314 fun execute version_id state =
   309   state |> map_state (fn (versions, commands, _) =>
   315   state |> map_state (fn (versions, commands, _) =>
   310     let
   316     let
   311       val version = the_version state version_id;
   317       val version = the_version state version_id;
   337                       SOME (_, exec) => if ! running then SOME (run node id exec) else NONE
   343                       SOME (_, exec) => if ! running then SOME (run node id exec) else NONE
   338                     | NONE => NONE)) node ()));
   344                     | NONE => NONE)) node ()));
   339 
   345 
   340     in (versions, commands, (version_id, group, running)) end);
   346     in (versions, commands, (version_id, group, running)) end);
   341 
   347 
   342 
   348 end;
   343 
   349 
   344 
       
   345 (** edits **)
       
   346 
   350 
   347 (* update *)
   351 (* update *)
   348 
   352 
   349 local
   353 local
       
   354 
       
   355 fun stable_finished_theory node =
       
   356   is_some (Exn.get_res (Exn.capture (fn () =>
       
   357     fst (Command.memo_result (get_result node))
       
   358     |> Toplevel.end_theory Position.none
       
   359     |> Global_Theory.join_proofs) ()));
       
   360 
       
   361 fun stable_command exec =
       
   362   (case Exn.capture Command.memo_result exec of
       
   363     Exn.Exn exn => not (Exn.is_interrupt exn)
       
   364   | Exn.Res (st, _) =>
       
   365       (case try Toplevel.theory_of st of
       
   366         NONE => true
       
   367       | SOME thy => is_some (Exn.get_res (Exn.capture Global_Theory.join_recent_proofs thy))));
   350 
   368 
   351 fun make_required nodes =
   369 fun make_required nodes =
   352   let
   370   let
   353     val all_visible =
   371     val all_visible =
   354       Graph.fold (fn (a, (node, _)) => visible_node node ? cons a) nodes []
   372       Graph.fold (fn (a, (node, _)) => visible_node node ? cons a) nodes []
   398       else
   416       else
   399         let val found' =
   417         let val found' =
   400           (case opt_exec of
   418           (case opt_exec of
   401             NONE => true
   419             NONE => true
   402           | SOME (exec_id, exec) =>
   420           | SOME (exec_id, exec) =>
   403               not (Command.memo_stable exec) orelse
   421               not (stable_command exec) orelse
   404               (case lookup_entry node0 id of
   422               (case lookup_entry node0 id of
   405                 NONE => true
   423                 NONE => true
   406               | SOME (exec_id0, _) => exec_id <> exec_id0));
   424               | SOME (exec_id0, _) => exec_id <> exec_id0));
   407         in SOME (found', (prev, update_flags prev flags)) end;
   425         in SOME (found', (prev, update_flags prev flags)) end;
   408     val (found, (common, flags)) =
   426     val (found, (common, flags)) =
   451     val is_required = make_required nodes;
   469     val is_required = make_required nodes;
   452 
   470 
   453     val updated =
   471     val updated =
   454       nodes |> Graph.schedule
   472       nodes |> Graph.schedule
   455         (fn deps => fn (name, node) =>
   473         (fn deps => fn (name, node) =>
   456           if is_touched node orelse is_required name andalso not (finished_theory node) then
   474           if is_touched node orelse is_required name andalso not (stable_finished_theory node) then
   457             let
   475             let
   458               val node0 = node_of old_version name;
   476               val node0 = node_of old_version name;
   459               fun init () = init_theory deps node;
   477               fun init () = init_theory deps node;
   460               val bad_init =
   478               val bad_init =
   461                 not (check_theory nodes name andalso forall (check_theory nodes o #1) deps);
   479                 not (check_theory nodes name andalso forall (check_theory nodes o #1) deps);