author | wenzelm |
Fri, 06 Mar 2015 23:57:01 +0100 | |
changeset 59643 | f3be9235503d |
parent 59621 | 291934bac95e |
child 60754 | 02924903a6fd |
permissions | -rw-r--r-- |
28308 | 1 |
(* Title: HOL/Statespace/state_fun.ML |
25171 | 2 |
Author: Norbert Schirmer, TU Muenchen |
3 |
*) |
|
4 |
||
25408 | 5 |
signature STATE_FUN = |
6 |
sig |
|
7 |
val lookupN : string |
|
8 |
val updateN : string |
|
25171 | 9 |
|
29302 | 10 |
val mk_constr : theory -> typ -> term |
11 |
val mk_destr : theory -> typ -> term |
|
25408 | 12 |
|
29302 | 13 |
val lookup_simproc : simproc |
14 |
val update_simproc : simproc |
|
15 |
val ex_lookup_eq_simproc : simproc |
|
16 |
val ex_lookup_ss : simpset |
|
17 |
val lazy_conj_simproc : simproc |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
18 |
val string_eq_simp_tac : Proof.context -> int -> tactic |
25408 | 19 |
end; |
20 |
||
45363 | 21 |
structure StateFun: STATE_FUN = |
25171 | 22 |
struct |
23 |
||
45363 | 24 |
val lookupN = @{const_name StateFun.lookup}; |
25 |
val updateN = @{const_name StateFun.update}; |
|
25171 | 26 |
|
25408 | 27 |
val sel_name = HOLogic.dest_string; |
25171 | 28 |
|
29 |
fun mk_name i t = |
|
30 |
(case try sel_name t of |
|
45363 | 31 |
SOME name => name |
32 |
| NONE => |
|
33 |
(case t of |
|
34 |
Free (x, _) => x |
|
35 |
| Const (x, _) => x |
|
36 |
| _ => "x" ^ string_of_int i)); |
|
37 |
||
25171 | 38 |
local |
32921 | 39 |
|
39159 | 40 |
val conj1_False = @{thm conj1_False}; |
41 |
val conj2_False = @{thm conj2_False}; |
|
42 |
val conj_True = @{thm conj_True}; |
|
43 |
val conj_cong = @{thm conj_cong}; |
|
32921 | 44 |
|
45363 | 45 |
fun isFalse (Const (@{const_name False}, _)) = true |
25171 | 46 |
| isFalse _ = false; |
45363 | 47 |
|
48 |
fun isTrue (Const (@{const_name True}, _)) = true |
|
25171 | 49 |
| isTrue _ = false; |
50 |
||
51 |
in |
|
32921 | 52 |
|
25171 | 53 |
val lazy_conj_simproc = |
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
54 |
Simplifier.simproc_global @{theory HOL} "lazy_conj_simp" ["P & Q"] |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
55 |
(fn ctxt => fn t => |
59643 | 56 |
(case t of (Const (@{const_name HOL.conj},_) $ P $ Q) => |
57 |
let |
|
58 |
val P_P' = Simplifier.rewrite ctxt (Thm.cterm_of ctxt P); |
|
59 |
val P' = P_P' |> Thm.prop_of |> Logic.dest_equals |> #2; |
|
60 |
in |
|
61 |
if isFalse P' then SOME (conj1_False OF [P_P']) |
|
62 |
else |
|
63 |
let |
|
64 |
val Q_Q' = Simplifier.rewrite ctxt (Thm.cterm_of ctxt Q); |
|
65 |
val Q' = Q_Q' |> Thm.prop_of |> Logic.dest_equals |> #2; |
|
66 |
in |
|
67 |
if isFalse Q' then SOME (conj2_False OF [Q_Q']) |
|
68 |
else if isTrue P' andalso isTrue Q' then SOME (conj_True OF [P_P', Q_Q']) |
|
69 |
else if P aconv P' andalso Q aconv Q' then NONE |
|
70 |
else SOME (conj_cong OF [P_P', Q_Q']) |
|
71 |
end |
|
72 |
end |
|
73 |
| _ => NONE)); |
|
25171 | 74 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
75 |
fun string_eq_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
76 |
simp_tac (put_simpset HOL_basic_ss ctxt |
58156 | 77 |
addsimps (@{thms list.inject} @ @{thms char.inject} @ @{thms list.distinct} @ @{thms simp_thms}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
78 |
addsimprocs [lazy_conj_simproc] |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
79 |
|> Simplifier.add_cong @{thm block_conj_cong}); |
32921 | 80 |
|
25171 | 81 |
end; |
82 |
||
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
83 |
val lookup_ss = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
84 |
simpset_of (put_simpset HOL_basic_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
85 |
addsimps (@{thms list.inject} @ @{thms char.inject} |
58156 | 86 |
@ @{thms list.distinct} @ @{thms simp_thms} |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
87 |
@ [@{thm StateFun.lookup_update_id_same}, @{thm StateFun.id_id_cancel}, |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
88 |
@{thm StateFun.lookup_update_same}, @{thm StateFun.lookup_update_other}]) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
89 |
addsimprocs [lazy_conj_simproc] |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
90 |
addSolver StateSpace.distinctNameSolver |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
91 |
|> fold Simplifier.add_cong @{thms block_conj_cong}); |
25171 | 92 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
93 |
val ex_lookup_ss = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
94 |
simpset_of (put_simpset HOL_ss @{context} addsimps @{thms StateFun.ex_id}); |
25171 | 95 |
|
33519 | 96 |
|
45363 | 97 |
structure Data = Generic_Data |
33519 | 98 |
( |
45363 | 99 |
type T = simpset * simpset * bool; (*lookup simpset, ex_lookup simpset, are simprocs installed*) |
25171 | 100 |
val empty = (empty_ss, empty_ss, false); |
101 |
val extend = I; |
|
33519 | 102 |
fun merge ((ss1, ex_ss1, b1), (ss2, ex_ss2, b2)) = |
45363 | 103 |
(merge_ss (ss1, ss2), merge_ss (ex_ss1, ex_ss2), b1 orelse b2); |
33519 | 104 |
); |
25171 | 105 |
|
58825 | 106 |
val _ = Theory.setup (Context.theory_map (Data.put (lookup_ss, ex_lookup_ss, false))); |
25171 | 107 |
|
108 |
val lookup_simproc = |
|
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
109 |
Simplifier.simproc_global @{theory} "lookup_simp" ["lookup d n (update d' c m v s)"] |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
110 |
(fn ctxt => fn t => |
45363 | 111 |
(case t of (Const (@{const_name StateFun.lookup}, lT) $ destr $ n $ |
112 |
(s as Const (@{const_name StateFun.update}, uT) $ _ $ _ $ _ $ _ $ _)) => |
|
25171 | 113 |
(let |
114 |
val (_::_::_::_::sT::_) = binder_types uT; |
|
115 |
val mi = maxidx_of_term t; |
|
45363 | 116 |
fun mk_upds (Const (@{const_name StateFun.update}, uT) $ d' $ c $ m $ v $ s) = |
117 |
let |
|
118 |
val (_ :: _ :: _ :: fT :: _ :: _) = binder_types uT; |
|
119 |
val vT = domain_type fT; |
|
120 |
val (s', cnt) = mk_upds s; |
|
121 |
val (v', cnt') = |
|
122 |
(case v of |
|
123 |
Const (@{const_name K_statefun}, KT) $ v'' => |
|
124 |
(case v'' of |
|
125 |
(Const (@{const_name StateFun.lookup}, _) $ |
|
126 |
(d as (Const (@{const_name Fun.id}, _))) $ n' $ _) => |
|
127 |
if d aconv c andalso n aconv m andalso m aconv n' |
|
128 |
then (v,cnt) (* Keep value so that |
|
129 |
lookup_update_id_same can fire *) |
|
130 |
else |
|
131 |
(Const (@{const_name StateFun.K_statefun}, KT) $ |
|
132 |
Var (("v", cnt), vT), cnt + 1) |
|
133 |
| _ => |
|
134 |
(Const (@{const_name StateFun.K_statefun}, KT) $ |
|
135 |
Var (("v", cnt), vT), cnt + 1)) |
|
136 |
| _ => (v, cnt)); |
|
137 |
in (Const (@{const_name StateFun.update}, uT) $ d' $ c $ m $ v' $ s', cnt') end |
|
138 |
| mk_upds s = (Var (("s", mi + 1), sT), mi + 2); |
|
139 |
||
140 |
val ct = |
|
59643 | 141 |
Thm.cterm_of ctxt |
142 |
(Const (@{const_name StateFun.lookup}, lT) $ destr $ n $ fst (mk_upds s)); |
|
45363 | 143 |
val basic_ss = #1 (Data.get (Context.Proof ctxt)); |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
144 |
val ctxt' = ctxt |> Config.put simp_depth_limit 100 |> put_simpset basic_ss; |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
145 |
val thm = Simplifier.rewrite ctxt' ct; |
45363 | 146 |
in |
59582 | 147 |
if (op aconv) (Logic.dest_equals (Thm.prop_of thm)) |
45363 | 148 |
then NONE |
149 |
else SOME thm |
|
25171 | 150 |
end |
45361 | 151 |
handle Option.Option => NONE) |
25171 | 152 |
| _ => NONE )); |
153 |
||
154 |
||
155 |
local |
|
45363 | 156 |
|
32921 | 157 |
val meta_ext = @{thm StateFun.meta_ext}; |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
158 |
val ss' = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
159 |
simpset_of (put_simpset HOL_ss @{context} addsimps |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
160 |
(@{thm StateFun.update_apply} :: @{thm Fun.o_apply} :: @{thms list.inject} @ @{thms char.inject} |
58156 | 161 |
@ @{thms list.distinct}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
162 |
addsimprocs [lazy_conj_simproc, StateSpace.distinct_simproc] |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
163 |
|> fold Simplifier.add_cong @{thms block_conj_cong}); |
45363 | 164 |
|
25171 | 165 |
in |
45363 | 166 |
|
25171 | 167 |
val update_simproc = |
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
168 |
Simplifier.simproc_global @{theory} "update_simp" ["update d c n v s"] |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
169 |
(fn ctxt => fn t => |
45363 | 170 |
(case t of |
55972 | 171 |
Const (@{const_name StateFun.update}, uT) $ _ $ _ $ _ $ _ $ _ => |
45363 | 172 |
let |
173 |
val (_ :: _ :: _ :: _ :: sT :: _) = binder_types uT; |
|
174 |
(*"('v => 'a1) => ('a2 => 'v) => 'n => ('a1 => 'a2) => ('n => 'v) => ('n => 'v)"*) |
|
175 |
fun init_seed s = (Bound 0, Bound 0, [("s", sT)], [], false); |
|
25171 | 176 |
|
45363 | 177 |
fun mk_comp f fT g gT = |
178 |
let val T = domain_type fT --> range_type gT |
|
179 |
in (Const (@{const_name Fun.comp}, gT --> fT --> T) $ g $ f, T) end; |
|
25171 | 180 |
|
45363 | 181 |
fun mk_comps fs = foldl1 (fn ((f, fT), (g, gT)) => mk_comp g gT f fT) fs; |
182 |
||
183 |
fun append n c cT f fT d dT comps = |
|
184 |
(case AList.lookup (op aconv) comps n of |
|
185 |
SOME gTs => AList.update (op aconv) (n, [(c, cT), (f, fT), (d, dT)] @ gTs) comps |
|
186 |
| NONE => AList.update (op aconv) (n, [(c, cT), (f, fT), (d, dT)]) comps); |
|
25171 | 187 |
|
45363 | 188 |
fun split_list (x :: xs) = let val (xs', y) = split_last xs in (x, xs', y) end |
189 |
| split_list _ = error "StateFun.split_list"; |
|
25171 | 190 |
|
45363 | 191 |
fun merge_upds n comps = |
192 |
let val ((c, cT), fs, (d, dT)) = split_list (the (AList.lookup (op aconv) comps n)) |
|
193 |
in ((c, cT), fst (mk_comps fs), (d, dT)) end; |
|
25171 | 194 |
|
45363 | 195 |
(* mk_updterm returns |
196 |
* - (orig-term-skeleton,simplified-term-skeleton, vars, b) |
|
197 |
* where boolean b tells if a simplification has occurred. |
|
198 |
"orig-term-skeleton = simplified-term-skeleton" is |
|
199 |
* the desired simplification rule. |
|
200 |
* The algorithm first walks down the updates to the seed-state while |
|
201 |
* memorising the updates in the already-table. While walking up the |
|
202 |
* updates again, the optimised term is constructed. |
|
203 |
*) |
|
204 |
fun mk_updterm already |
|
55972 | 205 |
((upd as Const (@{const_name StateFun.update}, uT)) $ d $ c $ n $ v $ s) = |
45363 | 206 |
let |
207 |
fun rest already = mk_updterm already; |
|
208 |
val (dT :: cT :: nT :: vT :: sT :: _) = binder_types uT; |
|
209 |
(*"('v => 'a1) => ('a2 => 'v) => 'n => ('a1 => 'a2) => |
|
210 |
('n => 'v) => ('n => 'v)"*) |
|
211 |
in |
|
212 |
if member (op aconv) already n then |
|
213 |
(case rest already s of |
|
214 |
(trm, trm', vars, comps, _) => |
|
215 |
let |
|
216 |
val i = length vars; |
|
217 |
val kv = (mk_name i n, vT); |
|
218 |
val kb = Bound i; |
|
219 |
val comps' = append n c cT kb vT d dT comps; |
|
220 |
in (upd $ d $ c $ n $ kb $ trm, trm', kv :: vars, comps',true) end) |
|
221 |
else |
|
222 |
(case rest (n :: already) s of |
|
223 |
(trm, trm', vars, comps, b) => |
|
224 |
let |
|
225 |
val i = length vars; |
|
226 |
val kv = (mk_name i n, vT); |
|
227 |
val kb = Bound i; |
|
228 |
val comps' = append n c cT kb vT d dT comps; |
|
229 |
val ((c', c'T), f', (d', d'T)) = merge_upds n comps'; |
|
230 |
val vT' = range_type d'T --> domain_type c'T; |
|
231 |
val upd' = |
|
232 |
Const (@{const_name StateFun.update}, |
|
233 |
d'T --> c'T --> nT --> vT' --> sT --> sT); |
|
234 |
in |
|
55972 | 235 |
(upd $ d $ c $ n $ kb $ trm, upd' $ d' $ c' $ n $ f' $ trm', kv :: vars, |
236 |
comps', b) |
|
45363 | 237 |
end) |
238 |
end |
|
239 |
| mk_updterm _ t = init_seed t; |
|
25171 | 240 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
241 |
val ctxt0 = Config.put simp_depth_limit 100 ctxt; |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
242 |
val ctxt1 = put_simpset ss' ctxt0; |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
243 |
val ctxt2 = put_simpset (#1 (Data.get (Context.Proof ctxt0))) ctxt0; |
45363 | 244 |
in |
245 |
(case mk_updterm [] t of |
|
246 |
(trm, trm', vars, _, true) => |
|
247 |
let |
|
248 |
val eq1 = |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
249 |
Goal.prove ctxt0 [] [] |
46218
ecf6375e2abb
renamed Term.list_all to Logic.list_all, in accordance to HOLogic.list_all;
wenzelm
parents:
45740
diff
changeset
|
250 |
(Logic.list_all (vars, Logic.mk_equals (trm, trm'))) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
251 |
(fn _ => rtac meta_ext 1 THEN simp_tac ctxt1 1); |
59582 | 252 |
val eq2 = Simplifier.asm_full_rewrite ctxt2 (Thm.dest_equals_rhs (Thm.cprop_of eq1)); |
45363 | 253 |
in SOME (Thm.transitive eq1 eq2) end |
254 |
| _ => NONE) |
|
255 |
end |
|
256 |
| _ => NONE)); |
|
25171 | 257 |
|
45363 | 258 |
end; |
25171 | 259 |
|
260 |
||
261 |
local |
|
45363 | 262 |
|
39159 | 263 |
val swap_ex_eq = @{thm StateFun.swap_ex_eq}; |
45363 | 264 |
|
25171 | 265 |
fun is_selector thy T sel = |
45363 | 266 |
let val (flds, more) = Record.get_recT_fields thy T |
267 |
in member (fn (s, (n, _)) => n = s) (more :: flds) sel end; |
|
268 |
||
25171 | 269 |
in |
45363 | 270 |
|
25171 | 271 |
val ex_lookup_eq_simproc = |
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
272 |
Simplifier.simproc_global @{theory HOL} "ex_lookup_eq_simproc" ["Ex t"] |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
273 |
(fn ctxt => fn t => |
45363 | 274 |
let |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
275 |
val thy = Proof_Context.theory_of ctxt; |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
276 |
|
45363 | 277 |
val ex_lookup_ss = #2 (Data.get (Context.Proof ctxt)); |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
278 |
val ctxt' = ctxt |> Config.put simp_depth_limit 100 |> put_simpset ex_lookup_ss; |
45363 | 279 |
fun prove prop = |
280 |
Goal.prove_global thy [] [] prop |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
46218
diff
changeset
|
281 |
(fn _ => Record.split_simp_tac ctxt [] (K ~1) 1 THEN simp_tac ctxt' 1); |
25171 | 282 |
|
45363 | 283 |
fun mkeq (swap, Teq, lT, lo, d, n, x, s) i = |
284 |
let |
|
285 |
val (_ :: nT :: _) = binder_types lT; |
|
286 |
(* ('v => 'a) => 'n => ('n => 'v) => 'a *) |
|
287 |
val x' = if not (Term.is_dependent x) then Bound 1 else raise TERM ("", [x]); |
|
288 |
val n' = if not (Term.is_dependent n) then Bound 2 else raise TERM ("", [n]); |
|
289 |
val sel' = lo $ d $ n' $ s; |
|
290 |
in (Const (@{const_name HOL.eq}, Teq) $ sel' $ x', hd (binder_types Teq), nT, swap) end; |
|
291 |
||
292 |
fun dest_state (s as Bound 0) = s |
|
293 |
| dest_state (s as (Const (sel, sT) $ Bound 0)) = |
|
294 |
if is_selector thy (domain_type sT) sel then s |
|
295 |
else raise TERM ("StateFun.ex_lookup_eq_simproc: not a record slector", [s]) |
|
296 |
| dest_state s = raise TERM ("StateFun.ex_lookup_eq_simproc: not a record slector", [s]); |
|
25171 | 297 |
|
45363 | 298 |
fun dest_sel_eq |
299 |
(Const (@{const_name HOL.eq}, Teq) $ |
|
300 |
((lo as (Const (@{const_name StateFun.lookup}, lT))) $ d $ n $ s) $ X) = |
|
301 |
(false, Teq, lT, lo, d, n, X, dest_state s) |
|
302 |
| dest_sel_eq |
|
303 |
(Const (@{const_name HOL.eq}, Teq) $ X $ |
|
304 |
((lo as (Const (@{const_name StateFun.lookup}, lT))) $ d $ n $ s)) = |
|
305 |
(true, Teq, lT, lo, d, n, X, dest_state s) |
|
306 |
| dest_sel_eq _ = raise TERM ("", []); |
|
307 |
in |
|
308 |
(case t of |
|
309 |
Const (@{const_name Ex}, Tex) $ Abs (s, T, t) => |
|
310 |
(let |
|
311 |
val (eq, eT, nT, swap) = mkeq (dest_sel_eq t) 0; |
|
312 |
val prop = |
|
46218
ecf6375e2abb
renamed Term.list_all to Logic.list_all, in accordance to HOLogic.list_all;
wenzelm
parents:
45740
diff
changeset
|
313 |
Logic.list_all ([("n", nT), ("x", eT)], |
45740 | 314 |
Logic.mk_equals (Const (@{const_name Ex}, Tex) $ Abs (s, T, eq), @{term True})); |
45363 | 315 |
val thm = Drule.export_without_context (prove prop); |
316 |
val thm' = if swap then swap_ex_eq OF [thm] else thm |
|
317 |
in SOME thm' end handle TERM _ => NONE) |
|
318 |
| _ => NONE) |
|
319 |
end handle Option.Option => NONE); |
|
25171 | 320 |
|
321 |
end; |
|
322 |
||
323 |
val val_sfx = "V"; |
|
324 |
val val_prfx = "StateFun." |
|
325 |
fun deco base_prfx s = val_prfx ^ (base_prfx ^ suffix val_sfx s); |
|
326 |
||
45363 | 327 |
fun mkUpper str = |
25171 | 328 |
(case String.explode str of |
329 |
[] => "" |
|
45363 | 330 |
| c::cs => String.implode (Char.toUpper c :: cs)); |
25171 | 331 |
|
32952 | 332 |
fun mkName (Type (T,args)) = implode (map mkName args) ^ mkUpper (Long_Name.base_name T) |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30289
diff
changeset
|
333 |
| mkName (TFree (x,_)) = mkUpper (Long_Name.base_name x) |
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30289
diff
changeset
|
334 |
| mkName (TVar ((x,_),_)) = mkUpper (Long_Name.base_name x); |
25171 | 335 |
|
58354
04ac60da613e
support (finite values of) codatatypes in Quickcheck
blanchet
parents:
58156
diff
changeset
|
336 |
fun is_datatype thy = is_some o BNF_LFP_Compat.get_info thy [BNF_LFP_Compat.Keep_Nesting]; |
25171 | 337 |
|
55465 | 338 |
fun mk_map @{type_name List.list} = Syntax.const @{const_name List.map} |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30289
diff
changeset
|
339 |
| mk_map n = Syntax.const ("StateFun.map_" ^ Long_Name.base_name n); |
25171 | 340 |
|
45363 | 341 |
fun gen_constr_destr comp prfx thy (Type (T, [])) = |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30289
diff
changeset
|
342 |
Syntax.const (deco prfx (mkUpper (Long_Name.base_name T))) |
25171 | 343 |
| gen_constr_destr comp prfx thy (T as Type ("fun",_)) = |
45363 | 344 |
let val (argTs, rangeT) = strip_type T; |
345 |
in |
|
346 |
comp |
|
32952 | 347 |
(Syntax.const (deco prfx (implode (map mkName argTs) ^ "Fun"))) |
45363 | 348 |
(fold (fn x => fn y => x $ y) |
349 |
(replicate (length argTs) (Syntax.const "StateFun.map_fun")) |
|
350 |
(gen_constr_destr comp prfx thy rangeT)) |
|
351 |
end |
|
352 |
| gen_constr_destr comp prfx thy (T' as Type (T, argTs)) = |
|
353 |
if is_datatype thy T |
|
354 |
then (* datatype args are recursively embedded into val *) |
|
355 |
(case argTs of |
|
356 |
[argT] => |
|
357 |
comp |
|
358 |
((Syntax.const (deco prfx (mkUpper (Long_Name.base_name T))))) |
|
359 |
((mk_map T $ gen_constr_destr comp prfx thy argT)) |
|
360 |
| _ => raise (TYPE ("StateFun.gen_constr_destr", [T'], []))) |
|
361 |
else (* type args are not recursively embedded into val *) |
|
362 |
Syntax.const (deco prfx (implode (map mkName argTs) ^ mkUpper (Long_Name.base_name T))) |
|
363 |
| gen_constr_destr thy _ _ T = raise (TYPE ("StateFun.gen_constr_destr", [T], [])); |
|
25171 | 364 |
|
45363 | 365 |
val mk_constr = gen_constr_destr (fn a => fn b => Syntax.const @{const_name Fun.comp} $ a $ b) ""; |
366 |
val mk_destr = gen_constr_destr (fn a => fn b => Syntax.const @{const_name Fun.comp} $ b $ a) "the_"; |
|
367 |
||
58825 | 368 |
val _ = |
369 |
Theory.setup |
|
370 |
(Attrib.setup @{binding statefun_simp} |
|
371 |
(Scan.succeed (Thm.declaration_attribute (fn thm => fn context => |
|
372 |
let |
|
373 |
val ctxt = Context.proof_of context; |
|
374 |
val (lookup_ss, ex_lookup_ss, simprocs_active) = Data.get context; |
|
375 |
val (lookup_ss', ex_lookup_ss') = |
|
59582 | 376 |
(case Thm.concl_of thm of |
58825 | 377 |
(_ $ ((Const (@{const_name Ex}, _) $ _))) => |
378 |
(lookup_ss, simpset_map ctxt (Simplifier.add_simp thm) ex_lookup_ss) |
|
379 |
| _ => |
|
380 |
(simpset_map ctxt (Simplifier.add_simp thm) lookup_ss, ex_lookup_ss)); |
|
381 |
val activate_simprocs = |
|
382 |
if simprocs_active then I |
|
383 |
else Simplifier.map_ss (fn ctxt => ctxt addsimprocs [lookup_simproc, update_simproc]); |
|
384 |
in |
|
385 |
context |
|
386 |
|> activate_simprocs |
|
387 |
|> Data.put (lookup_ss', ex_lookup_ss', true) |
|
388 |
end))) |
|
389 |
"simplification in statespaces"); |
|
45363 | 390 |
|
391 |
end; |