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