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