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