src/Pure/PIDE/headless.scala
author wenzelm
Wed, 04 Sep 2019 22:00:38 +0200
changeset 70653 f7c5b30fc432
parent 70649 9a40720750dc
child 70657 2bf1d0e57695
permissions -rw-r--r--
load theories in stages, to reduce ML heap requirements;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
     1
/*  Title:      Pure/PIDE/headless.scala
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     3
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
     4
Headless PIDE session and resources from file-system.
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     5
*/
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     6
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     7
package isabelle
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     8
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     9
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    10
import java.io.{File => JFile}
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    11
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
    12
import scala.annotation.tailrec
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
    13
import scala.collection.mutable
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
    14
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    15
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
    16
object Headless
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
    17
{
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
    18
  /** session **/
67061
2efa25302f34 synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents: 67059
diff changeset
    19
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    20
  private def stable_snapshot(
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    21
    state: Document.State, version: Document.Version, name: Document.Node.Name): Document.Snapshot =
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    22
  {
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    23
    val snapshot = state.snapshot(name)
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    24
    assert(version.id == snapshot.version.id)
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    25
    snapshot
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    26
  }
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    27
69013
bb4e4c253ebe tuned signature;
wenzelm
parents: 69012
diff changeset
    28
  class Use_Theories_Result private[Headless](
67883
171e7735ce25 support for "use_theories";
wenzelm
parents: 67881
diff changeset
    29
    val state: Document.State,
67889
wenzelm
parents: 67888
diff changeset
    30
    val version: Document.Version,
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    31
    val nodes: List[(Document.Node.Name, Document_Status.Node_Status)],
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    32
    val nodes_committed: List[(Document.Node.Name, Document_Status.Node_Status)])
67879
e4903b803b8b clarified signature;
wenzelm
parents: 67877
diff changeset
    33
  {
69032
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    34
    def nodes_pending: List[(Document.Node.Name, Document_Status.Node_Status)] =
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    35
    {
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    36
      val committed = nodes_committed.iterator.map(_._1).toSet
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    37
      nodes.filter(p => !committed(p._1))
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    38
    }
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    39
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    40
    def snapshot(name: Document.Node.Name): Document.Snapshot =
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    41
      stable_snapshot(state, version, name)
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    42
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    43
    def ok: Boolean =
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    44
      (nodes.iterator ++ nodes_committed.iterator).forall({ case (_, st) => st.ok })
67879
e4903b803b8b clarified signature;
wenzelm
parents: 67877
diff changeset
    45
  }
e4903b803b8b clarified signature;
wenzelm
parents: 67877
diff changeset
    46
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    47
  private object Checkpoints_State
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    48
  {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    49
    object Status extends Enumeration
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    50
    {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    51
      val INIT, LOADED, LOADED_DESCENDANTS = Value
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    52
    }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    53
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    54
    def init(nodes: List[Document.Node.Name]): Checkpoints_State =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    55
      Checkpoints_State(Status.INIT, nodes)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    56
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    57
    val last: Checkpoints_State =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    58
      Checkpoints_State(Status.LOADED_DESCENDANTS, Nil)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    59
  }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    60
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    61
  private sealed case class Checkpoints_State(
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    62
    status: Checkpoints_State.Status.Value,
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    63
    nodes: List[Document.Node.Name])
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    64
  {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    65
    def next(
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    66
      dep_graph: Document.Theory_Graph[Unit],
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    67
      finished: Document.Node.Name => Boolean): (List[Document.Node.Name], Checkpoints_State) =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    68
    {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    69
      import Checkpoints_State.Status
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    70
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    71
      def descendants: List[Document.Node.Name] =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    72
        nodes match {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    73
          case Nil => dep_graph.topological_order
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    74
          case current :: rest =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    75
            val dep_graph1 =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    76
              if (rest.isEmpty) dep_graph
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    77
              else {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    78
                val exclude = dep_graph.all_succs(rest).toSet
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    79
                dep_graph.restrict(name => !exclude(name))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    80
              }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    81
            dep_graph1.all_succs(List(current))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    82
        }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    83
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    84
      def requirements: List[Document.Node.Name] =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    85
        dep_graph.all_preds(nodes.headOption.toList).reverse
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    86
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    87
      val (load_theories, st1) =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    88
        (status, nodes) match {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    89
          case (Status.INIT, Nil) =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    90
            (descendants, Checkpoints_State.last)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    91
          case (Status.INIT, current :: _) =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    92
            (requirements, copy(status = Status.LOADED))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    93
          case (Status.LOADED, current :: rest) if finished(current) =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    94
            (descendants, copy(status = Status.LOADED_DESCENDANTS))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    95
          case (Status.LOADED_DESCENDANTS, _ :: rest) if descendants.forall(finished) =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    96
            Checkpoints_State.init(rest).next(dep_graph, finished)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    97
          case _ => (Nil, this)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    98
        }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
    99
      (load_theories.filterNot(finished), st1)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   100
    }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   101
  }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   102
69520
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   103
  class Session private[Headless](
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   104
    session_name: String,
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   105
    _session_options: => Options,
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   106
    override val resources: Resources) extends isabelle.Session(_session_options, resources)
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   107
  {
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   108
    session =>
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   109
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   110
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   111
    private def loaded_theory(name: Document.Node.Name): Boolean =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   112
      resources.session_base.loaded_theory(name.theory)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   113
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   114
69520
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   115
    /* options */
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   116
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   117
    def default_check_delay: Time = session_options.seconds("headless_check_delay")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   118
    def default_check_limit: Int = session_options.int("headless_check_limit")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   119
    def default_nodes_status_delay: Time = session_options.seconds("headless_nodes_status_delay")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   120
    def default_watchdog_timeout: Time = session_options.seconds("headless_watchdog_timeout")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   121
    def default_commit_cleanup_delay: Time = session_options.seconds("headless_commit_cleanup_delay")
67063
10d608cc7470 clarified modules;
wenzelm
parents: 67062
diff changeset
   122
68922
wenzelm
parents: 68916
diff changeset
   123
wenzelm
parents: 68916
diff changeset
   124
    /* temporary directory */
wenzelm
parents: 68916
diff changeset
   125
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   126
    val tmp_dir: JFile = Isabelle_System.tmp_dir("server_session")
67946
e1e57c288e45 session tmp_dir is default master_dir;
wenzelm
parents: 67945
diff changeset
   127
    val tmp_dir_name: String = File.path(tmp_dir).implode
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   128
68923
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   129
    def master_directory(master_dir: String): String =
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   130
      proper_string(master_dir) getOrElse tmp_dir_name
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   131
67945
984c3dc46cc0 tuned output;
wenzelm
parents: 67943
diff changeset
   132
    override def toString: String = session_name
984c3dc46cc0 tuned output;
wenzelm
parents: 67943
diff changeset
   133
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   134
    override def stop(): Process_Result =
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   135
    {
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   136
      try { super.stop() }
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   137
      finally { Isabelle_System.rm_tree(tmp_dir) }
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   138
    }
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
   139
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   140
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   141
    /* theories */
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   142
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   143
    private sealed case class Use_Theories_State(
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   144
      dependencies: resources.Dependencies[Unit],
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   145
      checkpoints_state: Checkpoints_State,
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   146
      watchdog_timeout: Time,
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   147
      commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit],
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   148
      last_update: Time = Time.now(),
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   149
      nodes_status: Document_Status.Nodes_Status = Document_Status.Nodes_Status.empty,
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   150
      already_committed: Map[Document.Node.Name, Document_Status.Node_Status] = Map.empty,
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   151
      result: Option[Exn.Result[Use_Theories_Result]] = None)
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   152
    {
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   153
      def dep_graph: Document.Theory_Graph[Unit] = dependencies.theory_graph
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   154
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   155
      def update(new_nodes_status: Document_Status.Nodes_Status): Use_Theories_State =
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   156
        copy(last_update = Time.now(), nodes_status = new_nodes_status)
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   157
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   158
      def watchdog: Boolean =
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   159
        watchdog_timeout > Time.zero && Time.now() - last_update > watchdog_timeout
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   160
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   161
      def finished_result: Boolean = result.isDefined
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   162
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   163
      def join_result: Option[(Exn.Result[Use_Theories_Result], Use_Theories_State)] =
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   164
        if (finished_result) Some((result.get, this)) else None
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   165
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   166
      def cancel_result: Use_Theories_State =
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   167
        if (finished_result) this else copy(result = Some(Exn.Exn(Exn.Interrupt())))
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   168
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   169
      def clean: Set[Document.Node.Name] =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   170
        already_committed.keySet -- checkpoints_state.nodes
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   171
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   172
      def check(state: Document.State, version: Document.Version, beyond_limit: Boolean)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   173
        : (List[Document.Node.Name], Use_Theories_State) =
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   174
      {
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   175
        val already_committed1 =
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   176
          commit match {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   177
            case None => already_committed
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   178
            case Some(commit_fn) =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   179
              (already_committed /: dep_graph.topological_order)(
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   180
                { case (committed, name) =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   181
                    def parents_committed: Boolean =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   182
                      version.nodes(name).header.imports.forall(parent =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   183
                        loaded_theory(parent) || committed.isDefinedAt(parent))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   184
                    if (!committed.isDefinedAt(name) && parents_committed &&
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   185
                        state.node_consolidated(version, name))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   186
                    {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   187
                      val snapshot = stable_snapshot(state, version, name)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   188
                      val status = Document_Status.Node_Status.make(state, version, name)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   189
                      commit_fn(snapshot, status)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   190
                      committed + (name -> status)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   191
                    }
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   192
                    else committed
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   193
                })
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   194
          }
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   195
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   196
        val result1 =
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   197
          if (!finished_result &&
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   198
            (beyond_limit || watchdog ||
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   199
              dep_graph.keys_iterator.forall(name =>
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   200
                already_committed1.isDefinedAt(name) ||
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   201
                state.node_consolidated(version, name) ||
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   202
                nodes_status.quasi_consolidated(name))))
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   203
          {
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   204
            val nodes =
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   205
              (for (name <- dep_graph.keys_iterator)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   206
                yield { (name -> Document_Status.Node_Status.make(state, version, name)) }).toList
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   207
            val nodes_committed =
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   208
              (for {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   209
                name <- dep_graph.keys_iterator
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   210
                status <- already_committed1.get(name)
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   211
              } yield (name -> status)).toList
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   212
            Some(Exn.Res(new Use_Theories_Result(state, version, nodes, nodes_committed)))
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   213
          }
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   214
          else result
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   215
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   216
        val (load_theories, checkpoints_state1) =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   217
          checkpoints_state.next(dep_graph,
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   218
            name =>
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   219
              loaded_theory(name) ||
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   220
              already_committed1.isDefinedAt(name) ||
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   221
              nodes_status.consolidated(name))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   222
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   223
        (load_theories,
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   224
          copy(already_committed = already_committed1, result = result1,
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   225
            checkpoints_state = checkpoints_state1))
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   226
      }
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   227
    }
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   228
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   229
    def use_theories(
67940
b4e80f062fbf clarified signature -- eliminated somewhat pointless positions;
wenzelm
parents: 67939
diff changeset
   230
      theories: List[String],
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   231
      qualifier: String = Sessions.DRAFT,
67881
812ed06dadec interruptible exploration of dependencies;
wenzelm
parents: 67879
diff changeset
   232
      master_dir: String = "",
69920
79c8ff387ed1 support unicode_symbols in input source;
wenzelm
parents: 69857
diff changeset
   233
      unicode_symbols: Boolean = false,
68943
e564605d4cac tuned signature;
wenzelm
parents: 68936
diff changeset
   234
      check_delay: Time = default_check_delay,
69520
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   235
      check_limit: Int = default_check_limit,
68947
ea804c814693 clarified defaults;
wenzelm
parents: 68943
diff changeset
   236
      watchdog_timeout: Time = default_watchdog_timeout,
68943
e564605d4cac tuned signature;
wenzelm
parents: 68936
diff changeset
   237
      nodes_status_delay: Time = default_nodes_status_delay,
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   238
      id: UUID.T = UUID.random(),
70625
1ae987cc052f support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents: 69920
diff changeset
   239
      share_common_data: Boolean = false,
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   240
      checkpoints: Set[Document.Node.Name] = Set.empty,
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   241
      // commit: must not block, must not fail
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   242
      commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit] = None,
68981
30daac7848b9 tuned signature;
wenzelm
parents: 68962
diff changeset
   243
      commit_cleanup_delay: Time = default_commit_cleanup_delay,
69013
bb4e4c253ebe tuned signature;
wenzelm
parents: 69012
diff changeset
   244
      progress: Progress = No_Progress): Use_Theories_Result =
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   245
    {
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   246
      val dependencies =
68894
1dbdad1b57a5 more robust: load_theories after consumer is installed;
wenzelm
parents: 68888
diff changeset
   247
      {
1dbdad1b57a5 more robust: load_theories after consumer is installed;
wenzelm
parents: 68888
diff changeset
   248
        val import_names =
68923
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   249
          theories.map(thy =>
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   250
            resources.import_name(qualifier, master_directory(master_dir), thy) -> Position.none)
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   251
        resources.dependencies(import_names, progress = progress).check_errors
68894
1dbdad1b57a5 more robust: load_theories after consumer is installed;
wenzelm
parents: 68888
diff changeset
   252
      }
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   253
      val dep_theories = dependencies.theories
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   254
      val dep_theories_set = dep_theories.toSet
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   255
      val dep_files =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   256
        dependencies.loaded_files(false).flatMap(_._2).
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   257
          map(path => Document.Node.Name(resources.append("", path)))
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   258
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   259
      val use_theories_state =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   260
      {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   261
        val checkpoints_state =
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   262
          Checkpoints_State.init(
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   263
            if (checkpoints.isEmpty) Nil
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   264
            else dependencies.theory_graph.topological_order.filter(checkpoints(_)))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   265
        Synchronized(Use_Theories_State(dependencies, checkpoints_state, watchdog_timeout, commit))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   266
      }
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   267
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   268
      def check_state(beyond_limit: Boolean = false)
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   269
      {
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   270
        val state = session.current_state()
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   271
        for (version <- state.stable_tip_version) {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   272
          val load_theories = use_theories_state.change_result(_.check(state, version, beyond_limit))
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   273
          if (load_theories.nonEmpty) {
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   274
            resources.load_theories(
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   275
              session, id, load_theories, dep_files, unicode_symbols, share_common_data, progress)
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   276
          }
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   277
        }
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   278
      }
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   279
67894
fee080c4045f more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents: 67893
diff changeset
   280
      val check_progress =
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   281
      {
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   282
        var check_count = 0
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   283
        Event_Timer.request(Time.now(), repeat = Some(check_delay))
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   284
          {
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   285
            if (progress.stopped) use_theories_state.change(_.cancel_result)
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   286
            else {
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   287
              check_count += 1
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   288
              check_state(check_limit > 0 && check_count > check_limit)
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   289
            }
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   290
          }
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   291
      }
67894
fee080c4045f more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents: 67893
diff changeset
   292
68906
wenzelm
parents: 68905
diff changeset
   293
      val consumer =
wenzelm
parents: 68905
diff changeset
   294
      {
wenzelm
parents: 68905
diff changeset
   295
        val delay_nodes_status =
wenzelm
parents: 68905
diff changeset
   296
          Standard_Thread.delay_first(nodes_status_delay max Time.zero) {
69818
60d0ee8f2ddb more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents: 69817
diff changeset
   297
            progress.nodes_status(use_theories_state.value.nodes_status)
68906
wenzelm
parents: 68905
diff changeset
   298
          }
68770
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   299
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   300
        val delay_commit_clean =
68981
30daac7848b9 tuned signature;
wenzelm
parents: 68962
diff changeset
   301
          Standard_Thread.delay_first(commit_cleanup_delay max Time.zero) {
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   302
            resources.clean_theories(session, id, use_theories_state.value.clean)
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   303
          }
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   304
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   305
        Session.Consumer[Session.Commands_Changed](getClass.getName) {
68330
d7920eb7de54 report theory progress via PIDE node status;
wenzelm
parents: 68321
diff changeset
   306
          case changed =>
68770
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   307
            if (changed.nodes.exists(dep_theories_set)) {
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   308
              val snapshot = session.snapshot()
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   309
              val state = snapshot.state
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   310
              val version = snapshot.version
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   311
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   312
              val theory_progress =
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   313
                use_theories_state.change_result(st =>
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   314
                  {
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   315
                    val domain =
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   316
                      if (st.nodes_status.is_empty) dep_theories_set
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   317
                      else changed.nodes.iterator.filter(dep_theories_set).toSet
68899
b15b03c13dbb more detailed progress;
wenzelm
parents: 68894
diff changeset
   318
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   319
                    val (nodes_status_changed, nodes_status1) =
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69035
diff changeset
   320
                      st.nodes_status.update(resources, state, version,
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   321
                        domain = Some(domain), trim = changed.assignment)
68899
b15b03c13dbb more detailed progress;
wenzelm
parents: 68894
diff changeset
   322
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   323
                    if (nodes_status_delay >= Time.zero && nodes_status_changed) {
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   324
                      delay_nodes_status.invoke
68899
b15b03c13dbb more detailed progress;
wenzelm
parents: 68894
diff changeset
   325
                    }
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   326
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   327
                    val theory_progress =
68905
90a6b714aca3 tuned -- prefer immutable data;
wenzelm
parents: 68904
diff changeset
   328
                      (for {
69818
60d0ee8f2ddb more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents: 69817
diff changeset
   329
                        (name, node_status) <- nodes_status1.present.iterator
68959
d4223afddd47 clarified message;
wenzelm
parents: 68958
diff changeset
   330
                        if changed.nodes.contains(name) && !st.already_committed.isDefinedAt(name)
68962
50676b0ab970 tuned message (again);
wenzelm
parents: 68959
diff changeset
   331
                        p1 = node_status.percentage
50676b0ab970 tuned message (again);
wenzelm
parents: 68959
diff changeset
   332
                        if p1 > 0 && Some(p1) != st.nodes_status.get(name).map(_.percentage)
50676b0ab970 tuned message (again);
wenzelm
parents: 68959
diff changeset
   333
                      } yield Progress.Theory(name.theory, percentage = Some(p1))).toList
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   334
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   335
                    (theory_progress, st.update(nodes_status1))
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   336
                  })
68330
d7920eb7de54 report theory progress via PIDE node status;
wenzelm
parents: 68321
diff changeset
   337
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   338
              theory_progress.foreach(progress.theory(_))
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   339
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   340
              check_state()
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   341
68981
30daac7848b9 tuned signature;
wenzelm
parents: 68962
diff changeset
   342
              if (commit.isDefined && commit_cleanup_delay > Time.zero) {
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   343
                if (use_theories_state.value.finished_result)
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   344
                  delay_commit_clean.revoke
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   345
                else delay_commit_clean.invoke
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   346
              }
68330
d7920eb7de54 report theory progress via PIDE node status;
wenzelm
parents: 68321
diff changeset
   347
            }
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   348
        }
68906
wenzelm
parents: 68905
diff changeset
   349
      }
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   350
67892
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   351
      try {
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   352
        session.commands_changed += consumer
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   353
        check_state()
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   354
        use_theories_state.guarded_access(_.join_result)
67894
fee080c4045f more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents: 67893
diff changeset
   355
        check_progress.cancel
67892
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   356
      }
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   357
      finally {
68907
wenzelm
parents: 68906
diff changeset
   358
        session.commands_changed -= consumer
67892
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   359
        resources.unload_theories(session, id, dep_theories)
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   360
      }
67884
43af581d7d8e unload_theories after consolidation -- reset node_required;
wenzelm
parents: 67883
diff changeset
   361
70644
b23a6dfcfd57 clarified state variable: avoid extra mutability via Promise;
wenzelm
parents: 70640
diff changeset
   362
      Exn.release(use_theories_state.guarded_access(_.join_result))
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   363
    }
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   364
67939
544a7a21298e clarified signature -- more like use_theories;
wenzelm
parents: 67936
diff changeset
   365
    def purge_theories(
68915
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   366
      theories: List[String],
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   367
      qualifier: String = Sessions.DRAFT,
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   368
      master_dir: String = "",
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   369
      all: Boolean = false): (List[Document.Node.Name], List[Document.Node.Name]) =
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   370
    {
68923
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   371
      val nodes =
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   372
        if (all) None
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   373
        else Some(theories.map(resources.import_name(qualifier, master_directory(master_dir), _)))
68915
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   374
      resources.purge_theories(session, nodes)
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   375
    }
