src/Pure/Thy/export_theory.ML
author wenzelm
Sat Sep 22 15:22:29 2018 +0200 (13 months ago)
changeset 69034 855c3c501b09
parent 69029 aec64b88e708
child 69069 b9aca3b9619f
permissions -rw-r--r--
obsolete (see aec64b88e708);
wenzelm@68154
     1
(*  Title:      Pure/Thy/export_theory.ML
wenzelm@68154
     2
    Author:     Makarius
wenzelm@68154
     3
wenzelm@69023
     4
Export foundational theory content and locale/class structure.
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@69023
    16
(* standardization of variables: only frees and named bounds *)
wenzelm@68725
    17
wenzelm@68725
    18
local
wenzelm@69023
    19
wenzelm@69023
    20
fun declare_names (Abs (_, _, b)) = declare_names b
wenzelm@69023
    21
  | declare_names (t $ u) = declare_names t #> declare_names u
wenzelm@69023
    22
  | declare_names (Const (c, _)) = Name.declare (Long_Name.base_name c)
wenzelm@69023
    23
  | declare_names (Free (x, _)) = Name.declare x
wenzelm@69023
    24
  | declare_names _ = I;
wenzelm@69023
    25
wenzelm@69023
    26
fun variant_abs bs (Abs (x, T, t)) =
wenzelm@69023
    27
      let
wenzelm@69023
    28
        val names = fold Name.declare bs (declare_names t Name.context);
wenzelm@69023
    29
        val x' = #1 (Name.variant x names);
wenzelm@69023
    30
        val t' = variant_abs (x' :: bs) t;
wenzelm@69023
    31
      in Abs (x', T, t') end
wenzelm@69023
    32
  | variant_abs bs (t $ u) = variant_abs bs t $ variant_abs bs u
wenzelm@69023
    33
  | variant_abs _ t = t;
wenzelm@69023
    34
wenzelm@69023
    35
in
wenzelm@69023
    36
wenzelm@69023
    37
fun standard_vars used =
wenzelm@69023
    38
  let
wenzelm@69023
    39
    fun zero_var_indexes tm =
wenzelm@69023
    40
      Term_Subst.instantiate (Term_Subst.zero_var_indexes_inst used [tm]) tm;
wenzelm@68725
    41
wenzelm@69023
    42
    fun unvarifyT ty = ty |> Term.map_atyps
wenzelm@69023
    43
      (fn TVar ((a, _), S) => TFree (a, S)
wenzelm@69023
    44
        | T as TFree (a, _) =>
wenzelm@69023
    45
            if Name.is_declared used a then T
wenzelm@69023
    46
            else raise TYPE (Logic.bad_fixed a, [ty], []));
wenzelm@69023
    47
wenzelm@69023
    48
    fun unvarify tm = tm |> Term.map_aterms
wenzelm@69023
    49
      (fn Var ((x, _), T) => Free (x, T)
wenzelm@69023
    50
        | t as Free (x, _) =>
wenzelm@69023
    51
            if Name.is_declared used x then t
wenzelm@69023
    52
            else raise TERM (Logic.bad_fixed x, [tm])
wenzelm@69023
    53
        | t => t);
wenzelm@69023
    54
wenzelm@69023
    55
  in zero_var_indexes #> map_types unvarifyT #> unvarify #> variant_abs [] end;
wenzelm@69023
    56
wenzelm@69023
    57
val standard_vars_global = standard_vars Name.context;
wenzelm@69023
    58
wenzelm@68725
    59
end;
wenzelm@68725
    60
wenzelm@68725
    61
wenzelm@69023
    62
(* free variables: not declared in the context *)
wenzelm@69023
    63
wenzelm@69023
    64
val is_free = not oo Name.is_declared;
wenzelm@69023
    65
wenzelm@69023
    66
fun add_frees used =
wenzelm@69023
    67
  fold_aterms (fn Free (x, T) => is_free used x ? insert (op =) (x, T) | _ => I);
wenzelm@69023
    68
wenzelm@69023
    69
fun add_tfrees used =
wenzelm@69023
    70
  (fold_types o fold_atyps) (fn TFree (a, S) => is_free used a ? insert (op =) (a, S) | _ => I);
wenzelm@69023
    71
wenzelm@69023
    72
wenzelm@69029
    73
(* locale content *)
wenzelm@69019
    74
wenzelm@69034
    75
fun locale_content thy loc =
wenzelm@69019
    76
  let
wenzelm@69029
    77
    val args = map #1 (Locale.params_of thy loc);
wenzelm@69029
    78
    val axioms =
wenzelm@69029
    79
      let
