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