src/Pure/Thy/export_theory.scala
author wenzelm
Thu, 17 May 2018 17:29:17 +0200
changeset 68206 dedf1a70d1fa
parent 68203 cda4f24331d5
child 68208 d9f2cf4fc002
permissions -rw-r--r--
export more theory and session structure;
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
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    26
  def read_session(session_name: String,
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    27
    system_mode: Boolean = false,
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 store = Sessions.store(system_mode)
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    32
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    33
    val thys =
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    34
      using(SQLite.open_database(store.the_database(session_name)))(db =>
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    35
      {
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    36
        db.transaction {
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    37
          Export.read_theory_names(db, session_name).iterator.map(_._1).toSet.iterator.
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    38
            map((theory_name: String) =>
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    39
              read_theory(db, session_name, theory_name, types = types, consts = consts)).toList
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
      })
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    42
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    43
    val graph0 =
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    44
      (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
    45
    val graph1 =
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    46
      (graph0 /: thys) { case (g0, thy) =>
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    47
        (g0 /: thy.parents) { case (g1, parent) =>
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    48
          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
    49
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    50
    Session(session_name, graph1)
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
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    53
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    54
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    55
  /** theory content **/
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    56
68206
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    57
  sealed case class Theory(
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    58
    name: String, parents: List[String], types: List[Type], consts: List[Const])
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
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68203
diff changeset
    63
  def empty_theory(name: String): Theory = Theory(name, 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,
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    67
    consts: Boolean = true): Theory =
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,
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    80
      if (consts) read_consts(db, session_name, theory_name) else Nil)
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
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
    83
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    84
  /* entities */
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    85
68172
0f14cf9c632f more concise information;
wenzelm
parents: 68171
diff changeset
    86
  sealed case class Entity(name: String, serial: Long, pos: Position.T)
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    87
  {
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    88
    override def toString: String = name
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    89
  }
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    90
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    91
  def decode_entity(tree: XML.Tree): (Entity, XML.Body) =
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 err(): Nothing = throw new XML.XML_Body(List(tree))
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    94
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    95
    tree match {
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    96
      case XML.Elem(Markup(Markup.ENTITY, props), body) =>
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    97
        val name = Markup.Name.unapply(props) getOrElse err()
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    98
        val serial = Markup.Serial.unapply(props) getOrElse err()
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
    99
        val pos = props.filter({ case (a, _) => Markup.POSITION_PROPERTIES(a) })
68172
0f14cf9c632f more concise information;
wenzelm
parents: 68171
diff changeset
   100
        (Entity(name, serial, pos), body)
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   101
      case _ => err()
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   102
    }
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   103
  }
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   104
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   105
  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
   106
    export_name: String, decode: XML.Tree => A): List[A] =
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   107
  {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   108
    Export.read_entry(db, session_name, theory_name, "theory/" + export_name) match {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   109
      case Some(entry) => entry.uncompressed_yxml().map(decode(_))
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   110
      case None => Nil
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
  }
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   113
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   114
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   115
  /* types */
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   116
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   117
  sealed case class Type(entity: Entity, args: List[String], abbrev: Option[Term.Typ])
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   118
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   119
  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
   120
    read_entities(db, session_name, theory_name, "types",
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   121
      (tree: XML.Tree) =>
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   122
        {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   123
          val (entity, body) = decode_entity(tree)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   124
          val (args, abbrev) =
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   125
          {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   126
            import XML.Decode._
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   127
            pair(list(string), option(Term_XML.Decode.typ))(body)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   128
          }
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   129
          Type(entity, args, abbrev)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   130
        })
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   131
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   132
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   133
  /* consts */
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   134
68173
7ed88a534bb6 more uniform types vs. consts;
wenzelm
parents: 68172
diff changeset
   135
  sealed case class Const(
7ed88a534bb6 more uniform types vs. consts;
wenzelm
parents: 68172
diff changeset
   136
    entity: Entity, typargs: List[String], typ: Term.Typ, abbrev: Option[Term.Term])
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   137
68203
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   138
  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
   139
    read_entities(db, session_name, theory_name, "consts",
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   140
      (tree: XML.Tree) =>
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   141
        {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   142
          val (entity, body) = decode_entity(tree)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   143
          val (args, typ, abbrev) =
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   144
          {
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   145
            import XML.Decode._
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   146
            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
   147
          }
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   148
          Const(entity, args, typ, abbrev)
cda4f24331d5 read theory content from session database;
wenzelm
parents: 68173
diff changeset
   149
        })
68171
13162bb3a677 export foundational theory content in Scala;
wenzelm
parents:
diff changeset
   150
}