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