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