src/Pure/Thy/export_theory.scala
author wenzelm
Mon Jun 11 18:05:43 2018 +0200 (13 months ago)
changeset 68418 366e43cddd20
parent 68346 b44010800a19
child 68710 3db37e950118
permissions -rw-r--r--
more abstract Export.Provider;
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@68209
    26
  def read_session(store: Sessions.Store,
wenzelm@68209
    27
    session_name: String,
wenzelm@68206
    28
    types: Boolean = true,
wenzelm@68264
    29
    consts: Boolean = true,
wenzelm@68264
    30
    axioms: Boolean = true,
wenzelm@68264
    31
    facts: Boolean = true,
wenzelm@68264
    32
    classes: Boolean = true,
wenzelm@68267
    33
    typedefs: Boolean = true,
wenzelm@68295
    34
    classrel: Boolean = true,
wenzelm@68295
    35
    arities: Boolean = true,
wenzelm@68267
    36
    cache: Term.Cache = Term.make_cache()): Session =
wenzelm@68206
    37
  {
wenzelm@68206
    38
    val thys =
wenzelm@68210
    39
      using(store.open_database(session_name))(db =>
wenzelm@68206
    40
      {
wenzelm@68206
    41
        db.transaction {
wenzelm@68222
    42
          Export.read_theory_names(db, session_name).map((theory_name: String) =>
wenzelm@68418
    43
            read_theory(Export.Provider.database(db, session_name, theory_name),
wenzelm@68418
    44
              session_name, theory_name, types = types, consts = consts,
wenzelm@68267
    45
              axioms = axioms, facts = facts, classes = classes, typedefs = typedefs,
wenzelm@68267
    46
              cache = Some(cache)))
wenzelm@68206
    47
        }
wenzelm@68206
    48
      })
wenzelm@68206
    49
wenzelm@68206
    50
    val graph0 =
wenzelm@68206
    51
      (Graph.string[Theory] /: thys) { case (g, thy) => g.new_node(thy.name, thy) }
wenzelm@68206
    52
    val graph1 =
wenzelm@68206
    53
      (graph0 /: thys) { case (g0, thy) =>
wenzelm@68206
    54
        (g0 /: thy.parents) { case (g1, parent) =>
wenzelm@68206
    55
          g1.default_node(parent, empty_theory(parent)).add_edge_acyclic(parent, thy.name) } }
wenzelm@68206
    56
wenzelm@68206
    57
    Session(session_name, graph1)
wenzelm@68206
    58
  }
wenzelm@68206
    59
wenzelm@68206
    60
wenzelm@68206
    61
wenzelm@68203
    62
  /** theory content **/
wenzelm@68203
    63
wenzelm@68346
    64
  val export_prefix: String = "theory/"
wenzelm@68346
    65
wenzelm@68208
    66
  sealed case class Theory(name: String, parents: List[String],
wenzelm@68208
    67
    types: List[Type],
wenzelm@68208
    68
    consts: List[Const],
wenzelm@68232
    69
    axioms: List[Axiom],
wenzelm@68264
    70
    facts: List[Fact],
wenzelm@68264
    71
    classes: List[Class],
wenzelm@68295
    72
    typedefs: List[Typedef],
wenzelm@68295
    73
    classrel: List[Classrel],
wenzelm@68295
    74
    arities: List[Arity])
wenzelm@68206
    75
  {
wenzelm@68206
    76
    override def toString: String = name
wenzelm@68267
    77
wenzelm@68267
    78
    def cache(cache: Term.Cache): Theory =
wenzelm@68267
    79
      Theory(cache.string(name),
wenzelm@68267
    80
        parents.map(cache.string(_)),
wenzelm@68267
    81
        types.map(_.cache(cache)),
wenzelm@68267
    82
        consts.map(_.cache(cache)),
wenzelm@68267
    83
        axioms.map(_.cache(cache)),
wenzelm@68267
    84
        facts.map(_.cache(cache)),
wenzelm@68267
    85
        classes.map(_.cache(cache)),
wenzelm@68295
    86
        typedefs.map(_.cache(cache)),
wenzelm@68295
    87
        classrel.map(_.cache(cache)),
wenzelm@68295
    88
        arities.map(_.cache(cache)))
wenzelm@68206
    89
  }
