haftmann@38533
|
1 |
(* Title: Pure/Isar/theory_target.ML
|
haftmann@38533
|
2 |
Author: Makarius
|
haftmann@38533
|
3 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@38533
|
4 |
|
haftmann@38533
|
5 |
Common target infrastructure.
|
haftmann@38533
|
6 |
*)
|
haftmann@38533
|
7 |
|
haftmann@38533
|
8 |
signature GENERIC_TARGET =
|
haftmann@38533
|
9 |
sig
|
haftmann@38537
|
10 |
val define: (((binding * typ) * mixfix) * (binding * term)
|
haftmann@38537
|
11 |
-> term list * term list -> local_theory -> (term * thm) * local_theory)
|
haftmann@38533
|
12 |
-> (binding * mixfix) * (Attrib.binding * term) -> local_theory
|
haftmann@38533
|
13 |
-> (term * (string * thm)) * local_theory
|
haftmann@38533
|
14 |
val notes: (string
|
haftmann@38533
|
15 |
-> (Attrib.binding * (thm list * Args.src list) list) list
|
haftmann@38533
|
16 |
-> (Attrib.binding * (thm list * Args.src list) list) list
|
haftmann@38533
|
17 |
-> local_theory -> local_theory)
|
haftmann@38533
|
18 |
-> string -> (Attrib.binding * (thm list * Args.src list) list) list
|
haftmann@38533
|
19 |
-> local_theory -> (string * thm list) list * local_theory
|
haftmann@38534
|
20 |
val abbrev: (string * bool -> binding * mixfix -> term * term
|
haftmann@38536
|
21 |
-> term list -> local_theory -> local_theory)
|
haftmann@38534
|
22 |
-> string * bool -> (binding * mixfix) * term -> local_theory
|
haftmann@38534
|
23 |
-> (term * term) * local_theory
|
haftmann@38577
|
24 |
|
haftmann@38577
|
25 |
val theory_declaration: declaration -> local_theory -> local_theory
|
haftmann@38577
|
26 |
val theory_foundation: ((binding * typ) * mixfix) * (binding * term)
|
haftmann@38577
|
27 |
-> term list * term list -> local_theory -> (term * thm) * local_theory
|
haftmann@38577
|
28 |
val theory_notes: string -> (Attrib.binding * (thm list * Args.src list) list) list
|
haftmann@38577
|
29 |
-> local_theory -> local_theory
|
haftmann@38577
|
30 |
val theory_abbrev: Syntax.mode -> (binding * mixfix) * term
|
haftmann@38577
|
31 |
-> local_theory -> local_theory
|
haftmann@38533
|
32 |
end;
|
haftmann@38533
|
33 |
|
haftmann@38533
|
34 |
structure Generic_Target: GENERIC_TARGET =
|
haftmann@38533
|
35 |
struct
|
haftmann@38533
|
36 |
|
haftmann@38577
|
37 |
(** lifting primitive to target operations **)
|
haftmann@38577
|
38 |
|
haftmann@38536
|
39 |
(* mixfix syntax *)
|
haftmann@38536
|
40 |
|
haftmann@38536
|
41 |
fun check_mixfix ctxt (b, extra_tfrees) mx =
|
haftmann@38536
|
42 |
if null extra_tfrees then mx
|
haftmann@38536
|
43 |
else
|
haftmann@38536
|
44 |
(warning
|
haftmann@38536
|
45 |
("Additional type variable(s) in specification of " ^ Binding.str_of b ^ ": " ^
|
haftmann@38536
|
46 |
commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
|
haftmann@38536
|
47 |
(if mx = NoSyn then ""
|
haftmann@38536
|
48 |
else "\nDropping mixfix syntax " ^ Pretty.string_of (Syntax.pretty_mixfix mx)));
|
haftmann@38536
|
49 |
NoSyn);
|
haftmann@38536
|
50 |
|
haftmann@38536
|
51 |
|
haftmann@38533
|
52 |
(* define *)
|
haftmann@38533
|
53 |
|
haftmann@38535
|
54 |
fun define foundation ((b, mx), ((proto_b_def, atts), rhs)) lthy =
|
haftmann@38533
|
55 |
let
|
haftmann@38533
|
56 |
val thy = ProofContext.theory_of lthy;
|
haftmann@38533
|
57 |
val thy_ctxt = ProofContext.init_global thy;
|
haftmann@38533
|
58 |
|
haftmann@38535
|
59 |
val b_def = Thm.def_binding_optional b proto_b_def;
|
haftmann@38533
|
60 |
|
haftmann@38533
|
61 |
(*term and type parameters*)
|
haftmann@38533
|
62 |
val crhs = Thm.cterm_of thy rhs;
|
haftmann@38533
|
63 |
val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
|
haftmann@38533
|
64 |
val rhs_conv = MetaSimplifier.rewrite true defs crhs;
|
haftmann@38533
|
65 |
|
haftmann@38533
|
66 |
val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
|
haftmann@38533
|
67 |
val T = Term.fastype_of rhs;
|
haftmann@38533
|
68 |
val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
|
haftmann@38533
|
69 |
val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
|
haftmann@38536
|
70 |
val mx' = check_mixfix lthy (b, extra_tfrees) mx;
|
haftmann@38533
|
71 |
|
haftmann@38533
|
72 |
val type_params = map (Logic.mk_type o TFree) extra_tfrees;
|
haftmann@38533
|
73 |
val term_params =
|
haftmann@38533
|
74 |
rev (Variable.fixes_of (Local_Theory.target_of lthy))
|
haftmann@38533
|
75 |
|> map_filter (fn (_, x) =>
|
haftmann@38533
|
76 |
(case AList.lookup (op =) xs x of
|
haftmann@38533
|
77 |
SOME T => SOME (Free (x, T))
|
haftmann@38533
|
78 |
| NONE => NONE));
|
haftmann@38533
|
79 |
val params = type_params @ term_params;
|
haftmann@38533
|
80 |
|
haftmann@38533
|
81 |
val U = map Term.fastype_of params ---> T;
|
haftmann@38533
|
82 |
|
haftmann@38533
|
83 |
(*foundation*)
|
haftmann@38539
|
84 |
val ((lhs', global_def), lthy2) = foundation
|
haftmann@38537
|
85 |
(((b, U), mx'), (b_def, rhs')) (type_params, term_params) lthy;
|
haftmann@38533
|
86 |
|
haftmann@38533
|
87 |
(*local definition*)
|
haftmann@38539
|
88 |
val ((lhs, local_def), lthy3) = lthy2
|
haftmann@38533
|
89 |
|> Local_Defs.add_def ((b, NoSyn), lhs');
|
haftmann@38539
|
90 |
val def = Local_Defs.trans_terms lthy3
|
haftmann@38533
|
91 |
[(*c == global.c xs*) local_def,
|
haftmann@38533
|
92 |
(*global.c xs == rhs'*) global_def,
|
haftmann@38533
|
93 |
(*rhs' == rhs*) Thm.symmetric rhs_conv];
|
haftmann@38533
|
94 |
|
haftmann@38533
|
95 |
(*note*)
|
haftmann@38539
|
96 |
val ([(res_name, [res])], lthy4) = lthy3
|
haftmann@38535
|
97 |
|> Local_Theory.notes_kind "" [((b_def, atts), [([def], [])])];
|
haftmann@38539
|
98 |
in ((lhs, (res_name, res)), lthy4) end;
|
haftmann@38533
|
99 |
|
haftmann@38533
|
100 |
|
haftmann@38533
|
101 |
(* notes *)
|
haftmann@38533
|
102 |
|
haftmann@38533
|
103 |
fun import_export_proof ctxt (name, raw_th) =
|
haftmann@38533
|
104 |
let
|
haftmann@38533
|
105 |
val thy = ProofContext.theory_of ctxt;
|
haftmann@38533
|
106 |
val thy_ctxt = ProofContext.init_global thy;
|
haftmann@38533
|
107 |
val certT = Thm.ctyp_of thy;
|
haftmann@38533
|
108 |
val cert = Thm.cterm_of thy;
|
haftmann@38533
|
109 |
|
haftmann@38533
|
110 |
(*export assumes/defines*)
|
haftmann@38533
|
111 |
val th = Goal.norm_result raw_th;
|
haftmann@38533
|
112 |
val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
|
haftmann@38534
|
113 |
val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume)
|
haftmann@38534
|
114 |
(Assumption.all_assms_of ctxt);
|
haftmann@38533
|
115 |
val nprems = Thm.nprems_of th' - Thm.nprems_of th;
|
haftmann@38533
|
116 |
|
haftmann@38533
|
117 |
(*export fixes*)
|
haftmann@38533
|
118 |
val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
|
haftmann@38533
|
119 |
val frees = map Free (Thm.fold_terms Term.add_frees th' []);
|
haftmann@38533
|
120 |
val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
|
haftmann@38533
|
121 |
|> Variable.export ctxt thy_ctxt
|
haftmann@38533
|
122 |
|> Drule.zero_var_indexes_list;
|
haftmann@38533
|
123 |
|
haftmann@38533
|
124 |
(*thm definition*)
|
haftmann@38533
|
125 |
val result = PureThy.name_thm true true name th'';
|
haftmann@38533
|
126 |
|
haftmann@38533
|
127 |
(*import fixes*)
|
haftmann@38533
|
128 |
val (tvars, vars) =
|
haftmann@38533
|
129 |
chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
|
haftmann@38533
|
130 |
|>> map Logic.dest_type;
|
haftmann@38533
|
131 |
|
haftmann@38533
|
132 |
val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
|
haftmann@38533
|
133 |
val inst = filter (is_Var o fst) (vars ~~ frees);
|
haftmann@38533
|
134 |
val cinstT = map (pairself certT o apfst TVar) instT;
|
haftmann@38533
|
135 |
val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
|
haftmann@38533
|
136 |
val result' = Thm.instantiate (cinstT, cinst) result;
|
haftmann@38533
|
137 |
|
haftmann@38533
|
138 |
(*import assumes/defines*)
|
haftmann@38533
|
139 |
val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
|
haftmann@38533
|
140 |
val result'' =
|
haftmann@38533
|
141 |
(case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
|
haftmann@38533
|
142 |
NONE => raise THM ("Failed to re-import result", 0, [result'])
|
haftmann@38533
|
143 |
| SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
|
haftmann@38533
|
144 |
|> Goal.norm_result
|
haftmann@38533
|
145 |
|> PureThy.name_thm false false name;
|
haftmann@38533
|
146 |
|
haftmann@38533
|
147 |
in (result'', result) end;
|
haftmann@38533
|
148 |
|
haftmann@38533
|
149 |
fun notes target_notes kind facts lthy =
|
haftmann@38533
|
150 |
let
|
haftmann@38533
|
151 |
val thy = ProofContext.theory_of lthy;
|
haftmann@38533
|
152 |
val facts' = facts
|
haftmann@38533
|
153 |
|> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi
|
haftmann@38533
|
154 |
(Local_Theory.full_name lthy (fst a))) bs))
|
haftmann@38533
|
155 |
|> PureThy.map_facts (import_export_proof lthy);
|
haftmann@38533
|
156 |
val local_facts = PureThy.map_facts #1 facts';
|
haftmann@38533
|
157 |
val global_facts = PureThy.map_facts #2 facts';
|
haftmann@38533
|
158 |
in
|
haftmann@38533
|
159 |
lthy
|
haftmann@38533
|
160 |
|> target_notes kind global_facts local_facts
|
haftmann@38533
|
161 |
|> ProofContext.note_thmss kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
|
haftmann@38533
|
162 |
end;
|
haftmann@38533
|
163 |
|
haftmann@38533
|
164 |
|
haftmann@38533
|
165 |
(* abbrev *)
|
haftmann@38533
|
166 |
|
haftmann@38533
|
167 |
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
|
haftmann@38533
|
168 |
let
|
haftmann@38533
|
169 |
val thy_ctxt = ProofContext.init_global (ProofContext.theory_of lthy);
|
haftmann@38533
|
170 |
val target_ctxt = Local_Theory.target_of lthy;
|
haftmann@38533
|
171 |
|
haftmann@38533
|
172 |
val t' = Assumption.export_term lthy target_ctxt t;
|
haftmann@38533
|
173 |
val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
|
haftmann@38533
|
174 |
val u = fold_rev lambda xs t';
|
haftmann@38533
|
175 |
|
haftmann@38533
|
176 |
val extra_tfrees =
|
haftmann@38533
|
177 |
subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
|
haftmann@38536
|
178 |
val mx' = check_mixfix lthy (b, extra_tfrees) mx;
|
haftmann@38533
|
179 |
|
haftmann@38533
|
180 |
val global_rhs =
|
haftmann@38533
|
181 |
singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
|
haftmann@38533
|
182 |
in
|
haftmann@38533
|
183 |
lthy
|
haftmann@38536
|
184 |
|> target_abbrev prmode (b, mx') (global_rhs, t') xs
|
haftmann@38533
|
185 |
|> ProofContext.add_abbrev Print_Mode.internal (b, t) |> snd
|
haftmann@38533
|
186 |
|> Local_Defs.fixed_abbrev ((b, NoSyn), t)
|
haftmann@38533
|
187 |
end;
|
haftmann@38533
|
188 |
|
haftmann@38577
|
189 |
|
haftmann@38577
|
190 |
(** primitive theory operations **)
|
haftmann@38577
|
191 |
|
haftmann@38577
|
192 |
fun theory_declaration decl lthy =
|
haftmann@38577
|
193 |
let
|
haftmann@38577
|
194 |
val global_decl = Morphism.form
|
haftmann@38577
|
195 |
(Morphism.transform (Local_Theory.global_morphism lthy) decl);
|
haftmann@38577
|
196 |
in
|
haftmann@38577
|
197 |
lthy
|
wenzelm@39032
|
198 |
|> Local_Theory.background_theory (Context.theory_map global_decl)
|
haftmann@38577
|
199 |
|> Local_Theory.target (Context.proof_map global_decl)
|
haftmann@38577
|
200 |
end;
|
haftmann@38577
|
201 |
|
haftmann@38577
|
202 |
fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
|
haftmann@38577
|
203 |
let
|
wenzelm@39032
|
204 |
val (const, lthy2) = lthy |> Local_Theory.background_theory_result
|
haftmann@38577
|
205 |
(Sign.declare_const ((b, U), mx));
|
haftmann@38577
|
206 |
val lhs = list_comb (const, type_params @ term_params);
|
wenzelm@39032
|
207 |
val ((_, def), lthy3) = lthy2 |> Local_Theory.background_theory_result
|
haftmann@38577
|
208 |
(Thm.add_def false false (b_def, Logic.mk_equals (lhs, rhs)));
|
haftmann@38577
|
209 |
in ((lhs, def), lthy3) end;
|
haftmann@38577
|
210 |
|
haftmann@38577
|
211 |
fun theory_notes kind global_facts lthy =
|
haftmann@38577
|
212 |
let
|
haftmann@38577
|
213 |
val thy = ProofContext.theory_of lthy;
|
haftmann@38577
|
214 |
val global_facts' = Attrib.map_facts (Attrib.attribute_i thy) global_facts;
|
haftmann@38577
|
215 |
in
|
haftmann@38577
|
216 |
lthy
|
wenzelm@39032
|
217 |
|> Local_Theory.background_theory (PureThy.note_thmss kind global_facts' #> snd)
|
haftmann@38577
|
218 |
|> Local_Theory.target (ProofContext.note_thmss kind global_facts' #> snd)
|
haftmann@38577
|
219 |
end;
|
haftmann@38577
|
220 |
|
wenzelm@39032
|
221 |
fun theory_abbrev prmode ((b, mx), t) =
|
wenzelm@39032
|
222 |
Local_Theory.background_theory
|
wenzelm@39032
|
223 |
(Sign.add_abbrev (#1 prmode) (b, t) #->
|
wenzelm@39032
|
224 |
(fn (lhs, _) => Sign.notation true prmode [(lhs, mx)]));
|
haftmann@38577
|
225 |
|
haftmann@38533
|
226 |
end;
|