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