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