src/Pure/Thy/thy_info.scala
author wenzelm
Wed, 12 Apr 2017 17:48:19 +0200
changeset 65468 c41791ad75c3
parent 65439 862bfd2b4fd4
child 65471 05e5bffcf1d8
permissions -rw-r--r--
early check and normalization of session directory, e.g. relevant for path information passed to ML process, which may have a different CWD;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/thy_info.scala
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     3
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     4
Theory and file dependencies.
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     5
*/
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     6
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     7
package isabelle
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     8
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     9
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    10
object Thy_Info
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    11
{
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    12
  /* dependencies */
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    13
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    14
  sealed case class Dep(
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    15
    name: Document.Node.Name,
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    16
    header: Document.Node.Header)
62865
cf03cb9578d4 tuned output;
wenzelm
parents: 61536
diff changeset
    17
  {
cf03cb9578d4 tuned output;
wenzelm
parents: 61536
diff changeset
    18
    override def toString: String = name.toString
cf03cb9578d4 tuned output;
wenzelm
parents: 61536
diff changeset
    19
  }
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    20
}
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    21
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55488
diff changeset
    22
class Thy_Info(resources: Resources)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    23
{
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    24
  /* messages */
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    25
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
    26
  private def show_path(names: List[Document.Node.Name]): String =
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
    27
    names.map(name => quote(name.theory)).mkString(" via ")
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    28
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
    29
  private def cycle_msg(names: List[Document.Node.Name]): String =
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    30
    "Cyclic dependency of " + show_path(names)
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    31
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
    32
  private def required_by(initiators: List[Document.Node.Name]): String =
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    33
    if (initiators.isEmpty) ""
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
    34
    else "\n(required by " + show_path(initiators.reverse) + ")"
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    35
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    36
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    37
  /* dependencies */
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    38
48872
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    39
  object Dependencies
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    40
  {
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    41
    val empty = new Dependencies(Nil, Nil, Nil, Set.empty, Multi_Map.empty)
48872
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    42
  }
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    43
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    44
  final class Dependencies private(
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    45
    rev_deps: List[Thy_Info.Dep],
48873
18b17f15bc62 more direct cumulation of (sparse) keywords;
wenzelm
parents: 48872
diff changeset
    46
    val keywords: Thy_Header.Keywords,
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 62865
diff changeset
    47
    val abbrevs: Thy_Header.Abbrevs,
61536
346aa2c5447f more accurate imports: allow re-uses of base names in PIDE interaction (amending 60c159d490a2);
wenzelm
parents: 60077
diff changeset
    48
    val seen: Set[Document.Node.Name],
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    49
    val seen_theory: Multi_Map[String, (Document.Node.Name, Position.T)])
48871
c82720f054c3 tuned signature;
wenzelm
parents: 48870
diff changeset
    50
  {
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    51
    def :: (dep: Thy_Info.Dep): Dependencies =
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 62865
diff changeset
    52
      new Dependencies(
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 62865
diff changeset
    53
        dep :: rev_deps, dep.header.keywords ::: keywords, dep.header.abbrevs ::: abbrevs,
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    54
        seen, seen_theory)
48873
18b17f15bc62 more direct cumulation of (sparse) keywords;
wenzelm
parents: 48872
diff changeset
    55
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    56
    def + (thy: (Document.Node.Name, Position.T)): Dependencies =
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    57
    {
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    58
      val (name, _) = thy
65405
68f8a0dab28b tuned whitespace;
wenzelm
parents: 65404
diff changeset
    59
      new Dependencies(rev_deps, keywords, abbrevs, seen + name, seen_theory + (name.theory -> thy))
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    60
    }
48872
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    61
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    62
    def deps: List[Thy_Info.Dep] = rev_deps.reverse
48872
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    63
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    64
    def errors: List[String] =
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    65
    {
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    66
      val header_errors = deps.flatMap(dep => dep.header.errors)
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    67
      val import_errors =
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    68
        (for {
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    69
          (theory, imports) <- seen_theory.iterator_list
65429
fcff401fb609 more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents: 65407
diff changeset
    70
          if !resources.session_base.loaded_theories.isDefinedAt(theory)
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    71
          if imports.length > 1
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    72
        } yield {
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    73
          "Incoherent imports for theory " + quote(theory) + ":\n" +
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    74
            cat_lines(imports.map({ case (name, pos) =>
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    75
              "  " + quote(name.node) + Position.here(pos) }))
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
    76
        }).toList
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    77
      header_errors ::: import_errors
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
    78
    }
54549
2a3053472ec3 actually expose errors of cumulative theory dependencies;
wenzelm
parents: 54515
diff changeset
    79
63584
68751fe1c036 tuned signature -- prover-independence is presently theoretical;
wenzelm
parents: 63579
diff changeset
    80
    lazy val syntax: Outer_Syntax =
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
    81
      resources.session_base.syntax.add_keywords(keywords).add_abbrevs(abbrevs)
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
    82
65429
fcff401fb609 more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents: 65407
diff changeset
    83
    def loaded_theories: Map[String, Document.Node.Name] =
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
    84
      (resources.session_base.loaded_theories /: rev_deps) {
65407
4546272431e9 clarified fall-back name;
wenzelm
parents: 65405
diff changeset
    85
        case (loaded, dep) =>
65429
fcff401fb609 more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents: 65407
diff changeset
    86
          val name = dep.name.loaded_theory
fcff401fb609 more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents: 65407
diff changeset
    87
          loaded + (name.theory -> name) +
65439
862bfd2b4fd4 tuned signature;
wenzelm
parents: 65429
diff changeset
    88
            (name.theory_base_name -> name)  // legacy
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
    89
      }
48872
6124e0d1120a some support for thy_load_commands;
wenzelm
parents: 48871
diff changeset
    90
56392
bc118a32a870 tuned signature (see also 0850d43cb355);
wenzelm
parents: 56208
diff changeset
    91
    def loaded_files: List[Path] =
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
    92
    {
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    93
      def loaded(dep: Thy_Info.Dep): List[Path] =
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    94
      {
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    95
        val string = resources.with_thy_reader(dep.name,
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    96
          reader => Symbol.decode(reader.source.toString))
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    97
        resources.loaded_files(syntax, string).
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    98
          map(a => Path.explode(dep.name.master_dir) + Path.explode(a))
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
    99
      }
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
   100
      val dep_files = Par_List.map(loaded _, rev_deps)
59136
c2b23cb8a677 added Par_List in Scala, in accordance to ML version;
wenzelm
parents: 56823
diff changeset
   101
      ((Nil: List[Path]) /: dep_files) { case (acc_files, files) => files ::: acc_files }
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
   102
    }
62865
cf03cb9578d4 tuned output;
wenzelm
parents: 61536
diff changeset
   103
65404
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   104
    def session_graph(parent_session: String, parent_base: Sessions.Base): Graph_Display.Graph =
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   105
    {
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   106
      val parent_session_node =
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   107
        Graph_Display.Node("[" + parent_session + "]", "session." + parent_session)
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   108
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   109
      def node(name: Document.Node.Name): Graph_Display.Node =
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   110
        if (parent_base.loaded_theory(name)) parent_session_node
65439
862bfd2b4fd4 tuned signature;
wenzelm
parents: 65429
diff changeset
   111
        else Graph_Display.Node(name.theory_base_name, "theory." + name.theory)
65404
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   112
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   113
      (Graph_Display.empty_graph /: deps) {
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   114
        case (g, dep) =>
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   115
          if (parent_base.loaded_theory(dep.name)) g
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   116
          else {
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   117
            val a = node(dep.name)
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   118
            val bs = dep.header.imports.map({ case (name, _) => node(name) })
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   119
            ((g /: (a :: bs))(_.default_node(_, Nil)) /: bs)(_.add_edge(_, a))
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   120
          }
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   121
      }
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   122
    }
2b819faf45e9 clarified modules;
wenzelm
parents: 65403
diff changeset
   123
62865
cf03cb9578d4 tuned output;
wenzelm
parents: 61536
diff changeset
   124
    override def toString: String = deps.toString
48871
c82720f054c3 tuned signature;
wenzelm
parents: 48870
diff changeset
   125
  }
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   126
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   127
  private def require_thys(initiators: List[Document.Node.Name], required: Dependencies,
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   128
      thys: List[(Document.Node.Name, Position.T)]): Dependencies =
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   129
    (required /: thys)(require_thy(initiators, _, _))
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
   130
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   131
  private def require_thy(initiators: List[Document.Node.Name], required: Dependencies,
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   132
    thy: (Document.Node.Name, Position.T)): Dependencies =
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
   133
  {
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
   134
    val (name, require_pos) = thy
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
   135
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
   136
    def message: String =
61536
346aa2c5447f more accurate imports: allow re-uses of base names in PIDE interaction (amending 60c159d490a2);
wenzelm
parents: 60077
diff changeset
   137
      "The error(s) above occurred for theory " + quote(name.theory) +
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
   138
        required_by(initiators) + Position.here(require_pos)
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
   139
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 54722
diff changeset
   140
    val required1 = required + thy
65403
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
   141
    if (required.seen(name)) required
4a042bf9488e clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents: 65361
diff changeset
   142
    else if (resources.session_base.loaded_theory(name)) required1
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   143
    else {
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   144
      try {
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44574
diff changeset
   145
        if (initiators.contains(name)) error(cycle_msg(initiators))
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
   146
        val header =
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   147
          try { resources.check_thy(name, Token.Pos.file(name.node)).cat_errors(message) }
54549
2a3053472ec3 actually expose errors of cumulative theory dependencies;
wenzelm
parents: 54515
diff changeset
   148
          catch { case ERROR(msg) => cat_error(msg, message) }
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   149
        Thy_Info.Dep(name, header) :: require_thys(name :: initiators, required1, header.imports)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   150
      }
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 46737
diff changeset
   151
      catch {
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 46737
diff changeset
   152
        case e: Throwable =>
60077
55cb9462e602 tuned signature, clarified modules;
wenzelm
parents: 59705
diff changeset
   153
          Thy_Info.Dep(name, Document.Node.bad_header(Exn.message(e))) :: required1
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 46737
diff changeset
   154
      }
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   155
    }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   156
  }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   157
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   158
  def dependencies(thys: List[(Document.Node.Name, Position.T)]): Dependencies =
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65355
diff changeset
   159
    require_thys(Nil, Dependencies.empty, thys)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   160
}