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