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