src/Pure/Thy/export_theory.ML
author wenzelm
Sun May 13 15:55:30 2018 +0200 (17 months ago)
changeset 68165 a7a2174ac014
parent 68154 42d63ea39161
child 68170 7e1daf6f2578
permissions -rw-r--r--
more exports;
misc tuning and clarification;
     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 end;
    10 
    11 structure Export_Theory: EXPORT_THEORY =
    12 struct
    13 
    14 (* name space entries *)
    15 
    16 fun export_decls export_decl parents space decls =
    17   (decls, []) |-> fold (fn (name, decl) =>
    18     if exists (fn space => Name_Space.declared space name) parents then I
    19     else
    20       (case export_decl decl of
    21         NONE => I
    22       | SOME body =>
    23           let val markup = Name_Space.markup_def space name
    24           in cons (name, XML.Elem (markup, body)) end))
    25   |> sort_by #1 |> map #2;
    26 
    27 
    28 (* present *)
    29 
    30 fun present get_space get_decls export name thy =
    31   if Options.default_bool "export_theory" then
    32     (case export (map get_space (Theory.parents_of thy)) (get_space thy) (get_decls thy) of
    33       [] => ()
    34     | body => Export.export thy ("theory/" ^ name) [YXML.string_of_body body])
    35   else ();
    36 
    37 fun present_decls get_space get_decls =
    38   present get_space get_decls o export_decls;
    39 
    40 val setup_presentation = Theory.setup o Thy_Info.add_presentation;
    41 
    42 
    43 (* types *)
    44 
    45 local
    46 
    47 val present_types =
    48   present_decls Sign.type_space (Name_Space.dest_table o #types o Type.rep_tsig o Sign.tsig_of);
    49 
    50 val encode_type =
    51   let open XML.Encode Term_XML.Encode
    52   in pair (list string) (option typ) end;
    53 
    54 fun export_type (Type.LogicalType n) = SOME (encode_type (Name.invent Name.context Name.aT n, NONE))
    55   | export_type (Type.Abbreviation (args, U, false)) = SOME (encode_type (args, SOME U))
    56   | export_type _ = NONE;
    57 
    58 in
    59 
    60 val _ = setup_presentation (present_types export_type "types");
    61 
    62 end;
    63 
    64 
    65 (* consts *)
    66 
    67 local
    68 
    69 val present_consts =
    70   present_decls Sign.const_space (#constants o Consts.dest o Sign.consts_of);
    71 
    72 val encode_const =
    73   let open XML.Encode Term_XML.Encode
    74   in pair typ (option term) end;
    75 
    76 in
    77 
    78 val _ = setup_presentation (present_consts (SOME o encode_const) "consts");
    79 
    80 end;
    81 
    82 end;