wenzelm@68206
    90
wenzelm@68295
    91
  def empty_theory(name: String): Theory =
wenzelm@68295
    92
    Theory(name, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil)
wenzelm@68203
    93
wenzelm@68418
    94
  def read_theory(provider: Export.Provider, session_name: String, theory_name: String,
wenzelm@68203
    95
    types: Boolean = true,
wenzelm@68208
    96
    consts: Boolean = true,
wenzelm@68232
    97
    axioms: Boolean = true,
wenzelm@68264
    98
    facts: Boolean = true,
wenzelm@68264
    99
    classes: Boolean = true,
wenzelm@68267
   100
    typedefs: Boolean = true,
wenzelm@68295
   101
    classrel: Boolean = true,
wenzelm@68295
   102
    arities: Boolean = true,
wenzelm@68267
   103
    cache: Option[Term.Cache] = None): Theory =
wenzelm@68203
   104
  {
wenzelm@68206
   105
    val parents =
wenzelm@68418
   106
      provider(export_prefix + "parents") match {
wenzelm@68231
   107
        case Some(entry) => split_lines(entry.uncompressed().text)
wenzelm@68206
   108
        case None =>
wenzelm@68206
   109
          error("Missing theory export in session " + quote(session_name) + ": " +
wenzelm@68206
   110
            quote(theory_name))
wenzelm@68206
   111
      }
wenzelm@68267
   112
    val theory =
wenzelm@68267
   113
      Theory(theory_name, parents,
wenzelm@68418
   114
        if (types) read_types(provider) else Nil,
wenzelm@68418
   115
        if (consts) read_consts(provider) else Nil,
wenzelm@68418
   116
        if (axioms) read_axioms(provider) else Nil,
wenzelm@68418
   117
        if (facts) read_facts(provider) else Nil,
wenzelm@68418
   118
        if (classes) read_classes(provider) else Nil,
wenzelm@68418
   119
        if (typedefs) read_typedefs(provider) else Nil,
wenzelm@68418
   120
        if (classrel) read_classrel(provider) else Nil,
wenzelm@68418
   121
        if (arities) read_arities(provider) else Nil)
wenzelm@68267
   122
    if (cache.isDefined) theory.cache(cache.get) else theory
wenzelm@68203
   123
  }
wenzelm@68203
   124
wenzelm@68203
   125
wenzelm@68171
   126
  /* entities */
wenzelm@68171
   127
wenzelm@68172
   128
  sealed case class Entity(name: String, serial: Long, pos: Position.T)
wenzelm@68171
   129
  {
wenzelm@68171
   130
    override def toString: String = name
wenzelm@68267
   131
wenzelm@68267
   132
    def cache(cache: Term.Cache): Entity =
wenzelm@68267
   133
      Entity(cache.string(name), serial, cache.position(pos))
wenzelm@68171
   134
  }
wenzelm@68171
   135
wenzelm@68171
   136
  def decode_entity(tree: XML.Tree): (Entity, XML.Body) =
wenzelm@68171
   137
  {
wenzelm@68171
   138
    def err(): Nothing = throw new XML.XML_Body(List(tree))
wenzelm@68171
   139
wenzelm@68171
   140
    tree match {
wenzelm@68171
   141
      case XML.Elem(Markup(Markup.ENTITY, props), body) =>
wenzelm@68171
   142
        val name = Markup.Name.unapply(props) getOrElse err()
wenzelm@68171
   143
        val serial = Markup.Serial.unapply(props) getOrElse err()
wenzelm@68171
   144
        val pos = props.filter({ case (a, _) => Markup.POSITION_PROPERTIES(a) })
wenzelm@68172
   145
        (Entity(name, serial, pos), body)
wenzelm@68171
   146
      case _ => err()
wenzelm@68171
   147
    }
wenzelm@68171
   148
  }
