src/Pure/Thy/export_theory.scala
author wenzelm
Fri May 18 17:21:12 2018 +0200 (14 months ago)
changeset 68210 65f79c0ddb0d
parent 68209 aeffd8f1f079
child 68222 3c1a716e7f59
permissions -rw-r--r--
tuned signature;
     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): Session =
    30   {
    31     val thys =
    32       using(store.open_database(session_name))(db =>
    33       {
    34         db.transaction {
    35           Export.read_theory_names(db, session_name).iterator.map(_._1).toSet.iterator.
    36             map((theory_name: String) =>
    37               read_theory(db, session_name, theory_name, types = types, consts = consts)).toList
    38         }
    39       })
    40 
    41     val graph0 =
    42       (Graph.string[Theory] /: thys) { case (g, thy) => g.new_node(thy.name, thy) }
    43     val graph1 =
    44       (graph0 /: thys) { case (g0, thy) =>
    45         (g0 /: thy.parents) { case (g1, parent) =>
    46           g1.default_node(parent, empty_theory(parent)).add_edge_acyclic(parent, thy.name) } }
    47 
    48     Session(session_name, graph1)
    49   }
    50 
    51 
    52 
    53   /** theory content **/
    54 
    55   sealed case class Theory(name: String, parents: List[String],
    56     types: List[Type],
    57     consts: List[Const],
    58     axioms: List[Axiom])
    59   {
    60     override def toString: String = name
    61   }
    62 
    63   def empty_theory(name: String): Theory = Theory(name, Nil, Nil, Nil, Nil)
    64 
    65   def read_theory(db: SQL.Database, session_name: String, theory_name: String,
    66     types: Boolean = true,
    67     consts: Boolean = true,
    68     axioms: Boolean = true): Theory =
    69   {
    70     val parents =
    71       Export.read_entry(db, session_name, theory_name, "theory/parents") match {
    72         case Some(entry) =>
    73           import XML.Decode._
    74           list(string)(entry.uncompressed_yxml())
    75         case None =>
    76           error("Missing theory export in session " + quote(session_name) + ": " +
    77             quote(theory_name))
    78       }
    79     Theory(theory_name, parents,
    80       if (types) read_types(db, session_name, theory_name) else Nil,
    81       if (consts) read_consts(db, session_name, theory_name) else Nil,
    82       if (axioms) read_axioms(db, session_name, theory_name) else Nil)
    83   }
    84 
    85 
    86   /* entities */
    87 
    88   sealed case class Entity(name: String, serial: Long, pos: Position.T)
    89   {
    90     override def toString: String = name
    91   }
    92 
    93   def decode_entity(tree: XML.Tree): (Entity, XML.Body) =
    94   {
    95     def err(): Nothing = throw new XML.XML_Body(List(tree))
    96 
    97     tree match {
    98       case XML.Elem(Markup(Markup.ENTITY, props), body) =>
    99         val name = Markup.Name.unapply(props) getOrElse err()
   100         val serial = Markup.Serial.unapply(props) getOrElse err()
   101         val pos = props.filter({ case (a, _) => Markup.POSITION_PROPERTIES(a) })
   102         (Entity(name, serial, pos), body)
   103       case _ => err()
   104     }
   105   }
   106 
   107   def read_entities[A](db: SQL.Database, session_name: String, theory_name: String,
   108     export_name: String, decode: XML.Tree => A): List[A] =
   109   {
   110     Export.read_entry(db, session_name, theory_name, "theory/" + export_name) match {
   111       case Some(entry) => entry.uncompressed_yxml().map(decode(_))
   112       case None => Nil
   113     }
   114   }
   115 
   116 
   117   /* types */
   118 
   119   sealed case class Type(entity: Entity, args: List[String], abbrev: Option[Term.Typ])
   120 
   121   def read_types(db: SQL.Database, session_name: String, theory_name: String): List[Type] =
   122     read_entities(db, session_name, theory_name, "types",
   123       (tree: XML.Tree) =>
   124         {
   125           val (entity, body) = decode_entity(tree)
   126           val (args, abbrev) =
   127           {
   128             import XML.Decode._
   129             pair(list(string), option(Term_XML.Decode.typ))(body)
   130           }
   131           Type(entity, args, abbrev)
   132         })
   133 
   134 
   135   /* consts */
   136 
   137   sealed case class Const(
   138     entity: Entity, typargs: List[String], typ: Term.Typ, abbrev: Option[Term.Term])
   139 
   140   def read_consts(db: SQL.Database, session_name: String, theory_name: String): List[Const] =
   141     read_entities(db, session_name, theory_name, "consts",
   142       (tree: XML.Tree) =>
   143         {
   144           val (entity, body) = decode_entity(tree)
   145           val (args, typ, abbrev) =
   146           {
   147             import XML.Decode._
   148             triple(list(string), Term_XML.Decode.typ, option(Term_XML.Decode.term))(body)
   149           }
   150           Const(entity, args, typ, abbrev)
   151         })
   152 
   153 
   154   /* axioms */
   155 
   156   sealed case class Axiom(
   157     entity: Entity,
   158     typargs: List[(String, Term.Sort)],
   159     args: List[(String, Term.Typ)],
   160     prop: Term.Term)
   161 
   162   def read_axioms(db: SQL.Database, session_name: String, theory_name: String): List[Axiom] =
   163     read_entities(db, session_name, theory_name, "axioms",
   164       (tree: XML.Tree) =>
   165         {
   166           val (entity, body) = decode_entity(tree)
   167           val (typargs, args, prop) =
   168           {
   169             import XML.Decode._
   170             import Term_XML.Decode._
   171             triple(list(pair(string, sort)), list(pair(string, typ)), term)(body)
   172           }
   173           Axiom(entity, typargs, args, prop)
   174         })
   175 }