src/Pure/Thy/export_theory.ML
author wenzelm
Sun Aug 05 14:50:11 2018 +0200 (14 months ago)
changeset 68725 367e60d9aa1b
parent 68724 7fafadbf16c7
child 68726 782d6b89fb19
permissions -rw-r--r--
explicit names for bound variables;
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@68201
    38
(* general setup *)
wenzelm@68154
    39
wenzelm@68201
    40
fun setup_presentation f =
wenzelm@68201
    41
  Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
wenzelm@68201
    42
    if Options.bool (#options context) "export_theory" then f context thy else ()));
wenzelm@68154
    43
wenzelm@68201
    44
fun export_body thy name body =
wenzelm@68230
    45
  Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty));
wenzelm@68165
    46
wenzelm@68154
    47
wenzelm@68201
    48
(* presentation *)
wenzelm@68201
    49
wenzelm@68201
    50
val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
wenzelm@68201
    51
  let
wenzelm@68264
    52
    val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
wenzelm@68264
    53
wenzelm@68264
    54
wenzelm@68206
    55
    (* parents *)
wenzelm@68206
    56
wenzelm@68206
    57
    val parents = Theory.parents_of thy;
wenzelm@68206
    58
    val _ =
wenzelm@68206
    59
      export_body thy "parents"
