author | wenzelm |
Fri, 31 Aug 2018 15:48:37 +0200 | |
changeset 68862 | 47e9912c53c3 |
parent 68830 | 44ec6fdaacf8 |
child 68864 | 1dacce27bc25 |
permissions | -rw-r--r-- |
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 |
|
68201 | 9 |
val setup_presentation: (Thy_Info.presentation_context -> theory -> unit) -> unit |
10 |
val export_body: theory -> string -> XML.body -> unit |
|
68154 | 11 |
end; |
12 |
||
13 |
structure Export_Theory: EXPORT_THEORY = |
|
14 |
struct |
|
15 |
||
68725 | 16 |
(* names for bound variables *) |
17 |
||
18 |
local |
|
19 |
fun declare_names (Abs (_, _, b)) = declare_names b |
|
20 |
| declare_names (t $ u) = declare_names t #> declare_names u |
|
21 |
| declare_names (Const (c, _)) = Name.declare (Long_Name.base_name c) |
|
22 |
| declare_names (Free (x, _)) = Name.declare x |
|
23 |
| declare_names _ = I; |
|
24 |
||
25 |
fun variant_abs bs (Abs (x, T, t)) = |
|
26 |
let |
|
27 |
val names = fold Name.declare bs (declare_names t Name.context); |
|
28 |
val x' = #1 (Name.variant x names); |
|
29 |
val t' = variant_abs (x' :: bs) t; |
|
30 |
in Abs (x', T, t') end |
|
31 |
| variant_abs bs (t $ u) = variant_abs bs t $ variant_abs bs u |
|
32 |
| variant_abs _ t = t; |
|
33 |
in |
|
34 |
val named_bounds = variant_abs []; |
|
35 |
end; |
|
36 |
||
37 |
||
68201 | 38 |
(* general setup *) |
68154 | 39 |
|
68201 | 40 |
fun setup_presentation f = |
41 |
Theory.setup (Thy_Info.add_presentation (fn context => fn thy => |
|
42 |
if Options.bool (#options context) "export_theory" then f context thy else ())); |
|
68154 | 43 |
|
68201 | 44 |
fun export_body thy name body = |
68230 | 45 |
Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty)); |
68165 | 46 |
|
68154 | 47 |
|
68201 | 48 |
(* presentation *) |
49 |
||
50 |
val _ = setup_presentation (fn {adjust_pos, ...} => fn thy => |
|
51 |
let |
|
68264 | 52 |
val rep_tsig = Type.rep_tsig (Sign.tsig_of thy); |
53 |
||
54 |
||
68206 | 55 |
(* parents *) |
56 |
||
57 |
val parents = Theory.parents_of thy; |
|
58 |
val _ = |
|
59 |
export_body thy "parents" |
|
68231 | 60 |
(XML.Encode.string (cat_lines (map Context.theory_long_name parents))); |
68206 | 61 |
|
62 |
||
68201 | 63 |
(* entities *) |
68154 | 64 |
|
68201 | 65 |
fun entity_markup space name = |
66 |
let |
|
67 |
val {serial, pos, ...} = Name_Space.the_entry space name; |
|
68 |
val props = |
|
68830
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68727
diff
changeset
|
69 |
Position.offset_properties_of (adjust_pos pos) @ |
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68727
diff
changeset
|
70 |
Position.id_properties_of pos @ |
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68727
diff
changeset
|
71 |
Markup.serial_properties serial; |
68201 | 72 |
in (Markup.entityN, (Markup.nameN, name) :: props) end; |
68154 | 73 |
|
68201 | 74 |
fun export_entities export_name export get_space decls = |
75 |
let val elems = |
|
76 |
let |
|
68206 | 77 |
val parent_spaces = map get_space parents; |
68201 | 78 |
val space = get_space thy; |
79 |
in |
|
80 |
(decls, []) |-> fold (fn (name, decl) => |
|
81 |
if exists (fn space => Name_Space.declared space name) parent_spaces then I |
|
82 |
else |
|
83 |
(case export name decl of |
|
84 |
NONE => I |
|
68724 | 85 |
| SOME body => |
86 |
cons (#serial (Name_Space.the_entry space name), |
|
87 |
XML.Elem (entity_markup space name, body)))) |
|
88 |
|> sort (int_ord o apply2 #1) |> map #2 |
|
68201 | 89 |
end; |
90 |
in if null elems then () else export_body thy export_name elems end; |
|
68165 | 91 |
|
92 |
||
68201 | 93 |
(* types *) |
68165 | 94 |
|
68201 | 95 |
val encode_type = |
96 |
let open XML.Encode Term_XML.Encode |
|
97 |
in pair (list string) (option typ) end; |
|
68165 | 98 |
|
68201 | 99 |
fun export_type (Type.LogicalType n) = |
100 |
SOME (encode_type (Name.invent Name.context Name.aT n, NONE)) |
|
101 |
| export_type (Type.Abbreviation (args, U, false)) = |
|
102 |
SOME (encode_type (args, SOME U)) |
|
103 |
| export_type _ = NONE; |
|
68165 | 104 |
|
68201 | 105 |
val _ = |
106 |
export_entities "types" (K export_type) Sign.type_space |
|
68264 | 107 |
(Name_Space.dest_table (#types rep_tsig)); |
68201 | 108 |
|
68173 | 109 |
|
68201 | 110 |
(* consts *) |
111 |
||
112 |
val encode_const = |
|
113 |
let open XML.Encode Term_XML.Encode |
|
68725 | 114 |
in triple (list string) typ (option (term o named_bounds)) end; |
68165 | 115 |
|
68201 | 116 |
fun export_const c (T, abbrev) = |
117 |
let |
|
118 |
val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts; |
|
119 |
val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts); |
|
120 |
val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T')); |
|
121 |
in SOME (encode_const (args, T', abbrev')) end; |
|
68165 | 122 |
|
68201 | 123 |
val _ = |
124 |
export_entities "consts" export_const Sign.const_space |
|
125 |
(#constants (Consts.dest (Sign.consts_of thy))); |
|
126 |
||
68208 | 127 |
|
68232 | 128 |
(* axioms and facts *) |
68208 | 129 |
|
68727 | 130 |
fun standard_prop_of raw_thm = |
131 |
let |
|
132 |
val thm = raw_thm |
|
133 |
|> Thm.transfer thy |
|
134 |
|> Thm.check_hyps (Context.Theory thy) |
|
135 |
|> Thm.strip_shyps; |
|
136 |
val prop = thm |
|
137 |
|> Thm.full_prop_of |
|
138 |
|> Term_Subst.zero_var_indexes; |
|
139 |
in (Thm.extra_shyps thm, prop) end; |
|
68539 | 140 |
|
68727 | 141 |
fun encode_prop (Ss, prop) = |
68208 | 142 |
let |
68726 | 143 |
val prop' = Logic.unvarify_global (named_bounds prop); |
144 |
val typargs = rev (Term.add_tfrees prop' []); |
|
68727 | 145 |
val sorts = Name.invent (Name.make_context (map #1 typargs)) Name.aT (length Ss) ~~ Ss; |
68726 | 146 |
val args = rev (Term.add_frees prop' []); |
147 |
in |
|
68727 | 148 |
(sorts @ typargs, args, prop') |> |
149 |
let open XML.Encode Term_XML.Encode |
|
150 |
in triple (list (pair string sort)) (list (pair string typ)) term end |
|
68726 | 151 |
end; |
68208 | 152 |
|
68726 | 153 |
val encode_fact = XML.Encode.list encode_prop o map standard_prop_of; |
68208 | 154 |
|
68232 | 155 |
val _ = |
68727 | 156 |
export_entities "axioms" (fn _ => fn t => SOME (encode_prop ([], t))) Theory.axiom_space |
68235
a3bd410db5b2
standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents:
68232
diff
changeset
|
157 |
(Theory.axioms_of thy); |
a3bd410db5b2
standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents:
68232
diff
changeset
|
158 |
val _ = |
68726 | 159 |
export_entities "facts" (K (SOME o encode_fact)) (Facts.space_of o Global_Theory.facts_of) |
68232 | 160 |
(Facts.dest_static true [] (Global_Theory.facts_of thy)); |
161 |
||
68264 | 162 |
|
163 |
(* type classes *) |
|
164 |
||
165 |
val encode_class = |
|
166 |
let open XML.Encode Term_XML.Encode |
|
68725 | 167 |
in pair (list (pair string typ)) (list (term o named_bounds)) end; |
68264 | 168 |
|
169 |
fun export_class name = |
|
170 |
(case try (Axclass.get_info thy) name of |
|
171 |
NONE => ([], []) |
|
172 |
| SOME {params, axioms, ...} => |
|
173 |
(params, map (Logic.unvarify_global o Thm.full_prop_of) axioms)) |
|
174 |
|> encode_class |> SOME; |
|
175 |
||
176 |
val _ = |
|
177 |
export_entities "classes" (fn name => fn () => export_class name) |
|
178 |
Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig))))); |
|
179 |
||
68295 | 180 |
|
181 |
(* sort algebra *) |
|
182 |
||
183 |
val {classrel, arities} = |
|
184 |
Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents) |
|
185 |
(#2 (#classes rep_tsig)); |
|
186 |
||
187 |
val encode_classrel = |
|
188 |
let open XML.Encode |
|
189 |
in list (pair string (list string)) end; |
|
190 |
||
191 |
val encode_arities = |
|
192 |
let open XML.Encode Term_XML.Encode |
|
193 |
in list (triple string (list sort) string) end; |
|
194 |
||
195 |
val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel); |
|
196 |
val _ = if null arities then () else export_body thy "arities" (encode_arities arities); |
|
197 |
||
68862 | 198 |
|
199 |
(* locales *) |
|
200 |
||
201 |
fun encode_locale {type_params, params, asm, defs} = |
|
202 |
(type_params, (map #1 params, (asm, defs))) |> |
|
203 |
let open XML.Encode Term_XML.Encode in |
|
204 |
pair (list (pair string sort)) |
|
205 |
(pair (list (pair string typ)) |
|
206 |
(pair (option term) (list term))) |
|
207 |
end; |
|
208 |
||
209 |
val _ = |
|
210 |
export_entities "locales" (fn _ => SOME o encode_locale) Locale.locale_space |
|
211 |
(Locale.dest_locales thy); |
|
212 |
||
68295 | 213 |
in () end); |
68165 | 214 |
|
215 |
end; |