author | skalberg |
Wed, 27 Aug 2003 10:11:30 +0200 | |
changeset 14167 | 3c29bba24aa4 |
parent 14166 | 1ed8f955727d |
child 14222 | 1e61f23fd359 |
permissions | -rw-r--r-- |
14115 | 1 |
(* Title: HOL/Tools/specification_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Sebastian Skalberg, TU Muenchen |
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
|
5 |
||
6 |
Package for defining constants by specification. |
|
7 |
*) |
|
8 |
||
9 |
signature SPECIFICATION_PACKAGE = |
|
10 |
sig |
|
11 |
val quiet_mode: bool ref |
|
14121
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
12 |
val add_specification_i: (bstring * xstring * bool) list |
14115 | 13 |
-> theory * thm -> theory * thm |
14 |
end |
|
15 |
||
16 |
structure SpecificationPackage : SPECIFICATION_PACKAGE = |
|
17 |
struct |
|
18 |
||
19 |
(* messages *) |
|
20 |
||
21 |
val quiet_mode = ref false |
|
22 |
fun message s = if ! quiet_mode then () else writeln s |
|
23 |
||
24 |
local |
|
25 |
val _ = Goal "[| Ex P ; c == Eps P |] ==> P c" |
|
26 |
val _ = by (Asm_simp_tac 1) |
|
27 |
val _ = by (rtac (thm "someI_ex") 1) |
|
28 |
val _ = ba 1 |
|
29 |
in |
|
30 |
val helper = Goals.result() |
|
31 |
end |
|
32 |
||
33 |
(* Akin to HOLogic.exists_const *) |
|
34 |
fun choice_const T = Const("Hilbert_Choice.Eps",(T-->HOLogic.boolT)-->T) |
|
35 |
||
36 |
(* Actual code *) |
|
37 |
||
38 |
fun proc_exprop [] arg = arg |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
39 |
| proc_exprop ((thname,cname,covld)::cos) (thy,thm) = |
14115 | 40 |
case concl_of thm of |
41 |
Const("Trueprop",_) $ (Const("Ex",_) $ P) => |
|
42 |
let |
|
43 |
val ctype = domain_type (type_of P) |
|
14117 | 44 |
val cname_full = Sign.intern_const (sign_of thy) cname |
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
45 |
val cdefname = if thname = "" |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
46 |
then Thm.def_name (Sign.base_name cname) |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
47 |
else thname |
14117 | 48 |
val def_eq = Logic.mk_equals (Const(cname_full,ctype),choice_const ctype $ P) |
14115 | 49 |
val (thy',thms) = PureThy.add_defs_i covld [((cdefname,def_eq),[])] thy |
50 |
val thm' = [thm,hd thms] MRS helper |
|
51 |
in |
|
52 |
proc_exprop cos (thy',thm') |
|
53 |
end |
|
54 |
| _ => raise THM ("Internal error: Bad specification theorem",0,[thm]) |
|
55 |
||
14121
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
56 |
fun add_specification_i cos arg = |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
57 |
arg |> apsnd freezeT |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
58 |
|> proc_exprop cos |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
59 |
|> apsnd standard |
14115 | 60 |
|
61 |
(* Collect all intances of constants in term *) |
|
62 |
||
63 |
fun collect_consts ( t $ u,tms) = collect_consts (u,collect_consts (t,tms)) |
|
64 |
| collect_consts ( Abs(_,_,t),tms) = collect_consts (t,tms) |
|
65 |
| collect_consts (tm as Const _,tms) = gen_ins (op aconv) (tm,tms) |
|
66 |
| collect_consts ( _,tms) = tms |
|
67 |
||
68 |
(* Complementing Type.varify... *) |
|
69 |
||
70 |
fun unvarify t fmap = |
|
71 |
let |
|
72 |
val fmap' = map Library.swap fmap |
|
73 |
fun unthaw (f as (a,S)) = |
|
74 |
(case assoc (fmap',a) of |
|
75 |
None => TVar f |
|
76 |
| Some b => TFree (b,S)) |
|
77 |
in |
|
78 |
map_term_types (map_type_tvar unthaw) t |
|
79 |
end |
|
80 |
||
81 |
(* The syntactic meddling needed to setup add_specification for work *) |
|
82 |
||
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
83 |
fun process_spec cos alt_props int thy = |
14115 | 84 |
let |
14167 | 85 |
fun zip3 [] [] [] = [] |
86 |
| zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs |
|
87 |
| zip3 _ _ _ = error "SpecificationPackage.process_spec internal error" |
|
88 |
||
89 |
fun myfoldr f [x] = x |
|
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
90 |
| myfoldr f (x::xs) = f (x,myfoldr f xs) |
14167 | 91 |
| myfoldr f [] = error "SpecificationPackage.process_spec internal error" |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
92 |
|
14115 | 93 |
val sg = sign_of thy |
94 |
fun typ_equiv t u = |
|
95 |
let |
|
96 |
val tsig = Sign.tsig_of sg |
|
97 |
in |
|
98 |
Type.typ_instance(tsig,t,u) andalso |
|
99 |
Type.typ_instance(tsig,u,t) |
|
100 |
end |
|
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
101 |
|
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
102 |
val cprops = map (Thm.read_cterm sg o rpair Term.propT o snd) alt_props |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
103 |
val ss = empty_ss setmksimps single addsimps [thm "HOL.atomize_imp",thm "HOL.atomize_all"] |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
104 |
val rew_imps = map (Simplifier.full_rewrite ss) cprops |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
105 |
val props' = map (HOLogic.dest_Trueprop o term_of o snd o dest_equals o cprop_of) rew_imps |
14166 | 106 |
|
107 |
fun proc_single prop = |
|
108 |
let |
|
109 |
val frees = Term.term_frees prop |
|
110 |
val tsig = Sign.tsig_of (sign_of thy) |
|
111 |
val _ = assert (forall (fn v => Type.of_sort tsig (type_of v,HOLogic.typeS)) frees) |
|
14167 | 112 |
"Specificaton: Only free variables of sort 'type' allowed" |
14166 | 113 |
val prop_closed = foldr (fn ((vname,T),prop) => HOLogic.mk_all (vname,T,prop)) (map dest_Free frees,prop) |
114 |
in |
|
115 |
(prop_closed,frees) |
|
116 |
end |
|
117 |
||
118 |
val props'' = map proc_single props' |
|
119 |
val frees = map snd props'' |
|
120 |
val prop = myfoldr HOLogic.mk_conj (map fst props'') |
|
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
121 |
val cprop = cterm_of sg (HOLogic.mk_Trueprop prop) |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
122 |
|
14115 | 123 |
val (prop_thawed,vmap) = Type.varify (prop,[]) |
124 |
val thawed_prop_consts = collect_consts (prop_thawed,[]) |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
125 |
val (altcos,overloaded) = Library.split_list cos |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
126 |
val (names,sconsts) = Library.split_list altcos |
14115 | 127 |
val consts = map (term_of o Thm.read_cterm sg o rpair TypeInfer.logicT) sconsts |
128 |
val _ = assert (not (Library.exists (not o Term.is_Const) consts)) |
|
14167 | 129 |
"Specification: Non-constant found as parameter" |
14166 | 130 |
|
14115 | 131 |
fun proc_const c = |
132 |
let |
|
133 |
val c' = fst (Type.varify (c,[])) |
|
134 |
val (cname,ctyp) = dest_Const c' |
|
135 |
in |
|
136 |
case filter (fn t => let val (name,typ) = dest_Const t |
|
137 |
in name = cname andalso typ_equiv typ ctyp |
|
138 |
end) thawed_prop_consts of |
|
14167 | 139 |
[] => error ("Specification: No suitable instances of constant \"" ^ (Sign.string_of_term sg c) ^ "\" found") |
14115 | 140 |
| [cf] => unvarify cf vmap |
14167 | 141 |
| _ => error ("Specification: Several variations of \"" ^ (Sign.string_of_term sg c) ^ "\" found (try applying explicit type constraints)") |
14115 | 142 |
end |
143 |
val proc_consts = map proc_const consts |
|
144 |
fun mk_exist (c,prop) = |
|
145 |
let |
|
146 |
val T = type_of c |
|
14166 | 147 |
val cname = Sign.base_name (fst (dest_Const c)) |
148 |
val vname = if Syntax.is_identifier cname |
|
149 |
then cname |
|
150 |
else "x" |
|
14115 | 151 |
in |
14166 | 152 |
HOLogic.exists_const T $ Abs(vname,T,Term.abstract_over (c,prop)) |
14115 | 153 |
end |
14166 | 154 |
val ex_prop = foldr mk_exist (proc_consts,prop) |
14115 | 155 |
val cnames = map (fst o dest_Const) proc_consts |
14121
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
156 |
fun post_process (arg as (thy,thm)) = |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
157 |
let |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
158 |
fun inst_all sg (thm,v) = |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
159 |
let |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
160 |
val cv = cterm_of sg v |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
161 |
val cT = ctyp_of_term cv |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
162 |
val spec' = instantiate' [Some cT] [None,Some cv] spec |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
163 |
in |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
164 |
thm RS spec' |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
165 |
end |
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
166 |
fun remove_alls frees thm = foldl (inst_all (sign_of_thm thm)) (thm,frees) |
14167 | 167 |
fun process_single ((name,atts),rew_imp,frees) args = |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
168 |
let |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
169 |
fun undo_imps thm = |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
170 |
equal_elim (symmetric rew_imp) thm |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
171 |
|
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
172 |
fun apply_atts arg = Thm.apply_attributes (arg,map (Attrib.global_attribute thy) atts) |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
173 |
fun add_final (arg as (thy,thm)) = |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
174 |
if name = "" |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
175 |
then arg |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
176 |
else (writeln (" " ^ name ^ ": " ^ (string_of_thm thm)); |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
177 |
PureThy.store_thm((name,thm),[]) thy) |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
178 |
in |
14166 | 179 |
args |> apsnd (remove_alls frees) |
14167 | 180 |
|> apsnd undo_imps |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
181 |
|> apsnd standard |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
182 |
|> apply_atts |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
183 |
|> add_final |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
184 |
end |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
185 |
|
14167 | 186 |
fun process_all [proc_arg] args = |
187 |
process_single proc_arg args |
|
188 |
| process_all (proc_arg::rest) (thy,thm) = |
|
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
189 |
let |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
190 |
val single_th = thm RS conjunct1 |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
191 |
val rest_th = thm RS conjunct2 |
14167 | 192 |
val (thy',_) = process_single proc_arg (thy,single_th) |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
193 |
in |
14167 | 194 |
process_all rest (thy',rest_th) |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
195 |
end |
14167 | 196 |
| process_all [] _ = error "SpecificationPackage.process_spec internal error" |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
197 |
val alt_names = map fst alt_props |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
198 |
val _ = if exists (fn(name,_) => not (name = "")) alt_names |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
199 |
then writeln "specification" |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
200 |
else () |
14121
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
201 |
in |
14122
433f9a414537
Added handling of meta implication and meta quantification.
skalberg
parents:
14121
diff
changeset
|
202 |
arg |> apsnd freezeT |
14167 | 203 |
|> process_all (zip3 alt_names rew_imps frees) |
14121
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
204 |
end |
14115 | 205 |
in |
206 |
IsarThy.theorem_i Drule.internalK |
|
14121
d2a0fd183f5f
Added handling of free variables (provided they are of sort HOL.type).
skalberg
parents:
14118
diff
changeset
|
207 |
(("",[add_specification_i (zip3 (names:string list) (cnames:string list) (overloaded:bool list)),post_process]), |
14115 | 208 |
(HOLogic.mk_Trueprop ex_prop,([],[]))) int thy |
209 |
end |
|
210 |
||
211 |
(* outer syntax *) |
|
212 |
||
213 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
|
214 |
||
215 |
(* taken from ~~/Pure/Isar/isar_syn.ML *) |
|
216 |
val opt_overloaded = |
|
217 |
Scan.optional (P.$$$ "(" |-- P.!!! ((P.$$$ "overloaded" >> K true) --| P.$$$ ")")) false |
|
218 |
||
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
219 |
val opt_name = Scan.optional (P.name --| P.$$$ ":") "" |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
220 |
|
14115 | 221 |
val specification_decl = |
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
222 |
P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" -- |
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
223 |
Scan.repeat1 (P.opt_thm_name ":" -- P.prop) |
14115 | 224 |
|
225 |
val specificationP = |
|
226 |
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
|
227 |
(specification_decl >> (fn (cos,alt_props) => |
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset
|
228 |
Toplevel.print o (Toplevel.theory_to_proof (process_spec cos alt_props)))) |
14115 | 229 |
|
230 |
val _ = OuterSyntax.add_parsers [specificationP] |
|
231 |
||
232 |
end |
|
233 |
||
234 |
||
235 |
end |