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