src/Pure/Thy/thy_info.scala
author wenzelm
Mon, 04 Jul 2011 16:51:45 +0200
changeset 43652 dcd0b667f73d
parent 43651 511df47bcadc
child 43673 29eb1cd29961
permissions -rw-r--r--
pervasive Basic_Library in Scala; tuned;
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
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    10
class Thy_Info(thy_load: Thy_Load)
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    11
{
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    12
  /* messages */
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    13
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    14
  private def show_path(names: List[String]): String =
43652
dcd0b667f73d pervasive Basic_Library in Scala;
wenzelm
parents: 43651
diff changeset
    15
    names.map(quote).mkString(" via ")
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    16
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    17
  private def cycle_msg(names: List[String]): String =
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    18
    "Cyclic dependency of " + show_path(names)
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    19
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    20
  private def required_by(s: String, initiators: List[String]): String =
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    21
    if (initiators.isEmpty) ""
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    22
    else s + "(required by " + show_path(initiators.reverse) + ")"
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
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    25
  /* dependencies */
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    26
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    27
  type Deps = Map[String, Exn.Result[(String, Thy_Header.Header)]]  // name -> (text, header)
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    28
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    29
  private def require_thys(
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    30
      initiators: List[String], dir: Path, deps: Deps, strs: List[String]): Deps =
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    31
    (deps /: strs)(require_thy(initiators, dir, _, _))
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    32
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    33
  private def require_thy(initiators: List[String], dir: Path, deps: Deps, str: String): Deps =
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    34
  {
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    35
    val path = Path.explode(str)
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    36
    val name = path.base.implode
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    37
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    38
    if (deps.isDefinedAt(name)) deps
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    39
    else {
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    40
      val dir1 = dir + path.dir
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    41
      try {
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    42
        if (initiators.contains(name)) error(cycle_msg(initiators))
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    43
        val (text, header) =
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    44
          try { thy_load.check_thy(dir1, name) }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    45
          catch {
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    46
            case ERROR(msg) =>
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    47
              cat_error(msg, "The error(s) above occurred while examining theory " +
43652
dcd0b667f73d pervasive Basic_Library in Scala;
wenzelm
parents: 43651
diff changeset
    48
                quote(name) + required_by("\n", initiators))
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    49
          }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    50
        require_thys(name :: initiators, dir1,
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    51
          deps + (name -> Exn.Res(text, header)), header.imports)
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    52
      }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    53
      catch { case e: Throwable => deps + (name -> Exn.Exn(e)) }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    54
    }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    55
  }
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    56
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    57
  def dependencies(dir: Path, str: String): Deps =
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    58
    require_thy(Nil, dir, Map.empty, str)  // FIXME capture errors in str (!??)
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    59
}