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;
     1 (*  Title:      Pure/Thy/export_theory.ML
     2     Author:     Makarius
     3 
     4 Export foundational theory content.
     5 *)
     6 
     7 signature EXPORT_THEORY =
     8 sig
     9   val setup_presentation: (Thy_Info.presentation_context -> theory -> unit) -> unit
    10   val export_body: theory -> string -> XML.body -> unit
    11 end;
    12 
    13 structure Export_Theory: EXPORT_THEORY =
    14 struct
    15 
    16 (* names for bound variables *)
    17 
    18 local
    19   fun declare_names (Abs (_, _, b)) = declare_names b
    20     | declare_names (t $ u) = declare_names t #> declare_names u
    21     | declare_names (Const (c, _)) = Name.declare (Long_Name.base_name c)
    22     | declare_names (Free (x, _)) = Name.declare x
    23     | declare_names _ = I;
    24 
    25   fun variant_abs bs (Abs (x, T, t)) =
    26         let
    27           val names = fold Name.declare bs (declare_names t Name.context);
    28           val x' = #1 (Name.variant x names);
    29           val t' = variant_abs (x' :: bs) t;
    30         in Abs (x', T, t') end
    31     | variant_abs bs (t $ u) = variant_abs bs t $ variant_abs bs u
    32     | variant_abs _ t = t;
    33 in
    34   val named_bounds = variant_abs [];
    35 end;
    36 
    37 
    38 (* general setup *)
    39 
    40 fun setup_presentation f =
    41   Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
    42     if Options.bool (#options context) "export_theory" then f context thy else ()));
    43 
    44 fun export_body thy name body =
    45   Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty));
    46 
    47 
    48 (* presentation *)
    49 
    50 val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
    51   let
    52     val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
    53 
    54 
    55     (* parents *)
    56 
    57     val parents = Theory.parents_of thy;
    58     val _ =
    59       export_body thy "parents"
    60         (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
    61 
    62 
    63     (* entities *)
    64 
    65     fun entity_markup space name =
    66       let
    67         val {serial, pos, ...} = Name_Space.the_entry space name;
    68         val props =
    69           Markup.serial_properties serial @
    70           Position.offset_properties_of (adjust_pos pos);
    71       in (Markup.entityN, (Markup.nameN, name) :: props) end;
    72 
    73     fun export_entities export_name export get_space decls =
    74       let val elems =
    75         let
    76           val parent_spaces = map get_space parents;
    77           val space = get_space thy;
    78         in
    79           (decls, []) |-> fold (fn (name, decl) =>
    80             if exists (fn space => Name_Space.declared space name) parent_spaces then I
    81             else
    82               (case export name decl of
    83                 NONE => I
    84               | SOME body =>
    85                   cons (#serial (Name_Space.the_entry space name),
    86                     XML.Elem (entity_markup space name, body))))
    87           |> sort (int_ord o apply2 #1) |> map #2
    88         end;
    89 
    90       in if null elems then () else export_body thy export_name elems end;
    91 
    92 
    93     (* types *)
    94 
    95     val encode_type =
    96       let open XML.Encode Term_XML.Encode
    97       in pair (list string) (option typ) end;
    98 
    99     fun export_type (Type.LogicalType n) =
   100           SOME (encode_type (Name.invent Name.context Name.aT n, NONE))
   101       | export_type (Type.Abbreviation (args, U, false)) =
   102           SOME (encode_type (args, SOME U))
   103       | export_type _ = NONE;
   104 
   105     val _ =
   106       export_entities "types" (K export_type) Sign.type_space
   107         (Name_Space.dest_table (#types rep_tsig));
   108 
   109 
   110     (* consts *)
   111 
   112     val encode_const =
   113       let open XML.Encode Term_XML.Encode
   114       in triple (list string) typ (option (term o named_bounds)) end;
   115 
   116     fun export_const c (T, abbrev) =
   117       let
   118         val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
   119         val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts);
   120         val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
   121       in SOME (encode_const (args, T', abbrev')) end;
   122 
   123     val _ =
   124       export_entities "consts" export_const Sign.const_space
   125         (#constants (Consts.dest (Sign.consts_of thy)));
   126 
   127 
   128     (* axioms and facts *)
   129 
   130     val standard_prop_of =
   131       Thm.transfer thy #>
   132       Thm.check_hyps (Context.Theory thy) #>
   133       Drule.sort_constraint_intr_shyps #>
   134       Thm.full_prop_of;
   135 
   136     val encode_props =
   137       let open XML.Encode Term_XML.Encode
   138       in triple (list (pair string sort)) (list (pair string typ)) (list (term o named_bounds)) end;
   139 
   140     fun export_props props =
   141       let
   142         val props' = map Logic.unvarify_global props;
   143         val typargs = rev (fold Term.add_tfrees props' []);
   144         val args = rev (fold Term.add_frees props' []);
   145       in encode_props (typargs, args, props') end;
   146 
   147     val export_axiom = export_props o single;
   148     val export_fact = export_props o Term_Subst.zero_var_indexes_list o map standard_prop_of;
   149 
   150     val _ =
   151       export_entities "axioms" (K (SOME o export_axiom)) Theory.axiom_space
   152         (Theory.axioms_of thy);
   153     val _ =
   154       export_entities "facts" (K (SOME o export_fact)) (Facts.space_of o Global_Theory.facts_of)
   155         (Facts.dest_static true [] (Global_Theory.facts_of thy));
   156 
   157 
   158     (* type classes *)
   159 
   160     val encode_class =
   161       let open XML.Encode Term_XML.Encode
   162       in pair (list (pair string typ)) (list (term o named_bounds)) end;
   163 
   164     fun export_class name =
   165       (case try (Axclass.get_info thy) name of
   166         NONE => ([], [])
   167       | SOME {params, axioms, ...} =>
   168           (params, map (Logic.unvarify_global o Thm.full_prop_of) axioms))
   169       |> encode_class |> SOME;
   170 
   171     val _ =
   172       export_entities "classes" (fn name => fn () => export_class name)
   173         Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
   174 
   175 
   176     (* sort algebra *)
   177 
   178     val {classrel, arities} =
   179       Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
   180         (#2 (#classes rep_tsig));
   181 
   182     val encode_classrel =
   183       let open XML.Encode
   184       in list (pair string (list string)) end;
   185 
   186     val encode_arities =
   187       let open XML.Encode Term_XML.Encode
   188       in list (triple string (list sort) string) end;
   189 
   190     val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel);
   191     val _ = if null arities then () else export_body thy "arities" (encode_arities arities);
   192 
   193   in () end);
   194 
   195 end;