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