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