author | wenzelm |
Wed, 12 Apr 2017 17:48:19 +0200 | |
changeset 65468 | c41791ad75c3 |
parent 65439 | 862bfd2b4fd4 |
child 65471 | 05e5bffcf1d8 |
permissions | -rw-r--r-- |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/Thy/thy_info.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 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
4 |
Theory and file dependencies. |
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 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
9 |
|
60077 | 10 |
object Thy_Info |
11 |
{ |
|
12 |
/* dependencies */ |
|
13 |
||
14 |
sealed case class Dep( |
|
15 |
name: Document.Node.Name, |
|
16 |
header: Document.Node.Header) |
|
62865 | 17 |
{ |
18 |
override def toString: String = name.toString |
|
19 |
} |
|
60077 | 20 |
} |
21 |
||
56208 | 22 |
class Thy_Info(resources: Resources) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
23 |
{ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
24 |
/* messages */ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
25 |
|
44615 | 26 |
private def show_path(names: List[Document.Node.Name]): String = |
27 |
names.map(name => quote(name.theory)).mkString(" via ") |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
28 |
|
44615 | 29 |
private def cycle_msg(names: List[Document.Node.Name]): String = |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
30 |
"Cyclic dependency of " + show_path(names) |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
31 |
|
44615 | 32 |
private def required_by(initiators: List[Document.Node.Name]): String = |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
33 |
if (initiators.isEmpty) "" |
44615 | 34 |
else "\n(required by " + show_path(initiators.reverse) + ")" |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
35 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
36 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
37 |
/* dependencies */ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
38 |
|
48872 | 39 |
object Dependencies |
40 |
{ |
|
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
41 |
val empty = new Dependencies(Nil, Nil, Nil, Set.empty, Multi_Map.empty) |
48872 | 42 |
} |
43 |
||
44 |
final class Dependencies private( |
|
60077 | 45 |
rev_deps: List[Thy_Info.Dep], |
48873 | 46 |
val keywords: Thy_Header.Keywords, |
63579 | 47 |
val abbrevs: Thy_Header.Abbrevs, |
61536
346aa2c5447f
more accurate imports: allow re-uses of base names in PIDE interaction (amending 60c159d490a2);
wenzelm
parents:
60077
diff
changeset
|
48 |
val seen: Set[Document.Node.Name], |
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
49 |
val seen_theory: Multi_Map[String, (Document.Node.Name, Position.T)]) |
48871 | 50 |
{ |
60077 | 51 |
def :: (dep: Thy_Info.Dep): Dependencies = |
63579 | 52 |
new Dependencies( |
53 |
dep :: rev_deps, dep.header.keywords ::: keywords, dep.header.abbrevs ::: abbrevs, |
|
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
54 |
seen, seen_theory) |
48873 | 55 |
|
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
56 |
def + (thy: (Document.Node.Name, Position.T)): Dependencies = |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
57 |
{ |
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
58 |
val (name, _) = thy |
65405 | 59 |
new Dependencies(rev_deps, keywords, abbrevs, seen + name, seen_theory + (name.theory -> thy)) |
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
60 |
} |
48872 | 61 |
|
60077 | 62 |
def deps: List[Thy_Info.Dep] = rev_deps.reverse |
48872 | 63 |
|
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
64 |
def errors: List[String] = |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
65 |
{ |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
66 |
val header_errors = deps.flatMap(dep => dep.header.errors) |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
67 |
val import_errors = |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
68 |
(for { |
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
69 |
(theory, imports) <- seen_theory.iterator_list |
65429
fcff401fb609
more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents:
65407
diff
changeset
|
70 |
if !resources.session_base.loaded_theories.isDefinedAt(theory) |
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
71 |
if imports.length > 1 |
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
72 |
} yield { |
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
73 |
"Incoherent imports for theory " + quote(theory) + ":\n" + |
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
74 |
cat_lines(imports.map({ case (name, pos) => |
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
75 |
" " + quote(name.node) + Position.here(pos) })) |
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
76 |
}).toList |
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
77 |
header_errors ::: import_errors |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
78 |
} |
54549
2a3053472ec3
actually expose errors of cumulative theory dependencies;
wenzelm
parents:
54515
diff
changeset
|
79 |
|
63584
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
wenzelm
parents:
63579
diff
changeset
|
80 |
lazy val syntax: Outer_Syntax = |
65361 | 81 |
resources.session_base.syntax.add_keywords(keywords).add_abbrevs(abbrevs) |
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
82 |
|
65429
fcff401fb609
more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents:
65407
diff
changeset
|
83 |
def loaded_theories: Map[String, Document.Node.Name] = |
65361 | 84 |
(resources.session_base.loaded_theories /: rev_deps) { |
65407 | 85 |
case (loaded, dep) => |
65429
fcff401fb609
more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents:
65407
diff
changeset
|
86 |
val name = dep.name.loaded_theory |
fcff401fb609
more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents:
65407
diff
changeset
|
87 |
loaded + (name.theory -> name) + |
65439 | 88 |
(name.theory_base_name -> name) // legacy |
65361 | 89 |
} |
48872 | 90 |
|
56392 | 91 |
def loaded_files: List[Path] = |
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
92 |
{ |
60077 | 93 |
def loaded(dep: Thy_Info.Dep): List[Path] = |
94 |
{ |
|
95 |
val string = resources.with_thy_reader(dep.name, |
|
96 |
reader => Symbol.decode(reader.source.toString)) |
|
97 |
resources.loaded_files(syntax, string). |
|
98 |
map(a => Path.explode(dep.name.master_dir) + Path.explode(a)) |
|
99 |
} |
|
100 |
val dep_files = Par_List.map(loaded _, rev_deps) |
|
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
56823
diff
changeset
|
101 |
((Nil: List[Path]) /: dep_files) { case (acc_files, files) => files ::: acc_files } |
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
102 |
} |
62865 | 103 |
|
65404 | 104 |
def session_graph(parent_session: String, parent_base: Sessions.Base): Graph_Display.Graph = |
105 |
{ |
|
106 |
val parent_session_node = |
|
107 |
Graph_Display.Node("[" + parent_session + "]", "session." + parent_session) |
|
108 |
||
109 |
def node(name: Document.Node.Name): Graph_Display.Node = |
|
110 |
if (parent_base.loaded_theory(name)) parent_session_node |
|
65439 | 111 |
else Graph_Display.Node(name.theory_base_name, "theory." + name.theory) |
65404 | 112 |
|
113 |
(Graph_Display.empty_graph /: deps) { |
|
114 |
case (g, dep) => |
|
115 |
if (parent_base.loaded_theory(dep.name)) g |
|
116 |
else { |
|
117 |
val a = node(dep.name) |
|
118 |
val bs = dep.header.imports.map({ case (name, _) => node(name) }) |
|
119 |
((g /: (a :: bs))(_.default_node(_, Nil)) /: bs)(_.add_edge(_, a)) |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
||
62865 | 124 |
override def toString: String = deps.toString |
48871 | 125 |
} |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
126 |
|
65359 | 127 |
private def require_thys(initiators: List[Document.Node.Name], required: Dependencies, |
128 |
thys: List[(Document.Node.Name, Position.T)]): Dependencies = |
|
129 |
(required /: thys)(require_thy(initiators, _, _)) |
|
44615 | 130 |
|
65359 | 131 |
private def require_thy(initiators: List[Document.Node.Name], required: Dependencies, |
132 |
thy: (Document.Node.Name, Position.T)): Dependencies = |
|
44615 | 133 |
{ |
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
134 |
val (name, require_pos) = thy |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
135 |
|
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
136 |
def message: String = |
61536
346aa2c5447f
more accurate imports: allow re-uses of base names in PIDE interaction (amending 60c159d490a2);
wenzelm
parents:
60077
diff
changeset
|
137 |
"The error(s) above occurred for theory " + quote(name.theory) + |
55488
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
138 |
required_by(initiators) + Position.here(require_pos) |
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
139 |
|
60c159d490a2
more integrity checks of theory names vs. full node names;
wenzelm
parents:
54722
diff
changeset
|
140 |
val required1 = required + thy |
65403
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
141 |
if (required.seen(name)) required |
4a042bf9488e
clarified checks -- avoid duplicated messages (amending 60c159d490a2);
wenzelm
parents:
65361
diff
changeset
|
142 |
else if (resources.session_base.loaded_theory(name)) required1 |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
143 |
else { |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
144 |
try { |
44615 | 145 |
if (initiators.contains(name)) error(cycle_msg(initiators)) |
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
146 |
val header = |
65359 | 147 |
try { resources.check_thy(name, Token.Pos.file(name.node)).cat_errors(message) } |
54549
2a3053472ec3
actually expose errors of cumulative theory dependencies;
wenzelm
parents:
54515
diff
changeset
|
148 |
catch { case ERROR(msg) => cat_error(msg, message) } |
65359 | 149 |
Thy_Info.Dep(name, header) :: require_thys(name :: initiators, required1, header.imports) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
150 |
} |
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
46737
diff
changeset
|
151 |
catch { |
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
46737
diff
changeset
|
152 |
case e: Throwable => |
60077 | 153 |
Thy_Info.Dep(name, Document.Node.bad_header(Exn.message(e))) :: required1 |
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
46737
diff
changeset
|
154 |
} |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
155 |
} |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
156 |
} |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
157 |
|
65359 | 158 |
def dependencies(thys: List[(Document.Node.Name, Position.T)]): Dependencies = |
159 |
require_thys(Nil, Dependencies.empty, thys) |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
160 |
} |