67063
10d608cc7470 clarified modules;
wenzelm
parents: 67062
diff changeset
   376
  }
10d608cc7470 clarified modules;
wenzelm
parents: 67062
diff changeset
   377
67061
2efa25302f34 synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents: 67059
diff changeset
   378
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
   379
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   380
  /** resources **/
68922
wenzelm
parents: 68916
diff changeset
   381
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   382
  object Resources
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   383
  {
69536
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   384
    def apply(base_info: Sessions.Base_Info, log: Logger = No_Logger): Resources =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   385
      new Resources(base_info, log = log)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   386
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   387
    def make(
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   388
      options: Options,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   389
      session_name: String,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   390
      session_dirs: List[Path] = Nil,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   391
      include_sessions: List[String] = Nil,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   392
      progress: Progress = No_Progress,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   393
      log: Logger = No_Logger): Resources =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   394
    {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   395
      val base_info =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   396
        Sessions.base_info(options, session_name, dirs = session_dirs,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   397
          include_sessions = include_sessions, progress = progress)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   398
      apply(base_info, log = log)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   399
    }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   400
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   401
    final class Theory private[Headless](
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   402
      val node_name: Document.Node.Name,
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   403
      val node_header: Document.Node.Header,
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   404
      val text: String,
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   405
      val node_required: Boolean)
68922
wenzelm
parents: 68916
diff changeset
   406
    {
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   407
      override def toString: String = node_name.toString
68922
wenzelm
parents: 68916
diff changeset
   408
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   409
      def node_perspective: Document.Node.Perspective_Text =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   410
        Document.Node.Perspective(node_required, Text.Perspective.empty, Document.Node.Overlays.empty)
68922
wenzelm
parents: 68916
diff changeset
   411
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   412
      def make_edits(text_edits: List[Text.Edit]): List[Document.Edit_Text] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   413
        List(node_name -> Document.Node.Deps(node_header),
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   414
          node_name -> Document.Node.Edits(text_edits),
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   415
          node_name -> node_perspective)
68922
wenzelm
parents: 68916
diff changeset
   416
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   417
      def node_edits(old: Option[Theory]): List[Document.Edit_Text] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   418
      {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   419
        val (text_edits, old_required) =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   420
          if (old.isEmpty) (Text.Edit.inserts(0, text), false)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   421
          else (Text.Edit.replace(0, old.get.text, text), old.get.node_required)
67887
a4d5342898b1 unload_theories: actually observe required state;
wenzelm
parents: 67885
diff changeset
   422
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   423
        if (text_edits.isEmpty && node_required == old_required) Nil
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   424
        else make_edits(text_edits)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   425
      }
67887
a4d5342898b1 unload_theories: actually observe required state;
wenzelm
parents: 67885
diff changeset
   426
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   427
      def purge_edits: List[Document.Edit_Text] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   428
        make_edits(Text.Edit.removes(0, text))
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   429
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   430
      def required(required: Boolean): Theory =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   431
        if (required == node_required) this
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   432
        else new Theory(node_name, node_header, text, required)
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   433
    }
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   434
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   435
    sealed case class State(
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   436
      blobs: Map[Document.Node.Name, Document.Blob] = Map.empty,
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   437
      theories: Map[Document.Node.Name, Theory] = Map.empty,
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   438
      required: Multi_Map[Document.Node.Name, UUID.T] = Multi_Map.empty)
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   439
    {
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   440
      /* blobs */
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   441
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   442
      def doc_blobs: Document.Blobs = Document.Blobs(blobs)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   443
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   444
      def update_blobs(names: List[Document.Node.Name]): (Document.Blobs, State) =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   445
      {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   446
        val new_blobs =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   447
          names.flatMap(name =>
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   448
          {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   449
            val bytes = Bytes.read(name.path)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   450
            def new_blob: Document.Blob =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   451
            {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   452
              val text = bytes.text
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   453
              Document.Blob(bytes, text, Symbol.Text_Chunk(text), changed = true)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   454
            }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   455
            blobs.get(name) match {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   456
              case Some(blob) => if (blob.bytes == bytes) None else Some(name -> new_blob)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   457
              case None => Some(name -> new_blob)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   458
            }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   459
          })
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   460
        val blobs1 = (blobs /: new_blobs)(_ + _)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   461
        val blobs2 = (blobs /: new_blobs)({ case (map, (a, b)) => map + (a -> b.unchanged) })
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   462
        (Document.Blobs(blobs1), copy(blobs = blobs2))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   463
      }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   464
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   465
      def blob_edits(name: Document.Node.Name, old_blob: Option[Document.Blob])
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   466
        : List[Document.Edit_Text] =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   467
      {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   468
        val blob = blobs.getOrElse(name, error("Missing blob " + quote(name.toString)))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   469
        val text_edits =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   470
          old_blob match {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   471
            case None => List(Text.Edit.insert(0, blob.source))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   472
            case Some(blob0) => Text.Edit.replace(0, blob0.source, blob.source)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   473
          }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   474
        if (text_edits.isEmpty) Nil
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   475
        else List(name -> Document.Node.Blob(blob), name -> Document.Node.Edits(text_edits))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   476
      }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   477
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   478
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   479
      /* theories */
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   480
70653
f7c5b30fc432 load theories in stages, to reduce ML heap requirements;
wenzelm
parents: 70649
diff changeset
   481
      lazy val theory_graph: Document.Theory_Graph[Unit] =
70647
3047b7671279 tuned signature;
wenzelm
parents: 70644
diff changeset
   482
        Document.theory_graph(
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   483
          for ((name, theory) <- theories.toList)
70647
3047b7671279 tuned signature;
wenzelm
parents: 70644
diff changeset
   484
          yield ((name, ()), theory.node_header.imports.filter(theories.isDefinedAt(_))))
67056
e35ae3eeec93 load theories via PIDE document update;
wenzelm
parents: 67054
diff changeset
   485
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   486
      def is_required(name: Document.Node.Name): Boolean = required.isDefinedAt(name)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   487
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   488
      def insert_required(id: UUID.T, names: List[Document.Node.Name]): State =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   489
        copy(required = (required /: names)(_.insert(_, id)))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   490
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   491
      def remove_required(id: UUID.T, names: List[Document.Node.Name]): State =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   492
        copy(required = (required /: names)(_.remove(_, id)))
68958
9199f9da512a tuned message;
wenzelm
parents: 68957
diff changeset
   493
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   494
      def update_theories(update: List[(Document.Node.Name, Theory)]): State =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   495
        copy(theories =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   496
          (theories /: update)({ case (thys, (name, thy)) =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   497
            thys.get(name) match {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   498
              case Some(thy1) if thy1 == thy => thys
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   499
              case _ => thys + (name -> thy)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   500
            }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   501
          }))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   502
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   503
      def remove_theories(remove: List[Document.Node.Name]): State =
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   504
      {
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   505
        require(remove.forall(name => !is_required(name)))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   506
        copy(theories = theories -- remove)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   507
      }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   508
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   509
      def unload_theories(session: Session, id: UUID.T, theories: List[Document.Node.Name])
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   510
        : State =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   511
      {
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   512
        val st1 = remove_required(id, theories)
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   513
        val theory_edits =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   514
          for {
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   515
            node_name <- theories
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   516
            theory <- st1.theories.get(node_name)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   517
          }
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   518
          yield {
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   519
            val theory1 = theory.required(st1.is_required(node_name))
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   520
            val edits = theory1.node_edits(Some(theory))
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   521
            (edits, (node_name, theory1))
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   522
          }
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   523
        session.update(doc_blobs, theory_edits.flatMap(_._1))
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   524
        st1.update_theories(theory_edits.map(_._2))
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   525
      }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   526
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   527
      def purge_theories(session: Session, nodes: List[Document.Node.Name])
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   528
        : ((List[Document.Node.Name], List[Document.Node.Name]), State) =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   529
      {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   530
        val all_nodes = theory_graph.topological_order
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   531
        val purge = nodes.filterNot(is_required(_)).toSet
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   532
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   533
        val retain = theory_graph.all_preds(all_nodes.filterNot(purge)).toSet
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   534
        val (retained, purged) = all_nodes.partition(retain)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   535
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   536
        val purge_edits = purged.flatMap(name => theories(name).purge_edits)
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   537
        session.update(doc_blobs, purge_edits)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   538
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   539
        ((purged, retained), remove_theories(purged))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   540
      }
67884
43af581d7d8e unload_theories after consolidation -- reset node_required;
wenzelm
parents: 67883
diff changeset
   541
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   542
      def frontier_theories(clean: Set[Document.Node.Name]): Set[Document.Node.Name] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   543
      {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   544
        @tailrec def frontier(base: List[Document.Node.Name], front: Set[Document.Node.Name])
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   545
          : Set[Document.Node.Name] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   546
        {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   547
          val add = base.filter(b => theory_graph.imm_succs(b).forall(front))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   548
          if (add.isEmpty) front
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   549
          else {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   550
            val pre_add = add.map(theory_graph.imm_preds)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   551
            val base1 = (pre_add.head /: pre_add.tail)(_ ++ _).toList.filter(clean)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   552
            frontier(base1, front ++ add)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   553
          }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   554
        }
70648
60cb2bfea2d2 tuned important special case;
wenzelm
parents: 70647
diff changeset
   555
        if (clean.isEmpty) Set.empty
60cb2bfea2d2 tuned important special case;
wenzelm
parents: 70647
diff changeset
   556
        else frontier(theory_graph.maximals.filter(clean), Set.empty)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   557
      }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   558
    }
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   559
  }
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   560
69536
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   561
  class Resources private[Headless](
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   562
      val session_base_info: Sessions.Base_Info,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   563
      log: Logger = No_Logger)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   564
    extends isabelle.Resources(session_base_info.check_base, log = log)
