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