src/Pure/Thy/thy_info.scala
author wenzelm
Mon, 29 Aug 2011 21:55:49 +0200
changeset 45446 24444588fddd
parent 45099 a8f921e6484f
child 45506 a4ff8a787202
permissions -rw-r--r--
actual auto loading of required files;
eliminated File_Store in favour of Thy_Load;
tuned;
     1 /*  Title:      Pure/Thy/thy_info.scala
     2     Author:     Makarius
     3 
     4 Theory and file dependencies.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Thy_Info
    11 {
    12   /* protocol messages */
    13 
    14   object Loaded_Theory {
    15     def unapply(msg: XML.Tree): Option[String] =
    16       msg match {
    17         case XML.Elem(Markup(Markup.LOADED_THEORY, List((Markup.NAME, name))), _) => Some(name)
    18         case _ => None
    19       }
    20   }
    21 }
    22 
    23 
    24 class Thy_Info(thy_load: Thy_Load)
    25 {
    26   /* messages */
    27 
    28   private def show_path(names: List[String]): String =
    29     names.map(quote).mkString(" via ")
    30 
    31   private def cycle_msg(names: List[String]): String =
    32     "Cyclic dependency of " + show_path(names)
    33 
    34   private def required_by(s: String, initiators: List[String]): String =
    35     if (initiators.isEmpty) ""
    36     else s + "(required by " + show_path(initiators.reverse) + ")"
    37 
    38 
    39   /* dependencies */
    40 
    41   type Deps = Map[String, Document.Node_Header]
    42 
    43   private def require_thys(initiators: List[String],
    44       deps: Deps, thys: List[(String, String)]): Deps =
    45     (deps /: thys)(require_thy(initiators, _, _))
    46 
    47   private def require_thy(initiators: List[String], deps: Deps, thy: (String, String)): Deps =
    48   {
    49     val (dir, str) = thy
    50     val path = Path.explode(str)
    51     val thy_name = path.base.implode
    52     val node_name = thy_load.append(dir, Thy_Header.thy_path(path))
    53 
    54     if (deps.isDefinedAt(node_name) || thy_load.is_loaded(thy_name)) deps
    55     else {
    56       val dir1 = thy_load.append(dir, path.dir)
    57       try {
    58         if (initiators.contains(node_name)) error(cycle_msg(initiators))
    59         val header =
    60           try { thy_load.check_thy(node_name) }
    61           catch {
    62             case ERROR(msg) =>
    63               cat_error(msg, "The error(s) above occurred while examining theory file " +
    64                 quote(node_name) + required_by("\n", initiators))
    65           }
    66         val thys = header.imports.map(str => (dir1, str))
    67         require_thys(node_name :: initiators, deps + (node_name -> Exn.Res(header)), thys)
    68       }
    69       catch { case e: Throwable => deps + (node_name -> Exn.Exn(e)) }
    70     }
    71   }
    72 
    73   def dependencies(thys: List[(String, String)]): Deps =
    74     require_thys(Nil, Map.empty, thys)
    75 }