wenzelm@68171
   149
wenzelm@68171
   150
wenzelm@68171
   151
  /* types */
wenzelm@68171
   152
wenzelm@68171
   153
  sealed case class Type(entity: Entity, args: List[String], abbrev: Option[Term.Typ])
wenzelm@68267
   154
  {
wenzelm@68267
   155
    def cache(cache: Term.Cache): Type =
wenzelm@68267
   156
      Type(entity.cache(cache),
wenzelm@68267
   157
        args.map(cache.string(_)),
wenzelm@68267
   158
        abbrev.map(cache.typ(_)))
wenzelm@68267
   159
  }
wenzelm@68171
   160
wenzelm@68418
   161
  def read_types(provider: Export.Provider): List[Type] =
wenzelm@68418
   162
    provider.uncompressed_yxml(export_prefix + "types").map((tree: XML.Tree) =>
wenzelm@68418
   163
      {
wenzelm@68418
   164
        val (entity, body) = decode_entity(tree)
wenzelm@68418
   165
        val (args, abbrev) =
wenzelm@68203
   166
        {
wenzelm@68418
   167
          import XML.Decode._
wenzelm@68418
   168
          pair(list(string), option(Term_XML.Decode.typ))(body)
wenzelm@68418
   169
        }
wenzelm@68418
   170
        Type(entity, args, abbrev)
wenzelm@68418
   171
      })
wenzelm@68171
   172
wenzelm@68171
   173
wenzelm@68171
   174
  /* consts */
wenzelm@68171
   175
wenzelm@68173
   176
  sealed case class Const(
wenzelm@68173
   177
    entity: Entity, typargs: List[String], typ: Term.Typ, abbrev: Option[Term.Term])
wenzelm@68267
   178
  {
wenzelm@68267
   179
    def cache(cache: Term.Cache): Const =
wenzelm@68267
   180
      Const(entity.cache(cache),
wenzelm@68267
   181
        typargs.map(cache.string(_)),
wenzelm@68267
   182
        cache.typ(typ),
wenzelm@68267
   183
        abbrev.map(cache.term(_)))
wenzelm@68267
   184
  }
wenzelm@68171
   185
wenzelm@68418
   186
  def read_consts(provider: Export.Provider): List[Const] =
wenzelm@68418
   187
    provider.uncompressed_yxml(export_prefix + "consts").map((tree: XML.Tree) =>
wenzelm@68418
   188
      {
wenzelm@68418
   189
        val (entity, body) = decode_entity(tree)
wenzelm@68418
   190
        val (args, typ, abbrev) =
wenzelm@68203
   191
        {
wenzelm@68418
   192
          import XML.Decode._
wenzelm@68418
   193
          triple(list(string), Term_XML.Decode.typ, option(Term_XML.Decode.term))(body)
wenzelm@68418
   194
        }
wenzelm@68418
   195
        Const(entity, args, typ, abbrev)
wenzelm@68418
   196
      })
wenzelm@68208
   197
wenzelm@68208
   198
wenzelm@68232
   199
  /* axioms and facts */
wenzelm@68232
   200
wenzelm@68232
   201
  def decode_props(body: XML.Body):
wenzelm@68232
   202
    (List[(String, Term.Sort)], List[(String, Term.Typ)], List[Term.Term]) =
wenzelm@68232
   203
  {
wenzelm@68232
   204
    import XML.Decode._
wenzelm@68232
   205
    import Term_XML.Decode._
wenzelm@68232
   206
    triple(list(pair(string, sort)), list(pair(string, typ)), list(term))(body)
wenzelm@68232
   207
  }
wenzelm@68208
   208
wenzelm@68208
   209
  sealed case class Axiom(
wenzelm@68208
   210
    entity: Entity,
wenzelm@68208
   211
    typargs: List[(String, Term.Sort)],
wenzelm@68208
   212
    args: List[(String, Term.Typ)],
wenzelm@68208
   213
    prop: Term.Term)
