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