src/Pure/Thy/export_theory.ML
author wenzelm
Fri Sep 21 22:26:10 2018 +0200 (13 months ago)
changeset 69029 aec64b88e708
parent 69028 f440bedb8e45
child 69034 855c3c501b09
permissions -rw-r--r--
clarified locale content: proper args with types for interpretation/axioms and typargs derived from the result;
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@69029
    75
fun locale_content ctxt loc =
wenzelm@69019
    76
  let
wenzelm@69029
    77
    val thy = Proof_Context.theory_of ctxt;
wenzelm@69029
    78
wenzelm@69029
    79
    val args = map #1 (Locale.params_of thy loc);
wenzelm@69029
    80
    val axioms =
wenzelm@69029
    81
      let
wenzelm@69029
    82
        val (intro1, intro2) = Locale.intros_of thy loc;
wenzelm@69029
    83
        fun intros_tac ctxt = Method.try_intros_tac ctxt (the_list intro1 @ the_list intro2);
wenzelm@69029
    84
        val inst = Expression.Named (args |> map (fn (x, T) => (x, Free (x, T))));
wenzelm@69029
    85
        val res =
wenzelm@69029
    86
          Proof_Context.init_global thy
wenzelm@69029
    87
          |> Interpretation.interpretation ([(loc, (("", false), (inst, [])))], [])
wenzelm@69029
    88
          |> Proof.refine (Method.Basic (METHOD o intros_tac))
wenzelm@69029
    89
          |> Seq.filter_results
wenzelm@69029
    90
          |> try Seq.hd;
wenzelm@69029
    91
      in
wenzelm@69029
    92
        (case res of
wenzelm@69029
    93
          SOME st => Thm.prems_of (#goal (Proof.goal st))
wenzelm@69029
    94
        | NONE => raise Fail ("Cannot unfold locale " ^ quote loc))
wenzelm@69029
    95
      end;
wenzelm@69029
    96
wenzelm@69029
    97
    val typargs = rev (fold Term.add_tfrees (map Free args @ axioms) []);
wenzelm@69029
    98
    val typargs_dups =
wenzelm@69029
    99
      AList.group (op =) typargs |> filter (fn (_, [_]) => false | _ => true)
wenzelm@69029
   100
        |> maps (fn (x, ys) => map (pair x) ys);
wenzelm@69029
   101
    val typargs_print = Syntax.string_of_typ (Config.put show_sorts true ctxt) o TFree;
wenzelm@69029
   102
    val _ =
wenzelm@69029
   103
      if null typargs_dups then ()
wenzelm@69029
   104
      else error ("Duplicate type parameters " ^ commas (map typargs_print typargs_dups));
wenzelm@69029
   105
  in {typargs = typargs, args = args, axioms = axioms} end;
wenzelm@69019
   106
wenzelm@69019
   107
wenzelm@68201
   108
(* general setup *)
wenzelm@68154
   109
wenzelm@68201
   110
fun setup_presentation f =
wenzelm@68201
   111
  Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
wenzelm@68201
   112
    if Options.bool (#options context) "export_theory" then f context thy else ()));
wenzelm@68154
   113
wenzelm@68201
   114
fun export_body thy name body =
wenzelm@68230
   115
  Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty));
wenzelm@68165
   116
wenzelm@68154
   117
wenzelm@68201
   118
(* presentation *)
wenzelm@68201
   119
wenzelm@68201
   120
val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
wenzelm@68201
   121
  let
wenzelm@68900
   122
    val parents = Theory.parents_of thy;
wenzelm@68264
   123
    val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
wenzelm@68264
   124
wenzelm@69003
   125
    val thy_ctxt = Proof_Context.init_global thy;
wenzelm@68997
   126
wenzelm@68264
   127
wenzelm@68201
   128
    (* entities *)
wenzelm@68154
   129
wenzelm@68201
   130
    fun entity_markup space name =
wenzelm@68201
   131
      let
wenzelm@69003
   132
        val xname = Name_Space.extern_shortest thy_ctxt space name;
wenzelm@68201
   133
        val {serial, pos, ...} = Name_Space.the_entry space name;
wenzelm@68201
   134
        val props =
wenzelm@68830
   135
          Position.offset_properties_of (adjust_pos pos) @
wenzelm@68830
   136
          Position.id_properties_of pos @
wenzelm@68830
   137
          Markup.serial_properties serial;
wenzelm@68997
   138
      in (Markup.entityN, (Markup.nameN, name) :: (Markup.xnameN, xname) :: props) end;
wenzelm@68154
   139
wenzelm@68201
   140
    fun export_entities export_name export get_space decls =
wenzelm@68201
   141
      let val elems =
wenzelm@68201
   142
        let
wenzelm@68206
   143
          val parent_spaces = map get_space parents;
wenzelm@68201
   144
          val space = get_space thy;
wenzelm@68201
   145
        in
