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