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;
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@68154
     9
end;
wenzelm@68154
    10
wenzelm@68154
    11
structure Export_Theory: EXPORT_THEORY =
wenzelm@68154
    12
struct
wenzelm@68154
    13
wenzelm@68165
    14
(* name space entries *)
wenzelm@68154
    15
wenzelm@68199
    16
fun entity_markup adjust_pos space name =
wenzelm@68170
    17
  let
wenzelm@68170
    18
    val {serial, pos, ...} = Name_Space.the_entry space name;
wenzelm@68199
    19
    val props = Markup.serial_properties serial @ Position.offset_properties_of (adjust_pos pos);
wenzelm@68172
    20
  in (Markup.entityN, (Markup.nameN, name) :: props) end;
wenzelm@68170
    21
wenzelm@68199
    22
fun export_decls export_decl parents thy adjust_pos space decls =
wenzelm@68165
    23
  (decls, []) |-> fold (fn (name, decl) =>
wenzelm@68165
    24
    if exists (fn space => Name_Space.declared space name) parents then I
wenzelm@68154
    25
    else
wenzelm@68173
    26
      (case export_decl thy name decl of
wenzelm@68154
    27
        NONE => I
wenzelm@68199
    28
      | SOME body => cons (name, XML.Elem (entity_markup adjust_pos space name, body))))
wenzelm@68154
    29
  |> sort_by #1 |> map #2;
wenzelm@68154
    30
wenzelm@68154
    31
wenzelm@68154
    32
(* present *)
wenzelm@68154
    33
wenzelm@68199
    34
fun present get_space get_decls export name adjust_pos thy =
wenzelm@68154
    35
  if Options.default_bool "export_theory" then
wenzelm@68199
    36
    (case
wenzelm@68199
    37
      export (map get_space (Theory.parents_of thy)) thy adjust_pos (get_space thy) (get_decls thy)
wenzelm@68199
    38
     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@68199
    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@68199
    66
val _ = setup_presentation (present_types (K (K export_type)) "types" o #adjust_pos);
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@68173
    80
  in triple (list string) typ (option term) end;
wenzelm@68173
    81
wenzelm@68173
    82
fun export_const thy c (T, abbrev) =
wenzelm@68173
    83
  let
wenzelm@68173
    84
    val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
wenzelm@68173
    85
    val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts);
wenzelm@68173
    86
    val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
wenzelm@68173
    87
  in SOME (encode_const (args, T', abbrev')) end;
wenzelm@68165
    88
wenzelm@68165
    89
in
wenzelm@68165
    90
wenzelm@68199
    91
val _ = setup_presentation (present_consts export_const "consts" o #adjust_pos);
wenzelm@68165
    92
wenzelm@68165
    93
end;
wenzelm@68165
    94
wenzelm@68165
    95
end;