src/Pure/Thy/export_theory.ML
author wenzelm
Wed Sep 19 22:18:36 2018 +0200 (13 months ago)
changeset 69019 a6ba77af6486
parent 69003 a015f1d3ba0c
child 69023 cef000855cf4
permissions -rw-r--r--
export semi-unfolded locale axioms;
wenzelm@68154
     1
(*  Title:      Pure/Thy/export_theory.ML
wenzelm@68154
     2
    Author:     Makarius
wenzelm@68154
     3
wenzelm@68154
     4
Export foundational theory content.
wenzelm@68154
     5
*)
wenzelm@68154
     6
wenzelm@68154
     7
signature EXPORT_THEORY =
wenzelm@68154
     8
sig
wenzelm@68201
     9
  val setup_presentation: (Thy_Info.presentation_context -> theory -> unit) -> unit
wenzelm@68201
    10
  val export_body: theory -> string -> XML.body -> unit
wenzelm@68154
    11
end;
wenzelm@68154
    12
wenzelm@68154
    13
structure Export_Theory: EXPORT_THEORY =
wenzelm@68154
    14
struct
wenzelm@68154
    15
wenzelm@68725
    16
(* names for bound variables *)
wenzelm@68725
    17
wenzelm@68725
    18
local
wenzelm@68725
    19
  fun declare_names (Abs (_, _, b)) = declare_names b
wenzelm@68725
    20
    | declare_names (t $ u) = declare_names t #> declare_names u
wenzelm@68725
    21
    | declare_names (Const (c, _)) = Name.declare (Long_Name.base_name c)
wenzelm@68725
    22
    | declare_names (Free (x, _)) = Name.declare x
wenzelm@68725
    23
    | declare_names _ = I;
wenzelm@68725
    24
wenzelm@68725
    25
  fun variant_abs bs (Abs (x, T, t)) =
wenzelm@68725
    26
        let
wenzelm@68725
    27
          val names = fold Name.declare bs (declare_names t Name.context);
wenzelm@68725
    28
          val x' = #1 (Name.variant x names);
wenzelm@68725
    29
          val t' = variant_abs (x' :: bs) t;
wenzelm@68725
    30
        in Abs (x', T, t') end
wenzelm@68725
    31
    | variant_abs bs (t $ u) = variant_abs bs t $ variant_abs bs u
wenzelm@68725
    32
    | variant_abs _ t = t;
wenzelm@68725
    33
in
wenzelm@68725
    34
  val named_bounds = variant_abs [];
wenzelm@68725
    35
end;
wenzelm@68725
    36
wenzelm@68725
    37
wenzelm@69019
    38
(* locale support *)
wenzelm@69019
    39
wenzelm@69019
    40
fun locale_axioms thy loc =
wenzelm@69019
    41
  let
wenzelm@69019
    42
    val (intro1, intro2) = Locale.intros_of thy loc;
wenzelm@69019
    43
    fun intros_tac ctxt = Method.try_intros_tac ctxt (the_list intro1 @ the_list intro2);
wenzelm@69019
    44
    val res =
wenzelm@69019
    45
      Proof_Context.init_global thy
wenzelm@69019
    46
      |> Interpretation.interpretation ([(loc, (("", false), (Expression.Named [], [])))], [])
wenzelm@69019
    47
      |> Proof.refine (Method.Basic (METHOD o intros_tac))
wenzelm@69019
    48
      |> Seq.filter_results
wenzelm@69019
    49
      |> try Seq.hd;
wenzelm@69019
    50
  in
wenzelm@69019
    51
    (case res of
wenzelm@69019
    52
      SOME st => Thm.prems_of (#goal (Proof.goal st))
wenzelm@69019
    53
    | NONE => raise Fail ("Cannot unfold locale " ^ quote loc))
wenzelm@69019
    54
  end;
wenzelm@69019
    55
wenzelm@69019
    56
wenzelm@68201
    57
(* general setup *)
wenzelm@68154
    58
wenzelm@68201
    59
fun setup_presentation f =
wenzelm@68201
    60
  Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
wenzelm@68201
    61
    if Options.bool (#options context) "export_theory" then f context thy else ()));
wenzelm@68154
    62
wenzelm@68201
    63
fun export_body thy name body =
wenzelm@68230
    64
  Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty));
