src/Pure/Thy/thy_load.scala
author wenzelm
Tue, 07 Aug 2012 17:46:30 +0200
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";
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_load.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
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents: 44574
diff changeset
     4
Primitives for loading theory files.
43651
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
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
     9
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    10
import java.io.{File => JFile}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    11
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    12
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    13
object Thy_Load
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    14
{
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    15
  def thy_path(path: Path): Path = path.ext("thy")
48484
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    16
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    17
  def is_ok(str: String): Boolean =
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    18
    try { thy_path(Path.explode(str)); true }
70898d016538 more explicit checks during parsing;
wenzelm
parents: 48422
diff changeset
    19
    catch { case ERROR(_) => false }
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    20
}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    21
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    22
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    23
class Thy_Load(preloaded: Set[String] = Set.empty)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    24
{
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    25
  /* loaded theories provided by prover */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    26
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    27
  private var loaded_theories: Set[String] = preloaded
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    28
48710
5b51ccdc8623 prefer static Build.session_content for loaded theories -- discontinued incremental protocol;
wenzelm
parents: 48707
diff changeset
    29
  def register_thy(name: String): Unit =
5b51ccdc8623 prefer static Build.session_content for loaded theories -- discontinued incremental protocol;
wenzelm
parents: 48707
diff changeset
    30
    synchronized { loaded_theories += name }
5b51ccdc8623 prefer static Build.session_content for loaded theories -- discontinued incremental protocol;
wenzelm
parents: 48707
diff changeset
    31
5b51ccdc8623 prefer static Build.session_content for loaded theories -- discontinued incremental protocol;
wenzelm
parents: 48707
diff changeset
    32
  def register_thys(names: Set[String]): Unit =
5b51ccdc8623 prefer static Build.session_content for loaded theories -- discontinued incremental protocol;
wenzelm
parents: 48707
diff changeset
    33
    synchronized { loaded_theories ++= names }
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    34
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    35
  def is_loaded(thy_name: String): Boolean =
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    36
    synchronized { loaded_theories.contains(thy_name) }
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    37
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    38
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    39
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    40
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    41
  def append(dir: String, source_path: Path): String =
48711
8d381fdef898 need to expand path in order to resolve imports like "~~/src/Tools/Code_Generator";
wenzelm
parents: 48710
diff changeset
    42
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    43
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    44
  def read_header(name: Document.Node.Name): Thy_Header =
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    45
  {
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    46
    val file = new JFile(name.node)
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    47
    if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    48
    Thy_Header.read(file)
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    49
  }
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    50
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    51
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    52
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    53
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    54
  private def import_name(dir: String, s: String): Document.Node.Name =
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    55
  {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    56
    val theory = Thy_Header.base_name(s)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    57
    if (is_loaded(theory)) Document.Node.Name(theory, "", theory)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    58
    else {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    59
      val path = Path.explode(s)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    60
      val node = append(dir, Thy_Load.thy_path(path))
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    61
      val dir1 = append(dir, path.dir)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    62
      Document.Node.Name(node, dir1, theory)
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    63
    }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    64
  }
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    65
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 48484
diff changeset
    66
  def check_header(name: Document.Node.Name, header: Thy_Header): Document.Node.Header =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    67
  {
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    68
    val name1 = header.name
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    69
    val imports = header.imports.map(import_name(name.dir, _))
46770
44c28a33c461 retain original "uses" (again) -- still required for Thy_Load.use_file etc. in ML (notably for maintaining required/provided);
wenzelm
parents: 46748
diff changeset
    70
    // FIXME val uses = header.uses.map(p => (append(name.dir, Path.explode(p._1)), p._2))
44c28a33c461 retain original "uses" (again) -- still required for Thy_Load.use_file etc. in ML (notably for maintaining required/provided);
wenzelm
parents: 46748
diff changeset
    71
    val uses = header.uses
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    72
    if (name.theory != name1)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    73
      error("Bad file name " + Thy_Load.thy_path(Path.basic(name.theory)) +
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    74
        " for theory " + quote(name1))
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 48484
diff changeset
    75
    Document.Node.Header(imports, header.keywords, uses)
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    76
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
    77
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 48484
diff changeset
    78
  def check_thy(name: Document.Node.Name): Document.Node.Header =
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
    79
    check_header(name, read_header(name))
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    80
}
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    81