7 header{*Binary arithmetic and Simplification for the Hyperreals*} |
7 header{*Binary arithmetic and Simplification for the Hyperreals*} |
8 |
8 |
9 theory HyperArith = HyperDef |
9 theory HyperArith = HyperDef |
10 files ("hypreal_arith.ML"): |
10 files ("hypreal_arith.ML"): |
11 |
11 |
12 subsection{*Binary Arithmetic for the Hyperreals*} |
12 |
|
13 subsection{*Numerals and Arithmetic*} |
13 |
14 |
14 instance hypreal :: number .. |
15 instance hypreal :: number .. |
15 |
16 |
16 defs (overloaded) |
17 primrec (*the type constraint is essential!*) |
17 hypreal_number_of_def: |
18 number_of_Pls: "number_of bin.Pls = 0" |
18 "number_of v == hypreal_of_real (number_of v)" |
19 number_of_Min: "number_of bin.Min = - (1::hypreal)" |
19 (*::bin=>hypreal ::bin=>real*) |
20 number_of_BIT: "number_of(w BIT x) = (if x then 1 else 0) + |
20 --{*This case is reduced to that for the reals.*} |
21 (number_of w) + (number_of w)" |
21 |
22 |
22 |
23 declare number_of_Pls [simp del] |
23 |
24 number_of_Min [simp del] |
24 subsubsection{*Embedding the Reals into the Hyperreals*} |
25 number_of_BIT [simp del] |
25 |
26 |
|
27 instance hypreal :: number_ring |
|
28 proof |
|
29 show "Numeral0 = (0::hypreal)" by (rule number_of_Pls) |
|
30 show "-1 = - (1::hypreal)" by (rule number_of_Min) |
|
31 fix w :: bin and x :: bool |
|
32 show "(number_of (w BIT x) :: hypreal) = |
|
33 (if x then 1 else 0) + number_of w + number_of w" |
|
34 by (rule number_of_BIT) |
|
35 qed |
|
36 |
|
37 |
|
38 text{*Collapse applications of @{term hypreal_of_real} to @{term number_of}*} |
26 lemma hypreal_number_of [simp]: "hypreal_of_real (number_of w) = number_of w" |
39 lemma hypreal_number_of [simp]: "hypreal_of_real (number_of w) = number_of w" |
27 by (simp add: hypreal_number_of_def) |
40 apply (induct w) |
28 |
41 apply (simp_all only: number_of hypreal_of_real_add hypreal_of_real_minus, simp_all) |
29 lemma hypreal_numeral_0_eq_0: "Numeral0 = (0::hypreal)" |
42 done |
30 by (simp add: hypreal_number_of_def) |
|
31 |
|
32 lemma hypreal_numeral_1_eq_1: "Numeral1 = (1::hypreal)" |
|
33 by (simp add: hypreal_number_of_def) |
|
34 |
|
35 subsubsection{*Addition*} |
|
36 |
|
37 lemma add_hypreal_number_of [simp]: |
|
38 "(number_of v :: hypreal) + number_of v' = number_of (bin_add v v')" |
|
39 by (simp only: hypreal_number_of_def hypreal_of_real_add [symmetric] |
|
40 add_real_number_of) |
|
41 |
|
42 |
|
43 subsubsection{*Subtraction*} |
|
44 |
|
45 lemma minus_hypreal_number_of [simp]: |
|
46 "- (number_of w :: hypreal) = number_of (bin_minus w)" |
|
47 by (simp only: hypreal_number_of_def minus_real_number_of |
|
48 hypreal_of_real_minus [symmetric]) |
|
49 |
|
50 lemma diff_hypreal_number_of [simp]: |
|
51 "(number_of v :: hypreal) - number_of w = |
|
52 number_of (bin_add v (bin_minus w))" |
|
53 by (unfold hypreal_number_of_def hypreal_diff_def, simp) |
|
54 |
|
55 |
|
56 subsubsection{*Multiplication*} |
|
57 |
|
58 lemma mult_hypreal_number_of [simp]: |
|
59 "(number_of v :: hypreal) * number_of v' = number_of (bin_mult v v')" |
|
60 by (simp only: hypreal_number_of_def hypreal_of_real_mult [symmetric] mult_real_number_of) |
|
61 |
|
62 text{*Lemmas for specialist use, NOT as default simprules*} |
|
63 lemma hypreal_mult_2: "2 * z = (z+z::hypreal)" |
|
64 proof - |
|
65 have eq: "(2::hypreal) = 1 + 1" |
|
66 by (simp add: hypreal_numeral_1_eq_1 [symmetric]) |
|
67 thus ?thesis by (simp add: eq left_distrib) |
|
68 qed |
|
69 |
|
70 lemma hypreal_mult_2_right: "z * 2 = (z+z::hypreal)" |
|
71 by (subst hypreal_mult_commute, rule hypreal_mult_2) |
|
72 |
|
73 |
|
74 subsubsection{*Comparisons*} |
|
75 |
|
76 (** Equals (=) **) |
|
77 |
|
78 lemma eq_hypreal_number_of [simp]: |
|
79 "((number_of v :: hypreal) = number_of v') = |
|
80 iszero (number_of (bin_add v (bin_minus v')) :: int)" |
|
81 apply (simp only: hypreal_number_of_def hypreal_of_real_eq_iff eq_real_number_of) |
|
82 done |
|
83 |
|
84 |
|
85 (** Less-than (<) **) |
|
86 |
|
87 (*"neg" is used in rewrite rules for binary comparisons*) |
|
88 lemma less_hypreal_number_of [simp]: |
|
89 "((number_of v :: hypreal) < number_of v') = |
|
90 neg (number_of (bin_add v (bin_minus v')) :: int)" |
|
91 by (simp only: hypreal_number_of_def hypreal_of_real_less_iff less_real_number_of) |
|
92 |
|
93 |
|
94 |
|
95 text{*New versions of existing theorems involving 0, 1*} |
|
96 |
|
97 lemma hypreal_minus_1_eq_m1 [simp]: "- 1 = (-1::hypreal)" |
|
98 by (simp add: hypreal_numeral_1_eq_1 [symmetric]) |
|
99 |
|
100 lemma hypreal_mult_minus1 [simp]: "-1 * z = -(z::hypreal)" |
|
101 proof - |
|
102 have "-1 * z = (- 1) * z" by (simp add: hypreal_minus_1_eq_m1) |
|
103 also have "... = - (1 * z)" by (simp only: minus_mult_left) |
|
104 also have "... = -z" by simp |
|
105 finally show ?thesis . |
|
106 qed |
|
107 |
|
108 lemma hypreal_mult_minus1_right [simp]: "(z::hypreal) * -1 = -z" |
|
109 by (subst hypreal_mult_commute, rule hypreal_mult_minus1) |
|
110 |
|
111 |
|
112 subsection{*Simplification of Arithmetic when Nested to the Right*} |
|
113 |
|
114 lemma hypreal_add_number_of_left [simp]: |
|
115 "number_of v + (number_of w + z) = (number_of(bin_add v w) + z::hypreal)" |
|
116 by (simp add: add_assoc [symmetric]) |
|
117 |
|
118 lemma hypreal_mult_number_of_left [simp]: |
|
119 "number_of v *(number_of w * z) = (number_of(bin_mult v w) * z::hypreal)" |
|
120 by (simp add: hypreal_mult_assoc [symmetric]) |
|
121 |
|
122 lemma hypreal_add_number_of_diff1: |
|
123 "number_of v + (number_of w - c) = number_of(bin_add v w) - (c::hypreal)" |
|
124 by (simp add: hypreal_diff_def hypreal_add_number_of_left) |
|
125 |
|
126 lemma hypreal_add_number_of_diff2 [simp]: |
|
127 "number_of v + (c - number_of w) = |
|
128 number_of (bin_add v (bin_minus w)) + (c::hypreal)" |
|
129 apply (subst diff_hypreal_number_of [symmetric]) |
|
130 apply (simp only: hypreal_diff_def add_ac) |
|
131 done |
|
132 |
|
133 |
|
134 declare hypreal_numeral_0_eq_0 [simp] hypreal_numeral_1_eq_1 [simp] |
|
135 |
|
136 |
43 |
137 |
44 |
138 use "hypreal_arith.ML" |
45 use "hypreal_arith.ML" |
139 |
46 |
140 setup hypreal_arith_setup |
47 setup hypreal_arith_setup |
141 |
48 |
142 lemma hypreal_le_add_order: "[| 0 \<le> x; 0 \<le> y |] ==> (0::hypreal) \<le> x + y" |
49 lemma hypreal_le_add_order: "[| 0 \<le> x; 0 \<le> y |] ==> (0::hypreal) \<le> x + y" |
143 by arith |
50 by arith |
144 |
|
145 |
|
146 subsubsection{*Division By @{term 1} and @{term "-1"}*} |
|
147 |
|
148 lemma hypreal_divide_minus1 [simp]: "x/-1 = -(x::hypreal)" |
|
149 by simp |
|
150 |
|
151 lemma hypreal_minus1_divide [simp]: "-1/(x::hypreal) = - (1/x)" |
|
152 by (simp add: hypreal_divide_def hypreal_minus_inverse) |
|
153 |
51 |
154 |
52 |
155 subsection{*The Function @{term hypreal_of_real}*} |
53 subsection{*The Function @{term hypreal_of_real}*} |
156 |
54 |
157 lemma number_of_less_hypreal_of_real_iff [simp]: |
55 lemma number_of_less_hypreal_of_real_iff [simp]: |
177 apply (subst hypreal_of_real_less_iff [symmetric]) |
75 apply (subst hypreal_of_real_less_iff [symmetric]) |
178 apply (simp (no_asm)) |
76 apply (simp (no_asm)) |
179 done |
77 done |
180 |
78 |
181 lemma hypreal_of_real_le_number_of_iff [simp]: |
79 lemma hypreal_of_real_le_number_of_iff [simp]: |
182 "(hypreal_of_real z <= number_of w) = (z <= number_of w)" |
80 "(hypreal_of_real z \<le> number_of w) = (z \<le> number_of w)" |
183 apply (subst hypreal_of_real_le_iff [symmetric]) |
81 apply (subst hypreal_of_real_le_iff [symmetric]) |
184 apply (simp (no_asm)) |
82 apply (simp (no_asm)) |
185 done |
83 done |
186 |
84 |
187 subsection{*Absolute Value Function for the Hyperreals*} |
85 subsection{*Absolute Value Function for the Hyperreals*} |
188 |
|
189 lemma hrabs_number_of [simp]: |
|
190 "abs (number_of v :: hypreal) = |
|
191 (if neg (number_of v :: int) then number_of (bin_minus v) |
|
192 else number_of v)" |
|
193 by (simp add: hrabs_def) |
|
194 |
|
195 |
86 |
196 declare abs_mult [simp] |
87 declare abs_mult [simp] |
197 |
88 |
198 lemma hrabs_add_less: "[| abs x < r; abs y < s |] ==> abs(x+y) < r + (s::hypreal)" |
89 lemma hrabs_add_less: "[| abs x < r; abs y < s |] ==> abs(x+y) < r + (s::hypreal)" |
199 apply (unfold hrabs_def) |
90 apply (unfold hrabs_def) |
200 apply (simp split add: split_if_asm) |
91 apply (simp split add: split_if_asm) |
201 done |
92 done |
202 |
93 |
|
94 text{*used once in NSA*} |
203 lemma hrabs_less_gt_zero: "abs x < r ==> (0::hypreal) < r" |
95 lemma hrabs_less_gt_zero: "abs x < r ==> (0::hypreal) < r" |
204 by (blast intro!: order_le_less_trans abs_ge_zero) |
96 by (blast intro!: order_le_less_trans abs_ge_zero) |
205 |
97 |
206 lemma hrabs_disj: "abs x = (x::hypreal) | abs x = -x" |
98 lemma hrabs_disj: "abs x = (x::hypreal) | abs x = -x" |
207 by (simp add: hrabs_def) |
99 by (simp add: hrabs_def) |
208 |
100 |
209 (* Needed in Geom.ML *) |
101 (* Needed in Geom.ML *) |
210 lemma hrabs_add_lemma_disj: "(y::hypreal) + - x + (y + - z) = abs (x + - z) ==> y = z | x = y" |
102 lemma hrabs_add_lemma_disj: "(y::hypreal) + - x + (y + - z) = abs (x + - z) ==> y = z | x = y" |
211 by (simp add: hrabs_def split add: split_if_asm) |
103 by (simp add: hrabs_def split add: split_if_asm) |
212 |
104 |
213 |
|
214 (*---------------------------------------------------------- |
|
215 Relating hrabs to abs through embedding of IR into IR* |
|
216 ----------------------------------------------------------*) |
|
217 lemma hypreal_of_real_hrabs: |
105 lemma hypreal_of_real_hrabs: |
218 "abs (hypreal_of_real r) = hypreal_of_real (abs r)" |
106 "abs (hypreal_of_real r) = hypreal_of_real (abs r)" |
219 apply (unfold hypreal_of_real_def) |
107 apply (unfold hypreal_of_real_def) |
220 apply (auto simp add: hypreal_hrabs) |
108 apply (auto simp add: hypreal_hrabs) |
221 done |
109 done |