src/Pure/Thy/export_theory.scala
author wenzelm
Fri Aug 31 15:48:37 2018 +0200 (10 months ago)
changeset 68862 47e9912c53c3
parent 68835 2e59da922630
child 68864 1dacce27bc25
permissions -rw-r--r--
export locale content;
read_theory_names: proper classrel, arities;
tuned signature;
     1 /*  Title:      Pure/Thy/export_theory.scala
     2     Author:     Makarius
     3 
     4 Export foundational theory content.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Export_Theory
    11 {
    12   /** session content **/
    13 
    14   sealed case class Session(name: String, theory_graph: Graph[String, Theory])
    15   {
    16     override def toString: String = name
    17 
    18     def theory(theory_name: String): Theory =
    19       if (theory_graph.defined(theory_name)) theory_graph.get_node(theory_name)
    20       else error("Bad theory " + quote(theory_name))
    21 
    22     def theories: List[Theory] =
    23       theory_graph.topological_order.map(theory_graph.get_node(_))
    24   }
    25 
    26   def read_session(store: Sessions.Store,
    27     session_name: String,
    28     types: Boolean = true,
    29     consts: Boolean = true,
    30     axioms: Boolean = true,
    31     facts: Boolean = true,
    32     classes: Boolean = true,
    33     locales: Boolean = true,
    34     classrel: Boolean = true,
    35     arities: Boolean = true,
    36     typedefs: Boolean = true,
    37     cache: Term.Cache = Term.make_cache()): Session =
    38   {
    39     val thys =
    40       using(store.open_database(session_name))(db =>
    41       {
    42         db.transaction {
    43           Export.read_theory_names(db, session_name).map((theory_name: String) =>
    44             read_theory(Export.Provider.database(db, session_name, theory_name),
    45               session_name, theory_name, types = types, consts = consts,
    46               axioms = axioms, facts = facts, classes = classes, locales = locales,
    47               classrel = classrel, arities = arities, typedefs = typedefs,
    48               cache = Some(cache)))
    49         }
    50       })
    51 
    52     val graph0 =
    53       (Graph.string[Theory] /: thys) { case (g, thy) => g.new_node(thy.name, thy) }
    54     val graph1 =
    55       (graph0 /: thys) { case (g0, thy) =>
    56         (g0 /: thy.parents) { case (g1, parent) =>
    57           g1.default_node(parent, empty_theory(parent)).add_edge_acyclic(parent, thy.name) } }
    58 
    59     Session(session_name, graph1)
    60   }
    61 
    62 
    63 
    64   /** theory content **/
    65 
    66   val export_prefix: String = "theory/"
    67 
    68   sealed case class Theory(name: String, parents: List[String],
    69     types: List[Type],
    70     consts: List[Const],
    71     axioms: List[Fact_Single],
    72     facts: List[Fact_Multi],
    73     classes: List[Class],
    74     locales: List[Locale],
    75     classrel: List[Classrel],
    76     arities: List[Arity],
    77     typedefs: List[Typedef])
    78   {
    79     override def toString: String = name
    80 
    81     lazy val entities: Set[Long] =
    82       Set.empty[Long] ++
    83         types.iterator.map(_.entity.serial) ++
    84         consts.iterator.map(_.entity.serial) ++
    85         axioms.iterator.map(_.entity.serial) ++
    86         facts.iterator.map(_.entity.serial) ++
    87         classes.iterator.map(_.entity.serial) ++
    88         locales.iterator.map(_.entity.serial)
    89 
    90     def cache(cache: Term.Cache): Theory =
    91       Theory(cache.string(name),
    92         parents.map(cache.string(_)),
    93         types.map(_.cache(cache)),
    94         consts.map(_.cache(cache)),
    95         axioms.map(_.cache(cache)),
    96         facts.map(_.cache(cache)),
    97         classes.map(_.cache(cache)),
    98         locales.map(_.cache(cache)),
    99         classrel.map(_.cache(cache)),
   100         arities.map(_.cache(cache)),
   101         typedefs.map(_.cache(cache)))
   102   }
   103 
   104   def empty_theory(name: String): Theory =
   105     Theory(name, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil)
   106 
   107   def read_theory(provider: Export.Provider, session_name: String, theory_name: String,
   108     types: Boolean = true,
   109     consts: Boolean = true,
   110     axioms: Boolean = true,
   111     facts: Boolean = true,
   112     classes: Boolean = true,
   113     locales: Boolean = true,
   114     classrel: Boolean = true,
   115     arities: Boolean = true,
   116     typedefs: Boolean = true,
   117     cache: Option[Term.Cache] = None): Theory =
   118   {
   119     val parents =
   120       provider(export_prefix + "parents") match {
   121         case Some(entry) => split_lines(entry.uncompressed().text)
   122         case None =>
   123           error("Missing theory export in session " + quote(session_name) + ": " +
   124             quote(theory_name))
   125       }
   126     val theory =
   127       Theory(theory_name, parents,
   128         if (types) read_types(provider) else Nil,
   129         if (consts) read_consts(provider) else Nil,
   130         if (axioms) read_axioms(provider) else Nil,
   131         if (facts) read_facts(provider) else Nil,
   132         if (classes) read_classes(provider) else Nil,
   133         if (locales) read_locales(provider) else Nil,
   134         if (classrel) read_classrel(provider) else Nil,
   135         if (arities) read_arities(provider) else Nil,
   136         if (typedefs) read_typedefs(provider) else Nil)
   137     if (cache.isDefined) theory.cache(cache.get) else theory
   138   }
   139 
   140   def read_pure_theory(store: Sessions.Store, cache: Option[Term.Cache] = None): Theory =
   141   {
   142     val session_name = Thy_Header.PURE
   143     val theory_name = Thy_Header.PURE
   144 
   145     using(store.open_database(session_name))(db =>
   146     {
   147       db.transaction {
   148         read_theory(Export.Provider.database(db, session_name, theory_name),
   149           session_name, theory_name, cache = cache)
   150       }
   151     })
   152   }
   153 
   154 
   155   /* entities */
   156 
   157   object Kind extends Enumeration
   158   {
   159     val TYPE = Value("type")
   160     val CONST = Value("const")
   161     val AXIOM = Value("axiom")
   162     val FACT = Value("fact")
   163     val CLASS = Value("class")
   164     val LOCALE = Value("locale")
   165   }
   166 
   167   sealed case class Entity(
   168     kind: Kind.Value, name: String, pos: Position.T, id: Option[Long], serial: Long)
   169   {
   170     override def toString: String = kind.toString + " " + quote(name)
   171 
   172     def cache(cache: Term.Cache): Entity =
   173       Entity(kind, cache.string(name), cache.position(pos), id, serial)
   174   }
   175 
   176   def decode_entity(kind: Kind.Value, tree: XML.Tree): (Entity, XML.Body) =
   177   {
   178     def err(): Nothing = throw new XML.XML_Body(List(tree))
   179 
   180     tree match {
   181       case XML.Elem(Markup(Markup.ENTITY, props), body) =>
   182         val name = Markup.Name.unapply(props) getOrElse err()
   183         val pos = props.filter({ case (a, _) => Markup.POSITION_PROPERTIES(a) && a != Markup.ID })
   184         val id = Position.Id.unapply(props)
   185         val serial = Markup.Serial.unapply(props) getOrElse err()
   186         (Entity(kind, name, pos, id, serial), body)
   187       case _ => err()
   188     }
   189   }
   190 
   191 
   192   /* types */
   193 
   194   sealed case class Type(entity: Entity, args: List[String], abbrev: Option[Term.Typ])
   195   {
   196     def cache(cache: Term.Cache): Type =
   197       Type(entity.cache(cache),
   198         args.map(cache.string(_)),
   199         abbrev.map(cache.typ(_)))
   200   }
   201 
   202   def read_types(provider: Export.Provider): List[Type] =
   203     provider.uncompressed_yxml(export_prefix + "types").map((tree: XML.Tree) =>
   204       {
   205         val (entity, body) = decode_entity(Kind.TYPE, tree)
   206         val (args, abbrev) =
   207         {
   208           import XML.Decode._
   209           pair(list(string), option(Term_XML.Decode.typ))(body)
   210         }
   211         Type(entity, args, abbrev)
   212       })
   213 
   214 
   215   /* consts */
   216 
   217   sealed case class Const(
   218     entity: Entity, typargs: List[String], typ: Term.Typ, abbrev: Option[Term.Term])
   219   {
   220     def cache(cache: Term.Cache): Const =
   221       Const(entity.cache(cache),
   222         typargs.map(cache.string(_)),
   223         cache.typ(typ),
   224         abbrev.map(cache.term(_)))
   225   }
   226 
   227   def read_consts(provider: Export.Provider): List[Const] =
   228     provider.uncompressed_yxml(export_prefix + "consts").map((tree: XML.Tree) =>
   229       {
   230         val (entity, body) = decode_entity(Kind.CONST, tree)
   231         val (args, typ, abbrev) =
   232         {
   233           import XML.Decode._
   234           triple(list(string), Term_XML.Decode.typ, option(Term_XML.Decode.term))(body)
   235         }
   236         Const(entity, args, typ, abbrev)
   237       })
   238 
   239 
   240   /* facts */
   241 
   242   sealed case class Prop(
   243     typargs: List[(String, Term.Sort)],
   244     args: List[(String, Term.Typ)],
   245     term: Term.Term)
   246   {
   247     def cache(cache: Term.Cache): Prop =
   248       Prop(
   249         typargs.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }),
   250         args.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
   251         cache.term(term))
   252   }
   253 
   254   def decode_prop(body: XML.Body): Prop =
   255   {
   256     val (typargs, args, t) =
   257     {
   258       import XML.Decode._
   259       import Term_XML.Decode._
   260       triple(list(pair(string, sort)), list(pair(string, typ)), term)(body)
   261     }
   262     Prop(typargs, args, t)
   263   }
   264 
   265   sealed case class Fact_Single(entity: Entity, prop: Prop)
   266   {
   267     def cache(cache: Term.Cache): Fact_Single =
   268       Fact_Single(entity.cache(cache), prop.cache(cache))
   269   }
   270 
   271   sealed case class Fact_Multi(entity: Entity, props: List[Prop])
   272   {
   273     def cache(cache: Term.Cache): Fact_Multi =
   274       Fact_Multi(entity.cache(cache), props.map(_.cache(cache)))
   275 
   276     def split: List[Fact_Single] =
   277       props match {
   278         case List(prop) => List(Fact_Single(entity, prop))
   279         case _ =>
   280           for ((prop, i) <- props.zipWithIndex)
   281           yield Fact_Single(entity.copy(name = entity.name + "(" + (i + 1) + ")"), prop)
   282       }
   283   }
   284 
   285   def read_axioms(provider: Export.Provider): List[Fact_Single] =
   286     provider.uncompressed_yxml(export_prefix + "axioms").map((tree: XML.Tree) =>
   287       {
   288         val (entity, body) = decode_entity(Kind.AXIOM, tree)
   289         val prop = decode_prop(body)
   290         Fact_Single(entity, prop)
   291       })
   292 
   293   def read_facts(provider: Export.Provider): List[Fact_Multi] =
   294     provider.uncompressed_yxml(export_prefix + "facts").map((tree: XML.Tree) =>
   295       {
   296         val (entity, body) = decode_entity(Kind.FACT, tree)
   297         val props = XML.Decode.list(decode_prop)(body)
   298         Fact_Multi(entity, props)
   299       })
   300 
   301 
   302   /* type classes */
   303 
   304   sealed case class Class(
   305     entity: Entity, params: List[(String, Term.Typ)], axioms: List[Term.Term])
   306   {
   307     def cache(cache: Term.Cache): Class =
   308       Class(entity.cache(cache),
   309         params.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
   310         axioms.map(cache.term(_)))
   311   }
   312 
   313   def read_classes(provider: Export.Provider): List[Class] =
   314     provider.uncompressed_yxml(export_prefix + "classes").map((tree: XML.Tree) =>
   315       {
   316         val (entity, body) = decode_entity(Kind.CLASS, tree)
   317         val (params, axioms) =
   318         {
   319           import XML.Decode._
   320           import Term_XML.Decode._
   321           pair(list(pair(string, typ)), list(term))(body)
   322         }
   323         Class(entity, params, axioms)
   324       })
   325 
   326 
   327   /* locales */
   328 
   329   sealed case class Locale(
   330     entity: Entity, type_params: List[(String, Term.Sort)], params: List[(String, Term.Typ)],
   331       asm: Option[Term.Term], defs: List[Term.Term])
   332   {
   333     def cache(cache: Term.Cache): Locale =
   334       Locale(entity.cache(cache),
   335         type_params.map({ case (name, sort) => (cache.string(name), cache.sort(sort)) }),
   336         params.map({ case (name, typ) => (cache.string(name), cache.typ(typ)) }),
   337         asm.map(cache.term(_)),
   338         defs.map(cache.term(_)))
   339   }
   340 
   341   def read_locales(provider: Export.Provider): List[Locale] =
   342     provider.uncompressed_yxml(export_prefix + "locales").map((tree: XML.Tree) =>
   343       {
   344         val (entity, body) = decode_entity(Kind.LOCALE, tree)
   345         val (type_params, (params, (asm, defs))) =
   346         {
   347           import XML.Decode._
   348           import Term_XML.Decode._
   349           pair(list(pair(string, sort)),
   350             pair(list(pair(string, typ)),
   351               pair(option(term), list(term))))(body)
   352         }
   353         Locale(entity, type_params, params, asm, defs)
   354       })
   355 
   356 
   357   /* sort algebra */
   358 
   359   sealed case class Classrel(class_name: String, super_names: List[String])
   360   {
   361     def cache(cache: Term.Cache): Classrel =
   362       Classrel(cache.string(class_name), super_names.map(cache.string(_)))
   363   }
   364 
   365   def read_classrel(provider: Export.Provider): List[Classrel] =
   366   {
   367     val body = provider.uncompressed_yxml(export_prefix + "classrel")
   368     val classrel =
   369     {
   370       import XML.Decode._
   371       list(pair(string, list(string)))(body)
   372     }
   373     for ((c, cs) <- classrel) yield Classrel(c, cs)
   374   }
   375 
   376   sealed case class Arity(type_name: String, domain: List[Term.Sort], codomain: String)
   377   {
   378     def cache(cache: Term.Cache): Arity =
   379       Arity(cache.string(type_name), domain.map(cache.sort(_)), cache.string(codomain))
   380   }
   381 
   382   def read_arities(provider: Export.Provider): List[Arity] =
   383   {
   384     val body = provider.uncompressed_yxml(export_prefix + "arities")
   385     val arities =
   386     {
   387       import XML.Decode._
   388       import Term_XML.Decode._
   389       list(triple(string, list(sort), string))(body)
   390     }
   391     for ((a, b, c) <- arities) yield Arity(a, b, c)
   392   }
   393 
   394 
   395   /* HOL typedefs */
   396 
   397   sealed case class Typedef(name: String,
   398     rep_type: Term.Typ, abs_type: Term.Typ, rep_name: String, abs_name: String, axiom_name: String)
   399   {
   400     def cache(cache: Term.Cache): Typedef =
   401       Typedef(cache.string(name),
   402         cache.typ(rep_type),
   403         cache.typ(abs_type),
   404         cache.string(rep_name),
   405         cache.string(abs_name),
   406         cache.string(axiom_name))
   407   }
   408 
   409   def read_typedefs(provider: Export.Provider): List[Typedef] =
   410   {
   411     val body = provider.uncompressed_yxml(export_prefix + "typedefs")
   412     val typedefs =
   413     {
   414       import XML.Decode._
   415       import Term_XML.Decode._
   416       list(pair(string, pair(typ, pair(typ, pair(string, pair(string, string))))))(body)
   417     }
   418     for { (name, (rep_type, (abs_type, (rep_name, (abs_name, axiom_name))))) <- typedefs }
   419     yield Typedef(name, rep_type, abs_type, rep_name, abs_name, axiom_name)
   420   }
   421 }