67884
43af581d7d8e unload_theories after consolidation -- reset node_required;
wenzelm
parents: 67883
diff changeset
   565
  {
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   566
    resources =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   567
69538
faf547d2834c clarified signature, notably cascade of dump_options, deps, resources, session;
wenzelm
parents: 69536
diff changeset
   568
    def options: Options = session_base_info.options
faf547d2834c clarified signature, notably cascade of dump_options, deps, resources, session;
wenzelm
parents: 69536
diff changeset
   569
69536
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   570
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   571
    /* session */
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   572
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   573
    def start_session(print_mode: List[String] = Nil, progress: Progress = No_Progress): Session =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   574
    {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   575
      val session = new Session(session_base_info.session, options, resources)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   576
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   577
      val session_error = Future.promise[String]
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   578
      var session_phase: Session.Consumer[Session.Phase] = null
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   579
      session_phase =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   580
        Session.Consumer(getClass.getName) {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   581
          case Session.Ready =>
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   582
            session.phase_changed -= session_phase
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   583
            session_error.fulfill("")
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   584
          case Session.Terminated(result) if !result.ok =>
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   585
            session.phase_changed -= session_phase
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   586
            session_error.fulfill("Session start failed: return code " + result.rc)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   587
          case _ =>
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   588
        }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   589
      session.phase_changed += session_phase
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   590
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   591
      progress.echo("Starting session " + session_base_info.session + " ...")
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   592
      Isabelle_Process.start(session, options,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   593
        logic = session_base_info.session, dirs = session_base_info.dirs, modes = print_mode)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   594
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   595
      session_error.join match {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   596
        case "" => session
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   597
        case msg => session.stop(); error(msg)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   598
      }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   599
    }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   600
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   601
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   602
    /* theories */
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   603
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   604
    private val state = Synchronized(Resources.State())
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   605
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   606
    def load_theories(
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   607
      session: Session,
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   608
      id: UUID.T,
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   609
      theories: List[Document.Node.Name],
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   610
      files: List[Document.Node.Name],
69920
79c8ff387ed1 support unicode_symbols in input source;
wenzelm
parents: 69857
diff changeset
   611
      unicode_symbols: Boolean,
70625
1ae987cc052f support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents: 69920
diff changeset
   612
      share_common_data: Boolean,
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   613
      progress: Progress)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   614
    {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   615
      val loaded_theories =
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   616
        for (node_name <- theories)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   617
        yield {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   618
          val path = node_name.path
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   619
          if (!node_name.is_theory) error("Not a theory file: " + path)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   620
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   621
          progress.expose_interrupt()
69920
79c8ff387ed1 support unicode_symbols in input source;
wenzelm
parents: 69857
diff changeset
   622
          val text0 = File.read(path)
79c8ff387ed1 support unicode_symbols in input source;
wenzelm
parents: 69857
diff changeset
   623
          val text = if (unicode_symbols) Symbol.decode(text0) else text0
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   624
          val node_header = resources.check_thy_reader(node_name, Scan.char_reader(text))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   625
          new Resources.Theory(node_name, node_header, text, true)
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   626
        }
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   627
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   628
      val loaded = loaded_theories.length
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   629
      if (loaded > 1) progress.echo("Loading " + loaded + " theories ...")
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   630
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   631
      state.change(st =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   632
        {
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   633
          val (doc_blobs1, st1) = st.insert_required(id, theories).update_blobs(files)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   634
          val theory_edits =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   635
            for (theory <- loaded_theories)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   636
            yield {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   637
              val node_name = theory.node_name
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   638
              val theory1 = theory.required(st1.is_required(node_name))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   639
              val edits = theory1.node_edits(st1.theories.get(node_name))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   640
              (edits, (node_name, theory1))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   641
            }
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   642
          val file_edits =
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   643
            for { node_name <- files if doc_blobs1.changed(node_name) }
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   644
            yield st1.blob_edits(node_name, st.blobs.get(node_name))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   645
70625
1ae987cc052f support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents: 69920
diff changeset
   646
          session.update(doc_blobs1, theory_edits.flatMap(_._1) ::: file_edits.flatten,
1ae987cc052f support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents: 69920
diff changeset
   647
            share_common_data = share_common_data)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   648
          st1.update_theories(theory_edits.map(_._2))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   649
        })
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   650
    }
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   651
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   652
    def unload_theories(session: Session, id: UUID.T, theories: List[Document.Node.Name])
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   653
    {
70649
9a40720750dc clarified signature;
wenzelm
parents: 70648
diff changeset
   654
      state.change(_.unload_theories(session, id, theories))
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   655
    }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   656
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   657
    def clean_theories(session: Session, id: UUID.T, clean: Set[Document.Node.Name])
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   658
    {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   659
      state.change(st =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   660
        {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   661
          val frontier = st.frontier_theories(clean).toList
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   662
          if (frontier.isEmpty) st
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   663
          else {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   664
            val st1 = st.unload_theories(session, id, frontier)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   665
            val (_, st2) = st1.purge_theories(session, frontier)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   666
            st2
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   667
          }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   668
        })
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   669
    }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   670
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   671
    def purge_theories(session: Session, nodes: Option[List[Document.Node.Name]])
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   672
      : (List[Document.Node.Name], List[Document.Node.Name]) =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   673
    {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   674
      state.change_result(st => st.purge_theories(session, nodes getOrElse st.theory_graph.keys))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   675
    }
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   676
  }
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
   677
}