src/Pure/Thy/export_theory.scala
author wenzelm
Sun, 20 May 2018 15:37:16 +0200
changeset 68231 0004e7a9fa10
parent 68222 3c1a716e7f59
child 68232 4b93573ac5b4
permissions -rw-r--r--
clarified encoding;
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 {
68231
0004e7a9fa10 clarified encoding;
wenzelm
parents: 68222
diff changeset
    71
        case Some(entry) => split_lines(entry.uncompressed().text)
68206
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    72
        case None =>
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    73
          error("Missing theory export in session " + quote(session_name) + ": " +
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    74
            quote(theory_name))
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    75
      }
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    76
    Theory(theory_name, parents,
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    77
      if (types) read_types(db, session_name, theory_name) else Nil,
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
    78
      if (consts) read_consts(db, session_name, theory_name) else Nil,
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
    79
      if (axioms) read_axioms(db, session_name, theory_name) else Nil)
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    80
  }
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    81
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    82
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    83
  /* entities */
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    84
68172
0f14cf9c632f more concise information;
wenzelm
parents: 68171
diff changeset
    85
  sealed case class Entity(name: String, serial: Long, pos: Position.T)
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    86
  {
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    87
    override def toString: String = name
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    88
  }
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    89
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    90
  def decode_entity(tree: XML.Tree): (Entity, XML.Body) =
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 err(): Nothing = throw new XML.XML_Body(List(tree))
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    93
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    94
    tree match {
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    95
      case XML.Elem(Markup(Markup.ENTITY, props), body) =>
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    96
        val name = Markup.Name.unapply(props) getOrElse err()
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    97
        val serial = Markup.Serial.unapply(props) getOrElse err()
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    98
        val pos = props.filter({ case (a, _) => Markup.POSITION_PROPERTIES(a) })
68172
0f14cf9c632f more concise information;
wenzelm
parents: 68171
diff changeset
    99
        (Entity(name, serial, pos), body)
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   100
      case _ => err()
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   101
    }
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   102
  }
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   103
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   104
  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
   105
    export_name: String, decode: XML.Tree => A): List[A] =
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   106
  {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   107
    Export.read_entry(db, session_name, theory_name, "theory/" + export_name) match {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   108
      case Some(entry) => entry.uncompressed_yxml().map(decode(_))
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   109
      case None => Nil
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   110
    }
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   111
  }
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   112
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   113
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   114
  /* types */
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   115
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   116
  sealed case class Type(entity: Entity, args: List[String], abbrev: Option[Term.Typ])
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   117
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   118
  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
   119
    read_entities(db, session_name, theory_name, "types",
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   120
      (tree: XML.Tree) =>
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   121
        {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   122
          val (entity, body) = decode_entity(tree)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   123
          val (args, abbrev) =
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
            import XML.Decode._
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   126
            pair(list(string), option(Term_XML.Decode.typ))(body)
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
          Type(entity, args, abbrev)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   129
        })
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   130
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   131
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   132
  /* consts */
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   133
68173
7ed88a534bb6 more uniform types vs. consts;
wenzelm
parents: 68172
diff changeset
   134
  sealed case class Const(
7ed88a534bb6 more uniform types vs. consts;
wenzelm
parents: 68172
diff changeset
   135
    entity: Entity, typargs: List[String], typ: Term.Typ, abbrev: Option[Term.Term])
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   136
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   137
  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
   138
    read_entities(db, session_name, theory_name, "consts",
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   139
      (tree: XML.Tree) =>
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   140
        {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   141
          val (entity, body) = decode_entity(tree)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   142
          val (args, typ, abbrev) =
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
            import XML.Decode._
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   145
            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
   146
          }
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   147
          Const(entity, args, typ, abbrev)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   148
        })
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   149
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   150
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   151
  /* axioms */
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   152
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   153
  sealed case class Axiom(
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   154
    entity: Entity,
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   155
    typargs: List[(String, Term.Sort)],
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   156
    args: List[(String, Term.Typ)],
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   157
    prop: Term.Term)
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   158
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   159
  def read_axioms(db: SQL.Database, session_name: String, theory_name: String): List[Axiom] =
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   160
    read_entities(db, session_name, theory_name, "axioms",
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   161
      (tree: XML.Tree) =>
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   162
        {
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   163
          val (entity, body) = decode_entity(tree)
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   164
          val (typargs, args, prop) =
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   165
          {
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   166
            import XML.Decode._
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   167
            import Term_XML.Decode._
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   168
            triple(list(pair(string, sort)), list(pair(string, typ)), term)(body)
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   169
          }
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   170
          Axiom(entity, typargs, args, prop)
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   171
        })
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   172
}