author | wenzelm |
Sun, 16 Sep 2018 22:45:34 +0200 | |
changeset 69003 | a015f1d3ba0c |
parent 68997 | 4278947ba336 |
child 69019 | a6ba77af6486 |
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 |
|
68900
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
52 |
val parents = Theory.parents_of thy; |
68264 | 53 |
val rep_tsig = Type.rep_tsig (Sign.tsig_of thy); |
54 |
||
69003 | 55 |
val thy_ctxt = Proof_Context.init_global thy; |
68997 | 56 |
|
68264 | 57 |
|
68201 | 58 |
(* entities *) |
68154 | 59 |
|
68201 | 60 |
fun entity_markup space name = |
61 |
let |
|
69003 | 62 |
val xname = Name_Space.extern_shortest thy_ctxt space name; |
68201 | 63 |
val {serial, pos, ...} = Name_Space.the_entry space name; |
64 |
val props = |
|
68830
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68727
diff
changeset
|
65 |
Position.offset_properties_of (adjust_pos pos) @ |
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68727
diff
changeset
|
66 |
Position.id_properties_of pos @ |
44ec6fdaacf8
retain original id, which is command_id/exec_id for PIDE;
wenzelm
parents:
68727
diff
changeset
|
67 |
Markup.serial_properties serial; |
68997 | 68 |
in (Markup.entityN, (Markup.nameN, name) :: (Markup.xnameN, xname) :: props) end; |
68154 | 69 |
|
68201 | 70 |
fun export_entities export_name export get_space decls = |
71 |
let val elems = |
|
72 |
let |
|
68206 | 73 |
val parent_spaces = map get_space parents; |
68201 | 74 |
val space = get_space thy; |
75 |
in |
|
76 |
(decls, []) |-> fold (fn (name, decl) => |
|
77 |
if exists (fn space => Name_Space.declared space name) parent_spaces then I |
|
78 |
else |
|
79 |
(case export name decl of |
|
80 |
NONE => I |
|
68724 | 81 |
| SOME body => |
82 |
cons (#serial (Name_Space.the_entry space name), |
|
83 |
XML.Elem (entity_markup space name, body)))) |
|
84 |
|> sort (int_ord o apply2 #1) |> map #2 |
|
68201 | 85 |
end; |
86 |
in if null elems then () else export_body thy export_name elems end; |
|
68165 | 87 |
|
88 |
||
69003 | 89 |
(* infix syntax *) |
90 |
||
91 |
fun get_infix_const ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_const; |
|
92 |
fun get_infix_type ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_type; |
|
93 |
||
94 |
fun encode_infix {assoc, delim, pri} = |
|
95 |
let |
|
96 |
val ass = |
|
97 |
(case assoc of |
|
98 |
Syntax_Ext.No_Assoc => 0 |
|
99 |
| Syntax_Ext.Left_Assoc => 1 |
|
100 |
| Syntax_Ext.Right_Assoc => 2); |
|
101 |
open XML.Encode Term_XML.Encode; |
|
102 |
in triple int string int (ass, delim, pri) end; |
|
103 |
||
104 |
||
68201 | 105 |
(* types *) |
68165 | 106 |
|
68201 | 107 |
val encode_type = |
108 |
let open XML.Encode Term_XML.Encode |
|
69003 | 109 |
in triple (option encode_infix) (list string) (option typ) end; |
68165 | 110 |
|
69003 | 111 |
fun export_type c (Type.LogicalType n) = |
112 |
SOME (encode_type (get_infix_type thy_ctxt c, Name.invent Name.context Name.aT n, NONE)) |
|
113 |
| export_type c (Type.Abbreviation (args, U, false)) = |
|
114 |
SOME (encode_type (get_infix_type thy_ctxt c, args, SOME U)) |
|
115 |
| export_type _ _ = NONE; |
|
68165 | 116 |
|
68201 | 117 |
val _ = |
69003 | 118 |
export_entities "types" export_type Sign.type_space |
68264 | 119 |
(Name_Space.dest_table (#types rep_tsig)); |
68201 | 120 |
|
68173 | 121 |
|
68201 | 122 |
(* consts *) |
123 |
||
124 |
val encode_const = |
|
69003 | 125 |
let open XML.Encode Term_XML.Encode in |
126 |
pair (option encode_infix) (pair (list string) (pair typ (option (term o named_bounds)))) |
|
127 |
end; |
|
68165 | 128 |
|
68201 | 129 |
fun export_const c (T, abbrev) = |
130 |
let |
|
69003 | 131 |
val syntax = get_infix_const thy_ctxt c; |
68201 | 132 |
val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts; |
133 |
val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts); |
|
134 |
val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T')); |
|
69003 | 135 |
in SOME (encode_const (syntax, (args, (T', abbrev')))) end; |
68165 | 136 |
|
68201 | 137 |
val _ = |
138 |
export_entities "consts" export_const Sign.const_space |
|
139 |
(#constants (Consts.dest (Sign.consts_of thy))); |
|
140 |
||
68208 | 141 |
|
68232 | 142 |
(* axioms and facts *) |
68208 | 143 |
|
68727 | 144 |
fun standard_prop_of raw_thm = |
145 |
let |
|
146 |
val thm = raw_thm |
|
147 |
|> Thm.transfer thy |
|
148 |
|> Thm.check_hyps (Context.Theory thy) |
|
149 |
|> Thm.strip_shyps; |
|
150 |
val prop = thm |
|
151 |
|> Thm.full_prop_of |
|
152 |
|> Term_Subst.zero_var_indexes; |
|
153 |
in (Thm.extra_shyps thm, prop) end; |
|
68539 | 154 |
|
68727 | 155 |
fun encode_prop (Ss, prop) = |
68208 | 156 |
let |
68726 | 157 |
val prop' = Logic.unvarify_global (named_bounds prop); |
158 |
val typargs = rev (Term.add_tfrees prop' []); |
|
68727 | 159 |
val sorts = Name.invent (Name.make_context (map #1 typargs)) Name.aT (length Ss) ~~ Ss; |
68726 | 160 |
val args = rev (Term.add_frees prop' []); |
161 |
in |
|
68727 | 162 |
(sorts @ typargs, args, prop') |> |
163 |
let open XML.Encode Term_XML.Encode |
|
164 |
in triple (list (pair string sort)) (list (pair string typ)) term end |
|
68726 | 165 |
end; |
68208 | 166 |
|
68726 | 167 |
val encode_fact = XML.Encode.list encode_prop o map standard_prop_of; |
68208 | 168 |
|
68232 | 169 |
val _ = |
68727 | 170 |
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
|
171 |
(Theory.axioms_of thy); |
a3bd410db5b2
standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents:
68232
diff
changeset
|
172 |
val _ = |
68726 | 173 |
export_entities "facts" (K (SOME o encode_fact)) (Facts.space_of o Global_Theory.facts_of) |
68232 | 174 |
(Facts.dest_static true [] (Global_Theory.facts_of thy)); |
175 |
||
68264 | 176 |
|
177 |
(* type classes *) |
|
178 |
||
179 |
val encode_class = |
|
180 |
let open XML.Encode Term_XML.Encode |
|
68725 | 181 |
in pair (list (pair string typ)) (list (term o named_bounds)) end; |
68264 | 182 |
|
183 |
fun export_class name = |
|
184 |
(case try (Axclass.get_info thy) name of |
|
185 |
NONE => ([], []) |
|
186 |
| SOME {params, axioms, ...} => |
|
187 |
(params, map (Logic.unvarify_global o Thm.full_prop_of) axioms)) |
|
188 |
|> encode_class |> SOME; |
|
189 |
||
190 |
val _ = |
|
191 |
export_entities "classes" (fn name => fn () => export_class name) |
|
192 |
Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig))))); |
|
193 |
||
68295 | 194 |
|
195 |
(* sort algebra *) |
|
196 |
||
197 |
val {classrel, arities} = |
|
198 |
Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents) |
|
199 |
(#2 (#classes rep_tsig)); |
|
200 |
||
201 |
val encode_classrel = |
|
202 |
let open XML.Encode |
|
203 |
in list (pair string (list string)) end; |
|
204 |
||
205 |
val encode_arities = |
|
206 |
let open XML.Encode Term_XML.Encode |
|
207 |
in list (triple string (list sort) string) end; |
|
208 |
||
209 |
val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel); |
|
210 |
val _ = if null arities then () else export_body thy "arities" (encode_arities arities); |
|
211 |
||
68862 | 212 |
|
213 |
(* locales *) |
|
214 |
||
68864 | 215 |
fun encode_locale ({type_params, params, asm, defs}: Locale.content) = |
216 |
let |
|
217 |
val args = map #1 params; |
|
218 |
val typargs = type_params @ rev (fold Term.add_tfrees (map Free args @ the_list asm) []); |
|
219 |
val encode = |
|
220 |
let open XML.Encode Term_XML.Encode in |
|
221 |
pair (list (pair string sort)) |
|
222 |
(pair (list (pair string typ)) |
|
223 |
(pair (option term) (list term))) |
|
224 |
end; |
|
225 |
in encode (typargs, (args, (asm, defs))) end; |
|
68862 | 226 |
|
227 |
val _ = |
|
228 |
export_entities "locales" (fn _ => SOME o encode_locale) Locale.locale_space |
|
229 |
(Locale.dest_locales thy); |
|
230 |
||
68900
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
231 |
|
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
232 |
(* parents *) |
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
233 |
|
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
234 |
val _ = |
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
235 |
export_body thy "parents" |
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
236 |
(XML.Encode.string (cat_lines (map Context.theory_long_name parents))); |
1145b25c53de
more robust: parents are strict in Export_Theory.read_theory and thus approximate "commit" of exports;
wenzelm
parents:
68864
diff
changeset
|
237 |
|
68295 | 238 |
in () end); |
68165 | 239 |
|
240 |
end; |