wenzelm@68201
   146
          (decls, []) |-> fold (fn (name, decl) =>
wenzelm@68201
   147
            if exists (fn space => Name_Space.declared space name) parent_spaces then I
wenzelm@68201
   148
            else
wenzelm@68201
   149
              (case export name decl of
wenzelm@68201
   150
                NONE => I
wenzelm@68724
   151
              | SOME body =>
wenzelm@68724
   152
                  cons (#serial (Name_Space.the_entry space name),
wenzelm@68724
   153
                    XML.Elem (entity_markup space name, body))))
wenzelm@68724
   154
          |> sort (int_ord o apply2 #1) |> map #2
wenzelm@68201
   155
        end;
wenzelm@68201
   156
      in if null elems then () else export_body thy export_name elems end;
wenzelm@68165
   157
wenzelm@68165
   158
wenzelm@69003
   159
    (* infix syntax *)
wenzelm@69003
   160
wenzelm@69003
   161
    fun get_infix_const ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_const;
wenzelm@69003
   162
    fun get_infix_type ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_type;
wenzelm@69003
   163
wenzelm@69003
   164
    fun encode_infix {assoc, delim, pri} =
wenzelm@69003
   165
      let
wenzelm@69003
   166
        val ass =
wenzelm@69003
   167
          (case assoc of
wenzelm@69003
   168
            Syntax_Ext.No_Assoc => 0
wenzelm@69003
   169
          | Syntax_Ext.Left_Assoc => 1
wenzelm@69003
   170
          | Syntax_Ext.Right_Assoc => 2);
wenzelm@69003
   171
        open XML.Encode Term_XML.Encode;
wenzelm@69003
   172
      in triple int string int (ass, delim, pri) end;
wenzelm@69003
   173
wenzelm@69003
   174
wenzelm@68201
   175
    (* types *)
wenzelm@68165
   176
wenzelm@68201
   177
    val encode_type =
wenzelm@68201
   178
      let open XML.Encode Term_XML.Encode
wenzelm@69003
   179
      in triple (option encode_infix) (list string) (option typ) end;
wenzelm@68165
   180
wenzelm@69003
   181
    fun export_type c (Type.LogicalType n) =
wenzelm@69003
   182
          SOME (encode_type (get_infix_type thy_ctxt c, Name.invent Name.context Name.aT n, NONE))
wenzelm@69003
   183
      | export_type c (Type.Abbreviation (args, U, false)) =
wenzelm@69003
   184
          SOME (encode_type (get_infix_type thy_ctxt c, args, SOME U))
wenzelm@69003
   185
      | export_type _ _ = NONE;
wenzelm@68165
   186
wenzelm@68201
   187
    val _ =
wenzelm@69003
   188
      export_entities "types" export_type Sign.type_space
wenzelm@68264
   189
        (Name_Space.dest_table (#types rep_tsig));
wenzelm@68201
   190
wenzelm@68173
   191
wenzelm@68201
   192
    (* consts *)
wenzelm@68201
   193
wenzelm@68201
   194
    val encode_const =
wenzelm@69023
   195
      let open XML.Encode Term_XML.Encode
wenzelm@69023
   196
      in pair (option encode_infix) (pair (list string) (pair typ (option term))) end;
wenzelm@68165
   197
wenzelm@68201
   198
    fun export_const c (T, abbrev) =
wenzelm@68201
   199
      let
wenzelm@69003
   200
        val syntax = get_infix_const thy_ctxt c;
wenzelm@68201
   201
        val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
wenzelm@69023
   202
        val abbrev' = abbrev |> Option.map (standard_vars_global #> map_types Type.strip_sorts);
wenzelm@68201
   203
        val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
wenzelm@69023
   204
      in encode_const (syntax, (args, (T', abbrev'))) end;
wenzelm@68165
   205
wenzelm@68201
   206
    val _ =
wenzelm@69023
   207
      export_entities "consts" (SOME oo export_const) Sign.const_space
wenzelm@68201
   208
        (#constants (Consts.dest (Sign.consts_of thy)));
wenzelm@68201
   209
wenzelm@68208
   210
wenzelm@68232
   211
    (* axioms and facts *)
wenzelm@68208
   212
wenzelm@69023
   213
    fun prop_of raw_thm =
wenzelm@68727
   214
      let
wenzelm@68727
   215
        val thm = raw_thm
wenzelm@68727
   216
          |> Thm.transfer thy
wenzelm@68727
   217
          |> Thm.check_hyps (Context.Theory thy)
wenzelm@68727
   218
          |> Thm.strip_shyps;
wenzelm@68727
   219
        val prop = thm
wenzelm@69023
   220
          |> Thm.full_prop_of;
wenzelm@68727
   221
      in (Thm.extra_shyps thm, prop) end;
wenzelm@68539
   222
wenzelm@69023
   223
    fun encode_prop used (Ss, raw_prop) =
wenzelm@68208
   224
      let
wenzelm@69023
   225
        val prop = standard_vars used raw_prop;
wenzelm@69023
   226
        val args = rev (add_frees used prop []);
wenzelm@69023
   227
        val typargs = rev (add_tfrees used prop []);
wenzelm@69023
   228
        val used' = fold (Name.declare o #1) typargs used;
wenzelm@69023
   229
        val sorts = Name.invent used' Name.aT (length Ss) ~~ Ss;
wenzelm@68726
   230
      in
wenzelm@69023
   231
        (sorts @ typargs, args, prop) |>
wenzelm@68727
   232
          let open XML.Encode Term_XML.Encode
wenzelm@68727
   233
          in triple (list (pair string sort)) (list (pair string typ)) term end
wenzelm@68726
   234
      end;
wenzelm@68208
   235
wenzelm@69023
   236
    fun encode_axiom used t = encode_prop used ([], t);
wenzelm@69023
   237
wenzelm@69023
   238
    val encode_fact_single = encode_prop Name.context o prop_of;
wenzelm@69023
   239
    val encode_fact_multi = XML.Encode.list (encode_prop Name.context) o map prop_of;
wenzelm@68208
   240
wenzelm@68232
   241
    val _ =
wenzelm@69023
   242
      export_entities "axioms" (fn _ => fn t => SOME (encode_axiom Name.context t))
wenzelm@69023
   243
        Theory.axiom_space (Theory.axioms_of thy);
wenzelm@68235
   244
    val _ =
wenzelm@69023
   245
      export_entities "facts" (K (SOME o encode_fact_multi))
wenzelm@69023
   246
        (Facts.space_of o Global_Theory.facts_of)
wenzelm@68232
   247
        (Facts.dest_static true [] (Global_Theory.facts_of thy));
wenzelm@68232
   248
wenzelm@68264
   249
wenzelm@68264
   250
    (* type classes *)
wenzelm@68264
   251
wenzelm@68264
   252
    val encode_class =
wenzelm@68264
   253
      let open XML.Encode Term_XML.Encode
wenzelm@69023
   254
      in pair (list (pair string typ)) (list encode_fact_single) end;
wenzelm@68264
   255
wenzelm@68264
   256
    fun export_class name =
wenzelm@68264
   257
      (case try (Axclass.get_info thy) name of
wenzelm@68264
   258
        NONE => ([], [])
wenzelm@69023
   259
      | SOME {params, axioms, ...} => (params, axioms))
wenzelm@68264
   260
      |> encode_class |> SOME;
wenzelm@68264
   261
wenzelm@68264
   262
    val _ =
wenzelm@68264
   263
      export_entities "classes" (fn name => fn () => export_class name)
wenzelm@68264
   264
        Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
wenzelm@68264
   265
wenzelm@68295
   266
wenzelm@68295
   267
    (* sort algebra *)
wenzelm@68295
   268
wenzelm@68295
   269
    val {classrel, arities} =
wenzelm@68295
   270
      Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
wenzelm@68295
   271
        (#2 (#classes rep_tsig));
wenzelm@68295
   272
wenzelm@68295
   273
    val encode_classrel =
wenzelm@68295
   274
      let open XML.Encode
wenzelm@68295
   275
      in list (pair string (list string)) end;
wenzelm@68295
   276
wenzelm@68295
   277
    val encode_arities =
wenzelm@68295
   278
      let open XML.Encode Term_XML.Encode
wenzelm@68295
   279
      in list (triple string (list sort) string) end;
wenzelm@68295
   280
wenzelm@68295
   281
    val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel);
wenzelm@68295
   282
    val _ = if null arities then () else export_body thy "arities" (encode_arities arities);
wenzelm@68295
   283
wenzelm@68862
   284
wenzelm@68862
   285
    (* locales *)
wenzelm@68862
   286
wenzelm@69023
   287
    fun encode_locale used =
wenzelm@69023
   288
      let open XML.Encode Term_XML.Encode
wenzelm@69023
   289
      in triple (list (pair string sort)) (list (pair string typ)) (list (encode_axiom used)) end;
wenzelm@69023
   290
wenzelm@69029
   291
    fun export_locale loc =
wenzelm@68864
   292
      let
wenzelm@69029
   293
        val {typargs, args, axioms} = locale_content thy_ctxt loc;
wenzelm@69023
   294
        val used = fold Name.declare (map #1 typargs @ map #1 args) Name.context;
wenzelm@69027
   295
      in encode_locale used (typargs, args, axioms) end
wenzelm@69027
   296
      handle ERROR msg =>
wenzelm@69027
   297
        cat_error msg ("The error(s) above occurred in locale " ^
wenzelm@69027
   298
          quote (Locale.markup_name thy_ctxt loc));
wenzelm@68862
   299
wenzelm@68862
   300
    val _ =
wenzelm@69029
   301
      export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
wenzelm@69029
   302
        Locale.locale_space
wenzelm@69029
   303
        (map (rpair ()) (Locale.get_locales thy));
wenzelm@68862
   304
wenzelm@68900
   305
wenzelm@68900
   306
    (* parents *)
wenzelm@68900
   307
wenzelm@68900
   308
    val _ =
wenzelm@68900
   309
      export_body thy "parents"
wenzelm@68900
   310
        (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
wenzelm@68900
   311
wenzelm@68295
   312
  in () end);
wenzelm@68165
   313
wenzelm@68165
   314
end;