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