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