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