author | hoelzl |
Mon, 24 Nov 2014 12:20:35 +0100 | |
changeset 59047 | 8d7cec9b861d |
parent 58965 | a62cdcc5344b |
child 59048 | 7dc8ac6f0895 |
permissions | -rw-r--r-- |
50387 | 1 |
(* Title: HOL/Probability/measurable.ML |
2 |
Author: Johannes Hölzl <hoelzl@in.tum.de> |
|
3 |
||
4 |
Measurability prover. |
|
5 |
*) |
|
6 |
||
7 |
signature MEASURABLE = |
|
8 |
sig |
|
9 |
datatype level = Concrete | Generic |
|
10 |
||
59047 | 11 |
val app_thm_attr : attribute context_parser |
12 |
val dest_thm_attr : attribute context_parser |
|
13 |
val measurable_thm_attr : bool * (bool * level) -> attribute |
|
53043
8cbfbeb566a4
more standard attribute_setup / method_setup -- export key ML operations instead of parsers;
wenzelm
parents:
51717
diff
changeset
|
14 |
|
50387 | 15 |
val measurable_tac : Proof.context -> thm list -> tactic |
16 |
||
53043
8cbfbeb566a4
more standard attribute_setup / method_setup -- export key ML operations instead of parsers;
wenzelm
parents:
51717
diff
changeset
|
17 |
val simproc : Proof.context -> cterm -> thm option |
50387 | 18 |
|
59047 | 19 |
val get_thms : Proof.context -> thm list |
50387 | 20 |
val get_all : Proof.context -> thm list |
21 |
end ; |
|
22 |
||
23 |
structure Measurable : MEASURABLE = |
|
24 |
struct |
|
25 |
||
26 |
datatype level = Concrete | Generic; |
|
27 |
||
59047 | 28 |
fun eq_measurable_thms ((th1, d1), (th2, d2)) = |
29 |
d1 = d2 andalso Thm.eq_thm_prop (th1, th2) ; |
|
30 |
||
50387 | 31 |
structure Data = Generic_Data |
32 |
( |
|
33 |
type T = { |
|
59047 | 34 |
measurable_thms : (thm * (bool * level)) Item_Net.T, |
50387 | 35 |
dest_thms : thm Item_Net.T, |
36 |
app_thms : thm Item_Net.T } |
|
37 |
val empty = { |
|
59047 | 38 |
measurable_thms = Item_Net.init eq_measurable_thms (single o Thm.prop_of o fst), |
50387 | 39 |
dest_thms = Thm.full_rules, |
59047 | 40 |
app_thms = Thm.full_rules }; |
50387 | 41 |
val extend = I; |
59047 | 42 |
fun merge ({measurable_thms = t1, dest_thms = dt1, app_thms = at1 }, |
43 |
{measurable_thms = t2, dest_thms = dt2, app_thms = at2 }) = { |
|
44 |
measurable_thms = Item_Net.merge (t1, t2), |
|
50387 | 45 |
dest_thms = Item_Net.merge (dt1, dt2), |
46 |
app_thms = Item_Net.merge (at1, at2) }; |
|
47 |
); |
|
48 |
||
49 |
val debug = |
|
50 |
Attrib.setup_config_bool @{binding measurable_debug} (K false) |
|
51 |
||
52 |
val split = |
|
53 |
Attrib.setup_config_bool @{binding measurable_split} (K true) |
|
54 |
||
59047 | 55 |
fun map_data f1 f2 f3 |
56 |
{measurable_thms = t1, dest_thms = t2, app_thms = t3} = |
|
57 |
{measurable_thms = f1 t1, dest_thms = f2 t2, app_thms = f3 t3 } |
|
50387 | 58 |
|
59047 | 59 |
fun map_measurable_thms f = map_data f I I |
60 |
fun map_dest_thms f = map_data I f I |
|
61 |
fun map_app_thms f = map_data I I f |
|
50387 | 62 |
|
59047 | 63 |
fun generic_add_del map = |
64 |
Scan.lift |
|
65 |
(Args.add >> K Item_Net.update || Args.del >> K Item_Net.remove || Scan.succeed Item_Net.update) >> |
|
66 |
(fn f => Thm.declaration_attribute (Data.map o map o f)) |
|
67 |
||
68 |
val app_thm_attr = generic_add_del map_app_thms |
|
50387 | 69 |
|
59047 | 70 |
val dest_thm_attr = generic_add_del map_dest_thms |
50387 | 71 |
|
59047 | 72 |
fun del_thm th net = |
73 |
let |
|
74 |
val thms = net |> Item_Net.content |> filter (fn (th', _) => Thm.eq_thm (th, th')) |
|
75 |
in fold Item_Net.remove thms net end ; |
|
76 |
||
77 |
fun measurable_thm_attr (do_add, d) = Thm.declaration_attribute |
|
78 |
(Data.map o map_measurable_thms o (if do_add then Item_Net.update o rpair d else del_thm)) |
|
50387 | 79 |
|
80 |
val get_dest = Item_Net.content o #dest_thms o Data.get; |
|
81 |
val get_app = Item_Net.content o #app_thms o Data.get; |
|
82 |
||
83 |
fun is_too_generic thm = |
|
84 |
let |
|
85 |
val concl = concl_of thm |
|
86 |
val concl' = HOLogic.dest_Trueprop concl handle TERM _ => concl |
|
87 |
in is_Var (head_of concl') end |
|
88 |
||
89 |
fun import_theorem ctxt thm = if is_too_generic thm then [] else |
|
90 |
[thm] @ map_filter (try (fn th' => thm RS th')) (get_dest ctxt); |
|
91 |
||
59047 | 92 |
val get = Context.Proof #> Data.get #> #measurable_thms #> Item_Net.content ; |
93 |
||
94 |
val get_all = get #> map fst ; |
|
58965 | 95 |
|
59047 | 96 |
fun get_thms ctxt = |
97 |
let |
|
98 |
val thms = ctxt |> get |> rev ; |
|
99 |
fun get lv = map_filter (fn (th, (rw, lv')) => if lv = lv' then SOME (th, rw) else NONE) thms |
|
100 |
in |
|
101 |
get Concrete @ get Generic |> |
|
102 |
maps (fn (th, rw) => if rw then [th] else import_theorem (Context.Proof ctxt) th) |
|
103 |
end; |
|
50387 | 104 |
|
56491 | 105 |
fun debug_tac ctxt msg f = if Config.get ctxt debug then print_tac ctxt (msg ()) THEN f else f |
50387 | 106 |
|
107 |
fun nth_hol_goal thm i = |
|
108 |
HOLogic.dest_Trueprop (Logic.strip_imp_concl (strip_all_body (nth (prems_of thm) (i - 1)))) |
|
109 |
||
110 |
fun dest_measurable_fun t = |
|
111 |
(case t of |
|
112 |
(Const (@{const_name "Set.member"}, _) $ f $ (Const (@{const_name "measurable"}, _) $ _ $ _)) => f |
|
113 |
| _ => raise (TERM ("not a measurability predicate", [t]))) |
|
114 |
||
115 |
fun is_cond_formula n thm = if length (prems_of thm) < n then false else |
|
116 |
(case nth_hol_goal thm n of |
|
117 |
(Const (@{const_name "Set.member"}, _) $ _ $ (Const (@{const_name "sets"}, _) $ _)) => false |
|
118 |
| (Const (@{const_name "Set.member"}, _) $ _ $ (Const (@{const_name "measurable"}, _) $ _ $ _)) => false |
|
119 |
| _ => true) |
|
120 |
handle TERM _ => true; |
|
121 |
||
122 |
fun indep (Bound i) t b = i < b orelse t <= i |
|
123 |
| indep (f $ t) top bot = indep f top bot andalso indep t top bot |
|
124 |
| indep (Abs (_,_,t)) top bot = indep t (top + 1) (bot + 1) |
|
125 |
| indep _ _ _ = true; |
|
126 |
||
127 |
fun cnt_prefixes ctxt (Abs (n, T, t)) = let |
|
128 |
fun is_countable t = Type.of_sort (Proof_Context.tsig_of ctxt) (t, @{sort countable}) |
|
129 |
fun cnt_walk (Abs (ns, T, t)) Ts = |
|
130 |
map (fn (t', t'') => (Abs (ns, T, t'), t'')) (cnt_walk t (T::Ts)) |
|
131 |
| cnt_walk (f $ g) Ts = let |
|
132 |
val n = length Ts - 1 |
|
133 |
in |
|
134 |
map (fn (f', t) => (f' $ g, t)) (cnt_walk f Ts) @ |
|
135 |
map (fn (g', t) => (f $ g', t)) (cnt_walk g Ts) @ |
|
136 |
(if is_countable (type_of1 (Ts, g)) andalso loose_bvar1 (g, n) |
|
137 |
andalso indep g n 0 andalso g <> Bound n |
|
138 |
then [(f $ Bound (n + 1), incr_boundvars (~ n) g)] |
|
139 |
else []) |
|
140 |
end |
|
141 |
| cnt_walk _ _ = [] |
|
142 |
in map (fn (t1, t2) => let |
|
143 |
val T1 = type_of1 ([T], t2) |
|
144 |
val T2 = type_of1 ([T], t) |
|
145 |
in ([SOME (Abs (n, T1, Abs (n, T, t1))), NONE, NONE, SOME (Abs (n, T, t2))], |
|
146 |
[SOME T1, SOME T, SOME T2]) |
|
147 |
end) (cnt_walk t [T]) |
|
148 |
end |
|
149 |
| cnt_prefixes _ _ = [] |
|
150 |
||
151 |
val split_countable_tac = |
|
152 |
Subgoal.FOCUS (fn {context = ctxt, ...} => SUBGOAL (fn (t, i) => |
|
153 |
let |
|
154 |
val f = dest_measurable_fun (HOLogic.dest_Trueprop t) |
|
155 |
fun cert f = map (Option.map (f (Proof_Context.theory_of ctxt))) |
|
156 |
fun inst t (ts, Ts) = Drule.instantiate' (cert ctyp_of Ts) (cert cterm_of ts) t |
|
157 |
val cps = cnt_prefixes ctxt f |> map (inst @{thm measurable_compose_countable}) |
|
158 |
in if null cps then no_tac else debug_tac ctxt (K "split countable fun") (resolve_tac cps i) end |
|
159 |
handle TERM _ => no_tac) 1) |
|
160 |
||
59047 | 161 |
val split_app_tac = |
162 |
Subgoal.FOCUS (fn {context = ctxt, ...} => SUBGOAL (fn (t, i) => |
|
163 |
let |
|
164 |
fun app_prefixes (Abs (n, T, (f $ g))) = let |
|
165 |
val ps = (if not (loose_bvar1 (g, 0)) then [(f, g)] else []) |
|
166 |
in map (fn (f, c) => (Abs (n, T, f), c, T, type_of c, type_of1 ([T], f $ c))) ps end |
|
167 |
| app_prefixes _ = [] |
|
168 |
||
169 |
fun dest_app (Abs (_, T, t as ((f $ Bound 0) $ c))) = (f, c, T, type_of c, type_of1 ([T], t)) |
|
170 |
| dest_app t = raise (TERM ("not a measurability predicate of an application", [t])) |
|
171 |
val thy = Proof_Context.theory_of ctxt |
|
172 |
val tunify = Sign.typ_unify thy |
|
173 |
val thms = map |
|
174 |
(fn thm => (thm, dest_app (dest_measurable_fun (HOLogic.dest_Trueprop (concl_of thm))))) |
|
175 |
(get_app (Context.Proof ctxt)) |
|
176 |
fun cert f = map (fn (t, t') => (f thy t, f thy t')) |
|
177 |
fun inst (f, c, T, Tc, Tf) (thm, (thmf, thmc, thmT, thmTc, thmTf)) = |
|
178 |
let |
|
179 |
val inst = |
|
180 |
(Vartab.empty, ~1) |
|
181 |
|> tunify (T, thmT) |
|
182 |
|> tunify (Tf, thmTf) |
|
183 |
|> tunify (Tc, thmTc) |
|
184 |
|> Vartab.dest o fst |
|
185 |
val subst = subst_TVars (map (apsnd snd) inst) |
|
186 |
in |
|
187 |
Thm.instantiate (cert ctyp_of (map (fn (n, (s, T)) => (TVar (n, s), T)) inst), |
|
188 |
cert cterm_of [(subst thmf, f), (subst thmc, c)]) thm |
|
189 |
end |
|
190 |
val cps = map_product inst (app_prefixes (dest_measurable_fun (HOLogic.dest_Trueprop t))) thms |
|
191 |
in if null cps then no_tac |
|
192 |
else debug_tac ctxt (K ("split app fun")) (resolve_tac cps i) |
|
193 |
ORELSE debug_tac ctxt (fn () => "FAILED") no_tac end |
|
194 |
handle TERM t => debug_tac ctxt (fn () => "TERM " ^ fst t ^ Pretty.str_of (Pretty.list "[" "]" (map (Syntax.pretty_term ctxt) (snd t)))) no_tac |
|
195 |
handle Type.TUNIFY => debug_tac ctxt (fn () => "TUNIFY") no_tac) 1) |
|
196 |
||
197 |
fun measurable_tac ctxt facts = |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50387
diff
changeset
|
198 |
let |
50387 | 199 |
val imported_thms = |
59047 | 200 |
(maps (import_theorem (Context.Proof ctxt) o Simplifier.norm_hhf ctxt) facts) @ get_thms ctxt |
50387 | 201 |
|
202 |
fun debug_facts msg () = |
|
203 |
msg ^ " + " ^ Pretty.str_of (Pretty.list "[" "]" |
|
204 |
(map (Syntax.pretty_term ctxt o prop_of) (maps (import_theorem (Context.Proof ctxt)) facts))); |
|
205 |
||
206 |
val splitter = if Config.get ctxt split then split_countable_tac ctxt else K no_tac |
|
207 |
||
208 |
fun REPEAT_cnt f n st = ((f n THEN REPEAT_cnt f (n + 1)) ORELSE all_tac) st |
|
209 |
||
210 |
val depth_measurable_tac = REPEAT_cnt (fn n => |
|
211 |
(COND (is_cond_formula 1) |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50387
diff
changeset
|
212 |
(debug_tac ctxt (K ("simp " ^ string_of_int n)) (SOLVED' (asm_full_simp_tac ctxt) 1)) |
50387 | 213 |
((debug_tac ctxt (K ("single " ^ string_of_int n)) (resolve_tac imported_thms 1)) APPEND |
214 |
(split_app_tac ctxt 1) APPEND |
|
215 |
(splitter 1)))) 0 |
|
216 |
||
217 |
in debug_tac ctxt (debug_facts "start") depth_measurable_tac end; |
|
218 |
||
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50387
diff
changeset
|
219 |
fun simproc ctxt redex = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50387
diff
changeset
|
220 |
let |
50387 | 221 |
val t = HOLogic.mk_Trueprop (term_of redex); |
222 |
fun tac {context = ctxt, prems = _ } = |
|
59047 | 223 |
SOLVE (measurable_tac ctxt (Simplifier.prems_of ctxt)); |
50387 | 224 |
in try (fn () => Goal.prove ctxt [] [] t tac RS @{thm Eq_TrueI}) () end; |
225 |
||
226 |
end |
|
227 |