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