author | haftmann |
Thu, 04 Dec 2008 14:43:33 +0100 | |
changeset 28965 | 1de908189869 |
parent 28083 | 103d9282a946 |
child 29006 | abe0f11cfa4e |
permissions | -rw-r--r-- |
18830 | 1 |
(* Title: Pure/Isar/local_defs.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
18840 | 5 |
Local definitions. |
18830 | 6 |
*) |
7 |
||
8 |
signature LOCAL_DEFS = |
|
9 |
sig |
|
20306 | 10 |
val cert_def: Proof.context -> term -> (string * typ) * term |
18830 | 11 |
val abs_def: term -> (string * typ) * term |
20306 | 12 |
val mk_def: Proof.context -> (string * term) list -> term list |
21684 | 13 |
val expand: cterm list -> thm -> thm |
20224
9c40a144ee0e
moved basic assumption operations from structure ProofContext to Assumption;
wenzelm
parents:
20049
diff
changeset
|
14 |
val def_export: Assumption.export |
28965 | 15 |
val add_defs: ((Binding.T * mixfix) * ((Binding.T * attribute list) * term)) list -> |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
16 |
Proof.context -> (term * (string * thm)) list * Proof.context |
28965 | 17 |
val add_def: (Binding.T * mixfix) * term -> Proof.context -> (term * thm) * Proof.context |
18 |
val fixed_abbrev: (Binding.T * mixfix) * term -> Proof.context -> |
|
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
19 |
(term * term) * Proof.context |
21591 | 20 |
val export: Proof.context -> Proof.context -> thm -> thm list * thm |
24985 | 21 |
val export_cterm: Proof.context -> Proof.context -> cterm -> cterm * thm |
21844 | 22 |
val trans_terms: Proof.context -> thm list -> thm |
23 |
val trans_props: Proof.context -> thm list -> 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 |
||
38 |
structure LocalDefs: LOCAL_DEFS = |
|
39 |
struct |
|
40 |
||
18840 | 41 |
(** primitive definitions **) |
42 |
||
18830 | 43 |
(* prepare defs *) |
44 |
||
45 |
fun cert_def ctxt eq = |
|
46 |
let |
|
25025 | 47 |
fun err msg = cat_error msg ("The error(s) above occurred in definition: " ^ |
48 |
quote (Syntax.string_of_term ctxt eq)); |
|
18950 | 49 |
val ((lhs, _), eq') = eq |
24981
4ec3f95190bf
dest/cert_def: replaced Pretty.pp by explicit Proof.context;
wenzelm
parents:
24961
diff
changeset
|
50 |
|> Sign.no_vars (Syntax.pp ctxt) |
4ec3f95190bf
dest/cert_def: replaced Pretty.pp by explicit Proof.context;
wenzelm
parents:
24961
diff
changeset
|
51 |
|> PrimitiveDefs.dest_def ctxt Term.is_Free (Variable.is_fixed ctxt) (K true) |
18950 | 52 |
handle TERM (msg, _) => err msg | ERROR msg => err msg; |
53 |
in (Term.dest_Free (Term.head_of lhs), eq') end; |
|
18830 | 54 |
|
24261 | 55 |
val abs_def = PrimitiveDefs.abs_def #>> Term.dest_Free; |
18830 | 56 |
|
18875 | 57 |
fun mk_def ctxt args = |
58 |
let |
|
59 |
val (xs, rhss) = split_list args; |
|
60 |
val (bind, _) = ProofContext.bind_fixes xs ctxt; |
|
61 |
val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args; |
|
62 |
in map Logic.mk_equals (lhss ~~ rhss) end; |
|
63 |
||
18830 | 64 |
|
65 |
(* export defs *) |
|
66 |
||
20021 | 67 |
val head_of_def = |
20887 | 68 |
#1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body; |
20021 | 69 |
|
18830 | 70 |
|
18875 | 71 |
(* |
20887 | 72 |
[x, x == a] |
18896 | 73 |
: |
74 |
B x |
|
75 |
----------- |
|
20887 | 76 |
B a |
18875 | 77 |
*) |
21684 | 78 |
fun expand defs = |
79 |
Drule.implies_intr_list defs |
|
80 |
#> Drule.generalize ([], map (head_of_def o Thm.term_of) defs) |
|
81 |
#> funpow (length defs) (fn th => Drule.reflexive_thm RS th); |
|
82 |
||
21801 | 83 |
val expand_term = Envir.expand_term_frees o map (abs_def o Thm.term_of); |
21684 | 84 |
|
85 |
fun def_export _ defs = (expand defs, expand_term defs); |
|
18830 | 86 |
|
87 |
||
88 |
(* add defs *) |
|
89 |
||
20887 | 90 |
fun add_defs defs ctxt = |
18830 | 91 |
let |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
92 |
val ((bvars, mxs), specs) = defs |> split_list |>> split_list; |
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
93 |
val ((bfacts, atts), rhss) = specs |> split_list |>> split_list; |
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
94 |
val xs = map Name.name_of bvars; |
28965 | 95 |
val names = map2 (Binding.map_base o Thm.def_name_optional) xs bfacts; |
20887 | 96 |
val eqs = mk_def ctxt (xs ~~ rhss); |
97 |
val lhss = map (fst o Logic.dest_equals) eqs; |
|
18830 | 98 |
in |
99 |
ctxt |
|
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
100 |
|> ProofContext.add_fixes_i (map2 (fn x => fn mx => (x, NONE, mx)) bvars mxs) |> #2 |
20980 | 101 |
|> fold Variable.declare_term eqs |
20887 | 102 |
|> ProofContext.add_assms_i def_export |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
26424
diff
changeset
|
103 |
(map2 (fn a => fn eq => (a, [(eq, [])])) (names ~~ atts) eqs) |
20887 | 104 |
|>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss |
18830 | 105 |
end; |
106 |
||
25104 | 107 |
fun add_def (var, rhs) ctxt = |
28965 | 108 |
let val ([(lhs, (_, th))], ctxt') = add_defs [(var, ((Binding.empty, []), rhs))] ctxt |
25104 | 109 |
in ((lhs, th), ctxt') end; |
25025 | 110 |
|
18840 | 111 |
|
25119 | 112 |
(* fixed_abbrev *) |
113 |
||
114 |
fun fixed_abbrev ((x, mx), rhs) ctxt = |
|
115 |
let |
|
116 |
val T = Term.fastype_of rhs; |
|
117 |
val ([x'], ctxt') = ctxt |
|
118 |
|> Variable.declare_term rhs |
|
119 |
|> ProofContext.add_fixes_i [(x, SOME T, mx)]; |
|
120 |
val lhs = Free (x', T); |
|
121 |
val _ = cert_def ctxt' (Logic.mk_equals (lhs, rhs)); |
|
122 |
fun abbrev_export _ _ = (I, Envir.expand_term_frees [((x', T), rhs)]); |
|
123 |
val (_, ctxt'') = Assumption.add_assms abbrev_export [] ctxt'; |
|
124 |
in ((lhs, rhs), ctxt'') end; |
|
125 |
||
126 |
||
24985 | 127 |
(* specific export -- result based on educated guessing *) |
21568 | 128 |
|
24985 | 129 |
(* |
130 |
[xs, xs == as] |
|
131 |
: |
|
132 |
B xs |
|
133 |
-------------- |
|
134 |
B as |
|
135 |
*) |
|
136 |
fun export inner outer = (*beware of closure sizes*) |
|
21568 | 137 |
let |
22671 | 138 |
val exp = Assumption.export false inner outer; |
139 |
val prems = Assumption.prems_of inner; |
|
140 |
in fn th => |
|
141 |
let |
|
142 |
val th' = exp th; |
|
22691 | 143 |
val still_fixed = map #1 (Thm.fold_terms Term.add_frees th' []); |
22671 | 144 |
val defs = prems |> filter_out (fn prem => |
145 |
(case try (head_of_def o Thm.prop_of) prem of |
|
146 |
SOME x => member (op =) still_fixed x |
|
147 |
| NONE => true)); |
|
148 |
in (map Drule.abs_def defs, th') end |
|
149 |
end; |
|
21568 | 150 |
|
24985 | 151 |
(* |
152 |
[xs, xs == as] |
|
153 |
: |
|
154 |
TERM b xs |
|
155 |
-------------- and -------------- |
|
156 |
TERM b as b xs == b as |
|
157 |
*) |
|
158 |
fun export_cterm inner outer ct = |
|
159 |
let val (defs, ct') = export inner outer (Drule.mk_term ct) ||> Drule.dest_term |
|
160 |
in (ct', MetaSimplifier.rewrite true defs ct) end; |
|
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 => |
|
22568
ed7aa5a350ef
renamed Variable.import to import_thms (avoid clash with Alice keywords);
wenzelm
parents:
21844
diff
changeset
|
174 |
let val ((_, th' :: eqs'), ctxt') = Variable.import_thms 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 |
||
184 |
||
18840 | 185 |
|
186 |
(** defived definitions **) |
|
187 |
||
188 |
(* transformation rules *) |
|
189 |
||
18859 | 190 |
structure Rules = GenericDataFun |
18840 | 191 |
( |
192 |
type T = thm list; |
|
193 |
val empty = [] |
|
194 |
val extend = I; |
|
24039
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents:
23541
diff
changeset
|
195 |
fun merge _ = Thm.merge_thms; |
18840 | 196 |
); |
197 |
||
22846 | 198 |
fun print_rules ctxt = |
199 |
Pretty.writeln (Pretty.big_list "definitional transformations:" |
|
200 |
(map (ProofContext.pretty_thm ctxt) (Rules.get (Context.Proof ctxt)))); |
|
18840 | 201 |
|
24039
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents:
23541
diff
changeset
|
202 |
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
|
203 |
val defn_del = Thm.declaration_attribute (Rules.map o Thm.del_thm); |
18840 | 204 |
|
205 |
||
18875 | 206 |
(* meta rewrite rules *) |
18840 | 207 |
|
23541 | 208 |
fun meta_rewrite_conv ctxt = |
18840 | 209 |
MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE)) |
26424 | 210 |
(MetaSimplifier.context ctxt MetaSimplifier.empty_ss |
211 |
addeqcongs [Drule.equals_cong] (*protect meta-level equality*) |
|
212 |
addsimps (Rules.get (Context.Proof ctxt))); |
|
18840 | 213 |
|
23541 | 214 |
val meta_rewrite_rule = Conv.fconv_rule o meta_rewrite_conv; |
18840 | 215 |
|
18875 | 216 |
|
217 |
(* rewriting with object-level rules *) |
|
218 |
||
21506 | 219 |
fun meta f ctxt = f o map (meta_rewrite_rule ctxt); |
18840 | 220 |
|
21708 | 221 |
val unfold = meta MetaSimplifier.rewrite_rule; |
222 |
val unfold_goals = meta MetaSimplifier.rewrite_goals_rule; |
|
223 |
val unfold_tac = meta MetaSimplifier.rewrite_goals_tac; |
|
224 |
val fold = meta MetaSimplifier.fold_rule; |
|
225 |
val fold_tac = meta MetaSimplifier.fold_goals_tac; |
|
18840 | 226 |
|
227 |
||
228 |
(* derived defs -- potentially within the object-logic *) |
|
229 |
||
18950 | 230 |
fun derived_def ctxt conditional prop = |
18840 | 231 |
let |
232 |
val ((c, T), rhs) = prop |
|
20049 | 233 |
|> Thm.cterm_of (ProofContext.theory_of ctxt) |
23541 | 234 |
|> meta_rewrite_conv ctxt |
18840 | 235 |
|> (snd o Logic.dest_equals o Thm.prop_of) |
21568 | 236 |
|> conditional ? Logic.strip_imp_concl |
18950 | 237 |
|> (abs_def o #2 o cert_def ctxt); |
20909 | 238 |
fun prove ctxt' def = |
18840 | 239 |
let |
240 |
val frees = Term.fold_aterms (fn Free (x, _) => |
|
20909 | 241 |
if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop []; |
18840 | 242 |
in |
20909 | 243 |
Goal.prove ctxt' frees [] prop (K (ALLGOALS |
23541 | 244 |
(CONVERSION (meta_rewrite_conv ctxt') THEN' |
245 |
MetaSimplifier.rewrite_goal_tac [def] THEN' |
|
21708 | 246 |
resolve_tac [Drule.reflexive_thm]))) |
18840 | 247 |
handle ERROR msg => cat_error msg "Failed to prove definitional specification." |
248 |
end; |
|
249 |
in (((c, T), rhs), prove) end; |
|
250 |
||
18830 | 251 |
end; |