wenzelm@68165
    65
wenzelm@68154
    66
wenzelm@68201
    67
(* presentation *)
wenzelm@68201
    68
wenzelm@68201
    69
val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
wenzelm@68201
    70
  let
wenzelm@68900
    71
    val parents = Theory.parents_of thy;
wenzelm@68264
    72
    val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
wenzelm@68264
    73
wenzelm@69003
    74
    val thy_ctxt = Proof_Context.init_global thy;
wenzelm@68997
    75
wenzelm@68264
    76
wenzelm@68201
    77
    (* entities *)
wenzelm@68154
    78
wenzelm@68201
    79
    fun entity_markup space name =
wenzelm@68201
    80
      let
wenzelm@69003
    81
        val xname = Name_Space.extern_shortest thy_ctxt space name;
wenzelm@68201
    82
        val {serial, pos, ...} = Name_Space.the_entry space name;
wenzelm@68201
    83
        val props =
wenzelm@68830
    84
          Position.offset_properties_of (adjust_pos pos) @
wenzelm@68830
    85
          Position.id_properties_of pos @
wenzelm@68830
    86
          Markup.serial_properties serial;
wenzelm@68997
    87
      in (Markup.entityN, (Markup.nameN, name) :: (Markup.xnameN, xname) :: props) end;
wenzelm@68154
    88
wenzelm@68201
    89
    fun export_entities export_name export get_space decls =
wenzelm@68201
    90
      let val elems =
wenzelm@68201
    91
        let
wenzelm@68206
    92
          val parent_spaces = map get_space parents;
wenzelm@68201
    93
          val space = get_space thy;
wenzelm@68201
    94
        in
wenzelm@68201
    95
          (decls, []) |-> fold (fn (name, decl) =>
wenzelm@68201
    96
            if exists (fn space => Name_Space.declared space name) parent_spaces then I
wenzelm@68201
    97
            else
wenzelm@68201
    98
              (case export name decl of
wenzelm@68201
    99
                NONE => I
wenzelm@68724
   100
              | SOME body =>
wenzelm@68724
   101
                  cons (#serial (Name_Space.the_entry space name),
wenzelm@68724
   102
                    XML.Elem (entity_markup space name, body))))
