src/Pure/Thy/thy_load.scala
author wenzelm
Tue Aug 07 17:46:30 2012 +0200 (2012-08-07)
changeset 48711 8d381fdef898
parent 48710 5b51ccdc8623
child 48827 8791d106e30b
permissions -rw-r--r--
need to expand path in order to resolve imports like "~~/src/Tools/Code_Generator";
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@48422
    13
object Thy_Load
wenzelm@48422
    14
{
wenzelm@48422
    15
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@48484
    16
wenzelm@48484
    17
  def is_ok(str: String): Boolean =
wenzelm@48484
    18
    try { thy_path(Path.explode(str)); true }
wenzelm@48484
    19
    catch { case ERROR(_) => false }
wenzelm@48422
    20
}
wenzelm@44953
    21
wenzelm@48422
    22
wenzelm@48422
    23
class Thy_Load(preloaded: Set[String] = Set.empty)
wenzelm@43651
    24
{
wenzelm@44953
    25
  /* loaded theories provided by prover */
wenzelm@44953
    26
wenzelm@48422
    27
  private var loaded_theories: Set[String] = preloaded
wenzelm@44953
    28
wenzelm@48710
    29
  def register_thy(name: String): Unit =
wenzelm@48710
    30
    synchronized { loaded_theories += name }
wenzelm@48710
    31
wenzelm@48710
    32
  def register_thys(names: Set[String]): Unit =
wenzelm@48710
    33
    synchronized { loaded_theories ++= names }
wenzelm@44953
    34
wenzelm@44953
    35
  def is_loaded(thy_name: String): Boolean =
wenzelm@44953
    36
    synchronized { loaded_theories.contains(thy_name) }
wenzelm@44953
    37
wenzelm@44953
    38
wenzelm@44953
    39
  /* file-system operations */
wenzelm@44953
    40
wenzelm@44953
    41
  def append(dir: String, source_path: Path): String =
wenzelm@48711
    42
    (Path.explode(dir) + source_path).expand.implode
wenzelm@44953
    43
wenzelm@46737
    44
  def read_header(name: Document.Node.Name): Thy_Header =
wenzelm@44953
    45
  {
wenzelm@48409
    46
    val file = new JFile(name.node)
wenzelm@44953
    47
    if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@44953
    48
    Thy_Header.read(file)
wenzelm@44953
    49
  }
wenzelm@46737
    50
wenzelm@46737
    51
wenzelm@46737
    52
  /* theory files */
wenzelm@46737
    53
wenzelm@46737
    54
  private def import_name(dir: String, s: String): Document.Node.Name =
wenzelm@46737
    55
  {
wenzelm@46737
    56
    val theory = Thy_Header.base_name(s)
wenzelm@46737
    57
    if (is_loaded(theory)) Document.Node.Name(theory, "", theory)
wenzelm@46737
    58
    else {
wenzelm@46737
    59
      val path = Path.explode(s)
wenzelm@48422
    60
      val node = append(dir, Thy_Load.thy_path(path))
wenzelm@46737
    61
      val dir1 = append(dir, path.dir)
wenzelm@46737
    62
      Document.Node.Name(node, dir1, theory)
wenzelm@46737
    63
    }
wenzelm@46737
    64
  }
wenzelm@46737
    65
wenzelm@48707
    66
  def check_header(name: Document.Node.Name, header: Thy_Header): Document.Node.Header =
wenzelm@46737
    67
  {
wenzelm@46737
    68
    val name1 = header.name
wenzelm@46737
    69
    val imports = header.imports.map(import_name(name.dir, _))
wenzelm@46770
    70
    // FIXME val uses = header.uses.map(p => (append(name.dir, Path.explode(p._1)), p._2))
wenzelm@46770
    71
    val uses = header.uses
wenzelm@46737
    72
    if (name.theory != name1)
wenzelm@48422
    73
      error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
wenzelm@48422
    74
        " for theory " + quote(name1))
wenzelm@48707
    75
    Document.Node.Header(imports, header.keywords, uses)
wenzelm@46737
    76
  }
wenzelm@46748
    77
wenzelm@48707
    78
  def check_thy(name: Document.Node.Name): Document.Node.Header =
wenzelm@46748
    79
    check_header(name, read_header(name))
wenzelm@43651
    80
}
wenzelm@43651
    81