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