wenzelm@68267
   214
  {
wenzelm@68267
   215
    def cache(cache: Term.Cache): Axiom =
wenzelm@68267
   216
      Axiom(entity.cache(cache),
wenzelm@68267
   217
        typargs.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }),
wenzelm@68267
   218
        args.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
wenzelm@68267
   219
        cache.term(prop))
wenzelm@68267
   220
  }
wenzelm@68208
   221
wenzelm@68418
   222
  def read_axioms(provider: Export.Provider): List[Axiom] =
wenzelm@68418
   223
    provider.uncompressed_yxml(export_prefix + "axioms").map((tree: XML.Tree) =>
wenzelm@68418
   224
      {
wenzelm@68418
   225
        val (entity, body) = decode_entity(tree)
wenzelm@68418
   226
        val (typargs, args, List(prop)) = decode_props(body)
wenzelm@68418
   227
        Axiom(entity, typargs, args, prop)
wenzelm@68418
   228
      })
wenzelm@68232
   229
wenzelm@68232
   230
  sealed case class Fact(
wenzelm@68232
   231
    entity: Entity,
wenzelm@68232
   232
    typargs: List[(String, Term.Sort)],
wenzelm@68232
   233
    args: List[(String, Term.Typ)],
wenzelm@68232
   234
    props: List[Term.Term])
wenzelm@68267
   235
  {
wenzelm@68267
   236
    def cache(cache: Term.Cache): Fact =
wenzelm@68267
   237
      Fact(entity.cache(cache),
wenzelm@68267
   238
        typargs.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }),
wenzelm@68267
   239
        args.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
wenzelm@68267
   240
        props.map(cache.term(_)))
wenzelm@68267
   241
  }
wenzelm@68232
   242
wenzelm@68418
   243
  def read_facts(provider: Export.Provider): List[Fact] =
wenzelm@68418
   244
    provider.uncompressed_yxml(export_prefix + "facts").map((tree: XML.Tree) =>
wenzelm@68418
   245
      {
wenzelm@68418
   246
        val (entity, body) = decode_entity(tree)
wenzelm@68418
   247
        val (typargs, args, props) = decode_props(body)
wenzelm@68418
   248
        Fact(entity, typargs, args, props)
wenzelm@68418
   249
      })
wenzelm@68264
   250
wenzelm@68264
   251
wenzelm@68264
   252
  /* type classes */
wenzelm@68264
   253
wenzelm@68264
   254
  sealed case class Class(
wenzelm@68264
   255
    entity: Entity, params: List[(String, Term.Typ)], axioms: List[Term.Term])
wenzelm@68267
   256
  {
wenzelm@68267
   257
    def cache(cache: Term.Cache): Class =
wenzelm@68267
   258
      Class(entity.cache(cache),
wenzelm@68267
   259
        params.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
wenzelm@68267
   260
        axioms.map(cache.term(_)))
wenzelm@68267
   261
  }
wenzelm@68264
   262
wenzelm@68418
   263
  def read_classes(provider: Export.Provider): List[Class] =
wenzelm@68418
   264
    provider.uncompressed_yxml(export_prefix + "classes").map((tree: XML.Tree) =>
wenzelm@68418
   265
      {
wenzelm@68418
   266
        val (entity, body) = decode_entity(tree)
wenzelm@68418
   267
        val (params, axioms) =
wenzelm@68264
   268
        {
wenzelm@68418
   269
          import XML.Decode._
wenzelm@68418
   270
          import Term_XML.Decode._
wenzelm@68418
   271
          pair(list(pair(string, typ)), list(term))(body)
wenzelm@68418
   272
        }
wenzelm@68418
   273
        Class(entity, params, axioms)
wenzelm@68418
   274
      })
wenzelm@68264
   275
wenzelm@68264
   276
wenzelm@68295
   277
  /* sort algebra */
wenzelm@68295
   278
wenzelm@68295
   279
  sealed case class Classrel(class_name: String, super_names: List[String])
