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