author | skalberg |
Sat, 19 Jul 2003 17:35:15 +0200 | |
changeset 14116 | 63337d8e6e0f |
parent 14115 | 65ec3f73d00b |
child 14117 | d3512dedbaea |
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 |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
12 |
val add_specification: (bstring * bstring * bool) list -> bstring * Args.src list |
14115 | 13 |
-> theory * thm -> theory * thm |
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
14 |
val add_specification_i: (bstring * bstring * bool) list -> bstring * theory attribute list |
14115 | 15 |
-> theory * thm -> theory * thm |
16 |
end |
|
17 |
||
18 |
structure SpecificationPackage : SPECIFICATION_PACKAGE = |
|
19 |
struct |
|
20 |
||
21 |
(* messages *) |
|
22 |
||
23 |
val quiet_mode = ref false |
|
24 |
fun message s = if ! quiet_mode then () else writeln s |
|
25 |
||
26 |
local |
|
27 |
val _ = Goal "[| Ex P ; c == Eps P |] ==> P c" |
|
28 |
val _ = by (Asm_simp_tac 1) |
|
29 |
val _ = by (rtac (thm "someI_ex") 1) |
|
30 |
val _ = ba 1 |
|
31 |
in |
|
32 |
val helper = Goals.result() |
|
33 |
end |
|
34 |
||
35 |
(* Akin to HOLogic.exists_const *) |
|
36 |
fun choice_const T = Const("Hilbert_Choice.Eps",(T-->HOLogic.boolT)-->T) |
|
37 |
||
38 |
(* Actual code *) |
|
39 |
||
40 |
fun proc_exprop [] arg = arg |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
41 |
| proc_exprop ((thname,cname,covld)::cos) (thy,thm) = |
14115 | 42 |
case concl_of thm of |
43 |
Const("Trueprop",_) $ (Const("Ex",_) $ P) => |
|
44 |
let |
|
45 |
val ctype = domain_type (type_of P) |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
46 |
val cdefname = if thname = "" |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
47 |
then Thm.def_name (Sign.base_name cname) |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
48 |
else thname |
14115 | 49 |
val def_eq = Logic.mk_equals (Const(cname,ctype),choice_const ctype $ P) |
50 |
val (thy',thms) = PureThy.add_defs_i covld [((cdefname,def_eq),[])] thy |
|
51 |
val thm' = [thm,hd thms] MRS helper |
|
52 |
in |
|
53 |
proc_exprop cos (thy',thm') |
|
54 |
end |
|
55 |
| _ => raise THM ("Internal error: Bad specification theorem",0,[thm]) |
|
56 |
||
57 |
fun add_specification_i cos (name,atts) arg = |
|
58 |
let |
|
59 |
fun apply_attributes arg = Thm.apply_attributes(arg,atts) |
|
60 |
fun add_final (arg as (thy,thm)) = |
|
61 |
if name = "" |
|
62 |
then arg |
|
63 |
else (writeln ("specification " ^ name ^ ": " ^ (string_of_thm thm)); |
|
64 |
PureThy.store_thm((name,thm),[]) thy) |
|
65 |
in |
|
66 |
arg |> apsnd freezeT |
|
67 |
|> proc_exprop cos |
|
68 |
|> apsnd standard |
|
69 |
|> apply_attributes |
|
70 |
|> add_final |
|
71 |
end |
|
72 |
||
73 |
fun add_specification cos (name,atts) arg = |
|
74 |
add_specification_i cos (name,map (Attrib.global_attribute thy) atts) arg |
|
75 |
||
76 |
(* Collect all intances of constants in term *) |
|
77 |
||
78 |
fun collect_consts ( t $ u,tms) = collect_consts (u,collect_consts (t,tms)) |
|
79 |
| collect_consts ( Abs(_,_,t),tms) = collect_consts (t,tms) |
|
80 |
| collect_consts (tm as Const _,tms) = gen_ins (op aconv) (tm,tms) |
|
81 |
| collect_consts ( _,tms) = tms |
|
82 |
||
83 |
(* Complementing Type.varify... *) |
|
84 |
||
85 |
fun unvarify t fmap = |
|
86 |
let |
|
87 |
val fmap' = map Library.swap fmap |
|
88 |
fun unthaw (f as (a,S)) = |
|
89 |
(case assoc (fmap',a) of |
|
90 |
None => TVar f |
|
91 |
| Some b => TFree (b,S)) |
|
92 |
in |
|
93 |
map_term_types (map_type_tvar unthaw) t |
|
94 |
end |
|
95 |
||
96 |
(* The syntactic meddling needed to setup add_specification for work *) |
|
97 |
||
98 |
fun process_spec cos alt_name sprop int thy = |
|
99 |
let |
|
100 |
val sg = sign_of thy |
|
101 |
fun typ_equiv t u = |
|
102 |
let |
|
103 |
val tsig = Sign.tsig_of sg |
|
104 |
in |
|
105 |
Type.typ_instance(tsig,t,u) andalso |
|
106 |
Type.typ_instance(tsig,u,t) |
|
107 |
end |
|
108 |
val prop = term_of (Thm.read_cterm sg (sprop,HOLogic.boolT)) |
|
109 |
val (prop_thawed,vmap) = Type.varify (prop,[]) |
|
110 |
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
|
111 |
val (altcos,overloaded) = Library.split_list cos |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
112 |
val (names,sconsts) = Library.split_list altcos |
14115 | 113 |
val consts = map (term_of o Thm.read_cterm sg o rpair TypeInfer.logicT) sconsts |
114 |
val _ = assert (not (Library.exists (not o Term.is_Const) consts)) |
|
115 |
"Non-constant found as parameter" |
|
116 |
fun proc_const c = |
|
117 |
let |
|
118 |
val c' = fst (Type.varify (c,[])) |
|
119 |
val (cname,ctyp) = dest_Const c' |
|
120 |
in |
|
121 |
case filter (fn t => let val (name,typ) = dest_Const t |
|
122 |
in name = cname andalso typ_equiv typ ctyp |
|
123 |
end) thawed_prop_consts of |
|
124 |
[] => error ("Constant \"" ^ (Sign.string_of_term sg c) ^ "\" not found in specification") |
|
125 |
| [cf] => unvarify cf vmap |
|
126 |
| _ => error ("Several variations of \"" ^ (Sign.string_of_term sg c) ^ "\" found in specification") |
|
127 |
end |
|
128 |
val proc_consts = map proc_const consts |
|
129 |
fun mk_exist (c,prop) = |
|
130 |
let |
|
131 |
val T = type_of c |
|
132 |
in |
|
133 |
HOLogic.exists_const T $ Abs("x",T,Term.abstract_over (c,prop)) |
|
134 |
end |
|
135 |
val ex_prop = foldr mk_exist (proc_consts,prop) |
|
136 |
val cnames = map (fst o dest_Const) proc_consts |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
137 |
fun zip3 [] [] [] = [] |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
138 |
| zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
139 |
| zip3 _ _ _ = error "Internal error: Bad specification syntax" |
14115 | 140 |
in |
141 |
IsarThy.theorem_i Drule.internalK |
|
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
142 |
(("",[add_specification (zip3 (names:string list) (cnames:string list) (overloaded:bool list)) alt_name]), |
14115 | 143 |
(HOLogic.mk_Trueprop ex_prop,([],[]))) int thy |
144 |
end |
|
145 |
||
146 |
(* outer syntax *) |
|
147 |
||
148 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
|
149 |
||
150 |
(* taken from ~~/Pure/Isar/isar_syn.ML *) |
|
151 |
val opt_overloaded = |
|
152 |
Scan.optional (P.$$$ "(" |-- P.!!! ((P.$$$ "overloaded" >> K true) --| P.$$$ ")")) false |
|
153 |
||
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
154 |
val opt_name = Scan.optional (P.name --| P.$$$ ":") "" |
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
155 |
|
14115 | 156 |
val specification_decl = |
14116
63337d8e6e0f
Added optional theorem names for the constant definitions added during
skalberg
parents:
14115
diff
changeset
|
157 |
P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" -- |
14115 | 158 |
P.opt_thm_name ":" -- P.prop |
159 |
||
160 |
val specificationP = |
|
161 |
OuterSyntax.command "specification" "define constants by specification" K.thy_goal |
|
162 |
(specification_decl >> (fn ((cos,alt_name), prop) => |
|
163 |
Toplevel.print o (Toplevel.theory_to_proof (process_spec cos alt_name prop)))) |
|
164 |
||
165 |
val _ = OuterSyntax.add_parsers [specificationP] |
|
166 |
||
167 |
end |
|
168 |
||
169 |
||
170 |
end |