wenzelm@68724
   103
          |> sort (int_ord o apply2 #1) |> map #2
wenzelm@68201
   104
        end;
wenzelm@68201
   105
      in if null elems then () else export_body thy export_name elems end;
wenzelm@68165
   106
wenzelm@68165
   107
wenzelm@69003
   108
    (* infix syntax *)
wenzelm@69003
   109
wenzelm@69003
   110
    fun get_infix_const ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_const;
wenzelm@69003
   111
    fun get_infix_type ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_type;
wenzelm@69003
   112
wenzelm@69003
   113
    fun encode_infix {assoc, delim, pri} =
wenzelm@69003
   114
      let
wenzelm@69003
   115
        val ass =
wenzelm@69003
   116
          (case assoc of
wenzelm@69003
   117
            Syntax_Ext.No_Assoc => 0
wenzelm@69003
   118
          | Syntax_Ext.Left_Assoc => 1
wenzelm@69003
   119
          | Syntax_Ext.Right_Assoc => 2);
wenzelm@69003
   120
        open XML.Encode Term_XML.Encode;
wenzelm@69003
   121
      in triple int string int (ass, delim, pri) end;
wenzelm@69003
   122
wenzelm@69003
   123
wenzelm@68201
   124
    (* types *)
wenzelm@68165
   125
wenzelm@68201
   126
    val encode_type =
wenzelm@68201
   127
      let open XML.Encode Term_XML.Encode
wenzelm@69003
   128
      in triple (option encode_infix) (list string) (option typ) end;
wenzelm@68165
   129
wenzelm@69003
   130
    fun export_type c (Type.LogicalType n) =
wenzelm@69003
   131
          SOME (encode_type (get_infix_type thy_ctxt c, Name.invent Name.context Name.aT n, NONE))
wenzelm@69003
   132
      | export_type c (Type.Abbreviation (args, U, false)) =
wenzelm@69003
   133
          SOME (encode_type (get_infix_type thy_ctxt c, args, SOME U))
wenzelm@69003
   134
      | export_type _ _ = NONE;
wenzelm@68165
   135
wenzelm@68201
   136
    val _ =
wenzelm@69003
   137
      export_entities "types" export_type Sign.type_space
wenzelm@68264
   138
        (Name_Space.dest_table (#types rep_tsig));
wenzelm@68201
   139
wenzelm@68173
   140
wenzelm@68201
   141
    (* consts *)
wenzelm@68201
   142
wenzelm@68201
   143
    val encode_const =
wenzelm@69003
   144
      let open XML.Encode Term_XML.Encode in
wenzelm@69003
   145
        pair (option encode_infix) (pair (list string) (pair typ (option (term o named_bounds))))
wenzelm@69003
   146
      end;
wenzelm@68165
   147
wenzelm@68201
   148
    fun export_const c (T, abbrev) =
wenzelm@68201
   149
      let
wenzelm@69003
   150
        val syntax = get_infix_const thy_ctxt c;
wenzelm@68201
   151
        val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
wenzelm@68201
   152
        val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts);
wenzelm@68201
   153
        val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
wenzelm@69003
   154
      in SOME (encode_const (syntax, (args, (T', abbrev')))) end;
wenzelm@68165
   155
wenzelm@68201
   156
    val _ =
wenzelm@68201
   157
      export_entities "consts" export_const Sign.const_space
wenzelm@68201
   158
        (#constants (Consts.dest (Sign.consts_of thy)));
wenzelm@68201
   159
wenzelm@68208
   160
wenzelm@68232
   161
    (* axioms and facts *)
wenzelm@68208
   162
wenzelm@68727
   163
    fun standard_prop_of raw_thm =
wenzelm@68727
   164
      let
wenzelm@68727
   165
        val thm = raw_thm
wenzelm@68727
   166
          |> Thm.transfer thy
wenzelm@68727
   167
          |> Thm.check_hyps (Context.Theory thy)
wenzelm@68727
   168
          |> Thm.strip_shyps;
wenzelm@68727
   169
        val prop = thm
wenzelm@68727
   170
          |> Thm.full_prop_of
wenzelm@68727
   171
          |> Term_Subst.zero_var_indexes;
wenzelm@68727
   172
      in (Thm.extra_shyps thm, prop) end;
wenzelm@68539
   173
wenzelm@68727
   174
    fun encode_prop (Ss, prop) =
wenzelm@68208
   175
      let
wenzelm@68726
   176
        val prop' = Logic.unvarify_global (named_bounds prop);
wenzelm@68726
   177
        val typargs = rev (Term.add_tfrees prop' []);
wenzelm@68727
   178
        val sorts = Name.invent (Name.make_context (map #1 typargs)) Name.aT (length Ss) ~~ Ss;
wenzelm@68726
   179
        val args = rev (Term.add_frees prop' []);
wenzelm@68726
   180
      in
wenzelm@68727
   181
        (sorts @ typargs, args, prop') |>
wenzelm@68727
   182
          let open XML.Encode Term_XML.Encode
wenzelm@68727
   183
          in triple (list (pair string sort)) (list (pair string typ)) term end
wenzelm@68726
   184
      end;
wenzelm@68208
   185
wenzelm@68726
   186
    val encode_fact = XML.Encode.list encode_prop o map standard_prop_of;
wenzelm@68208
   187
wenzelm@68232
   188
    val _ =
wenzelm@68727
   189
      export_entities "axioms" (fn _ => fn t => SOME (encode_prop ([], t))) Theory.axiom_space
wenzelm@68235
   190
        (Theory.axioms_of thy);
wenzelm@68235
   191
    val _ =
wenzelm@68726
   192
      export_entities "facts" (K (SOME o encode_fact)) (Facts.space_of o Global_Theory.facts_of)
wenzelm@68232
   193
        (Facts.dest_static true [] (Global_Theory.facts_of thy));
wenzelm@68232
   194
wenzelm@68264
   195
wenzelm@68264
   196
    (* type classes *)
wenzelm@68264
   197
wenzelm@68264
   198
    val encode_class =
wenzelm@68264
   199
      let open XML.Encode Term_XML.Encode
wenzelm@68725
   200
      in pair (list (pair string typ)) (list (term o named_bounds)) end;
wenzelm@68264
   201
wenzelm@68264
   202
    fun export_class name =
wenzelm@68264
   203
      (case try (Axclass.get_info thy) name of
wenzelm@68264
   204
        NONE => ([], [])
wenzelm@68264
   205
      | SOME {params, axioms, ...} =>
wenzelm@68264
   206
          (params, map (Logic.unvarify_global o Thm.full_prop_of) axioms))
wenzelm@68264
   207
      |> encode_class |> SOME;
wenzelm@68264
   208
wenzelm@68264
   209
    val _ =
wenzelm@68264
   210
      export_entities "classes" (fn name => fn () => export_class name)
wenzelm@68264
   211
        Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
wenzelm@68264
   212
wenzelm@68295
   213
wenzelm@68295
   214
    (* sort algebra *)
wenzelm@68295
   215
wenzelm@68295
   216
    val {classrel, arities} =
wenzelm@68295
   217
      Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
wenzelm@68295
   218
        (#2 (#classes rep_tsig));
wenzelm@68295
   219
wenzelm@68295
   220
    val encode_classrel =
wenzelm@68295
   221
      let open XML.Encode
wenzelm@68295
   222
      in list (pair string (list string)) end;
wenzelm@68295
   223
wenzelm@68295
   224
    val encode_arities =
wenzelm@68295
   225
      let open XML.Encode Term_XML.Encode
wenzelm@68295
   226
      in list (triple string (list sort) string) end;
wenzelm@68295
   227
wenzelm@68295
   228
    val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel);
wenzelm@68295
   229
    val _ = if null arities then () else export_body thy "arities" (encode_arities arities);
wenzelm@68295
   230
wenzelm@68862
   231
wenzelm@68862
   232
    (* locales *)
wenzelm@68862
   233
wenzelm@69019
   234
    fun encode_locale loc ({type_params, params, ...}: Locale.content) =
wenzelm@68864
   235
      let
wenzelm@69019
   236
        val axioms = locale_axioms thy loc;
wenzelm@68864
   237
        val args = map #1 params;
wenzelm@69019
   238
        val typargs = type_params @ rev (fold Term.add_tfrees (map Free args @ axioms) []);
wenzelm@68864
   239
        val encode =
wenzelm@69019
   240
          let open XML.Encode Term_XML.Encode
wenzelm@69019
   241
          in triple (list (pair string sort)) (list (pair string typ)) (list term) end;
wenzelm@69019
   242
      in encode (typargs, args, axioms) end;
wenzelm@68862
   243
wenzelm@68862
   244
    val _ =
wenzelm@69019
   245
      export_entities "locales" (SOME oo encode_locale) Locale.locale_space
wenzelm@68862
   246
        (Locale.dest_locales thy);
wenzelm@68862
   247
wenzelm@68900
   248
wenzelm@68900
   249
    (* parents *)
wenzelm@68900
   250
wenzelm@68900
   251
    val _ =
wenzelm@68900
   252
      export_body thy "parents"
wenzelm@68900
   253
        (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
wenzelm@68900
   254
wenzelm@68295
   255
  in () end);
wenzelm@68165
   256
wenzelm@68165
   257
end;