src/HOL/Decision_Procs/approximation.ML
author wenzelm
Mon, 22 Dec 2014 15:50:16 +0100
changeset 59174 15a73dd9df51
parent 58893 9e0ecb66d6a7
child 59498 50b60f501b05
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Decision_Procs/approximation.ML
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     2
    Author:     Johannes Hoelzl, TU Muenchen
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     3
*)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     4
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     5
signature APPROXIMATION =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     6
sig
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     7
  val approx: int -> Proof.context -> term -> term
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     8
end
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     9
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    10
structure Approximation: APPROXIMATION =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    11
struct
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    12
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    13
fun calculated_subterms (@{const Trueprop} $ t) = calculated_subterms t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    14
  | calculated_subterms (@{const HOL.implies} $ _ $ t) = calculated_subterms t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    15
  | calculated_subterms (@{term "op <= :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = [t1, t2]
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    16
  | calculated_subterms (@{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = [t1, t2]
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    17
  | calculated_subterms (@{term "op : :: real \<Rightarrow> real set \<Rightarrow> bool"} $ t1 $
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    18
                         (@{term "atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set"} $ t2 $ t3)) = [t1, t2, t3]
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    19
  | calculated_subterms t = raise TERM ("calculated_subterms", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    20
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    21
fun dest_interpret_form (@{const "interpret_form"} $ b $ xs) = (b, xs)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    22
  | dest_interpret_form t = raise TERM ("dest_interpret_form", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    23
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    24
fun dest_interpret (@{const "interpret_floatarith"} $ b $ xs) = (b, xs)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    25
  | dest_interpret t = raise TERM ("dest_interpret", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    26
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    27
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    28
fun dest_float (@{const "Float"} $ m $ e) =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    29
  (snd (HOLogic.dest_number m), snd (HOLogic.dest_number e))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    30
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    31
fun dest_ivl (Const (@{const_name "Some"}, _) $
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    32
              (Const (@{const_name Pair}, _) $ u $ l)) = SOME (dest_float u, dest_float l)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    33
  | dest_ivl (Const (@{const_name "None"}, _)) = NONE
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    34
  | dest_ivl t = raise TERM ("dest_result", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    35
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    36
fun mk_approx' prec t = (@{const "approx'"}
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    37
                       $ HOLogic.mk_number @{typ nat} prec
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    38
                       $ t $ @{term "[] :: (float * float) option list"})
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    39
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    40
fun mk_approx_form_eval prec t xs = (@{const "approx_form_eval"}
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    41
                       $ HOLogic.mk_number @{typ nat} prec
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    42
                       $ t $ xs)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    43
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    44
fun float2_float10 prec round_down (m, e) = (
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    45
  let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    46
    val (m, e) = (if e < 0 then (m,e) else (m * Integer.pow e 2, 0))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    47
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    48
    fun frac c p 0 digits cnt = (digits, cnt, 0)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    49
      | frac c 0 r digits cnt = (digits, cnt, r)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    50
      | frac c p r digits cnt = (let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    51
        val (d, r) = Integer.div_mod (r * 10) (Integer.pow (~e) 2)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    52
      in frac (c orelse d <> 0) (if d <> 0 orelse c then p - 1 else p) r
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    53
              (digits * 10 + d) (cnt + 1)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    54
      end)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    55
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    56
    val sgn = Int.sign m
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    57
    val m = abs m
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    58
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    59
    val round_down = (sgn = 1 andalso round_down) orelse
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    60
                     (sgn = ~1 andalso not round_down)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    61
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    62
    val (x, r) = Integer.div_mod m (Integer.pow (~e) 2)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    63
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    64
    val p = ((if x = 0 then prec else prec - (IntInf.log2 x + 1)) * 3) div 10 + 1
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    65
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    66
    val (digits, e10, r) = if p > 0 then frac (x <> 0) p r 0 0 else (0,0,0)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    67
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    68
    val digits = if round_down orelse r = 0 then digits else digits + 1
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    69
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    70
  in (sgn * (digits + x * (Integer.pow e10 10)), ~e10)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    71
  end)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    72
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    73
fun mk_result prec (SOME (l, u)) =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    74
  (let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    75
    fun mk_float10 rnd x = (let val (m, e) = float2_float10 prec rnd x
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    76
                       in if e = 0 then HOLogic.mk_number @{typ real} m
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    77
                     else if e = 1 then @{term "divide :: real \<Rightarrow> real \<Rightarrow> real"} $
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    78
                                        HOLogic.mk_number @{typ real} m $
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    79
                                        @{term "10"}
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    80
                                   else @{term "divide :: real \<Rightarrow> real \<Rightarrow> real"} $
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    81
                                        HOLogic.mk_number @{typ real} m $
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    82
                                        (@{term "power 10 :: nat \<Rightarrow> real"} $
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    83
                                         HOLogic.mk_number @{typ nat} (~e)) end)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    84
    in @{term "atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set"} $ mk_float10 true l $ mk_float10 false u end)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    85
  | mk_result _ NONE = @{term "UNIV :: real set"}
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    86
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    87
fun realify t =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    88
  let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    89
    val t = Logic.varify_global t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    90
    val m = map (fn (name, _) => (name, @{typ real})) (Term.add_tvars t [])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    91
    val t = Term.subst_TVars m t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    92
  in t end
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    93
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    94
fun apply_tactic ctxt term tactic =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    95
  cterm_of (Proof_Context.theory_of ctxt) term
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    96
  |> Goal.init
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    97
  |> SINGLE tactic
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    98
  |> the |> prems_of |> hd
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    99
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   100
fun prepare_form ctxt term = apply_tactic ctxt term (
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   101
    REPEAT (FIRST' [etac @{thm intervalE}, etac @{thm meta_eqE}, rtac @{thm impI}] 1)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   102
    THEN Subgoal.FOCUS (fn {prems, ...} => reorder_bounds_tac prems 1) ctxt 1
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   103
    THEN DETERM (TRY (filter_prems_tac (K false) 1)))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   104
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   105
fun reify_form ctxt term = apply_tactic ctxt term
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   106
   (Reification.tac ctxt form_equations NONE 1)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   107
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   108
fun approx_form prec ctxt t =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   109
        realify t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   110
     |> prepare_form ctxt
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   111
     |> (fn arith_term => reify_form ctxt arith_term
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   112
         |> HOLogic.dest_Trueprop |> dest_interpret_form
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   113
         |> (fn (data, xs) =>
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   114
            mk_approx_form_eval prec data (HOLogic.mk_list @{typ "(float * float) option"}
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   115
              (map (fn _ => @{term "None :: (float * float) option"}) (HOLogic.dest_list xs)))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   116
         |> approximate ctxt
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   117
         |> HOLogic.dest_list
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   118
         |> curry ListPair.zip (HOLogic.dest_list xs @ calculated_subterms arith_term)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   119
         |> map (fn (elem, s) => @{term "op : :: real \<Rightarrow> real set \<Rightarrow> bool"} $ elem $ mk_result prec (dest_ivl s))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   120
         |> foldr1 HOLogic.mk_conj))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   121
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   122
fun approx_arith prec ctxt t = realify t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   123
     |> Thm.cterm_of (Proof_Context.theory_of ctxt)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   124
     |> Reification.conv ctxt form_equations
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   125
     |> prop_of
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   126
     |> Logic.dest_equals |> snd
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   127
     |> dest_interpret |> fst
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   128
     |> mk_approx' prec
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   129
     |> approximate ctxt
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   130
     |> dest_ivl
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   131
     |> mk_result prec
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   132
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   133
fun approx prec ctxt t =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   134
  if type_of t = @{typ prop} then approx_form prec ctxt t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   135
  else if type_of t = @{typ bool} then approx_form prec ctxt (@{const Trueprop} $ t)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   136
  else approx_arith prec ctxt t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   137
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   138
fun approximate_cmd modes raw_t state =
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   139
  let
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   140
    val ctxt = Toplevel.context_of state;
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   141
    val t = Syntax.read_term ctxt raw_t;
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   142
    val t' = approx 30 ctxt t;
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   143
    val ty' = Term.type_of t';
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   144
    val ctxt' = Variable.auto_fixes t' ctxt;
59174
wenzelm
parents: 58893
diff changeset
   145
  in
wenzelm
parents: 58893
diff changeset
   146
    Print_Mode.with_modes modes (fn () =>
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   147
      Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
59174
wenzelm
parents: 58893
diff changeset
   148
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) ()
wenzelm
parents: 58893
diff changeset
   149
  end |> Pretty.writeln;
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   150
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   151
val opt_modes =
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   152
  Scan.optional (@{keyword "("} |-- Parse.!!! (Scan.repeat1 Parse.xname --| @{keyword ")"})) [];
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   153
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   154
val _ =
58893
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 56923
diff changeset
   155
  Outer_Syntax.command @{command_spec "approximate"} "print approximation of term"
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   156
    (opt_modes -- Parse.term
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   157
      >> (fn (modes, t) => Toplevel.keep (approximate_cmd modes t)));
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   158
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   159
end;