src/Pure/Thy/sessions.scala
author wenzelm
Mon, 09 Jan 2017 20:47:45 +0100
changeset 64856 5e9bf964510a
parent 63996 3f47fec9edfc
child 65251 4b0a43afc3fb
permissions -rw-r--r--
clarified modules; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/sessions.scala
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     3
62973
744266e32612 clarified modules;
wenzelm
parents: 62969
diff changeset
     4
Isabelle session information.
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     5
*/
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     6
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     8
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
     9
import java.nio.ByteBuffer
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
    10
import java.nio.channels.FileChannel
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
    11
import java.nio.file.StandardOpenOption
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    12
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    13
import scala.collection.SortedSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    14
import scala.collection.mutable
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    15
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    16
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    17
object Sessions
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    18
{
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    19
  /* Pure */
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    20
63022
785a59235a15 more IDE support for Isabelle/Pure bootstrap;
wenzelm
parents: 62973
diff changeset
    21
  def pure_name(name: String): Boolean = name == Thy_Header.PURE
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    22
62902
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    23
  def pure_files(resources: Resources, syntax: Outer_Syntax, dir: Path): List[Path] =
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    24
  {
62946
wenzelm
parents: 62944
diff changeset
    25
    val roots = Thy_Header.ml_roots.map(_._1)
62902
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    26
    val loaded_files =
62946
wenzelm
parents: 62944
diff changeset
    27
      roots.flatMap(root => resources.loaded_files(syntax, File.read(dir + Path.explode(root))))
wenzelm
parents: 62944
diff changeset
    28
    (roots ::: loaded_files).map(file => dir + Path.explode(file))
62902
3c0f53eae166 more conventional theory syntax for ML bootstrap, with 'ML_file' instead of 'use';
wenzelm
parents: 62883
diff changeset
    29
  }
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    30
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    31
64856
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    32
  /* base info */
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    33
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    34
  object Base
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    35
  {
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    36
    val empty: Base = Base()
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    37
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    38
    lazy val bootstrap: Base =
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    39
      Base(keywords = Thy_Header.bootstrap_header, syntax = Thy_Header.bootstrap_syntax)
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    40
  }
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    41
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    42
  sealed case class Base(
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    43
    loaded_theories: Set[String] = Set.empty,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    44
    known_theories: Map[String, Document.Node.Name] = Map.empty,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    45
    keywords: Thy_Header.Keywords = Nil,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    46
    syntax: Outer_Syntax = Outer_Syntax.empty,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    47
    sources: List[(Path, SHA1.Digest)] = Nil,
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    48
    session_graph: Graph_Display.Graph = Graph_Display.empty_graph)
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    49
5e9bf964510a clarified modules;
wenzelm
parents: 63996
diff changeset
    50
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    51
  /* info */
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    52
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    53
  sealed case class Info(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    54
    chapter: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    55
    select: Boolean,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    56
    pos: Position.T,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    57
    groups: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    58
    dir: Path,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    59
    parent: Option[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    60
    description: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    61
    options: Options,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    62
    theories: List[(Boolean, Options, List[Path])],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    63
    files: List[Path],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    64
    document_files: List[(Path, Path)],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    65
    meta_digest: SHA1.Digest)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    66
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    67
    def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    68
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    69
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    70
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    71
  /* session tree */
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    72
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    73
  object Tree
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    74
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    75
    def apply(infos: Seq[(String, Info)]): Tree =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    76
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    77
      val graph1 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    78
        (Graph.string[Info] /: infos) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    79
          case (graph, (name, info)) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    80
            if (graph.defined(name))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    81
              error("Duplicate session " + quote(name) + Position.here(info.pos) +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    82
                Position.here(graph.get_node(name).pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    83
            else graph.new_node(name, info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    84
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    85
      val graph2 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    86
        (graph1 /: graph1.iterator) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    87
          case (graph, (name, (info, _))) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    88
            info.parent match {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    89
              case None => graph
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    90
              case Some(parent) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    91
                if (!graph.defined(parent))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    92
                  error("Bad parent session " + quote(parent) + " for " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    93
                    quote(name) + Position.here(info.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    94
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    95
                try { graph.add_edge_acyclic(parent, name) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    96
                catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    97
                  case exn: Graph.Cycles[_] =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    98
                    error(cat_lines(exn.cycles.map(cycle =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    99
                      "Cyclic session dependency of " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   100
                        cycle.map(c => quote(c.toString)).mkString(" via "))) +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   101
                          Position.here(info.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   102
                }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   103
            }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   104
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   105
      new Tree(graph2)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   106
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   107
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   108
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   109
  final class Tree private(val graph: Graph[String, Info])
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   110
    extends PartialFunction[String, Info]
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   111
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   112
    def apply(name: String): Info = graph.get_node(name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   113
    def isDefinedAt(name: String): Boolean = graph.defined(name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   114
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   115
    def selection(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   116
      requirements: Boolean = false,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   117
      all_sessions: Boolean = false,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   118
      exclude_session_groups: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   119
      exclude_sessions: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   120
      session_groups: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   121
      sessions: List[String] = Nil): (List[String], Tree) =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   122
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   123
      val bad_sessions =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   124
        SortedSet((exclude_sessions ::: sessions).filterNot(isDefinedAt(_)): _*).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   125
      if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   126
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   127
      val excluded =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   128
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   129
        val exclude_group = exclude_session_groups.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   130
        val exclude_group_sessions =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   131
          (for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   132
            (name, (info, _)) <- graph.iterator
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   133
            if apply(name).groups.exists(exclude_group)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   134
          } yield name).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   135
        graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   136
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   137
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   138
      val pre_selected =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   139
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   140
        if (all_sessions) graph.keys
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   141
        else {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   142
          val select_group = session_groups.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   143
          val select = sessions.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   144
          (for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   145
            (name, (info, _)) <- graph.iterator
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   146
            if info.select || select(name) || apply(name).groups.exists(select_group)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   147
          } yield name).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   148
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   149
      }.filterNot(excluded)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   150
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   151
      val selected =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   152
        if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   153
        else pre_selected
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   154
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   155
      val graph1 = graph.restrict(graph.all_preds(selected).toSet)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   156
      (selected, new Tree(graph1))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   157
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   158
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   159
    def ancestors(name: String): List[String] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   160
      graph.all_preds(List(name)).tail.reverse
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   161
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   162
    def topological_order: List[(String, Info)] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   163
      graph.topological_order.map(name => (name, apply(name)))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   164
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   165
    override def toString: String = graph.keys_iterator.mkString("Sessions.Tree(", ", ", ")")
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   166
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   167
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   168
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   169
  /* parser */
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   170
62864
wenzelm
parents: 62769
diff changeset
   171
  val ROOT = Path.explode("ROOT")
wenzelm
parents: 62769
diff changeset
   172
  val ROOTS = Path.explode("ROOTS")
wenzelm
parents: 62769
diff changeset
   173
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   174
  private val CHAPTER = "chapter"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   175
  private val SESSION = "session"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   176
  private val IN = "in"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   177
  private val DESCRIPTION = "description"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   178
  private val OPTIONS = "options"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   179
  private val GLOBAL_THEORIES = "global_theories"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   180
  private val THEORIES = "theories"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   181
  private val FILES = "files"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   182
  private val DOCUMENT_FILES = "document_files"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   183
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   184
  lazy val root_syntax =
63443
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   185
    Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" + IN +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   186
      (CHAPTER, Keyword.THY_DECL) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   187
      (SESSION, Keyword.THY_DECL) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   188
      (DESCRIPTION, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   189
      (OPTIONS, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   190
      (GLOBAL_THEORIES, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   191
      (THEORIES, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   192
      (FILES, Keyword.QUASI_COMMAND) +
c037248d54e8 clarified keywords;
wenzelm
parents: 63022
diff changeset
   193
      (DOCUMENT_FILES, Keyword.QUASI_COMMAND)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   194
62968
4e4738698db4 clarified syntax;
wenzelm
parents: 62946
diff changeset
   195
  private object Parser extends Parse.Parser with Options.Parser
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   196
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   197
    private abstract class Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   198
    private sealed case class Chapter(name: String) extends Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   199
    private sealed case class Session_Entry(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   200
      pos: Position.T,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   201
      name: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   202
      groups: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   203
      path: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   204
      parent: Option[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   205
      description: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   206
      options: List[Options.Spec],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   207
      theories: List[(Boolean, List[Options.Spec], List[String])],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   208
      files: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   209
      document_files: List[(String, String)]) extends Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   210
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   211
    private val chapter: Parser[Chapter] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   212
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   213
      val chapter_name = atom("chapter name", _.is_name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   214
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   215
      command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   216
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   217
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   218
    private val session_entry: Parser[Session_Entry] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   219
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   220
      val session_name = atom("session name", _.is_name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   221
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   222
      val option =
62968
4e4738698db4 clarified syntax;
wenzelm
parents: 62946
diff changeset
   223
        option_name ~ opt($$$("=") ~! option_value ^^
4e4738698db4 clarified syntax;
wenzelm
parents: 62946
diff changeset
   224
          { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   225
      val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   226
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   227
      val theories =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   228
        ($$$(GLOBAL_THEORIES) | $$$(THEORIES)) ~!
62969
9f394a16c557 eliminated "xname" and variants;
wenzelm
parents: 62968
diff changeset
   229
          ((options | success(Nil)) ~ rep(theory_name)) ^^
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   230
          { case x ~ (y ~ z) => (x == GLOBAL_THEORIES, y, z) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   231
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   232
      val document_files =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   233
        $$$(DOCUMENT_FILES) ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   234
          (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   235
              { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   236
            rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   237
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   238
      command(SESSION) ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   239
        (position(session_name) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   240
          (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   241
          (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   242
          ($$$("=") ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   243
            (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   244
              (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   245
              (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   246
              rep1(theories) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   247
              (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   248
              (rep(document_files) ^^ (x => x.flatten))))) ^^
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   249
        { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i))) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   250
            Session_Entry(pos, a, b, c, d, e, f, g, h, i) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   251
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   252
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   253
    def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   254
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   255
      def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   256
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   257
        try {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   258
          val name = entry.name
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   259
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   260
          if (name == "") error("Bad session name")
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
   261
          if (pure_name(name) && entry.parent.isDefined) error("Illegal parent session")
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
   262
          if (!pure_name(name) && !entry.parent.isDefined) error("Missing parent session")
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   263
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   264
          val session_options = options ++ entry.options
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   265
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   266
          val theories =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   267
            entry.theories.map({ case (global, opts, thys) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   268
              (global, session_options ++ opts, thys.map(Path.explode(_))) })
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   269
          val files = entry.files.map(Path.explode(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   270
          val document_files =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   271
            entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   272
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   273
          val meta_digest =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   274
            SHA1.digest((entry_chapter, name, entry.parent, entry.options,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   275
              entry.theories, entry.files, entry.document_files).toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   276
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   277
          val info =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   278
            Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path),
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   279
              entry.parent, entry.description, session_options, theories, files,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   280
              document_files, meta_digest)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   281
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   282
          (name, info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   283
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   284
        catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   285
          case ERROR(msg) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   286
            error(msg + "\nThe error(s) above occurred in session entry " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   287
              quote(entry.name) + Position.here(entry.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   288
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   289
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   290
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   291
      val root = dir + ROOT
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   292
      if (root.is_file) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   293
        val toks = Token.explode(root_syntax.keywords, File.read(root))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   294
        val start = Token.Pos.file(root.implode)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   295
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   296
        parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   297
          case Success(result, _) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   298
            var entry_chapter = "Unsorted"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   299
            val infos = new mutable.ListBuffer[(String, Info)]
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   300
            result.foreach {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   301
              case Chapter(name) => entry_chapter = name
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   302
              case entry: Session_Entry => infos += make_info(entry_chapter, entry)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   303
            }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   304
            infos.toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   305
          case bad => error(bad.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   306
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   307
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   308
      else Nil
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   309
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   310
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   311
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   312
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   313
  /* load sessions from certain directories */
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   314
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   315
  private def is_session_dir(dir: Path): Boolean =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   316
    (dir + ROOT).is_file || (dir + ROOTS).is_file
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   317
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   318
  private def check_session_dir(dir: Path): Path =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   319
    if (is_session_dir(dir)) dir
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   320
    else error("Bad session root directory: " + dir.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   321
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   322
  def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): Tree =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   323
  {
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   324
    def load_dir(select: Boolean, dir: Path): List[(String, Info)] =
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   325
      load_root(select, dir) ::: load_roots(select, dir)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   326
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   327
    def load_root(select: Boolean, dir: Path): List[(String, Info)] =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   328
      Parser.parse(options, select, dir)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   329
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   330
    def load_roots(select: Boolean, dir: Path): List[(String, Info)] =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   331
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   332
      val roots = dir + ROOTS
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   333
      if (roots.is_file) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   334
        for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   335
          line <- split_lines(File.read(roots))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   336
          if !(line == "" || line.startsWith("#"))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   337
          dir1 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   338
            try { check_session_dir(dir + Path.explode(line)) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   339
            catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   340
              case ERROR(msg) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   341
                error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   342
            }
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   343
          info <- load_dir(select, dir1)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   344
        } yield info
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   345
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   346
      else Nil
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   347
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   348
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   349
    val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   350
    dirs.foreach(check_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   351
    select_dirs.foreach(check_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   352
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   353
    Tree(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   354
      for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   355
        (select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   356
        info <- load_dir(select, dir)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   357
      } yield info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   358
  }
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   359
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   360
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   361
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   362
  /** heap file with SHA1 digest **/
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   363
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   364
  private val sha1_prefix = "SHA1:"
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   365
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   366
  def read_heap_digest(heap: Path): Option[String] =
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   367
  {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   368
    if (heap.is_file) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   369
      val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   370
      try {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   371
        val len = file.size
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   372
        val n = sha1_prefix.length + SHA1.digest_length
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   373
        if (len >= n) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   374
          file.position(len - n)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   375
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   376
          val buf = ByteBuffer.allocate(n)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   377
          var i = 0
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   378
          var m = 0
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   379
          do {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   380
            m = file.read(buf)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   381
            if (m != -1) i += m
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   382
          }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   383
          while (m != -1 && n > i)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   384
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   385
          if (i == n) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   386
            val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   387
            val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   388
            if (prefix == sha1_prefix) Some(s) else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   389
          }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   390
          else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   391
        }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   392
        else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   393
      }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   394
      finally { file.close }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   395
    }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   396
    else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   397
  }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   398
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   399
  def write_heap_digest(heap: Path): String =
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   400
    read_heap_digest(heap) match {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   401
      case None =>
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   402
        val s = SHA1.digest(heap).rep
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   403
        File.append(heap, sha1_prefix + s)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   404
        s
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   405
      case Some(s) => s
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   406
    }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   407
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   408
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   409
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   410
  /** persistent store **/
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   411
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   412
  def log(name: String): Path = Path.basic("log") + Path.basic(name)
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   413
  def log_gz(name: String): Path = log(name).ext("gz")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   414
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   415
  def store(system_mode: Boolean = false): Store = new Store(system_mode)
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   416
63996
3f47fec9edfc tuned whitespace;
wenzelm
parents: 63443
diff changeset
   417
  class Store private[Sessions](system_mode: Boolean)
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   418
  {
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   419
    /* output */
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   420
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   421
    val browser_info: Path =
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   422
      if (system_mode) Path.explode("~~/browser_info")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   423
      else Path.explode("$ISABELLE_BROWSER_INFO")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   424
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   425
    val output_dir: Path =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   426
      if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   427
      else Path.explode("$ISABELLE_OUTPUT")
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   428
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   429
    def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   430
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   431
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   432
    /* input */
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   433
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   434
    private val input_dirs =
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   435
      if (system_mode) List(output_dir)
62633
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   436
      else {
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   437
        val ml_ident = Path.explode("$ML_IDENTIFIER").expand
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   438
        output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   439
      }
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   440
62636
e676ae9f1bf6 clarified signature;
wenzelm
parents: 62635
diff changeset
   441
    def find(name: String): Option[(Path, Option[String])] =
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   442
      input_dirs.find(dir => (dir + log_gz(name)).is_file).map(dir =>
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   443
        (dir + log_gz(name), read_heap_digest(dir + Path.basic(name))))
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   444
62633
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   445
    def find_log(name: String): Option[Path] =
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   446
      input_dirs.map(_ + log(name)).find(_.is_file)
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   447
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   448
    def find_log_gz(name: String): Option[Path] =
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   449
      input_dirs.map(_ + log_gz(name)).find(_.is_file)
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   450
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   451
    def find_heap(name: String): Option[Path] =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   452
      input_dirs.map(_ + Path.basic(name)).find(_.is_file)
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   453
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   454
    def heap(name: String): Path =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   455
      find_heap(name) getOrElse
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   456
        error("Unknown logic " + quote(name) + " -- no heap file found in:\n" +
62769
146945b9e83c tuned message;
wenzelm
parents: 62704
diff changeset
   457
          cat_lines(input_dirs.map(dir => "  " + dir.expand.implode)))
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   458
  }
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   459
}