wenzelm@69029
    80
        val (intro1, intro2) = Locale.intros_of thy loc;
wenzelm@69029
    81
        fun intros_tac ctxt = Method.try_intros_tac ctxt (the_list intro1 @ the_list intro2);
wenzelm@69029
    82
        val inst = Expression.Named (args |> map (fn (x, T) => (x, Free (x, T))));
wenzelm@69029
    83
        val res =
wenzelm@69029
    84
          Proof_Context.init_global thy
wenzelm@69029
    85
          |> Interpretation.interpretation ([(loc, (("", false), (inst, [])))], [])
wenzelm@69029
    86
          |> Proof.refine (Method.Basic (METHOD o intros_tac))
wenzelm@69029
    87
          |> Seq.filter_results
wenzelm@69029
    88
          |> try Seq.hd;
wenzelm@69029
    89
      in
wenzelm@69029
    90
        (case res of
wenzelm@69029
    91
          SOME st => Thm.prems_of (#goal (Proof.goal st))
wenzelm@69029
    92
        | NONE => raise Fail ("Cannot unfold locale " ^ quote loc))
wenzelm@69029
    93
      end;
wenzelm@69029
    94
    val typargs = rev (fold Term.add_tfrees (map Free args @ axioms) []);
wenzelm@69029
    95
  in {typargs = typargs, args = args, axioms = axioms} end;
wenzelm@69019
    96
wenzelm@69019
    97
wenzelm@68201
    98
(* general setup *)
wenzelm@68154
    99
wenzelm@68201
   100
fun setup_presentation f =
wenzelm@68201
   101
  Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
wenzelm@68201
   102
    if Options.bool (#options context) "export_theory" then f context thy else ()));
wenzelm@68154
   103
wenzelm@68201
   104
fun export_body thy name body =
wenzelm@68230
   105
  Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty));
wenzelm@68165
   106
wenzelm@68154
   107
wenzelm@68201
   108
(* presentation *)
wenzelm@68201
   109
wenzelm@68201
   110
val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
wenzelm@68201
   111
  let
wenzelm@68900
   112
    val parents = Theory.parents_of thy;
wenzelm@68264
   113
    val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
wenzelm@68264
   114
wenzelm@69003
   115
    val thy_ctxt = Proof_Context.init_global thy;
wenzelm@68997
   116
wenzelm@68264
   117
wenzelm@68201
   118
    (* entities *)
wenzelm@68154
   119
wenzelm@68201
   120
    fun entity_markup space name =
wenzelm@68201
   121
      let
wenzelm@69003
   122
        val xname = Name_Space.extern_shortest thy_ctxt space name;
wenzelm@68201
   123
        val {serial, pos, ...} = Name_Space.the_entry space name;
wenzelm@68201
   124
        val props =
wenzelm@68830
   125
          Position.offset_properties_of (adjust_pos pos) @
wenzelm@68830
   126
          Position.id_properties_of pos @
wenzelm@68830
   127
          Markup.serial_properties serial;
wenzelm@68997
   128
      in (Markup.entityN, (Markup.nameN, name) :: (Markup.xnameN, xname) :: props) end;
wenzelm@68154
   129
wenzelm@68201
   130
    fun export_entities export_name export get_space decls =
wenzelm@68201
   131
      let val elems =
wenzelm@68201
   132
        let
wenzelm@68206
   133
          val parent_spaces = map get_space parents;
wenzelm@68201
   134
          val space = get_space thy;
wenzelm@68201
   135
        in
wenzelm@68201
   136
          (decls, []) |-> fold (fn (name, decl) =>
wenzelm@68201
   137
            if exists (fn space => Name_Space.declared space name) parent_spaces then I
wenzelm@68201
   138
            else
wenzelm@68201
   139
              (case export name decl of
wenzelm@68201
   140
                NONE => I
wenzelm@68724
   141
              | SOME body =>
wenzelm@68724
   142
                  cons (#serial (Name_Space.the_entry space name),
wenzelm@68724
   143
                    XML.Elem (entity_markup space name, body))))