wenzelm@68295
   280
  {
wenzelm@68295
   281
    def cache(cache: Term.Cache): Classrel =
wenzelm@68295
   282
      Classrel(cache.string(class_name), super_names.map(cache.string(_)))
wenzelm@68295
   283
  }
wenzelm@68295
   284
wenzelm@68418
   285
  def read_classrel(provider: Export.Provider): List[Classrel] =
wenzelm@68418
   286
  {
wenzelm@68418
   287
    val body = provider.uncompressed_yxml(export_prefix + "classrel")
wenzelm@68418
   288
    val classrel =
wenzelm@68418
   289
    {
wenzelm@68418
   290
      import XML.Decode._
wenzelm@68418
   291
      list(pair(string, list(string)))(body)
wenzelm@68418
   292
    }
wenzelm@68418
   293
    for ((c, cs) <- classrel) yield Classrel(c, cs)
wenzelm@68418
   294
  }
wenzelm@68295
   295
wenzelm@68295
   296
  sealed case class Arity(type_name: String, domain: List[Term.Sort], codomain: String)
wenzelm@68295
   297
  {
wenzelm@68295
   298
    def cache(cache: Term.Cache): Arity =
wenzelm@68295
   299
      Arity(cache.string(type_name), domain.map(cache.sort(_)), cache.string(codomain))
wenzelm@68295
   300
  }
wenzelm@68295
   301
wenzelm@68418
   302
  def read_arities(provider: Export.Provider): List[Arity] =
wenzelm@68418
   303
  {
wenzelm@68418
   304
    val body = provider.uncompressed_yxml(export_prefix + "arities")
wenzelm@68418
   305
    val arities =
wenzelm@68418
   306
    {
wenzelm@68418
   307
      import XML.Decode._
wenzelm@68418
   308
      import Term_XML.Decode._
wenzelm@68418
   309
      list(triple(string, list(sort), string))(body)
wenzelm@68418
   310
    }
wenzelm@68418
   311
    for ((a, b, c) <- arities) yield Arity(a, b, c)
wenzelm@68418
   312
  }
wenzelm@68295
   313
wenzelm@68295
   314
wenzelm@68264
   315
  /* HOL typedefs */
wenzelm@68264
   316
wenzelm@68264
   317
  sealed case class Typedef(name: String,
wenzelm@68264
   318
    rep_type: Term.Typ, abs_type: Term.Typ, rep_name: String, abs_name: String, axiom_name: String)
wenzelm@68267
   319
  {
wenzelm@68267
   320
    def cache(cache: Term.Cache): Typedef =
wenzelm@68267
   321
      Typedef(cache.string(name),
wenzelm@68267
   322
        cache.typ(rep_type),
wenzelm@68267
   323
        cache.typ(abs_type),
wenzelm@68267
   324
        cache.string(rep_name),
wenzelm@68267
   325
        cache.string(abs_name),
wenzelm@68267
   326
        cache.string(axiom_name))
wenzelm@68267
   327
  }
wenzelm@68264
   328
wenzelm@68418
   329
  def read_typedefs(provider: Export.Provider): List[Typedef] =
wenzelm@68418
   330
  {
wenzelm@68418
   331
    val body = provider.uncompressed_yxml(export_prefix + "typedefs")
wenzelm@68418
   332
    val typedefs =
wenzelm@68418
   333
    {
wenzelm@68418
   334
      import XML.Decode._
wenzelm@68418
   335
      import Term_XML.Decode._
wenzelm@68418
   336
      list(pair(string, pair(typ, pair(typ, pair(string, pair(string, string))))))(body)
wenzelm@68418
   337
    }
wenzelm@68418
   338
    for { (name, (rep_type, (abs_type, (rep_name, (abs_name, axiom_name))))) <- typedefs }
wenzelm@68418
   339
    yield Typedef(name, rep_type, abs_type, rep_name, abs_name, axiom_name)
wenzelm@68418
   340
  }
wenzelm@68171
   341
}