author | wenzelm |
Sun, 10 Dec 2006 15:30:45 +0100 | |
changeset 21746 | 9d0652354513 |
parent 21708 | 45e7491bea47 |
child 21801 | c77bc21b3eef |
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 |
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 |
|
21699 | 49 |
val abs_def = Logic.abs_def #>> 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 |
*) |
21684 | 72 |
fun expand defs = |
73 |
Drule.implies_intr_list defs |
|
74 |
#> Drule.generalize ([], map (head_of_def o Thm.term_of) defs) |
|
75 |
#> funpow (length defs) (fn th => Drule.reflexive_thm RS th); |
|
76 |
||
21699 | 77 |
fun expand_term defs = |
78 |
let |
|
79 |
val eqs = defs |> map (Thm.term_of #> abs_def #> (fn ((x, U), u) => (x, (U, u)))); |
|
80 |
fun get_def (Free (x, _)) = AList.lookup (op =) eqs x |
|
81 |
| get_def _ = NONE; |
|
82 |
in Envir.expand_term get_def end; |
|
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 |
||
18840 | 105 |
|
21684 | 106 |
(* specific export -- result based on educated guessing *) |
21568 | 107 |
|
21591 | 108 |
fun export inner outer th = |
21568 | 109 |
let |
110 |
val th' = Assumption.export false inner outer th; |
|
21591 | 111 |
val still_fixed = map #1 (Drule.fold_terms Term.add_frees th' []); |
112 |
val defs = Assumption.prems_of inner |> filter_out (fn prem => |
|
113 |
(case try (head_of_def o Thm.prop_of) prem of |
|
114 |
SOME x => member (op =) still_fixed x |
|
115 |
| NONE => true)); |
|
116 |
in (map Drule.abs_def defs, th') end; |
|
21568 | 117 |
|
118 |
||
18840 | 119 |
|
120 |
(** defived definitions **) |
|
121 |
||
122 |
(* transformation rules *) |
|
123 |
||
18859 | 124 |
structure Rules = GenericDataFun |
18840 | 125 |
( |
126 |
val name = "Pure/derived_defs"; |
|
127 |
type T = thm list; |
|
128 |
val empty = [] |
|
129 |
val extend = I; |
|
130 |
fun merge _ = Drule.merge_rules; |
|
131 |
fun print context rules = |
|
132 |
Pretty.writeln (Pretty.big_list "definitional transformations:" |
|
133 |
(map (ProofContext.pretty_thm (Context.proof_of context)) rules)); |
|
134 |
); |
|
135 |
||
18859 | 136 |
val _ = Context.add_setup Rules.init; |
18840 | 137 |
|
21506 | 138 |
val print_rules = Rules.print o Context.Proof; |
18840 | 139 |
|
18859 | 140 |
val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule); |
141 |
val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule); |
|
18840 | 142 |
|
143 |
||
18875 | 144 |
(* meta rewrite rules *) |
18840 | 145 |
|
146 |
val equals_ss = |
|
147 |
MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss |
|
148 |
addeqcongs [Drule.equals_cong]; (*protect meta-level equality*) |
|
149 |
||
21506 | 150 |
fun meta_rewrite ctxt = |
18840 | 151 |
MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE)) |
21506 | 152 |
(equals_ss addsimps (Rules.get (Context.Proof ctxt))); |
18840 | 153 |
|
18859 | 154 |
val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite; |
18840 | 155 |
|
156 |
fun meta_rewrite_tac ctxt i = |
|
21506 | 157 |
PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite ctxt))); |
18840 | 158 |
|
18875 | 159 |
|
160 |
(* rewriting with object-level rules *) |
|
161 |
||
21506 | 162 |
fun meta f ctxt = f o map (meta_rewrite_rule ctxt); |
18840 | 163 |
|
21708 | 164 |
val unfold = meta MetaSimplifier.rewrite_rule; |
165 |
val unfold_goals = meta MetaSimplifier.rewrite_goals_rule; |
|
166 |
val unfold_tac = meta MetaSimplifier.rewrite_goals_tac; |
|
167 |
val fold = meta MetaSimplifier.fold_rule; |
|
168 |
val fold_tac = meta MetaSimplifier.fold_goals_tac; |
|
18840 | 169 |
|
170 |
||
171 |
(* derived defs -- potentially within the object-logic *) |
|
172 |
||
18950 | 173 |
fun derived_def ctxt conditional prop = |
18840 | 174 |
let |
175 |
val ((c, T), rhs) = prop |
|
20049 | 176 |
|> Thm.cterm_of (ProofContext.theory_of ctxt) |
21506 | 177 |
|> meta_rewrite ctxt |
18840 | 178 |
|> (snd o Logic.dest_equals o Thm.prop_of) |
21568 | 179 |
|> conditional ? Logic.strip_imp_concl |
18950 | 180 |
|> (abs_def o #2 o cert_def ctxt); |
20909 | 181 |
fun prove ctxt' def = |
18840 | 182 |
let |
183 |
val frees = Term.fold_aterms (fn Free (x, _) => |
|
20909 | 184 |
if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop []; |
18840 | 185 |
in |
20909 | 186 |
Goal.prove ctxt' frees [] prop (K (ALLGOALS |
18840 | 187 |
(meta_rewrite_tac ctxt' THEN' |
21687 | 188 |
Goal.rewrite_goal_tac [def] THEN' |
21708 | 189 |
resolve_tac [Drule.reflexive_thm]))) |
18840 | 190 |
handle ERROR msg => cat_error msg "Failed to prove definitional specification." |
191 |
end; |
|
192 |
in (((c, T), rhs), prove) end; |
|
193 |
||
18830 | 194 |
end; |