src/Pure/Thy/export_theory.scala
author wenzelm
Fri Aug 03 15:29:18 2018 +0200 (11 months ago)
changeset 68716 040c6298850b
parent 68714 1d5ab386eaf0
child 68717 54a5043d4cd5
permissions -rw-r--r--
more operations;
     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 {
    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 
    26   def read_session(store: Sessions.Store,
    27     session_name: String,
    28     types: Boolean = true,
    29     consts: Boolean = true,
    30     axioms: Boolean = true,
    31     facts: Boolean = true,
    32     classes: Boolean = true,
    33     typedefs: Boolean = true,
    34     classrel: Boolean = true,
    35     arities: Boolean = true,
    36     cache: Term.Cache = Term.make_cache()): Session =
    37   {
    38     val thys =
    39       using(store.open_database(session_name))(db =>
    40       {
    41         db.transaction {
    42           Export.read_theory_names(db, session_name).map((theory_name: String) =>
    43             read_theory(Export.Provider.database(db, session_name, theory_name),
    44               session_name, theory_name, types = types, consts = consts,
    45               axioms = axioms, facts = facts, classes = classes, typedefs = typedefs,
    46               cache = Some(cache)))
    47         }
    48       })
    49 
    50     val graph0 =
    51       (Graph.string[Theory] /: thys) { case (g, thy) => g.new_node(thy.name, thy) }
    52     val graph1 =
    53       (graph0 /: thys) { case (g0, thy) =>
    54         (g0 /: thy.parents) { case (g1, parent) =>
    55           g1.default_node(parent, empty_theory(parent)).add_edge_acyclic(parent, thy.name) } }
    56 
    57     Session(session_name, graph1)
    58   }
    59 
    60 
    61 
    62   /** theory content **/
    63 
    64   val export_prefix: String = "theory/"
    65 
    66   sealed case class Theory(name: String, parents: List[String],
    67     types: List[Type],
    68     consts: List[Const],
    69     axioms: List[Axiom],
    70     facts: List[Fact],
    71     classes: List[Class],
    72     typedefs: List[Typedef],
    73     classrel: List[Classrel],
    74     arities: List[Arity])
    75   {
    76     override def toString: String = name
    77 
    78     lazy val entities: Set[Long] =
    79       Set.empty[Long] ++
    80         types.iterator.map(_.entity.serial) ++
    81         consts.iterator.map(_.entity.serial) ++
    82         axioms.iterator.map(_.entity.serial) ++
    83         facts.iterator.map(_.entity.serial) ++
    84         classes.iterator.map(_.entity.serial)
    85 
    86     def cache(cache: Term.Cache): Theory =
    87       Theory(cache.string(name),
    88         parents.map(cache.string(_)),
    89         types.map(_.cache(cache)),
    90         consts.map(_.cache(cache)),
    91         axioms.map(_.cache(cache)),
    92         facts.map(_.cache(cache)),
    93         classes.map(_.cache(cache)),
    94         typedefs.map(_.cache(cache)),
    95         classrel.map(_.cache(cache)),
    96         arities.map(_.cache(cache)))
    97   }
    98 
    99   def empty_theory(name: String): Theory =
   100     Theory(name, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil)
   101 
   102   def read_theory(provider: Export.Provider, session_name: String, theory_name: String,
   103     types: Boolean = true,
   104     consts: Boolean = true,
   105     axioms: Boolean = true,
   106     facts: Boolean = true,
   107     classes: Boolean = true,
   108     typedefs: Boolean = true,
   109     classrel: Boolean = true,
   110     arities: Boolean = true,
   111     cache: Option[Term.Cache] = None): Theory =
   112   {
   113     val parents =
   114       provider(export_prefix + "parents") match {
   115         case Some(entry) => split_lines(entry.uncompressed().text)
   116         case None =>
   117           error("Missing theory export in session " + quote(session_name) + ": " +
   118             quote(theory_name))
   119       }
   120     val theory =
   121       Theory(theory_name, parents,
   122         if (types) read_types(provider) else Nil,
   123         if (consts) read_consts(provider) else Nil,
   124         if (axioms) read_axioms(provider) else Nil,
   125         if (facts) read_facts(provider) else Nil,
   126         if (classes) read_classes(provider) else Nil,
   127         if (typedefs) read_typedefs(provider) else Nil,
   128         if (classrel) read_classrel(provider) else Nil,
   129         if (arities) read_arities(provider) else Nil)
   130     if (cache.isDefined) theory.cache(cache.get) else theory
   131   }
   132 
   133   def read_pure_theory(store: Sessions.Store, cache: Option[Term.Cache] = None): Theory =
   134   {
   135     val session_name = Thy_Header.PURE
   136     val theory_name = Thy_Header.PURE
   137 
   138     using(store.open_database(session_name))(db =>
   139     {
   140       db.transaction {
   141         read_theory(Export.Provider.database(db, session_name, theory_name),
   142           session_name, theory_name, cache = cache)
   143       }
   144     })
   145   }
   146 
   147 
   148   /* entities */
   149 
   150   object Kind extends Enumeration
   151   {
   152     val TYPE = Value("type")
   153     val CONST = Value("const")
   154     val AXIOM = Value("axiom")
   155     val FACT = Value("fact")
   156     val CLASS = Value("class")
   157   }
   158 
   159   sealed case class Entity(kind: Kind.Value, name: String, serial: Long, pos: Position.T)
   160   {
   161     def kind_name: (Kind.Value, String) = (kind, name)
   162 
   163     override def toString: String = kind.toString + quote(name)
   164 
   165     def cache(cache: Term.Cache): Entity =
   166       Entity(kind, cache.string(name), serial, cache.position(pos))
   167   }
   168 
   169   def decode_entity(kind: Kind.Value, tree: XML.Tree): (Entity, XML.Body) =
   170   {
   171     def err(): Nothing = throw new XML.XML_Body(List(tree))
   172 
   173     tree match {
   174       case XML.Elem(Markup(Markup.ENTITY, props), body) =>
   175         val name = Markup.Name.unapply(props) getOrElse err()
   176         val serial = Markup.Serial.unapply(props) getOrElse err()
   177         val pos = props.filter({ case (a, _) => Markup.POSITION_PROPERTIES(a) })
   178         (Entity(kind, name, serial, pos), body)
   179       case _ => err()
   180     }
   181   }
   182 
   183 
   184   /* types */
   185 
   186   sealed case class Type(entity: Entity, args: List[String], abbrev: Option[Term.Typ])
   187   {
   188     def cache(cache: Term.Cache): Type =
   189       Type(entity.cache(cache),
   190         args.map(cache.string(_)),
   191         abbrev.map(cache.typ(_)))
   192   }
   193 
   194   def read_types(provider: Export.Provider): List[Type] =
   195     provider.uncompressed_yxml(export_prefix + "types").map((tree: XML.Tree) =>
   196       {
   197         val (entity, body) = decode_entity(Kind.TYPE, tree)
   198         val (args, abbrev) =
   199         {
   200           import XML.Decode._
   201           pair(list(string), option(Term_XML.Decode.typ))(body)
   202         }
   203         Type(entity, args, abbrev)
   204       })
   205 
   206 
   207   /* consts */
   208 
   209   sealed case class Const(
   210     entity: Entity, typargs: List[String], typ: Term.Typ, abbrev: Option[Term.Term])
   211   {
   212     def cache(cache: Term.Cache): Const =
   213       Const(entity.cache(cache),
   214         typargs.map(cache.string(_)),
   215         cache.typ(typ),
   216         abbrev.map(cache.term(_)))
   217   }
   218 
   219   def read_consts(provider: Export.Provider): List[Const] =
   220     provider.uncompressed_yxml(export_prefix + "consts").map((tree: XML.Tree) =>
   221       {
   222         val (entity, body) = decode_entity(Kind.CONST, tree)
   223         val (args, typ, abbrev) =
   224         {
   225           import XML.Decode._
   226           triple(list(string), Term_XML.Decode.typ, option(Term_XML.Decode.term))(body)
   227         }
   228         Const(entity, args, typ, abbrev)
   229       })
   230 
   231 
   232   /* axioms and facts */
   233 
   234   def decode_props(body: XML.Body):
   235     (List[(String, Term.Sort)], List[(String, Term.Typ)], List[Term.Term]) =
   236   {
   237     import XML.Decode._
   238     import Term_XML.Decode._
   239     triple(list(pair(string, sort)), list(pair(string, typ)), list(term))(body)
   240   }
   241 
   242   sealed case class Axiom(
   243     entity: Entity,
   244     typargs: List[(String, Term.Sort)],
   245     args: List[(String, Term.Typ)],
   246     prop: Term.Term)
   247   {
   248     def cache(cache: Term.Cache): Axiom =
   249       Axiom(entity.cache(cache),
   250         typargs.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }),
   251         args.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
   252         cache.term(prop))
   253   }
   254 
   255   def read_axioms(provider: Export.Provider): List[Axiom] =
   256     provider.uncompressed_yxml(export_prefix + "axioms").map((tree: XML.Tree) =>
   257       {
   258         val (entity, body) = decode_entity(Kind.AXIOM, tree)
   259         val (typargs, args, List(prop)) = decode_props(body)
   260         Axiom(entity, typargs, args, prop)
   261       })
   262 
   263   sealed case class Fact(
   264     entity: Entity,
   265     typargs: List[(String, Term.Sort)],
   266     args: List[(String, Term.Typ)],
   267     props: List[Term.Term])
   268   {
   269     def cache(cache: Term.Cache): Fact =
   270       Fact(entity.cache(cache),
   271         typargs.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }),
   272         args.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
   273         props.map(cache.term(_)))
   274   }
   275 
   276   def read_facts(provider: Export.Provider): List[Fact] =
   277     provider.uncompressed_yxml(export_prefix + "facts").map((tree: XML.Tree) =>
   278       {
   279         val (entity, body) = decode_entity(Kind.FACT, tree)
   280         val (typargs, args, props) = decode_props(body)
   281         Fact(entity, typargs, args, props)
   282       })
   283 
   284 
   285   /* type classes */
   286 
   287   sealed case class Class(
   288     entity: Entity, params: List[(String, Term.Typ)], axioms: List[Term.Term])
   289   {
   290     def cache(cache: Term.Cache): Class =
   291       Class(entity.cache(cache),
   292         params.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
   293         axioms.map(cache.term(_)))
   294   }
   295 
   296   def read_classes(provider: Export.Provider): List[Class] =
   297     provider.uncompressed_yxml(export_prefix + "classes").map((tree: XML.Tree) =>
   298       {
   299         val (entity, body) = decode_entity(Kind.CLASS, tree)
   300         val (params, axioms) =
   301         {
   302           import XML.Decode._
   303           import Term_XML.Decode._
   304           pair(list(pair(string, typ)), list(term))(body)
   305         }
   306         Class(entity, params, axioms)
   307       })
   308 
   309 
   310   /* sort algebra */
   311 
   312   sealed case class Classrel(class_name: String, super_names: List[String])
   313   {
   314     def cache(cache: Term.Cache): Classrel =
   315       Classrel(cache.string(class_name), super_names.map(cache.string(_)))
   316   }
   317 
   318   def read_classrel(provider: Export.Provider): List[Classrel] =
   319   {
   320     val body = provider.uncompressed_yxml(export_prefix + "classrel")
   321     val classrel =
   322     {
   323       import XML.Decode._
   324       list(pair(string, list(string)))(body)
   325     }
   326     for ((c, cs) <- classrel) yield Classrel(c, cs)
   327   }
   328 
   329   sealed case class Arity(type_name: String, domain: List[Term.Sort], codomain: String)
   330   {
   331     def cache(cache: Term.Cache): Arity =
   332       Arity(cache.string(type_name), domain.map(cache.sort(_)), cache.string(codomain))
   333   }
   334 
   335   def read_arities(provider: Export.Provider): List[Arity] =
   336   {
   337     val body = provider.uncompressed_yxml(export_prefix + "arities")
   338     val arities =
   339     {
   340       import XML.Decode._
   341       import Term_XML.Decode._
   342       list(triple(string, list(sort), string))(body)
   343     }
   344     for ((a, b, c) <- arities) yield Arity(a, b, c)
   345   }
   346 
   347 
   348   /* HOL typedefs */
   349 
   350   sealed case class Typedef(name: String,
   351     rep_type: Term.Typ, abs_type: Term.Typ, rep_name: String, abs_name: String, axiom_name: String)
   352   {
   353     def cache(cache: Term.Cache): Typedef =
   354       Typedef(cache.string(name),
   355         cache.typ(rep_type),
   356         cache.typ(abs_type),
   357         cache.string(rep_name),
   358         cache.string(abs_name),
   359         cache.string(axiom_name))
   360   }
   361 
   362   def read_typedefs(provider: Export.Provider): List[Typedef] =
   363   {
   364     val body = provider.uncompressed_yxml(export_prefix + "typedefs")
   365     val typedefs =
   366     {
   367       import XML.Decode._
   368       import Term_XML.Decode._
   369       list(pair(string, pair(typ, pair(typ, pair(string, pair(string, string))))))(body)
   370     }
   371     for { (name, (rep_type, (abs_type, (rep_name, (abs_name, axiom_name))))) <- typedefs }
   372     yield Typedef(name, rep_type, abs_type, rep_name, abs_name, axiom_name)
   373   }
   374 }