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