author  wenzelm 
Tue, 13 Jun 2006 23:41:39 +0200  
changeset 19876  11d447d5d68c 
parent 19585  70a1ce3b23ae 
child 20344  d02b43ea722e 
permissions  rwrr 
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 

17336  10 
val quiet_mode: bool ref 
18729  11 
val add_specification: string option > (bstring * xstring * bool) list > 
12 
theory * thm > theory * thm 

14115  13 
end 
14 

17895  15 
structure SpecificationPackage: SPECIFICATION_PACKAGE = 
14115  16 
struct 
17 

18 
(* messages *) 

19 

20 
val quiet_mode = ref false 

21 
fun message s = if ! quiet_mode then () else writeln s 

22 

23 

24 
(* Actual code *) 

25 

14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

26 
local 
17895  27 
val exE_some = thm "exE_some"; 
28 

14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

29 
fun mk_definitional [] arg = arg 
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

30 
 mk_definitional ((thname,cname,covld)::cos) (thy,thm) = 
17336  31 
case HOLogic.dest_Trueprop (concl_of thm) of 
32 
Const("Ex",_) $ P => 

33 
let 

34 
val ctype = domain_type (type_of P) 

35 
val cname_full = Sign.intern_const (sign_of thy) cname 

36 
val cdefname = if thname = "" 

37 
then Thm.def_name (Sign.base_name cname) 

38 
else thname 

39 
val def_eq = Logic.mk_equals (Const(cname_full,ctype), 

40 
HOLogic.choice_const ctype $ P) 

