src/HOL/Decision_Procs/approximation.ML
author wenzelm
Fri, 05 Apr 2019 17:05:32 +0200
changeset 70067 9b34dbeb1103
parent 69597 ff784d5a5bfb
child 70308 7f568724d67e
permissions -rw-r--r--
auxiliary operation for common uses of 'compile_generated_files';
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
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
     7
  val reify_form: Proof.context -> term -> term
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
     8
  val approx: int -> Proof.context -> term -> term
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
     9
  val approximate: Proof.context -> term -> term
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    10
  val approximation_tac : int -> (string * int) list -> int option -> Proof.context -> int -> tactic
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    11
end
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    12
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    13
structure Approximation: APPROXIMATION =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    14
struct
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
    15
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    16
fun reorder_bounds_tac ctxt prems i =
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    17
  let
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    18
    fun variable_of_bound (Const (\<^const_name>\<open>Trueprop\<close>, _) $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    19
                           (Const (\<^const_name>\<open>Set.member\<close>, _) $
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    20
                            Free (name, _) $ _)) = name
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    21
      | variable_of_bound (Const (\<^const_name>\<open>Trueprop\<close>, _) $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    22
                           (Const (\<^const_name>\<open>HOL.eq\<close>, _) $
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    23
                            Free (name, _) $ _)) = name
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    24
      | variable_of_bound t = raise TERM ("variable_of_bound", [t])
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    25
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    26
    val variable_bounds
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    27
      = map (`(variable_of_bound o Thm.prop_of)) prems
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    28
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    29
    fun add_deps (name, bnds)
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    30
      = Graph.add_deps_acyclic (name,
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    31
          remove (op =) name (Term.add_free_names (Thm.prop_of bnds) []))
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    32
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    33
    val order = Graph.empty
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    34
                |> fold Graph.new_node variable_bounds
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    35
                |> fold add_deps variable_bounds
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    36
                |> Graph.strong_conn |> map the_single |> rev
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    37
                |> map_filter (AList.lookup (op =) variable_bounds)
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    38
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    39
    fun prepend_prem th tac =
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    40
      tac THEN resolve_tac ctxt [th RSN (2, @{thm mp})] i
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    41
  in
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    42
    fold prepend_prem order all_tac
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    43
  end
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    44
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    45
fun approximation_conv ctxt ct =
64547
a955511171a8 avoid spurious messages;
wenzelm
parents: 63930
diff changeset
    46
  approximation_oracle (Proof_Context.theory_of ctxt, Thm.term_of ct);
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    47
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    48
fun approximate ctxt t =
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    49
  approximation_oracle (Proof_Context.theory_of ctxt, t)
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    50
  |> Thm.prop_of |> Logic.dest_equals |> snd;
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    51
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    52
(* Should be in HOL.thy ? *)
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    53
fun gen_eval_tac conv ctxt =
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    54
  CONVERSION
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    55
    (Object_Logic.judgment_conv ctxt (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt))
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    56
  THEN' resolve_tac ctxt [TrueI]
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    57
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    58
fun rewrite_interpret_form_tac ctxt prec splitting taylor i st = let
63930
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
    59
    fun lookup_splitting (Free (name, _)) =
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
    60
        (case AList.lookup (op =) splitting name
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    61
          of SOME s => HOLogic.mk_number \<^typ>\<open>nat\<close> s
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    62
           | NONE => \<^term>\<open>0 :: nat\<close>)
63930
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
    63
      | lookup_splitting t = raise TERM ("lookup_splitting", [t])
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    64
    val vs = nth (Thm.prems_of st) (i - 1)
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    65
             |> Logic.strip_imp_concl
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    66
             |> HOLogic.dest_Trueprop
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    67
             |> Term.strip_comb |> snd |> List.last
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    68
             |> HOLogic.dest_list
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    69
    val p = prec
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    70
            |> HOLogic.mk_number \<^typ>\<open>nat\<close>
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    71
            |> Thm.cterm_of ctxt
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    72
  in case taylor
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    73
  of NONE => let
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    74
       val n = vs |> length
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    75
               |> HOLogic.mk_number \<^typ>\<open>nat\<close>
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    76
               |> Thm.cterm_of ctxt
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    77
       val s = vs
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    78
               |> map lookup_splitting
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    79
               |> HOLogic.mk_list \<^typ>\<open>nat\<close>
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    80
               |> Thm.cterm_of ctxt
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    81
     in
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    82
       (resolve_tac ctxt [Thm.instantiate ([], [((("n", 0), \<^typ>\<open>nat\<close>), n),
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    83
                                   ((("prec", 0), \<^typ>\<open>nat\<close>), p),
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    84
                                   ((("ss", 0), \<^typ>\<open>nat list\<close>), s)])
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    85
            @{thm approx_form}] i
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    86
        THEN simp_tac (put_simpset (simpset_of \<^context>) ctxt) i) st
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    87
     end
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    88
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    89
   | SOME t =>
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    90
     if length vs <> 1
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    91
     then raise (TERM ("More than one variable used for taylor series expansion", [Thm.prop_of st]))
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    92
     else let
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    93
       val t = t
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    94
            |> HOLogic.mk_number \<^typ>\<open>nat\<close>
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    95
            |> Thm.cterm_of ctxt
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    96
       val s = vs |> map lookup_splitting |> hd
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    97
            |> Thm.cterm_of ctxt
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
    98
     in
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
    99
       resolve_tac ctxt [Thm.instantiate ([], [((("s", 0), \<^typ>\<open>nat\<close>), s),
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   100
                                   ((("t", 0), \<^typ>\<open>nat\<close>), t),
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   101
                                   ((("prec", 0), \<^typ>\<open>nat\<close>), p)])
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   102
            @{thm approx_tse_form}] i st
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
   103
     end
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
   104
  end
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
   105
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   106
fun calculated_subterms (\<^const>\<open>Trueprop\<close> $ t) = calculated_subterms t
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   107
  | calculated_subterms (\<^const>\<open>HOL.implies\<close> $ _ $ t) = calculated_subterms t
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   108
  | calculated_subterms (\<^term>\<open>(\<le>) :: real \<Rightarrow> real \<Rightarrow> bool\<close> $ t1 $ t2) = [t1, t2]
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   109
  | calculated_subterms (\<^term>\<open>(<) :: real \<Rightarrow> real \<Rightarrow> bool\<close> $ t1 $ t2) = [t1, t2]
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   110
  | calculated_subterms (\<^term>\<open>(\<in>) :: real \<Rightarrow> real set \<Rightarrow> bool\<close> $ t1 $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   111
                         (\<^term>\<open>atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set\<close> $ t2 $ t3)) = [t1, t2, t3]
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   112
  | calculated_subterms t = raise TERM ("calculated_subterms", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   113
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   114
fun dest_interpret_form (\<^const>\<open>interpret_form\<close> $ b $ xs) = (b, xs)
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   115
  | dest_interpret_form t = raise TERM ("dest_interpret_form", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   116
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   117
fun dest_interpret (\<^const>\<open>interpret_floatarith\<close> $ b $ xs) = (b, xs)
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   118
  | dest_interpret t = raise TERM ("dest_interpret", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   119
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   120
fun dest_interpret_env (\<^const>\<open>interpret_form\<close> $ _ $ xs) = xs
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   121
  | dest_interpret_env (\<^const>\<open>interpret_floatarith\<close> $ _ $ xs) = xs
63930
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   122
  | dest_interpret_env t = raise TERM ("dest_interpret_env", [t])
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   123
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   124
fun dest_float (\<^const>\<open>Float\<close> $ m $ e) = (snd (HOLogic.dest_number m), snd (HOLogic.dest_number e))
63930
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   125
  | dest_float t = raise TERM ("dest_float", [t])
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   126
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   127
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   128
fun dest_ivl (Const (\<^const_name>\<open>Some\<close>, _) $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   129
              (Const (\<^const_name>\<open>Pair\<close>, _) $ u $ l)) = SOME (dest_float u, dest_float l)
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   130
  | dest_ivl (Const (\<^const_name>\<open>None\<close>, _)) = NONE
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   131
  | dest_ivl t = raise TERM ("dest_result", [t])
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   132
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   133
fun mk_approx' prec t = (\<^const>\<open>approx'\<close>
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   134
                       $ HOLogic.mk_number \<^typ>\<open>nat\<close> prec
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   135
                       $ t $ \<^term>\<open>[] :: (float * float) option list\<close>)
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   136
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   137
fun mk_approx_form_eval prec t xs = (\<^const>\<open>approx_form_eval\<close>
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   138
                       $ HOLogic.mk_number \<^typ>\<open>nat\<close> prec
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   139
                       $ t $ xs)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   140
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   141
fun float2_float10 prec round_down (m, e) = (
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   142
  let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   143
    val (m, e) = (if e < 0 then (m,e) else (m * Integer.pow e 2, 0))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   144
63930
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   145
    fun frac _ _ 0 digits cnt = (digits, cnt, 0)
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   146
      | frac _ 0 r digits cnt = (digits, cnt, r)
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   147
      | frac c p r digits cnt = (let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   148
        val (d, r) = Integer.div_mod (r * 10) (Integer.pow (~e) 2)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   149
      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
   150
              (digits * 10 + d) (cnt + 1)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   151
      end)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   152
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   153
    val sgn = Int.sign m
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   154
    val m = abs m
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   155
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   156
    val round_down = (sgn = 1 andalso round_down) orelse
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   157
                     (sgn = ~1 andalso not round_down)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   158
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   159
    val (x, r) = Integer.div_mod m (Integer.pow (~e) 2)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   160
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   161
    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
   162
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   163
    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
   164
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   165
    val digits = if round_down orelse r = 0 then digits else digits + 1
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   166
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   167
  in (sgn * (digits + x * (Integer.pow e10 10)), ~e10)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   168
  end)
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   169
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   170
fun mk_result prec (SOME (l, u)) =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   171
  (let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   172
    fun mk_float10 rnd x = (let val (m, e) = float2_float10 prec rnd x
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   173
                       in if e = 0 then HOLogic.mk_number \<^typ>\<open>real\<close> m
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   174
                     else if e = 1 then \<^term>\<open>divide :: real \<Rightarrow> real \<Rightarrow> real\<close> $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   175
                                        HOLogic.mk_number \<^typ>\<open>real\<close> m $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   176
                                        \<^term>\<open>10\<close>
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   177
                                   else \<^term>\<open>divide :: real \<Rightarrow> real \<Rightarrow> real\<close> $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   178
                                        HOLogic.mk_number \<^typ>\<open>real\<close> m $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   179
                                        (\<^term>\<open>power 10 :: nat \<Rightarrow> real\<close> $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   180
                                         HOLogic.mk_number \<^typ>\<open>nat\<close> (~e)) end)
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   181
    in \<^term>\<open>atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set\<close> $ mk_float10 true l $ mk_float10 false u end)
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   182
  | mk_result _ NONE = \<^term>\<open>UNIV :: real set\<close>
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   183
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   184
fun realify t =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   185
  let
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   186
    val t = Logic.varify_global t
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   187
    val m = map (fn (name, _) => (name, \<^typ>\<open>real\<close>)) (Term.add_tvars t [])
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   188
    val t = Term.subst_TVars m t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   189
  in t end
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   190
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   191
fun apply_tactic ctxt term tactic =
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59582
diff changeset
   192
  Thm.cterm_of ctxt term
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   193
  |> Goal.init
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   194
  |> SINGLE tactic
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   195
  |> the |> Thm.prems_of |> hd
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   196
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   197
fun preproc_form_conv ctxt =
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   198
  Simplifier.rewrite
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   199
   (put_simpset HOL_basic_ss ctxt addsimps
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   200
     (Named_Theorems.get ctxt \<^named_theorems>\<open>approximation_preproc\<close>))
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   201
63930
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   202
fun reify_form_conv ctxt ct =
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   203
  let
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   204
    val thm =
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   205
       Reification.conv ctxt @{thms interpret_form.simps interpret_floatarith.simps} ct
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   206
       handle ERROR msg =>
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   207
        cat_error ("Reification failed: " ^ msg)
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   208
          ("Approximation does not support " ^
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   209
            quote (Syntax.string_of_term ctxt (Thm.term_of ct)))
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   210
    fun check_env (Free _) = ()
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   211
      | check_env (Var _) = ()
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   212
      | check_env t =
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   213
          cat_error "Term not supported by approximation:" (Syntax.string_of_term ctxt t)
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   214
    val _ = Thm.rhs_of thm |> Thm.term_of |> dest_interpret_env |> HOLogic.dest_list |> map check_env
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   215
  in thm end
867ca0d92ea2 provide more information on error
immler
parents: 63929
diff changeset
   216
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   217
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   218
fun reify_form_tac ctxt i = CONVERSION (Conv.arg_conv (reify_form_conv ctxt)) i
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   219
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   220
fun prepare_form_tac ctxt i =
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   221
  REPEAT (FIRST' [eresolve_tac ctxt @{thms intervalE},
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   222
    eresolve_tac ctxt @{thms meta_eqE},
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   223
    resolve_tac ctxt @{thms impI}] i)
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   224
  THEN Subgoal.FOCUS (fn {prems, context = ctxt', ...} => reorder_bounds_tac ctxt' prems i) ctxt i
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   225
  THEN DETERM (TRY (filter_prems_tac ctxt (K false) i))
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   226
  THEN CONVERSION (Conv.arg_conv (preproc_form_conv ctxt)) i
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   227
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   228
fun prepare_form ctxt term = apply_tactic ctxt term (prepare_form_tac ctxt 1)
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   229
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   230
fun apply_reify_form ctxt t = apply_tactic ctxt t (reify_form_tac ctxt 1)
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   231
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   232
fun reify_form ctxt t = HOLogic.mk_Trueprop t
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   233
  |> prepare_form ctxt
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   234
  |> apply_reify_form ctxt
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   235
  |> HOLogic.dest_Trueprop
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   236
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   237
fun approx_form prec ctxt t =
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   238
        realify t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   239
     |> prepare_form ctxt
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   240
     |> (fn arith_term => apply_reify_form ctxt arith_term
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   241
         |> HOLogic.dest_Trueprop
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   242
         |> dest_interpret_form
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   243
         |> (fn (data, xs) =>
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   244
            mk_approx_form_eval prec data (HOLogic.mk_list \<^typ>\<open>(float * float) option\<close>
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   245
              (map (fn _ => \<^term>\<open>None :: (float * float) option\<close>) (HOLogic.dest_list xs)))
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   246
         |> approximate ctxt
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   247
         |> HOLogic.dest_list
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   248
         |> curry ListPair.zip (HOLogic.dest_list xs @ calculated_subterms arith_term)
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   249
         |> map (fn (elem, s) => \<^term>\<open>(\<in>) :: real \<Rightarrow> real set \<Rightarrow> bool\<close> $ elem $ mk_result prec (dest_ivl s))
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   250
         |> foldr1 HOLogic.mk_conj))
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   251
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   252
fun approx_arith prec ctxt t = realify t
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59582
diff changeset
   253
     |> Thm.cterm_of ctxt
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   254
     |> (preproc_form_conv ctxt then_conv reify_form_conv ctxt)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59580
diff changeset
   255
     |> Thm.prop_of
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   256
     |> Logic.dest_equals |> snd
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   257
     |> dest_interpret |> fst
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   258
     |> mk_approx' prec
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   259
     |> approximate ctxt
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   260
     |> dest_ivl
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   261
     |> mk_result prec
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   262
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   263
fun approx prec ctxt t =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   264
  if type_of t = \<^typ>\<open>prop\<close> then approx_form prec ctxt t
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   265
  else if type_of t = \<^typ>\<open>bool\<close> then approx_form prec ctxt (\<^const>\<open>Trueprop\<close> $ t)
56813
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   266
  else approx_arith prec ctxt t
80a5905c1610 separate ML module
haftmann
parents:
diff changeset
   267
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   268
fun approximate_cmd modes raw_t state =
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   269
  let
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   270
    val ctxt = Toplevel.context_of state;
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   271
    val t = Syntax.read_term ctxt raw_t;
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   272
    val t' = approx 30 ctxt t;
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   273
    val ty' = Term.type_of t';
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   274
    val ctxt' = Variable.auto_fixes t' ctxt;
59174
wenzelm
parents: 58893
diff changeset
   275
  in
wenzelm
parents: 58893
diff changeset
   276
    Print_Mode.with_modes modes (fn () =>
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   277
      Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
59174
wenzelm
parents: 58893
diff changeset
   278
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) ()
wenzelm
parents: 58893
diff changeset
   279
  end |> Pretty.writeln;
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   280
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   281
val opt_modes =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   282
  Scan.optional (\<^keyword>\<open>(\<close> |-- Parse.!!! (Scan.repeat1 Parse.name --| \<^keyword>\<open>)\<close>)) [];
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   283
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   284
val _ =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67613
diff changeset
   285
  Outer_Syntax.command \<^command_keyword>\<open>approximate\<close> "print approximation of term"
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   286
    (opt_modes -- Parse.term
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   287
      >> (fn (modes, t) => Toplevel.keep (approximate_cmd modes t)));
c062543d380e prefer separate command for approximation
haftmann
parents: 56813
diff changeset
   288
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
   289
fun approximation_tac prec splitting taylor ctxt i =
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   290
  prepare_form_tac ctxt i
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   291
  THEN reify_form_tac ctxt i
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
   292
  THEN rewrite_interpret_form_tac ctxt prec splitting taylor i
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59621
diff changeset
   293
  THEN gen_eval_tac (approximation_conv ctxt) ctxt i
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 62969
diff changeset
   294
62391
1658fc9b2618 more canonical names
nipkow
parents: 60754
diff changeset
   295
end;