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