18358  41 
val (thms, thy') = PureThy.add_defs_i covld [((cdefname,def_eq),[])] thy 
17895  42 
val thm' = [thm,hd thms] MRS exE_some 
17336  43 
in 
44 
mk_definitional cos (thy',thm') 

45 
end 

46 
 _ => raise THM ("Internal error: Bad specification theorem",0,[thm]) 

14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

47 

1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

48 
fun mk_axiomatic axname cos arg = 
17336  49 
let 
50 
fun process [] (thy,tm) = 

51 
let 

18377  52 
val (thms, thy') = PureThy.add_axioms_i [((axname,HOLogic.mk_Trueprop tm),[])] thy 
17336  53 
in 
54 
(thy',hd thms) 

55 
end 

56 
 process ((thname,cname,covld)::cos) (thy,tm) = 

57 
case tm of 

58 
Const("Ex",_) $ P => 

59 
let 

60 
val ctype = domain_type (type_of P) 

61 
val cname_full = Sign.intern_const (sign_of thy) cname 

62 
val cdefname = if thname = "" 

63 
then Thm.def_name (Sign.base_name cname) 

64 
else thname 

65 
val co = Const(cname_full,ctype) 

66 
val thy' = Theory.add_finals_i covld [co] thy 

67 
val tm' = case P of 

68 
Abs(_, _, bodt) => subst_bound (co, bodt) 

69 
 _ => P $ co 

70 
in 

71 
process cos (thy',tm') 

72 
end 

73 
 _ => raise TERM ("Internal error: Bad specification theorem",[tm]) 

74 
in 

75 
process cos arg 

76 
end 

14115  77 

14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

78 
in 
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

79 
fun proc_exprop axiomatic cos arg = 
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

80 
case axiomatic of 
17336  81 
SOME axname => mk_axiomatic axname cos (apsnd (HOLogic.dest_Trueprop o concl_of) arg) 
15531  82 
 NONE => mk_definitional cos arg 
14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

83 
end 
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

84 

18729  85 
fun add_specification axiomatic cos arg = 
19876  86 
arg > apsnd Thm.freezeT 
17336  87 
> proc_exprop axiomatic cos 
88 
> apsnd standard 

14115  89 

18729  90 
fun add_spec x y (context, thm) = 
91 
(Context.the_theory context, thm) > add_specification x y >> Context.Theory; 

92 

93 

14115  94 
(* Collect all intances of constants in term *) 
95 

96 
fun collect_consts ( t $ u,tms) = collect_consts (u,collect_consts (t,tms)) 

97 
 collect_consts ( Abs(_,_,t),tms) = collect_consts (t,tms) 

18921  98 
 collect_consts (tm as Const _,tms) = insert (op aconv) tm tms 
14115  99 
 collect_consts ( _,tms) = tms 
100 

101 
(* Complementing Type.varify... *) 

102 

103 
fun unvarify t fmap = 

104 
let 

17336  105 
val fmap' = map Library.swap fmap 
17377  106 
fun unthaw (f as (a, S)) = 
107 
(case AList.lookup (op =) fmap' a of 

17336  108 
NONE => TVar f 
17377  109 
 SOME (b, _) => TFree (b, S)) 
14115  110 
in 
17336  111 
map_term_types (map_type_tvar unthaw) t 
14115  112 
end 
113 

114 
(* The syntactic meddling needed to setup add_specification for work *) 

115 

17336  116 
fun process_spec axiomatic cos alt_props thy = 
14115  117 
let 
17336  118 
fun zip3 [] [] [] = [] 
119 
 zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs 

120 
 zip3 _ _ _ = error "SpecificationPackage.process_spec internal error" 

14167  121 

17336  122 
fun myfoldr f [x] = x 
123 
 myfoldr f (x::xs) = f (x,myfoldr f xs) 

124 
 myfoldr f [] = error "SpecificationPackage.process_spec internal error" 

14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset

125 

17895  126 
val rew_imps = alt_props > 
18776  127 
map (ObjectLogic.atomize_cterm o Thm.read_cterm thy o rpair Term.propT o snd) 
17895  128 
val props' = rew_imps > 
129 
map (HOLogic.dest_Trueprop o term_of o snd o dest_equals o cprop_of) 

14166  130 

17336  131 
fun proc_single prop = 
132 
let 

133 
val frees = Term.term_frees prop 

134 
val tsig = Sign.tsig_of (sign_of thy) 

135 
val _ = assert (forall (fn v => Type.of_sort tsig (type_of v,HOLogic.typeS)) frees) 

136 
"Specificaton: Only free variables of sort 'type' allowed" 

137 
val prop_closed = foldr (fn ((vname,T),prop) => HOLogic.mk_all (vname,T,prop)) prop (map dest_Free frees) 

138 
in 

139 
(prop_closed,frees) 

140 
end 

14166  141 

17336  142 
val props'' = map proc_single props' 
143 
val frees = map snd props'' 

144 
val prop = myfoldr HOLogic.mk_conj (map fst props'') 

17895  145 
val cprop = cterm_of thy (HOLogic.mk_Trueprop prop) 
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset

146 

17336  147 
val (prop_thawed,vmap) = Type.varify (prop,[]) 
148 
val thawed_prop_consts = collect_consts (prop_thawed,[]) 

149 
val (altcos,overloaded) = Library.split_list cos 

150 
val (names,sconsts) = Library.split_list altcos 

17895  151 
val consts = map (term_of o Thm.read_cterm thy o rpair TypeInfer.logicT) sconsts 
17336  152 
val _ = assert (not (Library.exists (not o Term.is_Const) consts)) 
153 
"Specification: Nonconstant found as parameter" 

14166  154 

17336  155 
fun proc_const c = 
156 
let 

157 
val c' = fst (Type.varify (c,[])) 

158 
val (cname,ctyp) = dest_Const c' 

159 
in 

160 
case List.filter (fn t => let val (name,typ) = dest_Const t 

19414  161 
in name = cname andalso Sign.typ_equiv thy (typ, ctyp) 
17336  162 
end) thawed_prop_consts of 
17895  163 
[] => error ("Specification: No suitable instances of constant \"" ^ (Sign.string_of_term thy c) ^ "\" found") 
17336  164 
 [cf] => unvarify cf vmap 
17895  165 
 _ => error ("Specification: Several variations of \"" ^ (Sign.string_of_term thy c) ^ "\" found (try applying explicit type constraints)") 
17336  166 
end 
167 
val proc_consts = map proc_const consts 

168 
fun mk_exist (c,prop) = 

169 
let 

170 
val T = type_of c 

171 
val cname = Sign.base_name (fst (dest_Const c)) 

172 
val vname = if Syntax.is_identifier cname 

173 
then cname 

174 
else "x" 

175 
in 

176 
HOLogic.exists_const T $ Abs(vname,T,Term.abstract_over (c,prop)) 

177 
end 

178 
val ex_prop = foldr mk_exist prop proc_consts 

179 
val cnames = map (fst o dest_Const) proc_consts 

180 
fun post_process (arg as (thy,thm)) = 

181 
let 

17895  182 
fun inst_all thy (thm,v) = 
17336  183 
let 
17895  184 
val cv = cterm_of thy v 
17336  185 
val cT = ctyp_of_term cv 
186 
val spec' = instantiate' [SOME cT] [NONE,SOME cv] spec 

187 
in 

188 
thm RS spec' 

189 
end 

190 
fun remove_alls frees thm = Library.foldl (inst_all (sign_of_thm thm)) (thm,frees) 

191 
fun process_single ((name,atts),rew_imp,frees) args = 

192 
let 

193 
fun undo_imps thm = 

194 
equal_elim (symmetric rew_imp) thm 

14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset

195 

18358  196 
fun add_final (arg as (thy, thm)) = 
17336  197 
if name = "" 
18358  198 
then arg > Library.swap 
17336  199 
else (writeln (" " ^ name ^ ": " ^ (string_of_thm thm)); 
18358  200 
PureThy.store_thm ((name, thm), []) thy) 
17336  201 
in 
202 
args > apsnd (remove_alls frees) 

203 
> apsnd undo_imps 

204 
> apsnd standard 

18729  205 
> Thm.theory_attributes (map (Attrib.attribute thy) atts) 
17336  206 
> add_final 
18358  207 
> Library.swap 
17336  208 
end 
14164
8c3fab596219
Allowed for splitting the specification over several lemmas.
skalberg
parents:
14122
diff
changeset

209 

17336  210 
fun process_all [proc_arg] args = 
211 
process_single proc_arg args 

212 
 process_all (proc_arg::rest) (thy,thm) = 

213 
let 

214 
val single_th = thm RS conjunct1 

215 
val rest_th = thm RS conjunct2 

216 
val (thy',_) = process_single proc_arg (thy,single_th) 

217 
in 

218 
process_all rest (thy',rest_th) 

219 
end 

220 
 process_all [] _ = error "SpecificationPackage.process_spec internal error" 

221 
val alt_names = map fst alt_props 

222 
val _ = if exists (fn(name,_) => not (name = "")) alt_names 

223 
then writeln "specification" 

224 
else () 

225 
in 

19876  226 
arg > apsnd Thm.freezeT 
17336  227 
> process_all (zip3 alt_names rew_imps frees) 
228 
end 

18729  229 
fun post_proc (context, th) = 
230 
post_process (Context.theory_of context, th) >> Context.Theory; 

14115  231 
in 
18799  232 
IsarThy.theorem_i PureThy.internalK 
18729  233 
("", [add_spec axiomatic (zip3 names cnames overloaded), post_proc]) 
19585  234 
(HOLogic.mk_Trueprop ex_prop, []) thy 
14115  235 
end 
236 

17336  237 

14115  238 
(* outer syntax *) 
239 

17057  240 
local structure P = OuterParse and K = OuterKeyword in 
14115  241 

14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset

242 
val opt_name = Scan.optional (P.name  P.$$$ ":") "" 
17336  243 
val opt_overloaded = P.opt_keyword "overloaded"; 
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset

244 

14115  245 
val specification_decl = 
17336  246 
P.$$$ "("  Scan.repeat1 (opt_name  P.term  opt_overloaded)  P.$$$ ")"  
247 
Scan.repeat1 (P.opt_thm_name ":"  P.prop) 

14115  248 

249 
val specificationP = 

250 
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

251 
(specification_decl >> (fn (cos,alt_props) => 
17336  252 
Toplevel.print o (Toplevel.theory_to_proof 
253 
(process_spec NONE cos alt_props)))) 

14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

254 

1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

255 
val ax_specification_decl = 
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

256 
P.name  
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

257 
(P.$$$ "("  Scan.repeat1 (opt_name  P.term  opt_overloaded)  P.$$$ ")"  
17336  258 
Scan.repeat1 (P.opt_thm_name ":"  P.prop)) 
14115  259 

14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

260 
val ax_specificationP = 
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

261 
OuterSyntax.command "ax_specification" "define constants by specification" K.thy_goal 
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

262 
(ax_specification_decl >> (fn (axname,(cos,alt_props)) => 
17336  263 
Toplevel.print o (Toplevel.theory_to_proof 
264 
(process_spec (SOME axname) cos alt_props)))) 

14222
1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

265 

1e61f23fd359
Added axiomatic specifications (ax_specification).
skalberg
parents:
14167
diff
changeset

266 
val _ = OuterSyntax.add_parsers [specificationP,ax_specificationP] 
14115  267 

268 
end 

269 

270 

271 
end 