author | wenzelm |
Fri, 31 Aug 2018 15:48:37 +0200 | |
changeset 68862 | 47e9912c53c3 |
parent 68835 | 2e59da922630 |
child 68864 | 1dacce27bc25 |
permissions | -rw-r--r-- |
68171 | 1 |
/* Title: Pure/Thy/export_theory.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Export foundational theory content. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Export_Theory |
|
11 |
{ |
|
68206 | 12 |
/** session content **/ |
13 |
||
14 |
sealed case class Session(name: String, theory_graph: Graph[String, Theory]) |
|
15 |
{ |
|
16 |
override def toString: String = name |
|
17 |
||
18 |
def theory(theory_name: String): Theory = |
|
19 |
if (theory_graph.defined(theory_name)) theory_graph.get_node(theory_name) |
|
20 |
else error("Bad theory " + quote(theory_name)) |
|
21 |
||
22 |
def theories: List[Theory] = |
|
23 |
theory_graph.topological_order.map(theory_graph.get_node(_)) |
|
24 |
} |
|
25 |
||
68209 | 26 |
def read_session(store: Sessions.Store, |
27 |
session_name: String, |
|
68206 | 28 |
types: Boolean = true, |
68264 | 29 |
consts: Boolean = true, |
30 |
axioms: Boolean = true, |
|
31 |
facts: Boolean = true, |
|
32 |
classes: Boolean = true, |
|
68862 | 33 |
locales: Boolean = true, |
68295 | 34 |
classrel: Boolean = true, |
35 |
arities: Boolean = true, |
|
68862 | 36 |
typedefs: Boolean = true, |
68267 | 37 |
cache: Term.Cache = Term.make_cache()): Session = |
68206 | 38 |
{ |
39 |
val thys = |
|
68210 | 40 |
using(store.open_database(session_name))(db => |
68206 | 41 |
{ |
42 |
db.transaction { |
|
68222 | 43 |
Export.read_theory_names(db, session_name).map((theory_name: String) => |
68418 | 44 |
read_theory(Export.Provider.database(db, session_name, theory_name), |
45 |
session_name, theory_name, types = types, consts = consts, |
|
68862 | 46 |
axioms = axioms, facts = facts, classes = classes, locales = locales, |
47 |
classrel = classrel, arities = arities, typedefs = typedefs, |
|
68267 | 48 |
cache = Some(cache))) |
68206 | 49 |
} |
50 |
}) |
|
51 |
||
52 |
val graph0 = |
|
53 |
(Graph.string[Theory] /: thys) { case (g, thy) => g.new_node(thy.name, thy) } |
|
54 |
val graph1 = |
|
55 |
(graph0 /: thys) { case (g0, thy) => |
|
56 |
(g0 /: thy.parents) { case (g1, parent) => |
|
57 |
g1.default_node(parent, empty_theory(parent)).add_edge_acyclic(parent, thy.name) } } |
|
58 |
||
59 |
Session(session_name, graph1) |
|
60 |
} |
|
61 |
||
62 |
||
63 |
||
68203 | 64 |
/** theory content **/ |
65 |
||
68346 | 66 |
val export_prefix: String = "theory/" |
67 |
||
68208 | 68 |
sealed case class Theory(name: String, parents: List[String], |
69 |
types: List[Type], |
|
70 |
consts: List[Const], |
|
68726 | 71 |
axioms: List[Fact_Single], |
72 |
facts: List[Fact_Multi], |
|
68264 | 73 |
classes: List[Class], |
68862 | 74 |
locales: List[Locale], |
68295 | 75 |
classrel: List[Classrel], |
68862 | 76 |
arities: List[Arity], |
77 |
typedefs: List[Typedef]) |
|
68206 | 78 |
{ |
79 |
override def toString: String = name |
|
68267 | 80 |
|
68711 | 81 |
lazy val entities: Set[Long] = |
82 |
Set.empty[Long] ++ |
|
83 |
types.iterator.map(_.entity.serial) ++ |
|
84 |
consts.iterator.map(_.entity.serial) ++ |
|
85 |
axioms.iterator.map(_.entity.serial) ++ |
|
86 |
facts.iterator.map(_.entity.serial) ++ |
|
68862 | 87 |
classes.iterator.map(_.entity.serial) ++ |
88 |
locales.iterator.map(_.entity.serial) |
|
68711 | 89 |
|
68267 | 90 |
def cache(cache: Term.Cache): Theory = |
91 |
Theory(cache.string(name), |
|
92 |
parents.map(cache.string(_)), |
|
93 |
types.map(_.cache(cache)), |
|
94 |
consts.map(_.cache(cache)), |
|
95 |
axioms.map(_.cache(cache)), |
|
96 |
facts.map(_.cache(cache)), |
|
97 |
classes.map(_.cache(cache)), |
|
68862 | 98 |
locales.map(_.cache(cache)), |
68295 | 99 |
classrel.map(_.cache(cache)), |
68862 | 100 |
arities.map(_.cache(cache)), |
101 |
typedefs.map(_.cache(cache))) |
|
68206 | 102 |
} |
103 |
||
68295 | 104 |
def empty_theory(name: String): Theory = |
68862 | 105 |
Theory(name, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil) |
68203 | 106 |
|
68418 | 107 |
def read_theory(provider: Export.Provider, session_name: String, theory_name: String, |
68203 | 108 |
types: Boolean = true, |
68208 | 109 |
consts: Boolean = true, |
68232 | 110 |
axioms: Boolean = true, |
68264 | 111 |
facts: Boolean = true, |
112 |
classes: Boolean = true, |
|
68862 | 113 |
locales: Boolean = true, |
68295 | 114 |
classrel: Boolean = true, |
115 |
arities: Boolean = true, |
|
68862 | 116 |
typedefs: Boolean = true, |
68267 | 117 |
cache: Option[Term.Cache] = None): Theory = |
68203 | 118 |
{ |
68206 | 119 |
val parents = |
68418 | 120 |
provider(export_prefix + "parents") match { |
68231 | 121 |
case Some(entry) => split_lines(entry.uncompressed().text) |
68206 | 122 |
case None => |
123 |
error("Missing theory export in session " + quote(session_name) + ": " + |
|
124 |
quote(theory_name)) |
|
125 |
} |
|
68267 | 126 |
val theory = |
127 |
Theory(theory_name, parents, |
|
68418 | 128 |
if (types) read_types(provider) else Nil, |
129 |
if (consts) read_consts(provider) else Nil, |
|
130 |
if (axioms) read_axioms(provider) else Nil, |
|
131 |
if (facts) read_facts(provider) else Nil, |
|
132 |
if (classes) read_classes(provider) else Nil, |
|
68862 | 133 |
if (locales) read_locales(provider) else Nil, |
68418 | 134 |
if (classrel) read_classrel(provider) else Nil, |
68862 | 135 |
if (arities) read_arities(provider) else Nil, |
136 |
if (typedefs) read_typedefs(provider) else Nil) |
|
68267 | 137 |
if (cache.isDefined) theory.cache(cache.get) else theory |
68203 | 138 |
} |
139 |
||
68711 | 140 |
def read_pure_theory(store: Sessions.Store, cache: Option[Term.Cache] = None): Theory = |
68710 | 141 |
{ |
142 |
val session_name = Thy_Header.PURE |
|
143 |
val theory_name = Thy_Header.PURE |
|
144 |
||
145 |
using(store.open_database(session_name))(db => |
|
146 |
{ |
|
147 |
db.transaction { |
|
148 |
read_theory(Export.Provider.database(db, session_name, theory_name), |
|
68711 | 149 |
session_name, theory_name, cache = cache) |
68710 | 150 |
} |
151 |
}) |
|
152 |
} |
|
153 |
||
68203 | 154 |
|
68171 | 155 |
/* entities */ |
156 |
||
68714 | 157 |
object Kind extends Enumeration |
68171 | 158 |
{ |
68714 | 159 |
val TYPE = Value("type") |
160 |
val CONST = Value("const") |
|
161 |
val AXIOM = Value("axiom") |
|
162 |
val FACT = Value("fact") |
|
163 |
val CLASS = Value("class") |
|
68862 | 164 |
val LOCALE = Value("locale") |
68714 | 165 |
} |
166 |
||
68835 | 167 |
sealed case class Entity( |
168 |
kind: Kind.Value, name: String, pos: Position.T, id: Option[Long], serial: Long) |
|
68714 | 169 |
{ |
68718 | 170 |
override def toString: String = kind.toString + " " + quote(name) |
68267 | 171 |
|
172 |
def cache(cache: Term.Cache): Entity = |
|
68830
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68726
diff
changeset
|
173 |
Entity(kind, cache.string(name), cache.position(pos), id, serial) |
68171 | 174 |
} |
175 |
||
68714 | 176 |
def decode_entity(kind: Kind.Value, tree: XML.Tree): (Entity, XML.Body) = |
68171 | 177 |
{ |
178 |
def err(): Nothing = throw new XML.XML_Body(List(tree)) |
|
179 |
||
180 |
tree match { |
|
181 |
case XML.Elem(Markup(Markup.ENTITY, props), body) => |
|
182 |
val name = Markup.Name.unapply(props) getOrElse err() |
|
68830
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68726
diff
changeset
|
183 |
val pos = props.filter({ case (a, _) => Markup.POSITION_PROPERTIES(a) && a != Markup.ID }) |
68835 | 184 |
val id = Position.Id.unapply(props) |
68171 | 185 |
val serial = Markup.Serial.unapply(props) getOrElse err() |
68830
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68726
diff
changeset
|
186 |
(Entity(kind, name, pos, id, serial), body) |
68171 | 187 |
case _ => err() |
188 |
} |
|
189 |
} |
|
190 |
||
191 |
||
192 |
/* types */ |
|
193 |
||
194 |
sealed case class Type(entity: Entity, args: List[String], abbrev: Option[Term.Typ]) |
|
68267 | 195 |
{ |
196 |
def cache(cache: Term.Cache): Type = |
|
197 |
Type(entity.cache(cache), |
|
198 |
args.map(cache.string(_)), |
|
199 |
abbrev.map(cache.typ(_))) |
|
200 |
} |
|
68171 | 201 |
|
68418 | 202 |
def read_types(provider: Export.Provider): List[Type] = |
203 |
provider.uncompressed_yxml(export_prefix + "types").map((tree: XML.Tree) => |
|
204 |
{ |
|
68714 | 205 |
val (entity, body) = decode_entity(Kind.TYPE, tree) |
68418 | 206 |
val (args, abbrev) = |
68203 | 207 |
{ |
68418 | 208 |
import XML.Decode._ |
209 |
pair(list(string), option(Term_XML.Decode.typ))(body) |
|
210 |
} |
|
211 |
Type(entity, args, abbrev) |
|
212 |
}) |
|
68171 | 213 |
|
214 |
||
215 |
/* consts */ |
|
216 |
||
68173 | 217 |
sealed case class Const( |
218 |
entity: Entity, typargs: List[String], typ: Term.Typ, abbrev: Option[Term.Term]) |
|
68267 | 219 |
{ |
220 |
def cache(cache: Term.Cache): Const = |
|
221 |
Const(entity.cache(cache), |
|
222 |
typargs.map(cache.string(_)), |
|
223 |
cache.typ(typ), |
|
224 |
abbrev.map(cache.term(_))) |
|
225 |
} |
|
68171 | 226 |
|
68418 | 227 |
def read_consts(provider: Export.Provider): List[Const] = |
228 |
provider.uncompressed_yxml(export_prefix + "consts").map((tree: XML.Tree) => |
|
229 |
{ |
|
68714 | 230 |
val (entity, body) = decode_entity(Kind.CONST, tree) |
68418 | 231 |
val (args, typ, abbrev) = |
68203 | 232 |
{ |
68418 | 233 |
import XML.Decode._ |
234 |
triple(list(string), Term_XML.Decode.typ, option(Term_XML.Decode.term))(body) |
|
235 |
} |
|
236 |
Const(entity, args, typ, abbrev) |
|
237 |
}) |
|
68208 | 238 |
|
239 |
||
68726 | 240 |
/* facts */ |
68232 | 241 |
|
68726 | 242 |
sealed case class Prop( |
243 |
typargs: List[(String, Term.Sort)], |
|
244 |
args: List[(String, Term.Typ)], |
|
245 |
term: Term.Term) |
|
68232 | 246 |
{ |
68726 | 247 |
def cache(cache: Term.Cache): Prop = |
248 |
Prop( |
|
249 |
typargs.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }), |
|
250 |
args.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }), |
|
251 |
cache.term(term)) |
|
68232 | 252 |
} |
68208 | 253 |
|
68726 | 254 |
def decode_prop(body: XML.Body): Prop = |
68267 | 255 |
{ |
68726 | 256 |
val (typargs, args, t) = |
257 |
{ |
|
258 |
import XML.Decode._ |
|
259 |
import Term_XML.Decode._ |
|
260 |
triple(list(pair(string, sort)), list(pair(string, typ)), term)(body) |
|
261 |
} |
|
262 |
Prop(typargs, args, t) |
|
68267 | 263 |
} |
68208 | 264 |
|
68726 | 265 |
sealed case class Fact_Single(entity: Entity, prop: Prop) |
266 |
{ |
|
267 |
def cache(cache: Term.Cache): Fact_Single = |
|
268 |
Fact_Single(entity.cache(cache), prop.cache(cache)) |
|
269 |
} |
|
270 |
||
271 |
sealed case class Fact_Multi(entity: Entity, props: List[Prop]) |
|
272 |
{ |
|
273 |
def cache(cache: Term.Cache): Fact_Multi = |
|
274 |
Fact_Multi(entity.cache(cache), props.map(_.cache(cache))) |
|
275 |
||
276 |
def split: List[Fact_Single] = |
|
277 |
props match { |
|
278 |
case List(prop) => List(Fact_Single(entity, prop)) |
|
279 |
case _ => |
|
280 |
for ((prop, i) <- props.zipWithIndex) |
|
281 |
yield Fact_Single(entity.copy(name = entity.name + "(" + (i + 1) + ")"), prop) |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
def read_axioms(provider: Export.Provider): List[Fact_Single] = |
|
68418 | 286 |
provider.uncompressed_yxml(export_prefix + "axioms").map((tree: XML.Tree) => |
287 |
{ |
|
68714 | 288 |
val (entity, body) = decode_entity(Kind.AXIOM, tree) |
68726 | 289 |
val prop = decode_prop(body) |
290 |
Fact_Single(entity, prop) |
|
68418 | 291 |
}) |
68232 | 292 |
|
68726 | 293 |
def read_facts(provider: Export.Provider): List[Fact_Multi] = |
68418 | 294 |
provider.uncompressed_yxml(export_prefix + "facts").map((tree: XML.Tree) => |
295 |
{ |
|
68714 | 296 |
val (entity, body) = decode_entity(Kind.FACT, tree) |
68726 | 297 |
val props = XML.Decode.list(decode_prop)(body) |
298 |
Fact_Multi(entity, props) |
|
68418 | 299 |
}) |
68264 | 300 |
|
301 |
||
302 |
/* type classes */ |
|
303 |
||
304 |
sealed case class Class( |
|
305 |
entity: Entity, params: List[(String, Term.Typ)], axioms: List[Term.Term]) |
|
68267 | 306 |
{ |
307 |
def cache(cache: Term.Cache): Class = |
|
308 |
Class(entity.cache(cache), |
|
309 |
params.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }), |
|
310 |
axioms.map(cache.term(_))) |
|
311 |
} |
|
68264 | 312 |
|
68418 | 313 |
def read_classes(provider: Export.Provider): List[Class] = |
314 |
provider.uncompressed_yxml(export_prefix + "classes").map((tree: XML.Tree) => |
|
315 |
{ |
|
68714 | 316 |
val (entity, body) = decode_entity(Kind.CLASS, tree) |
68418 | 317 |
val (params, axioms) = |
68264 | 318 |
{ |
68418 | 319 |
import XML.Decode._ |
320 |
import Term_XML.Decode._ |
|
321 |
pair(list(pair(string, typ)), list(term))(body) |
|
322 |
} |
|
323 |
Class(entity, params, axioms) |
|
324 |
}) |
|
68264 | 325 |
|
326 |
||
68862 | 327 |
/* locales */ |
328 |
||
329 |
sealed case class Locale( |
|
330 |
entity: Entity, type_params: List[(String, Term.Sort)], params: List[(String, Term.Typ)], |
|
331 |
asm: Option[Term.Term], defs: List[Term.Term]) |
|
332 |
{ |
|
333 |
def cache(cache: Term.Cache): Locale = |
|
334 |
Locale(entity.cache(cache), |
|
335 |
type_params.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }), |
|
336 |
params.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }), |
|
337 |
asm.map(cache.term(_)), |
|
338 |
defs.map(cache.term(_))) |
|
339 |
} |
|
340 |
||
341 |
def read_locales(provider: Export.Provider): List[Locale] = |
|
342 |
provider.uncompressed_yxml(export_prefix + "locales").map((tree: XML.Tree) => |
|
343 |
{ |
|
344 |
val (entity, body) = decode_entity(Kind.LOCALE, tree) |
|
345 |
val (type_params, (params, (asm, defs))) = |
|
346 |
{ |
|
347 |
import XML.Decode._ |
|
348 |
import Term_XML.Decode._ |
|
349 |
pair(list(pair(string, sort)), |
|
350 |
pair(list(pair(string, typ)), |
|
351 |
pair(option(term), list(term))))(body) |
|
352 |
} |
|
353 |
Locale(entity, type_params, params, asm, defs) |
|
354 |
}) |
|
355 |
||
356 |
||
68295 | 357 |
/* sort algebra */ |
358 |
||
359 |
sealed case class Classrel(class_name: String, super_names: List[String]) |
|
360 |
{ |
|
361 |
def cache(cache: Term.Cache): Classrel = |
|
362 |
Classrel(cache.string(class_name), super_names.map(cache.string(_))) |
|
363 |
} |
|
364 |
||
68418 | 365 |
def read_classrel(provider: Export.Provider): List[Classrel] = |
366 |
{ |
|
367 |
val body = provider.uncompressed_yxml(export_prefix + "classrel") |
|
368 |
val classrel = |
|
369 |
{ |
|
370 |
import XML.Decode._ |
|
371 |
list(pair(string, list(string)))(body) |
|
372 |
} |
|
373 |
for ((c, cs) <- classrel) yield Classrel(c, cs) |
|
374 |
} |
|
68295 | 375 |
|
376 |
sealed case class Arity(type_name: String, domain: List[Term.Sort], codomain: String) |
|
377 |
{ |
|
378 |
def cache(cache: Term.Cache): Arity = |
|
379 |
Arity(cache.string(type_name), domain.map(cache.sort(_)), cache.string(codomain)) |
|
380 |
} |
|
381 |
||
68418 | 382 |
def read_arities(provider: Export.Provider): List[Arity] = |
383 |
{ |
|
384 |
val body = provider.uncompressed_yxml(export_prefix + "arities") |
|
385 |
val arities = |
|
386 |
{ |
|
387 |
import XML.Decode._ |
|
388 |
import Term_XML.Decode._ |
|
389 |
list(triple(string, list(sort), string))(body) |
|
390 |
} |
|
391 |
for ((a, b, c) <- arities) yield Arity(a, b, c) |
|
392 |
} |
|
68295 | 393 |
|
394 |
||
68264 | 395 |
/* HOL typedefs */ |
396 |
||
397 |
sealed case class Typedef(name: String, |
|
398 |
rep_type: Term.Typ, abs_type: Term.Typ, rep_name: String, abs_name: String, axiom_name: String) |
|
68267 | 399 |
{ |
400 |
def cache(cache: Term.Cache): Typedef = |
|
401 |
Typedef(cache.string(name), |
|
402 |
cache.typ(rep_type), |
|
403 |
cache.typ(abs_type), |
|
404 |
cache.string(rep_name), |
|
405 |
cache.string(abs_name), |
|
406 |
cache.string(axiom_name)) |
|
407 |
} |
|
68264 | 408 |
|
68418 | 409 |
def read_typedefs(provider: Export.Provider): List[Typedef] = |
410 |
{ |
|
411 |
val body = provider.uncompressed_yxml(export_prefix + "typedefs") |
|
412 |
val typedefs = |
|
413 |
{ |
|
414 |
import XML.Decode._ |
|
415 |
import Term_XML.Decode._ |
|
416 |
list(pair(string, pair(typ, pair(typ, pair(string, pair(string, string))))))(body) |
|
417 |
} |
|
418 |
for { (name, (rep_type, (abs_type, (rep_name, (abs_name, axiom_name))))) <- typedefs } |
|
419 |
yield Typedef(name, rep_type, abs_type, rep_name, abs_name, axiom_name) |
|
420 |
} |
|
68171 | 421 |
} |