src/Pure/Thy/export_theory.ML
author wenzelm
Mon May 14 11:29:22 2018 +0200 (17 months ago)
changeset 68180 112d9624c020
parent 68173 7ed88a534bb6
child 68199 f551dd2178ab
permissions -rw-r--r--
more general presentation hook, with document preparation as application;
     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 entity_markup: Name_Space.T -> string -> Markup.T
    10 end;
    11 
    12 structure Export_Theory: EXPORT_THEORY =
    13 struct
    14 
    15 (* name space entries *)
    16 
    17 fun entity_markup space name =
    18   let
    19     val {serial, pos, ...} = Name_Space.the_entry space name;
    20     val props = Markup.serial_properties serial @ Position.offset_properties_of pos;
    21   in (Markup.entityN, (Markup.nameN, name) :: props) end;
    22 
    23 fun export_decls export_decl parents thy space decls =
    24   (decls, []) |-> fold (fn (name, decl) =>
    25     if exists (fn space => Name_Space.declared space name) parents then I
    26     else
    27       (case export_decl thy name decl of
    28         NONE => I
    29       | SOME body => cons (name, XML.Elem (entity_markup space name, body))))
    30   |> sort_by #1 |> map #2;
    31 
    32 
    33 (* present *)
    34 
    35 fun present get_space get_decls export name thy =
    36   if Options.default_bool "export_theory" then
    37     (case export (map get_space (Theory.parents_of thy)) thy (get_space thy) (get_decls thy) of
    38       [] => ()
    39     | body => Export.export thy ("theory/" ^ name) [YXML.string_of_body body])
    40   else ();
    41 
    42 fun present_decls get_space get_decls =
    43   present get_space get_decls o export_decls;
    44 
    45 fun setup_presentation f = Theory.setup (Thy_Info.add_presentation (K f));
    46 
    47 
    48 (* types *)
    49 
    50 local
    51 
    52 val present_types =
    53   present_decls Sign.type_space (Name_Space.dest_table o #types o Type.rep_tsig o Sign.tsig_of);
    54 
    55 val encode_type =
    56   let open XML.Encode Term_XML.Encode
    57   in pair (list string) (option typ) end;
    58 
    59 fun export_type (Type.LogicalType n) = SOME (encode_type (Name.invent Name.context Name.aT n, NONE))
    60   | export_type (Type.Abbreviation (args, U, false)) = SOME (encode_type (args, SOME U))
    61   | export_type _ = NONE;
    62 
    63 in
    64 
    65 val _ = setup_presentation (present_types (K (K export_type)) "types");
    66 
    67 end;
    68 
    69 
    70 (* consts *)
    71 
    72 local
    73 
    74 val present_consts =
    75   present_decls Sign.const_space (#constants o Consts.dest o Sign.consts_of);
    76 
    77 val encode_const =
    78   let open XML.Encode Term_XML.Encode
    79   in triple (list string) typ (option term) end;
    80 
    81 fun export_const thy c (T, abbrev) =
    82   let
    83     val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
    84     val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts);
    85     val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
    86   in SOME (encode_const (args, T', abbrev')) end;
    87 
    88 in
    89 
    90 val _ = setup_presentation (present_consts export_const "consts");
    91 
    92 end;
    93 
    94 end;