src/Pure/Thy/thy_load.scala
author wenzelm
Sun, 22 Jul 2012 00:00:22 +0200
changeset 49437 9613780a805b
parent 49424 0d2114eb412a
child 49499 70898d016538
permissions -rw-r--r--
determine source dependencies, relatively to preloaded theories;
tuned signature;
     1 /*  Title:      Pure/Thy/thy_load.scala
     2     Author:     Makarius
     3 
     4 Primitives for loading theory files.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.io.{File => JFile}
    11 
    12 
    13 object Thy_Load
    14 {
    15   def thy_path(path: Path): Path = path.ext("thy")
    16 }
    17 
    18 
    19 class Thy_Load(preloaded: Set[String] = Set.empty)
    20 {
    21   /* loaded theories provided by prover */
    22 
    23   private var loaded_theories: Set[String] = preloaded
    24 
    25   def register_thy(thy_name: String): Unit =
    26     synchronized { loaded_theories += thy_name }
    27 
    28   def is_loaded(thy_name: String): Boolean =
    29     synchronized { loaded_theories.contains(thy_name) }
    30 
    31 
    32   /* file-system operations */
    33 
    34   def append(dir: String, source_path: Path): String =
    35     (Path.explode(dir) + source_path).implode
    36 
    37   def read_header(name: Document.Node.Name): Thy_Header =
    38   {
    39     val file = new JFile(name.node)
    40     if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
    41     Thy_Header.read(file)
    42   }
    43 
    44 
    45   /* theory files */
    46 
    47   private def import_name(dir: String, s: String): Document.Node.Name =
    48   {
    49     val theory = Thy_Header.base_name(s)
    50     if (is_loaded(theory)) Document.Node.Name(theory, "", theory)
    51     else {
    52       val path = Path.explode(s)
    53       val node = append(dir, Thy_Load.thy_path(path))
    54       val dir1 = append(dir, path.dir)
    55       Document.Node.Name(node, dir1, theory)
    56     }
    57   }
    58 
    59   def check_header(name: Document.Node.Name, header: Thy_Header): Document.Node.Deps =
    60   {
    61     val name1 = header.name
    62     val imports = header.imports.map(import_name(name.dir, _))
    63     // FIXME val uses = header.uses.map(p => (append(name.dir, Path.explode(p._1)), p._2))
    64     val uses = header.uses
    65     if (name.theory != name1)
    66       error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
    67         " for theory " + quote(name1))
    68     Document.Node.Deps(imports, header.keywords, uses)
    69   }
    70 
    71   def check_thy(name: Document.Node.Name): Document.Node.Deps =
    72     check_header(name, read_header(name))
    73 }
    74