author | wenzelm |
Tue, 03 Apr 2007 19:24:13 +0200 | |
changeset 22568 | ed7aa5a350ef |
parent 21844 | e10b8bd7a886 |
child 22671 | 3c62305fbee6 |
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 |
|
21591 | 17 |
val export: Proof.context -> Proof.context -> thm -> thm list * thm |
21844 | 18 |
val trans_terms: Proof.context -> thm list -> thm |
19 |
val trans_props: Proof.context -> thm list -> thm |
|
21506 | 20 |
val print_rules: Proof.context -> unit |
18840 | 21 |
val defn_add: attribute |
22 |
val defn_del: attribute |
|
21506 | 23 |
val meta_rewrite_rule: Proof.context -> thm -> thm |
20306 | 24 |
val unfold: Proof.context -> thm list -> thm -> thm |
25 |
val unfold_goals: Proof.context -> thm list -> thm -> thm |
|
26 |
val unfold_tac: Proof.context -> thm list -> tactic |
|
27 |
val fold: Proof.context -> thm list -> thm -> thm |
|
28 |
val fold_tac: Proof.context -> thm list -> tactic |
|
29 |
val derived_def: Proof.context -> bool -> term -> |
|
20909 | 30 |
((string * typ) * term) * (Proof.context -> thm -> thm) |
18830 | 31 |
end; |
32 |
||
33 |
structure LocalDefs: LOCAL_DEFS = |
|
34 |
struct |
|
35 |
||
18840 | 36 |
(** primitive definitions **) |
37 |
||
18830 | 38 |
(* prepare defs *) |
39 |
||
40 |
fun cert_def ctxt eq = |
|
41 |
let |
|
18950 | 42 |
val pp = ProofContext.pp ctxt; |
43 |
val display_term = quote o Pretty.string_of_term pp; |
|
44 |
fun err msg = cat_error msg ("The error(s) above occurred in definition: " ^ display_term eq); |
|
45 |
val ((lhs, _), eq') = eq |
|
46 |
|> Sign.no_vars pp |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19585
diff
changeset
|
47 |
|> Logic.dest_def pp Term.is_Free (Variable.is_fixed ctxt) (K true) |
18950 | 48 |
handle TERM (msg, _) => err msg | ERROR msg => err msg; |
49 |
in (Term.dest_Free (Term.head_of lhs), eq') end; |
|
18830 | 50 |
|
21699 | 51 |
val abs_def = Logic.abs_def #>> Term.dest_Free; |
18830 | 52 |
|
18875 | 53 |
fun mk_def ctxt args = |
54 |
let |
|
55 |
val (xs, rhss) = split_list args; |
|
56 |
val (bind, _) = ProofContext.bind_fixes xs ctxt; |
|
57 |
val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args; |
|
58 |
in map Logic.mk_equals (lhss ~~ rhss) end; |
|
59 |
||
18830 | 60 |
|
61 |
(* export defs *) |
|
62 |
||
20021 | 63 |
val head_of_def = |
20887 | 64 |
#1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body; |
20021 | 65 |
|
18830 | 66 |
|
18875 | 67 |
(* |
20887 | 68 |
[x, x == a] |
18896 | 69 |
: |
70 |
B x |
|
71 |
----------- |
|
20887 | 72 |
B a |
18875 | 73 |
*) |
21684 | 74 |
fun expand defs = |
75 |
Drule.implies_intr_list defs |
|
76 |
#> Drule.generalize ([], map (head_of_def o Thm.term_of) defs) |
|
77 |
#> funpow (length defs) (fn th => Drule.reflexive_thm RS th); |
|
78 |
||
21801 | 79 |
val expand_term = Envir.expand_term_frees o map (abs_def o Thm.term_of); |
21684 | 80 |
|
81 |
fun def_export _ defs = (expand defs, expand_term defs); |
|
18830 | 82 |
|
83 |
||
84 |
(* add defs *) |
|
85 |
||
20887 | 86 |
fun add_defs defs ctxt = |
18830 | 87 |
let |
20887 | 88 |
val ((xs, mxs), specs) = defs |> split_list |>> split_list; |
89 |
val ((names, atts), rhss) = specs |> split_list |>> split_list; |
|
90 |
val names' = map2 Thm.def_name_optional xs names; |
|
91 |
val eqs = mk_def ctxt (xs ~~ rhss); |
|
92 |
val lhss = map (fst o Logic.dest_equals) eqs; |
|
18830 | 93 |
in |
94 |
ctxt |
|
20887 | 95 |
|> ProofContext.add_fixes_i (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2 |
20980 | 96 |
|> fold Variable.declare_term eqs |
20887 | 97 |
|> ProofContext.add_assms_i def_export |
98 |
(map2 (fn a => fn eq => (a, [(eq, [])])) (names' ~~ atts) eqs) |
|
99 |
|>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss |
|
18830 | 100 |
end; |
101 |
||
18840 | 102 |
|
21684 | 103 |
(* specific export -- result based on educated guessing *) |
21568 | 104 |
|
21591 | 105 |
fun export inner outer th = |
21568 | 106 |
let |
107 |
val th' = Assumption.export false inner outer th; |
|
21591 | 108 |
val still_fixed = map #1 (Drule.fold_terms Term.add_frees th' []); |
109 |
val defs = Assumption.prems_of inner |> filter_out (fn prem => |
|
110 |
(case try (head_of_def o Thm.prop_of) prem of |
|
111 |
SOME x => member (op =) still_fixed x |
|
112 |
| NONE => true)); |
|
113 |
in (map Drule.abs_def defs, th') end; |
|
21568 | 114 |
|
115 |
||
21844 | 116 |
(* basic transitivity reasoning -- modulo beta-eta *) |
117 |
||
118 |
local |
|
119 |
||
120 |
val is_trivial = Pattern.aeconv o Logic.dest_equals o Thm.prop_of; |
|
121 |
||
122 |
fun trans_list _ _ [] = raise Empty |
|
123 |
| trans_list trans ctxt (th :: raw_eqs) = |
|
124 |
(case filter_out is_trivial raw_eqs of |
|
125 |
[] => th |
|
126 |
| eqs => |
|
22568
ed7aa5a350ef
renamed Variable.import to import_thms (avoid clash with Alice keywords);
wenzelm
parents:
21844
diff
changeset
|
127 |
let val ((_, th' :: eqs'), ctxt') = Variable.import_thms true (th :: eqs) ctxt |
21844 | 128 |
in singleton (Variable.export ctxt' ctxt) (fold trans eqs' th') end); |
129 |
||
130 |
in |
|
131 |
||
132 |
val trans_terms = trans_list (fn eq2 => fn eq1 => eq2 COMP (eq1 COMP Drule.transitive_thm)); |
|
133 |
val trans_props = trans_list (fn eq => fn th => th COMP (eq COMP Drule.equal_elim_rule1)); |
|
134 |
||
135 |
end; |
|
136 |
||
137 |
||
18840 | 138 |
|
139 |
(** defived definitions **) |
|
140 |
||
141 |
(* transformation rules *) |
|
142 |
||
18859 | 143 |
structure Rules = GenericDataFun |
18840 | 144 |
( |
145 |
val name = "Pure/derived_defs"; |
|
146 |
type T = thm list; |
|
147 |
val empty = [] |
|
148 |
val extend = I; |
|
149 |
fun merge _ = Drule.merge_rules; |
|
150 |
fun print context rules = |
|
151 |
Pretty.writeln (Pretty.big_list "definitional transformations:" |
|
152 |
(map (ProofContext.pretty_thm (Context.proof_of context)) rules)); |
|
153 |
); |
|
154 |
||
18859 | 155 |
val _ = Context.add_setup Rules.init; |
18840 | 156 |
|
21506 | 157 |
val print_rules = Rules.print o Context.Proof; |
18840 | 158 |
|
18859 | 159 |
val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule); |
160 |
val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule); |
|
18840 | 161 |
|
162 |
||
18875 | 163 |
(* meta rewrite rules *) |
18840 | 164 |
|
165 |
val equals_ss = |
|
166 |
MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss |
|
167 |
addeqcongs [Drule.equals_cong]; (*protect meta-level equality*) |
|
168 |
||
21506 | 169 |
fun meta_rewrite ctxt = |
18840 | 170 |
MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE)) |
21506 | 171 |
(equals_ss addsimps (Rules.get (Context.Proof ctxt))); |
18840 | 172 |
|
18859 | 173 |
val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite; |
18840 | 174 |
|
175 |
fun meta_rewrite_tac ctxt i = |
|
21506 | 176 |
PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite ctxt))); |
18840 | 177 |
|
18875 | 178 |
|
179 |
(* rewriting with object-level rules *) |
|
180 |
||
21506 | 181 |
fun meta f ctxt = f o map (meta_rewrite_rule ctxt); |
18840 | 182 |
|
21708 | 183 |
val unfold = meta MetaSimplifier.rewrite_rule; |
184 |
val unfold_goals = meta MetaSimplifier.rewrite_goals_rule; |
|
185 |
val unfold_tac = meta MetaSimplifier.rewrite_goals_tac; |
|
186 |
val fold = meta MetaSimplifier.fold_rule; |
|
187 |
val fold_tac = meta MetaSimplifier.fold_goals_tac; |
|
18840 | 188 |
|
189 |
||
190 |
(* derived defs -- potentially within the object-logic *) |
|
191 |
||
18950 | 192 |
fun derived_def ctxt conditional prop = |
18840 | 193 |
let |
194 |
val ((c, T), rhs) = prop |
|
20049 | 195 |
|> Thm.cterm_of (ProofContext.theory_of ctxt) |
21506 | 196 |
|> meta_rewrite ctxt |
18840 | 197 |
|> (snd o Logic.dest_equals o Thm.prop_of) |
21568 | 198 |
|> conditional ? Logic.strip_imp_concl |
18950 | 199 |
|> (abs_def o #2 o cert_def ctxt); |
20909 | 200 |
fun prove ctxt' def = |
18840 | 201 |
let |
202 |
val frees = Term.fold_aterms (fn Free (x, _) => |
|
20909 | 203 |
if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop []; |
18840 | 204 |
in |
20909 | 205 |
Goal.prove ctxt' frees [] prop (K (ALLGOALS |
18840 | 206 |
(meta_rewrite_tac ctxt' THEN' |
21687 | 207 |
Goal.rewrite_goal_tac [def] THEN' |
21708 | 208 |
resolve_tac [Drule.reflexive_thm]))) |
18840 | 209 |
handle ERROR msg => cat_error msg "Failed to prove definitional specification." |
210 |
end; |
|
211 |
in (((c, T), rhs), prove) end; |
|
212 |
||
18830 | 213 |
end; |