src/HOL/Library/Interval_Float.thy
author immler
Sun, 03 Nov 2019 19:58:02 -0500
changeset 71036 dfcc1882d05a
child 71037 f630f2e707a6
permissions -rw-r--r--
moved theory Interval_Approximation from the AFP
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71036
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     1
section \<open>Approximate Operations on Intervals of Floating Point Numbers\<close>
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     2
theory Interval_Float
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     3
  imports
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     4
    Interval
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     5
    Float
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     6
begin
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     7
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     8
definition "split_float_interval x = split_interval x ((lower x + upper x) * Float 1 (-1))"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
     9
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    10
lemma split_float_intervalD: "split_float_interval X = (A, B) \<Longrightarrow> set_of X \<subseteq> set_of A \<union> set_of B"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    11
  by (auto dest!: split_intervalD simp: split_float_interval_def)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    12
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    13
lemmas float_round_down_le[intro] = order_trans[OF float_round_down]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    14
  and float_round_up_ge[intro] = order_trans[OF _ float_round_up]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    15
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    16
definition mid :: "float interval \<Rightarrow> float"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    17
  where "mid i = (lower i + upper i) * Float 1 (-1)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    18
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    19
lemma mid_in_interval: "mid i \<in>\<^sub>i i"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    20
  using lower_le_upper[of i]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    21
  by (auto simp: mid_def set_of_eq powr_minus)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    22
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    23
definition centered :: "float interval \<Rightarrow> float interval"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    24
  where "centered i = i - interval_of (mid i)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    25
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    26
text \<open>TODO: many of the lemmas should move to theories Float or Approximation
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    27
  (the latter should be based on type @{type interval}.\<close>
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    28
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    29
subsection "Intervals with Floating Point Bounds"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    30
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    31
context includes interval.lifting begin
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    32
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    33
lift_definition round_interval :: "nat \<Rightarrow> float interval \<Rightarrow> float interval"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    34
  is "\<lambda>p. \<lambda>(l, u). (float_round_down p l, float_round_up p u)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    35
  by (auto simp: intro!: float_round_down_le float_round_up_le)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    36
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    37
lemma lower_round_ivl[simp]: "lower (round_interval p x) = float_round_down p (lower x)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    38
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    39
lemma upper_round_ivl[simp]: "upper (round_interval p x) = float_round_up p (upper x)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    40
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    41
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    42
lemma round_ivl_correct: "set_of A \<subseteq> set_of (round_interval prec A)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    43
  by (auto simp: set_of_eq float_round_down_le float_round_up_le)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    44
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    45
lift_definition truncate_ivl :: "nat \<Rightarrow> real interval \<Rightarrow> real interval"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    46
  is "\<lambda>p. \<lambda>(l, u). (truncate_down p l, truncate_up p u)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    47
  by (auto intro!: truncate_down_le truncate_up_le)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    48
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    49
lemma lower_truncate_ivl[simp]: "lower (truncate_ivl p x) = truncate_down p (lower x)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    50
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    51
lemma upper_truncate_ivl[simp]: "upper (truncate_ivl p x) = truncate_up p (upper x)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    52
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    53
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    54
lemma truncate_ivl_correct: "set_of A \<subseteq> set_of (truncate_ivl prec A)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    55
  by (auto simp: set_of_eq intro!: truncate_down_le truncate_up_le)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    56
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    57
lift_definition real_interval::"float interval \<Rightarrow> real interval"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    58
  is "\<lambda>(l, u). (real_of_float l, real_of_float u)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    59
  by auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    60
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    61
lemma lower_real_interval[simp]: "lower (real_interval x) = lower x"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    62
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    63
lemma upper_real_interval[simp]: "upper (real_interval x) = upper x"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    64
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    65
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    66
definition "set_of' x = (case x of None \<Rightarrow> UNIV | Some i \<Rightarrow> set_of (real_interval i))"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    67
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    68
lemma real_interval_min_interval[simp]:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    69
  "real_interval (min_interval a b) = min_interval (real_interval a) (real_interval b)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    70
  by (auto simp: interval_eq_set_of_iff set_of_eq real_of_float_min)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    71
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    72
lemma real_interval_max_interval[simp]:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    73
  "real_interval (max_interval a b) = max_interval (real_interval a) (real_interval b)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    74
  by (auto simp: interval_eq_set_of_iff set_of_eq real_of_float_max)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    75
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    76
lemma in_intervalI:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    77
  "x \<in>\<^sub>i X" if "lower X \<le> x" "x \<le> upper X"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    78
  using that by (auto simp: set_of_eq)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    79
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    80
abbreviation in_real_interval ("(_/ \<in>\<^sub>r _)" [51, 51] 50) where
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    81
  "x \<in>\<^sub>r X \<equiv> x \<in>\<^sub>i real_interval X"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    82
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    83
lemma in_real_intervalI:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    84
  "x \<in>\<^sub>r X" if "lower X \<le> x" "x \<le> upper X" for x::real and X::"float interval"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    85
  using that
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    86
  by (intro in_intervalI) auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    87
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    88
lemma lower_Interval: "lower (Interval x) = fst x"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    89
  and upper_Interval: "upper (Interval x) = snd x"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    90
  if "fst x \<le> snd x"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    91
  using that
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    92
  by (auto simp: lower_def upper_def Interval_inverse split_beta')
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    93
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    94
definition all_in_i :: "'a::preorder list \<Rightarrow> 'a interval list \<Rightarrow> bool"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    95
  (infix "(all'_in\<^sub>i)" 50)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    96
  where "x all_in\<^sub>i I = (length x = length I \<and> (\<forall>i < length I. x ! i \<in>\<^sub>i I ! i))"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    97
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    98
definition all_in :: "real list \<Rightarrow> float interval list \<Rightarrow> bool"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
    99
  (infix "(all'_in)" 50)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   100
  where "x all_in I = (length x = length I \<and> (\<forall>i < length I. x ! i \<in>\<^sub>r I ! i))"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   101
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   102
definition all_subset :: "'a::order interval list \<Rightarrow> 'a interval list \<Rightarrow> bool"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   103
  (infix "(all'_subset)" 50)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   104
  where "I all_subset J = (length I = length J \<and> (\<forall>i < length I. set_of (I!i) \<subseteq> set_of (J!i)))"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   105
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   106
lemmas [simp] = all_in_def all_subset_def
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   107
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   108
lemma all_subsetD:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   109
  assumes "I all_subset J"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   110
  assumes "x all_in I"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   111
  shows "x all_in J"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   112
  using assms
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   113
  by (auto simp: set_of_eq; fastforce)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   114
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   115
lemma round_interval_mono: "set_of (round_interval prec X) \<subseteq> set_of (round_interval prec Y)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   116
  if "set_of X \<subseteq> set_of Y"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   117
  using that
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   118
  by transfer
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   119
    (auto simp: float_round_down.rep_eq float_round_up.rep_eq truncate_down_mono truncate_up_mono)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   120
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   121
lemma Ivl_simps[simp]: "lower (Ivl a b) = min a b" "upper (Ivl a b) = b"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   122
  subgoal by transfer simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   123
  subgoal by transfer simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   124
  done
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   125
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   126
lemma set_of_subset_iff: "set_of X \<subseteq> set_of Y \<longleftrightarrow> lower Y \<le> lower X \<and> upper X \<le> upper Y"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   127
  for X Y::"'a::linorder interval"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   128
  by (auto simp: set_of_eq subset_iff)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   129
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   130
lemma bounds_of_interval_eq_lower_upper:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   131
  "bounds_of_interval ivl = (lower ivl, upper ivl)" if "lower ivl \<le> upper ivl"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   132
  using that
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   133
  by (auto simp: lower.rep_eq upper.rep_eq)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   134
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   135
lemma real_interval_Ivl: "real_interval (Ivl a b) = Ivl a b"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   136
  by transfer (auto simp: min_def)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   137
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   138
lemma set_of_mul_contains_real_zero:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   139
  "0 \<in>\<^sub>r (A * B)" if "0 \<in>\<^sub>r A \<or> 0 \<in>\<^sub>r B"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   140
  using that set_of_mul_contains_zero[of A B]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   141
  by (auto simp: set_of_eq)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   142
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   143
fun subdivide_interval :: "nat \<Rightarrow> float interval \<Rightarrow> float interval list"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   144
  where "subdivide_interval 0 I = [I]"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   145
  | "subdivide_interval (Suc n) I = (
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   146
         let m = mid I
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   147
         in (subdivide_interval n (Ivl (lower I) m)) @ (subdivide_interval n (Ivl m (upper I)))
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   148
       )"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   149
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   150
lemma subdivide_interval_length:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   151
  shows "length (subdivide_interval n I) = 2^n"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   152
  by(induction n arbitrary: I, simp_all add: Let_def)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   153
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   154
lemma lower_le_mid: "lower x \<le> mid x" "real_of_float (lower x) \<le> mid x"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   155
  and mid_le_upper: "mid x \<le> upper x" "real_of_float (mid x) \<le> upper x"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   156
  unfolding mid_def
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   157
  subgoal by transfer (auto simp: powr_neg_one)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   158
  subgoal by transfer (auto simp: powr_neg_one)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   159
  subgoal by transfer (auto simp: powr_neg_one)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   160
  subgoal by transfer (auto simp: powr_neg_one)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   161
  done
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   162
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   163
lemma subdivide_interval_correct:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   164
  "list_ex (\<lambda>i. x \<in>\<^sub>r i) (subdivide_interval n I)" if "x \<in>\<^sub>r I" for x::real
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   165
  using that
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   166
proof(induction n arbitrary: x I)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   167
  case 0
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   168
  then show ?case by simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   169
next
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   170
  case (Suc n)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   171
  from \<open>x \<in>\<^sub>r I\<close> consider "x \<in>\<^sub>r Ivl (lower I) (mid I)" | "x \<in>\<^sub>r Ivl (mid I) (upper I)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   172
    by (cases "x \<le> real_of_float (mid I)")
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   173
      (auto simp: set_of_eq min_def lower_le_mid mid_le_upper)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   174
  from this[case_names lower upper] show ?case
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   175
    by cases (use Suc.IH in \<open>auto simp: Let_def\<close>)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   176
qed
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   177
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   178
fun interval_list_union :: "'a::lattice interval list \<Rightarrow> 'a interval"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   179
  where "interval_list_union [] = undefined"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   180
  | "interval_list_union [I] = I"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   181
  | "interval_list_union (I#Is) = sup I (interval_list_union Is)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   182
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   183
lemma interval_list_union_correct:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   184
  assumes "S \<noteq> []"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   185
  assumes "i < length S"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   186
  shows "set_of (S!i) \<subseteq> set_of (interval_list_union S)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   187
  using assms
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   188
proof(induction S arbitrary: i)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   189
  case (Cons a S i)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   190
  thus ?case
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   191
  proof(cases S)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   192
    fix b S'
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   193
    assume "S = b # S'"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   194
    hence "S \<noteq> []"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   195
      by simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   196
    show ?thesis
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   197
    proof(cases i)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   198
      case 0
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   199
      show ?thesis
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   200
        apply(cases S)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   201
        using interval_union_mono1
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   202
        by (auto simp add: 0)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   203
    next
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   204
      case (Suc i_prev)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   205
      hence "i_prev < length S"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   206
        using Cons(3) by simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   207
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   208
      from Cons(1)[OF \<open>S \<noteq> []\<close> this] Cons(1)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   209
      have "set_of ((a # S) ! i) \<subseteq> set_of (interval_list_union S)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   210
        by (simp add: \<open>i = Suc i_prev\<close>)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   211
      also have "... \<subseteq> set_of (interval_list_union (a # S))"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   212
        using \<open>S \<noteq> []\<close>
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   213
        apply(cases S)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   214
        using interval_union_mono2
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   215
        by auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   216
      finally show ?thesis .
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   217
    qed
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   218
  qed simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   219
qed simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   220
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   221
lemma split_domain_correct:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   222
  fixes x :: "real list"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   223
  assumes "x all_in I"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   224
  assumes split_correct: "\<And>x a I. x \<in>\<^sub>r I \<Longrightarrow> list_ex (\<lambda>i::float interval. x \<in>\<^sub>r i) (split I)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   225
  shows "list_ex (\<lambda>s. x all_in s) (split_domain split I)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   226
  using assms(1)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   227
proof(induction I arbitrary: x)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   228
  case (Cons I Is x)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   229
  have "x \<noteq> []"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   230
    using Cons(2) by auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   231
  obtain x' xs where x_decomp: "x = x' # xs"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   232
    using \<open>x \<noteq> []\<close> list.exhaust by auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   233
  hence "x' \<in>\<^sub>r I" "xs all_in Is"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   234
    using Cons(2)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   235
    by auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   236
  show ?case
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   237
    using Cons(1)[OF \<open>xs all_in Is\<close>]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   238
      split_correct[OF \<open>x' \<in>\<^sub>r I\<close>]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   239
    apply (auto simp add: list_ex_iff set_of_eq)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   240
    by (smt length_Cons less_Suc_eq_0_disj nth_Cons_0 nth_Cons_Suc x_decomp)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   241
qed simp
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   242
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   243
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   244
lift_definition(code_dt) inverse_float_interval::"nat \<Rightarrow> float interval \<Rightarrow> float interval option" is
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   245
  "\<lambda>prec (l, u). if (0 < l \<or> u < 0) then Some (float_divl prec 1 u, float_divr prec 1 l) else None"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   246
  by (auto intro!: order_trans[OF float_divl] order_trans[OF _ float_divr]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   247
      simp: divide_simps)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   248
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   249
lemma inverse_float_interval_eq_Some_conv:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   250
  defines "one \<equiv> (1::float)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   251
  shows 
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   252
    "inverse_float_interval p X = Some R \<longleftrightarrow>
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   253
    (lower X > 0 \<or> upper X < 0) \<and>
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   254
    lower R = float_divl p one (upper X) \<and>
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   255
    upper R = float_divr p one (lower X)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   256
  by clarsimp (transfer fixing: one, force simp: one_def split: if_splits)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   257
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   258
lemma inverse_float_interval:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   259
  "inverse ` set_of (real_interval X) \<subseteq> set_of (real_interval Y)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   260
  if "inverse_float_interval p X = Some Y"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   261
  using that
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   262
  apply (clarsimp simp: set_of_eq inverse_float_interval_eq_Some_conv)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   263
  by (intro order_trans[OF float_divl] order_trans[OF _ float_divr] conjI)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   264
    (auto simp: divide_simps)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   265
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   266
lemma inverse_float_intervalI:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   267
  "x \<in>\<^sub>r X \<Longrightarrow> inverse x \<in> set_of' (inverse_float_interval p X)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   268
  using inverse_float_interval[of p X]
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   269
  by (auto simp: set_of'_def split: option.splits)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   270
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   271
lemma real_interval_abs_interval[simp]:
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   272
  "real_interval (abs_interval x) = abs_interval (real_interval x)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   273
  by (auto simp: interval_eq_set_of_iff set_of_eq real_of_float_max real_of_float_min)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   274
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   275
lift_definition floor_float_interval::"float interval \<Rightarrow> float interval" is
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   276
  "\<lambda>(l, u). (floor_fl l, floor_fl u)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   277
  by (auto intro!: floor_mono simp: floor_fl.rep_eq)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   278
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   279
lemma lower_floor_float_interval[simp]: "lower (floor_float_interval x) = floor_fl (lower x)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   280
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   281
lemma upper_floor_float_interval[simp]: "upper (floor_float_interval x) = floor_fl (upper x)"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   282
  by transfer auto
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   283
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   284
lemma floor_float_intervalI: "\<lfloor>x\<rfloor> \<in>\<^sub>r floor_float_interval X" if "x \<in>\<^sub>r X"
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   285
  using that by (auto simp: set_of_eq floor_fl_def floor_mono)
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   286
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   287
end
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   288
dfcc1882d05a moved theory Interval_Approximation from the AFP
immler
parents:
diff changeset
   289
end