author | wenzelm |
Tue, 26 Apr 2011 15:56:15 +0200 | |
changeset 42473 | aca720fb3936 |
parent 42360 | da8817d01e7c |
child 42482 | 42c7ef050bc3 |
permissions | -rw-r--r-- |
18830 | 1 |
(* Title: Pure/Isar/local_defs.ML |
2 |
Author: Makarius |
|
3 |
||
18840 | 4 |
Local definitions. |
18830 | 5 |
*) |
6 |
||
7 |
signature LOCAL_DEFS = |
|
8 |
sig |
|
20306 | 9 |
val cert_def: Proof.context -> term -> (string * typ) * term |
18830 | 10 |
val abs_def: term -> (string * typ) * term |
20306 | 11 |
val mk_def: Proof.context -> (string * term) list -> term list |
21684 | 12 |
val expand: cterm list -> thm -> thm |
20224
9c40a144ee0e
moved basic assumption operations from structure ProofContext to Assumption;
wenzelm
parents:
20049
diff
changeset
|
13 |
val def_export: Assumption.export |
30211 | 14 |
val add_defs: ((binding * mixfix) * (Thm.binding * term)) list -> Proof.context -> |
15 |
(term * (string * thm)) list * Proof.context |
|
29581 | 16 |
val add_def: (binding * mixfix) * term -> Proof.context -> (term * thm) * Proof.context |
17 |
val fixed_abbrev: (binding * mixfix) * term -> Proof.context -> |
|
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
18 |
(term * term) * Proof.context |
21591 | 19 |
val export: Proof.context -> Proof.context -> thm -> thm list * thm |
35717 | 20 |
val export_cterm: Proof.context -> Proof.context -> cterm -> thm list * cterm |
21844 | 21 |
val trans_terms: Proof.context -> thm list -> thm |
22 |
val trans_props: Proof.context -> thm list -> thm |
|
35739 | 23 |
val contract: Proof.context -> thm list -> cterm -> thm -> thm |
21506 | 24 |
val print_rules: Proof.context -> unit |
18840 | 25 |
val defn_add: attribute |
26 |
val defn_del: attribute |
|
23541 | 27 |
val meta_rewrite_conv: Proof.context -> conv |
21506 | 28 |
val meta_rewrite_rule: Proof.context -> thm -> thm |
20306 | 29 |
val unfold: Proof.context -> thm list -> thm -> thm |
30 |
val unfold_goals: Proof.context -> thm list -> thm -> thm |
|
31 |
val unfold_tac: Proof.context -> thm list -> tactic |
|
32 |
val fold: Proof.context -> thm list -> thm -> thm |
|
33 |
val fold_tac: Proof.context -> thm list -> tactic |
|
34 |
val derived_def: Proof.context -> bool -> term -> |
|
20909 | 35 |
((string * typ) * term) * (Proof.context -> thm -> thm) |
18830 | 36 |
end; |
37 |
||
35624 | 38 |
structure Local_Defs: LOCAL_DEFS = |
18830 | 39 |
struct |
40 |
||
18840 | 41 |
(** primitive definitions **) |
42 |
||
18830 | 43 |
(* prepare defs *) |
44 |
||
45 |
fun cert_def ctxt eq = |
|
46 |
let |
|
40242 | 47 |
fun err msg = |
48 |
cat_error msg ("The error(s) above occurred in definition:\n" ^ |
|
49 |
quote (Syntax.string_of_term ctxt eq)); |
|
18950 | 50 |
val ((lhs, _), eq') = eq |
39133
70d3915c92f0
pretty printing: prefer regular Proof.context over Pretty.pp, which is mostly for special bootstrap purposes involving theory merge, for example;
wenzelm
parents:
35739
diff
changeset
|
51 |
|> Sign.no_vars ctxt |
33385 | 52 |
|> Primitive_Defs.dest_def ctxt Term.is_Free (Variable.is_fixed ctxt) (K true) |
18950 | 53 |
handle TERM (msg, _) => err msg | ERROR msg => err msg; |
54 |
in (Term.dest_Free (Term.head_of lhs), eq') end; |
|
18830 | 55 |
|
33385 | 56 |
val abs_def = Primitive_Defs.abs_def #>> Term.dest_Free; |
18830 | 57 |
|
18875 | 58 |
fun mk_def ctxt args = |
59 |
let |
|
60 |
val (xs, rhss) = split_list args; |
|
42360 | 61 |
val (bind, _) = Proof_Context.bind_fixes xs ctxt; |
18875 | 62 |
val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args; |
63 |
in map Logic.mk_equals (lhss ~~ rhss) end; |
|
64 |
||
18830 | 65 |
|
66 |
(* export defs *) |
|
67 |
||
20021 | 68 |
val head_of_def = |
20887 | 69 |
#1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body; |
20021 | 70 |
|
18830 | 71 |
|
18875 | 72 |
(* |
20887 | 73 |
[x, x == a] |
18896 | 74 |
: |
75 |
B x |
|
76 |
----------- |
|
20887 | 77 |
B a |
18875 | 78 |
*) |
21684 | 79 |
fun expand defs = |
80 |
Drule.implies_intr_list defs |
|
81 |
#> Drule.generalize ([], map (head_of_def o Thm.term_of) defs) |
|
82 |
#> funpow (length defs) (fn th => Drule.reflexive_thm RS th); |
|
83 |
||
21801 | 84 |
val expand_term = Envir.expand_term_frees o map (abs_def o Thm.term_of); |
21684 | 85 |
|
86 |
fun def_export _ defs = (expand defs, expand_term defs); |
|
18830 | 87 |
|
88 |
||
89 |
(* add defs *) |
|
90 |
||
20887 | 91 |
fun add_defs defs ctxt = |
18830 | 92 |
let |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
93 |
val ((bvars, mxs), specs) = defs |> split_list |>> split_list; |
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
94 |
val ((bfacts, atts), rhss) = specs |> split_list |>> split_list; |
42357 | 95 |
val xs = map Variable.name bvars; |
30434 | 96 |
val names = map2 Thm.def_binding_optional bvars bfacts; |
20887 | 97 |
val eqs = mk_def ctxt (xs ~~ rhss); |
98 |
val lhss = map (fst o Logic.dest_equals) eqs; |
|
18830 | 99 |
in |
100 |
ctxt |
|
42360 | 101 |
|> Proof_Context.add_fixes (map2 (fn x => fn mx => (x, NONE, mx)) bvars mxs) |> #2 |
20980 | 102 |
|> fold Variable.declare_term eqs |
42360 | 103 |
|> Proof_Context.add_assms_i def_export |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
104 |
(map2 (fn a => fn eq => (a, [(eq, [])])) (names ~~ atts) eqs) |
20887 | 105 |
|>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss |
18830 | 106 |
end; |
107 |
||
25104 | 108 |
fun add_def (var, rhs) ctxt = |
30211 | 109 |
let val ([(lhs, (_, th))], ctxt') = add_defs [(var, (Thm.empty_binding, rhs))] ctxt |
25104 | 110 |
in ((lhs, th), ctxt') end; |
25025 | 111 |
|
18840 | 112 |
|
25119 | 113 |
(* fixed_abbrev *) |
114 |
||
115 |
fun fixed_abbrev ((x, mx), rhs) ctxt = |
|
116 |
let |
|
117 |
val T = Term.fastype_of rhs; |
|
118 |
val ([x'], ctxt') = ctxt |
|
119 |
|> Variable.declare_term rhs |
|
42360 | 120 |
|> Proof_Context.add_fixes [(x, SOME T, mx)]; |
25119 | 121 |
val lhs = Free (x', T); |
122 |
val _ = cert_def ctxt' (Logic.mk_equals (lhs, rhs)); |
|
123 |
fun abbrev_export _ _ = (I, Envir.expand_term_frees [((x', T), rhs)]); |
|
124 |
val (_, ctxt'') = Assumption.add_assms abbrev_export [] ctxt'; |
|
125 |
in ((lhs, rhs), ctxt'') end; |
|
126 |
||
127 |
||
24985 | 128 |
(* specific export -- result based on educated guessing *) |
21568 | 129 |
|
24985 | 130 |
(* |
131 |
[xs, xs == as] |
|
132 |
: |
|
133 |
B xs |
|
134 |
-------------- |
|
135 |
B as |
|
136 |
*) |
|
137 |
fun export inner outer = (*beware of closure sizes*) |
|
21568 | 138 |
let |
22671 | 139 |
val exp = Assumption.export false inner outer; |
30473
e0b66c11e7e4
Assumption.all_prems_of, Assumption.all_assms_of;
wenzelm
parents:
30434
diff
changeset
|
140 |
val prems = Assumption.all_prems_of inner; |
22671 | 141 |
in fn th => |
142 |
let |
|
143 |
val th' = exp th; |
|
22691 | 144 |
val still_fixed = map #1 (Thm.fold_terms Term.add_frees th' []); |
22671 | 145 |
val defs = prems |> filter_out (fn prem => |
146 |
(case try (head_of_def o Thm.prop_of) prem of |
|
147 |
SOME x => member (op =) still_fixed x |
|
148 |
| NONE => true)); |
|
149 |
in (map Drule.abs_def defs, th') end |
|
150 |
end; |
|
21568 | 151 |
|
24985 | 152 |
(* |
153 |
[xs, xs == as] |
|
154 |
: |
|
155 |
TERM b xs |
|
156 |
-------------- and -------------- |
|
157 |
TERM b as b xs == b as |
|
158 |
*) |
|
159 |
fun export_cterm inner outer ct = |
|
35717 | 160 |
export inner outer (Drule.mk_term ct) ||> Drule.dest_term; |
24985 | 161 |
|
21568 | 162 |
|
21844 | 163 |
(* basic transitivity reasoning -- modulo beta-eta *) |
164 |
||
165 |
local |
|
166 |
||
167 |
val is_trivial = Pattern.aeconv o Logic.dest_equals o Thm.prop_of; |
|
168 |
||
169 |
fun trans_list _ _ [] = raise Empty |
|
170 |
| trans_list trans ctxt (th :: raw_eqs) = |
|
171 |
(case filter_out is_trivial raw_eqs of |
|
172 |
[] => th |
|
173 |
| eqs => |
|
31794
71af1fd6a5e4
renamed Variable.import_thms to Variable.import (back again cf. ed7aa5a350ef -- Alice is no longer supported);
wenzelm
parents:
30763
diff
changeset
|
174 |
let val ((_, th' :: eqs'), ctxt') = Variable.import true (th :: eqs) ctxt |
21844 | 175 |
in singleton (Variable.export ctxt' ctxt) (fold trans eqs' th') end); |
176 |
||
177 |
in |
|
178 |
||
179 |
val trans_terms = trans_list (fn eq2 => fn eq1 => eq2 COMP (eq1 COMP Drule.transitive_thm)); |
|
180 |
val trans_props = trans_list (fn eq => fn th => th COMP (eq COMP Drule.equal_elim_rule1)); |
|
181 |
||
182 |
end; |
|
183 |
||
35739 | 184 |
fun contract ctxt defs ct th = |
41228
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
185 |
trans_props ctxt [th, Thm.symmetric (Raw_Simplifier.rewrite true defs ct)]; |
21844 | 186 |
|
18840 | 187 |
|
188 |
(** defived definitions **) |
|
189 |
||
190 |
(* transformation rules *) |
|
191 |
||
33519 | 192 |
structure Rules = Generic_Data |
18840 | 193 |
( |
194 |
type T = thm list; |
|
33519 | 195 |
val empty = []; |
18840 | 196 |
val extend = I; |
33519 | 197 |
val merge = Thm.merge_thms; |
18840 | 198 |
); |
199 |
||
22846 | 200 |
fun print_rules ctxt = |
201 |
Pretty.writeln (Pretty.big_list "definitional transformations:" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
31794
diff
changeset
|
202 |
(map (Display.pretty_thm ctxt) (Rules.get (Context.Proof ctxt)))); |
18840 | 203 |
|
24039
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents:
23541
diff
changeset
|
204 |
val defn_add = Thm.declaration_attribute (Rules.map o Thm.add_thm); |
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents:
23541
diff
changeset
|
205 |
val defn_del = Thm.declaration_attribute (Rules.map o Thm.del_thm); |
18840 | 206 |
|
207 |
||
18875 | 208 |
(* meta rewrite rules *) |
18840 | 209 |
|
23541 | 210 |
fun meta_rewrite_conv ctxt = |
41228
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
211 |
Raw_Simplifier.rewrite_cterm (false, false, false) (K (K NONE)) |
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
212 |
(Raw_Simplifier.context ctxt empty_ss |
26424 | 213 |
addeqcongs [Drule.equals_cong] (*protect meta-level equality*) |
214 |
addsimps (Rules.get (Context.Proof ctxt))); |
|
18840 | 215 |
|
23541 | 216 |
val meta_rewrite_rule = Conv.fconv_rule o meta_rewrite_conv; |
18840 | 217 |
|
18875 | 218 |
|
219 |
(* rewriting with object-level rules *) |
|
220 |
||
21506 | 221 |
fun meta f ctxt = f o map (meta_rewrite_rule ctxt); |
18840 | 222 |
|
41228
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
223 |
val unfold = meta Raw_Simplifier.rewrite_rule; |
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
224 |
val unfold_goals = meta Raw_Simplifier.rewrite_goals_rule; |
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
225 |
val unfold_tac = meta Raw_Simplifier.rewrite_goals_tac; |
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
226 |
val fold = meta Raw_Simplifier.fold_rule; |
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
227 |
val fold_tac = meta Raw_Simplifier.fold_goals_tac; |
18840 | 228 |
|
229 |
||
230 |
(* derived defs -- potentially within the object-logic *) |
|
231 |
||
18950 | 232 |
fun derived_def ctxt conditional prop = |
18840 | 233 |
let |
234 |
val ((c, T), rhs) = prop |
|
42360 | 235 |
|> Thm.cterm_of (Proof_Context.theory_of ctxt) |
23541 | 236 |
|> meta_rewrite_conv ctxt |
18840 | 237 |
|> (snd o Logic.dest_equals o Thm.prop_of) |
21568 | 238 |
|> conditional ? Logic.strip_imp_concl |
18950 | 239 |
|> (abs_def o #2 o cert_def ctxt); |
20909 | 240 |
fun prove ctxt' def = |
18840 | 241 |
let |
242 |
val frees = Term.fold_aterms (fn Free (x, _) => |
|
20909 | 243 |
if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop []; |
18840 | 244 |
in |
20909 | 245 |
Goal.prove ctxt' frees [] prop (K (ALLGOALS |
23541 | 246 |
(CONVERSION (meta_rewrite_conv ctxt') THEN' |
41228
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents:
40242
diff
changeset
|
247 |
rewrite_goal_tac [def] THEN' |
21708 | 248 |
resolve_tac [Drule.reflexive_thm]))) |
40242 | 249 |
handle ERROR msg => cat_error msg "Failed to prove definitional specification" |
18840 | 250 |
end; |
251 |
in (((c, T), rhs), prove) end; |
|
252 |
||
18830 | 253 |
end; |