author | wenzelm |
Sat, 17 May 2008 13:54:30 +0200 | |
changeset 26928 | ca87aff1ad2d |
parent 26481 | 92e901171cc8 |
child 26939 | 1035c89b4c02 |
permissions | -rw-r--r-- |
14115 | 1 |
(* Title: HOL/Tools/specification_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Sebastian Skalberg, TU Muenchen |
|
4 |
||
5 |
Package for defining constants by specification. |
|
6 |
*) |
|
7 |
||
8 |
signature SPECIFICATION_PACKAGE = |
|
9 |
sig |
|
18729 | 10 |
val add_specification: string option -> (bstring * xstring * bool) list -> |
11 |
theory * thm -> theory * thm |
|
14115 | 12 |
end |
13 |
||
17895 | 14 |
structure SpecificationPackage: SPECIFICATION_PACKAGE = |
14115 | 15 |
struct |
16 |
||
26478 | 17 |
(* actual code *) |
14115 | 18 |
|
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
19 |
local |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
20 |
fun mk_definitional [] arg = arg |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
21 |
| mk_definitional ((thname,cname,covld)::cos) (thy,thm) = |
17336 | 22 |
case HOLogic.dest_Trueprop (concl_of thm) of |
23 |
Const("Ex",_) $ P => |
|
24 |
let |
|
25 |
val ctype = domain_type (type_of P) |
|
22578 | 26 |
val cname_full = Sign.intern_const thy cname |
17336 | 27 |
val cdefname = if thname = "" |
28 |
then Thm.def_name (Sign.base_name cname) |
|
29 |
else thname |
|
30 |
val def_eq = Logic.mk_equals (Const(cname_full,ctype), |
|
31 |
HOLogic.choice_const ctype $ P) |
|
18358 | 32 |
val (thms, thy') = PureThy.add_defs_i covld [((cdefname,def_eq),[])] thy |
26478 | 33 |
val thm' = [thm,hd thms] MRS @{thm exE_some} |
17336 | 34 |
in |
35 |
mk_definitional cos (thy',thm') |
|
36 |
end |
|
37 |
| _ => raise THM ("Internal error: Bad specification theorem",0,[thm]) |
|
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
38 |
|
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
39 |
fun mk_axiomatic axname cos arg = |
17336 | 40 |
let |
41 |
fun process [] (thy,tm) = |
|
42 |
let |
|
18377 | 43 |
val (thms, thy') = PureThy.add_axioms_i [((axname,HOLogic.mk_Trueprop tm),[])] thy |
17336 | 44 |
in |
45 |
(thy',hd thms) |
|
46 |
end |
|
47 |
| process ((thname,cname,covld)::cos) (thy,tm) = |
|
48 |
case tm of |
|
49 |
Const("Ex",_) $ P => |
|
50 |
let |
|
51 |
val ctype = domain_type (type_of P) |
|
22578 | 52 |
val cname_full = Sign.intern_const thy cname |
17336 | 53 |
val cdefname = if thname = "" |
54 |
then Thm.def_name (Sign.base_name cname) |
|
55 |
else thname |
|
56 |
val co = Const(cname_full,ctype) |
|
57 |
val thy' = Theory.add_finals_i covld [co] thy |
|
58 |
val tm' = case P of |
|
59 |
Abs(_, _, bodt) => subst_bound (co, bodt) |
|
60 |
| _ => P $ co |
|
61 |
in |
|
62 |
process cos (thy',tm') |
|
63 |
end |
|
64 |
| _ => raise TERM ("Internal error: Bad specification theorem",[tm]) |
|
65 |
in |
|
66 |
process cos arg |
|
67 |
end |
|
14115 | 68 |
|
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
69 |
in |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
70 |
fun proc_exprop axiomatic cos arg = |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
71 |
case axiomatic of |
17336 | 72 |
SOME axname => mk_axiomatic axname cos (apsnd (HOLogic.dest_Trueprop o concl_of) arg) |
15531 | 73 |
| NONE => mk_definitional cos arg |
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
74 |
end |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
75 |
|
18729 | 76 |
fun add_specification axiomatic cos arg = |
19876 | 77 |
arg |> apsnd Thm.freezeT |
17336 | 78 |
|> proc_exprop axiomatic cos |
79 |
|> apsnd standard |
|
14115 | 80 |
|
18729 | 81 |
|
14115 | 82 |
(* Collect all intances of constants in term *) |
83 |
||
84 |
fun collect_consts ( t $ u,tms) = collect_consts (u,collect_consts (t,tms)) |
|
85 |
| collect_consts ( Abs(_,_,t),tms) = collect_consts (t,tms) |
|
18921 | 86 |
| collect_consts (tm as Const _,tms) = insert (op aconv) tm tms |
14115 | 87 |
| collect_consts ( _,tms) = tms |
88 |
||
89 |
(* Complementing Type.varify... *) |
|
90 |
||
91 |
fun unvarify t fmap = |
|
92 |
let |
|
17336 | 93 |
val fmap' = map Library.swap fmap |
17377 | 94 |
fun unthaw (f as (a, S)) = |
95 |
(case AList.lookup (op =) fmap' a of |
|
17336 | 96 |
NONE => TVar f |
17377 | 97 |
| SOME (b, _) => TFree (b, S)) |
14115 | 98 |
in |
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20344
diff
changeset
|
99 |
map_types (map_type_tvar unthaw) t |
14115 | 100 |
end |
101 |
||
102 |
(* The syntactic meddling needed to setup add_specification for work *) |
|
103 |
||
17336 | 104 |
fun process_spec axiomatic cos alt_props thy = |
14115 | 105 |
let |
17336 | 106 |
fun zip3 [] [] [] = [] |
107 |
| zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs |
|
108 |
| zip3 _ _ _ = error "SpecificationPackage.process_spec internal error" |
|
14167 | 109 |
|
17336 | 110 |
fun myfoldr f [x] = x |
111 |
| myfoldr f (x::xs) = f (x,myfoldr f xs) |
|
112 |
| myfoldr f [] = error "SpecificationPackage.process_spec internal error" |
|
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
113 |
|
17895 | 114 |
val rew_imps = alt_props |> |
24707 | 115 |
map (ObjectLogic.atomize o Thm.cterm_of thy o Syntax.read_prop_global thy o snd) |
17895 | 116 |
val props' = rew_imps |> |
22902
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents:
22709
diff
changeset
|
117 |
map (HOLogic.dest_Trueprop o term_of o snd o Thm.dest_equals o cprop_of) |
14166 | 118 |
|
17336 | 119 |
fun proc_single prop = |
120 |
let |
|
121 |
val frees = Term.term_frees prop |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21434
diff
changeset
|
122 |
val _ = forall (fn v => Sign.of_sort thy (type_of v,HOLogic.typeS)) frees |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21434
diff
changeset
|
123 |
orelse error "Specificaton: Only free variables of sort 'type' allowed" |
17336 | 124 |
val prop_closed = foldr (fn ((vname,T),prop) => HOLogic.mk_all (vname,T,prop)) prop (map dest_Free frees) |
125 |
in |
|
126 |
(prop_closed,frees) |
|
127 |
end |
|
14166 | 128 |
|
17336 | 129 |
val props'' = map proc_single props' |
130 |
val frees = map snd props'' |
|
131 |
val prop = myfoldr HOLogic.mk_conj (map fst props'') |
|
17895 | 132 |
val cprop = cterm_of thy (HOLogic.mk_Trueprop prop) |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
133 |
|
21116 | 134 |
val (vmap, prop_thawed) = Type.varify [] prop |
17336 | 135 |
val thawed_prop_consts = collect_consts (prop_thawed,[]) |
136 |
val (altcos,overloaded) = Library.split_list cos |
|
137 |
val (names,sconsts) = Library.split_list altcos |
|
24707 | 138 |
val consts = map (Syntax.read_term_global thy) sconsts |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21434
diff
changeset
|
139 |
val _ = not (Library.exists (not o Term.is_Const) consts) |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21434
diff
changeset
|
140 |
orelse error "Specification: Non-constant found as parameter" |
14166 | 141 |
|
17336 | 142 |
fun proc_const c = |
143 |
let |
|
21116 | 144 |
val (_, c') = Type.varify [] c |
17336 | 145 |
val (cname,ctyp) = dest_Const c' |
146 |
in |
|
147 |
case List.filter (fn t => let val (name,typ) = dest_Const t |
|
19414 | 148 |
in name = cname andalso Sign.typ_equiv thy (typ, ctyp) |
17336 | 149 |
end) thawed_prop_consts of |
17895 | 150 |
[] => error ("Specification: No suitable instances of constant \"" ^ (Sign.string_of_term thy c) ^ "\" found") |
17336 | 151 |
| [cf] => unvarify cf vmap |
17895 | 152 |
| _ => error ("Specification: Several variations of \"" ^ (Sign.string_of_term thy c) ^ "\" found (try applying explicit type constraints)") |
17336 | 153 |
end |
154 |
val proc_consts = map proc_const consts |
|
155 |
fun mk_exist (c,prop) = |
|
156 |
let |
|
157 |
val T = type_of c |
|
158 |
val cname = Sign.base_name (fst (dest_Const c)) |
|
159 |
val vname = if Syntax.is_identifier cname |
|
160 |
then cname |
|
161 |
else "x" |
|
162 |
in |
|
163 |
HOLogic.exists_const T $ Abs(vname,T,Term.abstract_over (c,prop)) |
|
164 |
end |
|
165 |
val ex_prop = foldr mk_exist prop proc_consts |
|
166 |
val cnames = map (fst o dest_Const) proc_consts |
|
167 |
fun post_process (arg as (thy,thm)) = |
|
168 |
let |
|
17895 | 169 |
fun inst_all thy (thm,v) = |
17336 | 170 |
let |
17895 | 171 |
val cv = cterm_of thy v |
17336 | 172 |
val cT = ctyp_of_term cv |
173 |
val spec' = instantiate' [SOME cT] [NONE,SOME cv] spec |
|
174 |
in |
|
175 |
thm RS spec' |
|
176 |
end |
|
22578 | 177 |
fun remove_alls frees thm = |
178 |
Library.foldl (inst_all (Thm.theory_of_thm thm)) (thm,frees) |
|
17336 | 179 |
fun process_single ((name,atts),rew_imp,frees) args = |
180 |
let |
|
181 |
fun undo_imps thm = |
|
182 |
equal_elim (symmetric rew_imp) thm |
|
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
183 |
|
18358 | 184 |
fun add_final (arg as (thy, thm)) = |
17336 | 185 |
if name = "" |
18358 | 186 |
then arg |> Library.swap |
26928 | 187 |
else (writeln (" " ^ name ^ ": " ^ (Display.string_of_thm thm)); |
26481 | 188 |
PureThy.store_thm (name, thm) thy) |
17336 | 189 |
in |
190 |
args |> apsnd (remove_alls frees) |
|
191 |
|> apsnd undo_imps |
|
192 |
|> apsnd standard |
|
18729 | 193 |
|> Thm.theory_attributes (map (Attrib.attribute thy) atts) |
17336 | 194 |
|> add_final |
18358 | 195 |
|> Library.swap |
17336 | 196 |
end |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
197 |
|
17336 | 198 |
fun process_all [proc_arg] args = |
199 |
process_single proc_arg args |
|
200 |
| process_all (proc_arg::rest) (thy,thm) = |
|
201 |
let |
|
202 |
val single_th = thm RS conjunct1 |
|
203 |
val rest_th = thm RS conjunct2 |
|
204 |
val (thy',_) = process_single proc_arg (thy,single_th) |
|
205 |
in |
|
206 |
process_all rest (thy',rest_th) |
|
207 |
end |
|
208 |
| process_all [] _ = error "SpecificationPackage.process_spec internal error" |
|
209 |
val alt_names = map fst alt_props |
|
210 |
val _ = if exists (fn(name,_) => not (name = "")) alt_names |
|
211 |
then writeln "specification" |
|
212 |
else () |
|
213 |
in |
|
19876 | 214 |
arg |> apsnd Thm.freezeT |
17336 | 215 |
|> process_all (zip3 alt_names rew_imps frees) |
216 |
end |
|
20903 | 217 |
|
218 |
fun after_qed [[thm]] = ProofContext.theory (fn thy => |
|
219 |
#1 (post_process (add_specification axiomatic (zip3 names cnames overloaded) (thy, thm)))); |
|
14115 | 220 |
in |
20903 | 221 |
thy |
222 |
|> ProofContext.init |
|
21434 | 223 |
|> Proof.theorem_i NONE after_qed [[(HOLogic.mk_Trueprop ex_prop, [])]] |
20903 | 224 |
end; |
14115 | 225 |
|
17336 | 226 |
|
14115 | 227 |
(* outer syntax *) |
228 |
||
17057 | 229 |
local structure P = OuterParse and K = OuterKeyword in |
14115 | 230 |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
231 |
val opt_name = Scan.optional (P.name --| P.$$$ ":") "" |
17336 | 232 |
val opt_overloaded = P.opt_keyword "overloaded"; |
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
233 |
|
14115 | 234 |
val specification_decl = |
17336 | 235 |
P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" -- |
22101 | 236 |
Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop) |
14115 | 237 |
|
24867 | 238 |
val _ = |
14115 | 239 |
OuterSyntax.command "specification" "define constants by specification" K.thy_goal |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
240 |
(specification_decl >> (fn (cos,alt_props) => |
17336 | 241 |
Toplevel.print o (Toplevel.theory_to_proof |
242 |
(process_spec NONE cos alt_props)))) |
|
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
243 |
|
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
244 |
val ax_specification_decl = |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
245 |
P.name -- |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
246 |
(P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" -- |
22101 | 247 |
Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop)) |
14115 | 248 |
|
24867 | 249 |
val _ = |
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
250 |
OuterSyntax.command "ax_specification" "define constants by specification" K.thy_goal |
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
251 |
(ax_specification_decl >> (fn (axname,(cos,alt_props)) => |
17336 | 252 |
Toplevel.print o (Toplevel.theory_to_proof |
253 |
(process_spec (SOME axname) cos alt_props)))) |
|
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset
|
254 |
|
14115 | 255 |
end |
256 |
||
257 |
||
258 |
end |