wenzelm@68724
   144
          |> sort (int_ord o apply2 #1) |> map #2
wenzelm@68201
   145
        end;
wenzelm@68201
   146
      in if null elems then () else export_body thy export_name elems end;
wenzelm@68165
   147
wenzelm@68165
   148
wenzelm@69003
   149
    (* infix syntax *)
wenzelm@69003
   150
wenzelm@69003
   151
    fun get_infix_const ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_const;
wenzelm@69003
   152
    fun get_infix_type ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_type;
wenzelm@69003
   153
wenzelm@69003
   154
    fun encode_infix {assoc, delim, pri} =
wenzelm@69003
   155
      let
wenzelm@69003
   156
        val ass =
wenzelm@69003
   157
          (case assoc of
wenzelm@69003
   158
            Syntax_Ext.No_Assoc => 0
wenzelm@69003
   159
          | Syntax_Ext.Left_Assoc => 1
wenzelm@69003
   160
          | Syntax_Ext.Right_Assoc => 2);
wenzelm@69003
   161
        open XML.Encode Term_XML.Encode;
wenzelm@69003
   162
      in triple int string int (ass, delim, pri) end;
wenzelm@69003
   163
wenzelm@69003
   164
wenzelm@68201
   165
    (* types *)
wenzelm@68165
   166
wenzelm@68201
   167
    val encode_type =
wenzelm@68201
   168
      let open XML.Encode Term_XML.Encode
wenzelm@69003
   169
      in triple (option encode_infix) (list string) (option typ) end;
wenzelm@68165
   170
wenzelm@69003
   171
    fun export_type c (Type.LogicalType n) =
wenzelm@69003
   172
          SOME (encode_type (get_infix_type thy_ctxt c, Name.invent Name.context Name.aT n, NONE))
wenzelm@69003
   173
      | export_type c (Type.Abbreviation (args, U, false)) =
wenzelm@69003
   174
          SOME (encode_type (get_infix_type thy_ctxt c, args, SOME U))
wenzelm@69003
   175
      | export_type _ _ = NONE;
wenzelm@68165
   176
wenzelm@68201
   177
    val _ =
wenzelm@69003
   178
      export_entities "types" export_type Sign.type_space
wenzelm@68264
   179
        (Name_Space.dest_table (#types rep_tsig));
wenzelm@68201
   180
wenzelm@68173
   181
wenzelm@68201
   182
    (* consts *)
wenzelm@68201
   183
wenzelm@68201
   184
    val encode_const =
wenzelm@69023
   185
      let open XML.Encode Term_XML.Encode
wenzelm@69023
   186
      in pair (option encode_infix) (pair (list string) (pair typ (option term))) end;
wenzelm@68165
   187
wenzelm@68201
   188
    fun export_const c (T, abbrev) =
wenzelm@68201
   189
      let
wenzelm@69003
   190
        val syntax = get_infix_const thy_ctxt c;
wenzelm@68201
   191
        val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
wenzelm@69023
   192
        val abbrev' = abbrev |> Option.map (standard_vars_global #> map_types Type.strip_sorts);
wenzelm@68201
   193
        val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
wenzelm@69023
   194
      in encode_const (syntax, (args, (T', abbrev'))) end;
wenzelm@68165
   195
wenzelm@68201
   196
    val _ =
wenzelm@69023
   197
      export_entities "consts" (SOME oo export_const) Sign.const_space
wenzelm@68201
   198
        (#constants (Consts.dest (Sign.consts_of thy)));
wenzelm@68201
   199
wenzelm@68208
   200
wenzelm@68232
   201
    (* axioms and facts *)
wenzelm@68208
   202
wenzelm@69023
   203
    fun prop_of raw_thm =
wenzelm@68727
   204
      let
wenzelm@68727
   205
        val thm = raw_thm
wenzelm@68727
   206
          |> Thm.transfer thy
wenzelm@68727
   207
          |> Thm.check_hyps (Context.Theory thy)
wenzelm@68727
   208
          |> Thm.strip_shyps;
wenzelm@68727
   209
        val prop = thm
wenzelm@69023
   210
          |> Thm.full_prop_of;
wenzelm@68727
   211
      in (Thm.extra_shyps thm, prop) end;
wenzelm@68539
   212
wenzelm@69023
   213
    fun encode_prop used (Ss, raw_prop) =
wenzelm@68208
   214
      let
wenzelm@69023
   215
        val prop = standard_vars used raw_prop;
wenzelm@69023
   216
        val args = rev (add_frees used prop []);
wenzelm@69023
   217
        val typargs = rev (add_tfrees used prop []);
wenzelm@69023
   218
        val used' = fold (Name.declare o #1) typargs used;
wenzelm@69023
   219
        val sorts = Name.invent used' Name.aT (length Ss) ~~ Ss;
wenzelm@68726
   220
      in
wenzelm@69023
   221
        (sorts @ typargs, args, prop) |>
wenzelm@68727
   222
          let open XML.Encode Term_XML.Encode
wenzelm@68727
   223
          in triple (list (pair string sort)) (list (pair string typ)) term end
wenzelm@68726
   224
      end;
wenzelm@68208
   225
wenzelm@69023
   226
    fun encode_axiom used t = encode_prop used ([], t);
wenzelm@69023
   227
wenzelm@69023
   228
    val encode_fact_single = encode_prop Name.context o prop_of;
wenzelm@69023
   229
    val encode_fact_multi = XML.Encode.list (encode_prop Name.context) o map prop_of;
wenzelm@68208
   230
wenzelm@68232
   231
    val _ =
wenzelm@69023
   232
      export_entities "axioms" (fn _ => fn t => SOME (encode_axiom Name.context t))
wenzelm@69023
   233
        Theory.axiom_space (Theory.axioms_of thy);
wenzelm@68235
   234
    val _ =
wenzelm@69023
   235
      export_entities "facts" (K (SOME o encode_fact_multi))
wenzelm@69023
   236
        (Facts.space_of o Global_Theory.facts_of)
wenzelm@68232
   237
        (Facts.dest_static true [] (Global_Theory.facts_of thy));
wenzelm@68232
   238
wenzelm@68264
   239
wenzelm@68264
   240
    (* type classes *)
wenzelm@68264
   241
wenzelm@68264
   242
    val encode_class =
wenzelm@68264
   243
      let open XML.Encode Term_XML.Encode
wenzelm@69023
   244
      in pair (list (pair string typ)) (list encode_fact_single) end;
wenzelm@68264
   245
wenzelm@68264
   246
    fun export_class name =
wenzelm@68264
   247
      (case try (Axclass.get_info thy) name of
wenzelm@68264
   248
        NONE => ([], [])
wenzelm@69023
   249
      | SOME {params, axioms, ...} => (params, axioms))
wenzelm@68264
   250
      |> encode_class |> SOME;
wenzelm@68264
   251
wenzelm@68264
   252
    val _ =
wenzelm@68264
   253
      export_entities "classes" (fn name => fn () => export_class name)
wenzelm@68264
   254
        Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
wenzelm@68264
   255
wenzelm@68295
   256
wenzelm@68295
   257
    (* sort algebra *)
wenzelm@68295
   258
wenzelm@68295
   259
    val {classrel, arities} =
wenzelm@68295
   260
      Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
wenzelm@68295
   261
        (#2 (#classes rep_tsig));
wenzelm@68295
   262
wenzelm@68295
   263
    val encode_classrel =
wenzelm@68295
   264
      let open XML.Encode
wenzelm@68295
   265
      in list (pair string (list string)) end;
wenzelm@68295
   266
wenzelm@68295
   267
    val encode_arities =
wenzelm@68295
   268
      let open XML.Encode Term_XML.Encode
wenzelm@68295
   269
      in list (triple string (list sort) string) end;
wenzelm@68295
   270
wenzelm@68295
   271
    val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel);
wenzelm@68295
   272
    val _ = if null arities then () else export_body thy "arities" (encode_arities arities);
wenzelm@68295
   273
wenzelm@68862
   274
wenzelm@68862
   275
    (* locales *)
wenzelm@68862
   276
wenzelm@69023
   277
    fun encode_locale used =
wenzelm@69023
   278
      let open XML.Encode Term_XML.Encode
wenzelm@69023
   279
      in triple (list (pair string sort)) (list (pair string typ)) (list (encode_axiom used)) end;
wenzelm@69023
   280
wenzelm@69029
   281
    fun export_locale loc =
wenzelm@68864
   282
      let
wenzelm@69034
   283
        val {typargs, args, axioms} = locale_content thy loc;
wenzelm@69023
   284
        val used = fold Name.declare (map #1 typargs @ map #1 args) Name.context;
wenzelm@69027
   285
      in encode_locale used (typargs, args, axioms) end
wenzelm@69027
   286
      handle ERROR msg =>
wenzelm@69027
   287
        cat_error msg ("The error(s) above occurred in locale " ^
wenzelm@69027
   288
          quote (Locale.markup_name thy_ctxt loc));
wenzelm@68862
   289
wenzelm@68862
   290
    val _ =
wenzelm@69029
   291
      export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
wenzelm@69029
   292
        Locale.locale_space
wenzelm@69029
   293
        (map (rpair ()) (Locale.get_locales thy));
wenzelm@68862
   294
wenzelm@68900
   295
wenzelm@68900
   296
    (* parents *)
wenzelm@68900
   297
wenzelm@68900
   298
    val _ =
wenzelm@68900
   299
      export_body thy "parents"
wenzelm@68900
   300
        (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
wenzelm@68900
   301
wenzelm@68295
   302
  in () end);
wenzelm@68165
   303
wenzelm@68165
   304
end;