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