wenzelm@68231
    60
        (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
wenzelm@68206
    61
wenzelm@68206
    62
wenzelm@68201
    63
    (* entities *)
wenzelm@68154
    64
wenzelm@68201
    65
    fun entity_markup space name =
wenzelm@68201
    66
      let
wenzelm@68201
    67
        val {serial, pos, ...} = Name_Space.the_entry space name;
wenzelm@68201
    68
        val props =
wenzelm@68201
    69
          Markup.serial_properties serial @
wenzelm@68201
    70
          Position.offset_properties_of (adjust_pos pos);
wenzelm@68201
    71
      in (Markup.entityN, (Markup.nameN, name) :: props) end;
wenzelm@68154
    72
wenzelm@68201
    73
    fun export_entities export_name export get_space decls =
wenzelm@68201
    74
      let val elems =
wenzelm@68201
    75
        let
wenzelm@68206
    76
          val parent_spaces = map get_space parents;
wenzelm@68201
    77
          val space = get_space thy;
wenzelm@68201
    78
        in
wenzelm@68201
    79
          (decls, []) |-> fold (fn (name, decl) =>
wenzelm@68201
    80
            if exists (fn space => Name_Space.declared space name) parent_spaces then I
wenzelm@68201
    81
            else
wenzelm@68201
    82
              (case export name decl of
wenzelm@68201
    83
                NONE => I
wenzelm@68724
    84
              | SOME body =>
wenzelm@68724
    85
                  cons (#serial (Name_Space.the_entry space name),
wenzelm@68724
    86
                    XML.Elem (entity_markup space name, body))))
wenzelm@68724
    87
          |> sort (int_ord o apply2 #1) |> map #2
wenzelm@68201
    88
        end;
wenzelm@68724
    89
wenzelm@68201
    90
      in if null elems then () else export_body thy export_name elems end;
wenzelm@68165
    91
wenzelm@68165
    92
wenzelm@68201
    93
    (* types *)
wenzelm@68165
    94
wenzelm@68201
    95
    val encode_type =
wenzelm@68201
    96
      let open XML.Encode Term_XML.Encode
wenzelm@68201
    97
      in pair (list string) (option typ) end;
wenzelm@68165
    98
wenzelm@68201
    99
    fun export_type (Type.LogicalType n) =
wenzelm@68201
   100
          SOME (encode_type (Name.invent Name.context Name.aT n, NONE))
wenzelm@68201
   101
      | export_type (Type.Abbreviation (args, U, false)) =
wenzelm@68201
   102
          SOME (encode_type (args, SOME U))
wenzelm@68201
   103
      | export_type _ = NONE;
wenzelm@68165
   104
wenzelm@68201
   105
    val _ =
wenzelm@68201
   106
      export_entities "types" (K export_type) Sign.type_space
wenzelm@68264
   107
        (Name_Space.dest_table (#types rep_tsig));
wenzelm@68201
   108
wenzelm@68173
   109
wenzelm@68201
   110
    (* consts *)
wenzelm@68201
   111
wenzelm@68201
   112
    val encode_const =
wenzelm@68201
   113
      let open XML.Encode Term_XML.Encode
wenzelm@68725
   114
      in triple (list string) typ (option (term o named_bounds)) end;
wenzelm@68165
   115
wenzelm@68201
   116
    fun export_const c (T, abbrev) =
wenzelm@68201
   117
      let
wenzelm@68201
   118
        val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
wenzelm@68201
   119
        val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts);
wenzelm@68201
   120
        val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
wenzelm@68201
   121
      in SOME (encode_const (args, T', abbrev')) end;
wenzelm@68165
   122
wenzelm@68201
   123
    val _ =
wenzelm@68201
   124
      export_entities "consts" export_const Sign.const_space
wenzelm@68201
   125
        (#constants (Consts.dest (Sign.consts_of thy)));
wenzelm@68201
   126
wenzelm@68208
   127
wenzelm@68232
   128
    (* axioms and facts *)
wenzelm@68208
   129
wenzelm@68539
   130
    val standard_prop_of =
wenzelm@68539
   131
      Thm.transfer thy #>
wenzelm@68539
   132
      Thm.check_hyps (Context.Theory thy) #>
wenzelm@68539
   133
      Drule.sort_constraint_intr_shyps #>
wenzelm@68539
   134
      Thm.full_prop_of;
wenzelm@68539
   135
wenzelm@68232
   136
    val encode_props =
wenzelm@68208
   137
      let open XML.Encode Term_XML.Encode
wenzelm@68725
   138
      in triple (list (pair string sort)) (list (pair string typ)) (list (term o named_bounds)) end;
wenzelm@68208
   139
wenzelm@68232
   140
    fun export_props props =
wenzelm@68208
   141
      let
wenzelm@68232
   142
        val props' = map Logic.unvarify_global props;
wenzelm@68232
   143
        val typargs = rev (fold Term.add_tfrees props' []);
wenzelm@68232
   144
        val args = rev (fold Term.add_frees props' []);
wenzelm@68232
   145
      in encode_props (typargs, args, props') end;
wenzelm@68208
   146
wenzelm@68235
   147
    val export_axiom = export_props o single;
wenzelm@68539
   148
    val export_fact = export_props o Term_Subst.zero_var_indexes_list o map standard_prop_of;
wenzelm@68208
   149
wenzelm@68232
   150
    val _ =
wenzelm@68235
   151
      export_entities "axioms" (K (SOME o export_axiom)) Theory.axiom_space
wenzelm@68235
   152
        (Theory.axioms_of thy);
wenzelm@68235
   153
    val _ =
wenzelm@68235
   154
      export_entities "facts" (K (SOME o export_fact)) (Facts.space_of o Global_Theory.facts_of)
wenzelm@68232
   155
        (Facts.dest_static true [] (Global_Theory.facts_of thy));
wenzelm@68232
   156
wenzelm@68264
   157
wenzelm@68264
   158
    (* type classes *)
wenzelm@68264
   159
wenzelm@68264
   160
    val encode_class =
wenzelm@68264
   161
      let open XML.Encode Term_XML.Encode
wenzelm@68725
   162
      in pair (list (pair string typ)) (list (term o named_bounds)) end;
wenzelm@68264
   163
wenzelm@68264
   164
    fun export_class name =
wenzelm@68264
   165
      (case try (Axclass.get_info thy) name of
wenzelm@68264
   166
        NONE => ([], [])
wenzelm@68264
   167
      | SOME {params, axioms, ...} =>
wenzelm@68264
   168
          (params, map (Logic.unvarify_global o Thm.full_prop_of) axioms))
wenzelm@68264
   169
      |> encode_class |> SOME;
wenzelm@68264
   170
wenzelm@68264
   171
    val _ =
wenzelm@68264
   172
      export_entities "classes" (fn name => fn () => export_class name)
wenzelm@68264
   173
        Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
wenzelm@68264
   174
wenzelm@68295
   175
wenzelm@68295
   176
    (* sort algebra *)
wenzelm@68295
   177
wenzelm@68295
   178
    val {classrel, arities} =
wenzelm@68295
   179
      Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
wenzelm@68295
   180
        (#2 (#classes rep_tsig));
wenzelm@68295
   181
wenzelm@68295
   182
    val encode_classrel =
wenzelm@68295
   183
      let open XML.Encode
wenzelm@68295
   184
      in list (pair string (list string)) end;
wenzelm@68295
   185
wenzelm@68295
   186
    val encode_arities =
wenzelm@68295
   187
      let open XML.Encode Term_XML.Encode
wenzelm@68295
   188
      in list (triple string (list sort) string) end;
wenzelm@68295
   189
wenzelm@68295
   190
    val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel);
wenzelm@68295
   191
    val _ = if null arities then () else export_body thy "arities" (encode_arities arities);
wenzelm@68295
   192
wenzelm@68295
   193
  in () end);
wenzelm@68165
   194
wenzelm@68165
   195
end;