author | wenzelm |
Thu, 12 Sep 2024 13:10:36 +0200 | |
changeset 80868 | 0ed02f473cf9 |
parent 80761 | bc936d3d8b45 |
child 81124 | 6ce0c8d59f5a |
permissions | -rw-r--r-- |
23146 | 1 |
(* Title: ZF/Bin.thy |
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
3 |
Copyright 1994 University of Cambridge |
|
4 |
||
5 |
The sign Pls stands for an infinite string of leading 0's. |
|
6 |
The sign Min stands for an infinite string of leading 1's. |
|
7 |
||
8 |
A number can have multiple representations, namely leading 0's with sign |
|
9 |
Pls and leading 1's with sign Min. See twos-compl.ML/int_of_binary for |
|
10 |
the numerical interpretation. |
|
11 |
||
12 |
The representation expects that (m mod 2) is 0 or 1, even if m is negative; |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
13 |
For instance, \<not>5 div 2 = \<not>3 and \<not>5 mod 2 = 1; thus \<not>5 = (\<not>3)*2 + 1 |
23146 | 14 |
*) |
15 |
||
60770 | 16 |
section\<open>Arithmetic on Binary Integers\<close> |
23146 | 17 |
|
18 |
theory Bin |
|
68490
eb53f944c8cd
simplified ZF theory names (in contrast to 6a0801279f4c): session-qualification already achieves disjointness;
wenzelm
parents:
68233
diff
changeset
|
19 |
imports Int Datatype |
23146 | 20 |
begin |
21 |
||
22 |
consts bin :: i |
|
23 |
datatype |
|
24 |
"bin" = Pls |
|
25 |
| Min |
|
69587 | 26 |
| Bit ("w \<in> bin", "b \<in> bool") (infixl \<open>BIT\<close> 90) |
23146 | 27 |
|
28 |
consts |
|
76215
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
29 |
integ_of :: "i\<Rightarrow>i" |
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
30 |
NCons :: "[i,i]\<Rightarrow>i" |
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
31 |
bin_succ :: "i\<Rightarrow>i" |
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
32 |
bin_pred :: "i\<Rightarrow>i" |
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
33 |
bin_minus :: "i\<Rightarrow>i" |
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
34 |
bin_adder :: "i\<Rightarrow>i" |
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
35 |
bin_mult :: "[i,i]\<Rightarrow>i" |
23146 | 36 |
|
37 |
primrec |
|
38 |
integ_of_Pls: "integ_of (Pls) = $# 0" |
|
39 |
integ_of_Min: "integ_of (Min) = $-($#1)" |
|
40 |
integ_of_BIT: "integ_of (w BIT b) = $#b $+ integ_of(w) $+ integ_of(w)" |
|
41 |
||
42 |
(** recall that cond(1,b,c)=b and cond(0,b,c)=0 **) |
|
43 |
||
44 |
primrec (*NCons adds a bit, suppressing leading 0s and 1s*) |
|
45 |
NCons_Pls: "NCons (Pls,b) = cond(b,Pls BIT b,Pls)" |
|
46 |
NCons_Min: "NCons (Min,b) = cond(b,Min,Min BIT b)" |
|
47 |
NCons_BIT: "NCons (w BIT c,b) = w BIT c BIT b" |
|
48 |
||
49 |
primrec (*successor. If a BIT, can change a 0 to a 1 without recursion.*) |
|
50 |
bin_succ_Pls: "bin_succ (Pls) = Pls BIT 1" |
|
51 |
bin_succ_Min: "bin_succ (Min) = Pls" |
|
52 |
bin_succ_BIT: "bin_succ (w BIT b) = cond(b, bin_succ(w) BIT 0, NCons(w,1))" |
|
53 |
||
54 |
primrec (*predecessor*) |
|
55 |
bin_pred_Pls: "bin_pred (Pls) = Min" |
|
56 |
bin_pred_Min: "bin_pred (Min) = Min BIT 0" |
|
57 |
bin_pred_BIT: "bin_pred (w BIT b) = cond(b, NCons(w,0), bin_pred(w) BIT 1)" |
|
58 |
||
59 |
primrec (*unary negation*) |
|
60 |
bin_minus_Pls: |
|
61 |
"bin_minus (Pls) = Pls" |
|
62 |
bin_minus_Min: |
|
63 |
"bin_minus (Min) = Pls BIT 1" |
|
64 |
bin_minus_BIT: |
|
65 |
"bin_minus (w BIT b) = cond(b, bin_pred(NCons(bin_minus(w),0)), |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
26190
diff
changeset
|
66 |
bin_minus(w) BIT 0)" |
23146 | 67 |
|
68 |
primrec (*sum*) |
|
69 |
bin_adder_Pls: |
|
46820 | 70 |
"bin_adder (Pls) = (\<lambda>w\<in>bin. w)" |
23146 | 71 |
bin_adder_Min: |
46820 | 72 |
"bin_adder (Min) = (\<lambda>w\<in>bin. bin_pred(w))" |
23146 | 73 |
bin_adder_BIT: |
46820 | 74 |
"bin_adder (v BIT x) = |
75 |
(\<lambda>w\<in>bin. |
|
76 |
bin_case (v BIT x, bin_pred(v BIT x), |
|
76215
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
77 |
\<lambda>w y. NCons(bin_adder (v) ` cond(x and y, bin_succ(w), w), |
23146 | 78 |
x xor y), |
79 |
w))" |
|
80 |
||
81 |
(*The bin_case above replaces the following mutually recursive function: |
|
82 |
primrec |
|
83 |
"adding (v,x,Pls) = v BIT x" |
|
84 |
"adding (v,x,Min) = bin_pred(v BIT x)" |
|
46820 | 85 |
"adding (v,x,w BIT y) = NCons(bin_adder (v, cond(x and y, bin_succ(w), w)), |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
26190
diff
changeset
|
86 |
x xor y)" |
23146 | 87 |
*) |
88 |
||
24893 | 89 |
definition |
76215
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
90 |
bin_add :: "[i,i]\<Rightarrow>i" where |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
91 |
"bin_add(v,w) \<equiv> bin_adder(v)`w" |
23146 | 92 |
|
93 |
||
94 |
primrec |
|
95 |
bin_mult_Pls: |
|
96 |
"bin_mult (Pls,w) = Pls" |
|
97 |
bin_mult_Min: |
|
98 |
"bin_mult (Min,w) = bin_minus(w)" |
|
99 |
bin_mult_BIT: |
|
100 |
"bin_mult (v BIT b,w) = cond(b, bin_add(NCons(bin_mult(v,w),0),w), |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
26190
diff
changeset
|
101 |
NCons(bin_mult(v,w),0))" |
23146 | 102 |
|
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
32960
diff
changeset
|
103 |
syntax |
71546 | 104 |
"_Int0" :: i (\<open>#' 0\<close>) |
105 |
"_Int1" :: i (\<open>#' 1\<close>) |
|
106 |
"_Int2" :: i (\<open>#' 2\<close>) |
|
107 |
"_Neg_Int1" :: i (\<open>#-' 1\<close>) |
|
108 |
"_Neg_Int2" :: i (\<open>#-' 2\<close>) |
|
58421 | 109 |
translations |
110 |
"#0" \<rightleftharpoons> "CONST integ_of(CONST Pls)" |
|
111 |
"#1" \<rightleftharpoons> "CONST integ_of(CONST Pls BIT 1)" |
|
112 |
"#2" \<rightleftharpoons> "CONST integ_of(CONST Pls BIT 1 BIT 0)" |
|
113 |
"#-1" \<rightleftharpoons> "CONST integ_of(CONST Min)" |
|
114 |
"#-2" \<rightleftharpoons> "CONST integ_of(CONST Min BIT 0)" |
|
115 |
||
116 |
syntax |
|
76215
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
117 |
"_Int" :: "num_token \<Rightarrow> i" (\<open>#_\<close> 1000) |
a642599ffdea
More syntactic cleanup. LaTeX markup working
paulson <lp15@cam.ac.uk>
parents:
76214
diff
changeset
|
118 |
"_Neg_Int" :: "num_token \<Rightarrow> i" (\<open>#-_\<close> 1000) |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
32960
diff
changeset
|
119 |
|
80761 | 120 |
syntax_consts |
121 |
"_Int0" "_Int1" "_Int2" "_Int" "_Neg_Int1" "_Neg_Int2" "_Neg_Int" \<rightleftharpoons> integ_of |
|
122 |
||
69605 | 123 |
ML_file \<open>Tools/numeral_syntax.ML\<close> |
23146 | 124 |
|
125 |
||
126 |
declare bin.intros [simp,TC] |
|
127 |
||
128 |
lemma NCons_Pls_0: "NCons(Pls,0) = Pls" |
|
129 |
by simp |
|
130 |
||
131 |
lemma NCons_Pls_1: "NCons(Pls,1) = Pls BIT 1" |
|
132 |
by simp |
|
133 |
||
134 |
lemma NCons_Min_0: "NCons(Min,0) = Min BIT 0" |
|
135 |
by simp |
|
136 |
||
137 |
lemma NCons_Min_1: "NCons(Min,1) = Min" |
|
138 |
by simp |
|
139 |
||
140 |
lemma NCons_BIT: "NCons(w BIT x,b) = w BIT x BIT b" |
|
141 |
by (simp add: bin.case_eqns) |
|
142 |
||
46820 | 143 |
lemmas NCons_simps [simp] = |
23146 | 144 |
NCons_Pls_0 NCons_Pls_1 NCons_Min_0 NCons_Min_1 NCons_BIT |
145 |
||
146 |
||
147 |
||
148 |
(** Type checking **) |
|
149 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
150 |
lemma integ_of_type [TC]: "w \<in> bin \<Longrightarrow> integ_of(w) \<in> int" |
23146 | 151 |
apply (induct_tac "w") |
152 |
apply (simp_all add: bool_into_nat) |
|
153 |
done |
|
154 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
155 |
lemma NCons_type [TC]: "\<lbrakk>w \<in> bin; b \<in> bool\<rbrakk> \<Longrightarrow> NCons(w,b) \<in> bin" |
23146 | 156 |
by (induct_tac "w", auto) |
157 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
158 |
lemma bin_succ_type [TC]: "w \<in> bin \<Longrightarrow> bin_succ(w) \<in> bin" |
23146 | 159 |
by (induct_tac "w", auto) |
160 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
161 |
lemma bin_pred_type [TC]: "w \<in> bin \<Longrightarrow> bin_pred(w) \<in> bin" |
23146 | 162 |
by (induct_tac "w", auto) |
163 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
164 |
lemma bin_minus_type [TC]: "w \<in> bin \<Longrightarrow> bin_minus(w) \<in> bin" |
23146 | 165 |
by (induct_tac "w", auto) |
166 |
||
167 |
(*This proof is complicated by the mutual recursion*) |
|
71082 | 168 |
lemma bin_add_type [rule_format]: |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
169 |
"v \<in> bin \<Longrightarrow> \<forall>w\<in>bin. bin_add(v,w) \<in> bin" |
76216
9fc34f76b4e8
getting rid of apply (unfold ...)
paulson <lp15@cam.ac.uk>
parents:
76215
diff
changeset
|
170 |
unfolding bin_add_def |
23146 | 171 |
apply (induct_tac "v") |
172 |
apply (rule_tac [3] ballI) |
|
173 |
apply (rename_tac [3] "w'") |
|
174 |
apply (induct_tac [3] "w'") |
|
175 |
apply (simp_all add: NCons_type) |
|
176 |
done |
|
177 |
||
71082 | 178 |
declare bin_add_type [TC] |
179 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
180 |
lemma bin_mult_type [TC]: "\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> \<Longrightarrow> bin_mult(v,w) \<in> bin" |
23146 | 181 |
by (induct_tac "v", auto) |
182 |
||
183 |
||
60770 | 184 |
subsubsection\<open>The Carry and Borrow Functions, |
69593 | 185 |
\<^term>\<open>bin_succ\<close> and \<^term>\<open>bin_pred\<close>\<close> |
23146 | 186 |
|
187 |
(*NCons preserves the integer value of its argument*) |
|
188 |
lemma integ_of_NCons [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
189 |
"\<lbrakk>w \<in> bin; b \<in> bool\<rbrakk> \<Longrightarrow> integ_of(NCons(w,b)) = integ_of(w BIT b)" |
23146 | 190 |
apply (erule bin.cases) |
46820 | 191 |
apply (auto elim!: boolE) |
23146 | 192 |
done |
193 |
||
194 |
lemma integ_of_succ [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
195 |
"w \<in> bin \<Longrightarrow> integ_of(bin_succ(w)) = $#1 $+ integ_of(w)" |
23146 | 196 |
apply (erule bin.induct) |
46820 | 197 |
apply (auto simp add: zadd_ac elim!: boolE) |
23146 | 198 |
done |
199 |
||
200 |
lemma integ_of_pred [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
201 |
"w \<in> bin \<Longrightarrow> integ_of(bin_pred(w)) = $- ($#1) $+ integ_of(w)" |
23146 | 202 |
apply (erule bin.induct) |
46820 | 203 |
apply (auto simp add: zadd_ac elim!: boolE) |
23146 | 204 |
done |
205 |
||
206 |
||
69593 | 207 |
subsubsection\<open>\<^term>\<open>bin_minus\<close>: Unary Negation of Binary Integers\<close> |
23146 | 208 |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
209 |
lemma integ_of_minus: "w \<in> bin \<Longrightarrow> integ_of(bin_minus(w)) = $- integ_of(w)" |
23146 | 210 |
apply (erule bin.induct) |
46820 | 211 |
apply (auto simp add: zadd_ac zminus_zadd_distrib elim!: boolE) |
23146 | 212 |
done |
213 |
||
214 |
||
69593 | 215 |
subsubsection\<open>\<^term>\<open>bin_add\<close>: Binary Addition\<close> |
23146 | 216 |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
217 |
lemma bin_add_Pls [simp]: "w \<in> bin \<Longrightarrow> bin_add(Pls,w) = w" |
23146 | 218 |
by (unfold bin_add_def, simp) |
219 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
220 |
lemma bin_add_Pls_right: "w \<in> bin \<Longrightarrow> bin_add(w,Pls) = w" |
76216
9fc34f76b4e8
getting rid of apply (unfold ...)
paulson <lp15@cam.ac.uk>
parents:
76215
diff
changeset
|
221 |
unfolding bin_add_def |
23146 | 222 |
apply (erule bin.induct, auto) |
223 |
done |
|
224 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
225 |
lemma bin_add_Min [simp]: "w \<in> bin \<Longrightarrow> bin_add(Min,w) = bin_pred(w)" |
23146 | 226 |
by (unfold bin_add_def, simp) |
227 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
228 |
lemma bin_add_Min_right: "w \<in> bin \<Longrightarrow> bin_add(w,Min) = bin_pred(w)" |
76216
9fc34f76b4e8
getting rid of apply (unfold ...)
paulson <lp15@cam.ac.uk>
parents:
76215
diff
changeset
|
229 |
unfolding bin_add_def |
23146 | 230 |
apply (erule bin.induct, auto) |
231 |
done |
|
232 |
||
233 |
lemma bin_add_BIT_Pls [simp]: "bin_add(v BIT x,Pls) = v BIT x" |
|
234 |
by (unfold bin_add_def, simp) |
|
235 |
||
236 |
lemma bin_add_BIT_Min [simp]: "bin_add(v BIT x,Min) = bin_pred(v BIT x)" |
|
237 |
by (unfold bin_add_def, simp) |
|
238 |
||
239 |
lemma bin_add_BIT_BIT [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
240 |
"\<lbrakk>w \<in> bin; y \<in> bool\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
241 |
\<Longrightarrow> bin_add(v BIT x, w BIT y) = |
23146 | 242 |
NCons(bin_add(v, cond(x and y, bin_succ(w), w)), x xor y)" |
243 |
by (unfold bin_add_def, simp) |
|
244 |
||
245 |
lemma integ_of_add [rule_format]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
246 |
"v \<in> bin \<Longrightarrow> |
46820 | 247 |
\<forall>w\<in>bin. integ_of(bin_add(v,w)) = integ_of(v) $+ integ_of(w)" |
23146 | 248 |
apply (erule bin.induct, simp, simp) |
249 |
apply (rule ballI) |
|
250 |
apply (induct_tac "wa") |
|
46820 | 251 |
apply (auto simp add: zadd_ac elim!: boolE) |
23146 | 252 |
done |
253 |
||
254 |
(*Subtraction*) |
|
46820 | 255 |
lemma diff_integ_of_eq: |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
256 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
257 |
\<Longrightarrow> integ_of(v) $- integ_of(w) = integ_of(bin_add (v, bin_minus(w)))" |
76216
9fc34f76b4e8
getting rid of apply (unfold ...)
paulson <lp15@cam.ac.uk>
parents:
76215
diff
changeset
|
258 |
unfolding zdiff_def |
23146 | 259 |
apply (simp add: integ_of_add integ_of_minus) |
260 |
done |
|
261 |
||
262 |
||
69593 | 263 |
subsubsection\<open>\<^term>\<open>bin_mult\<close>: Binary Multiplication\<close> |
23146 | 264 |
|
265 |
lemma integ_of_mult: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
266 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
267 |
\<Longrightarrow> integ_of(bin_mult(v,w)) = integ_of(v) $* integ_of(w)" |
23146 | 268 |
apply (induct_tac "v", simp) |
269 |
apply (simp add: integ_of_minus) |
|
46820 | 270 |
apply (auto simp add: zadd_ac integ_of_add zadd_zmult_distrib elim!: boolE) |
23146 | 271 |
done |
272 |
||
273 |
||
60770 | 274 |
subsection\<open>Computations\<close> |
23146 | 275 |
|
276 |
(** extra rules for bin_succ, bin_pred **) |
|
277 |
||
278 |
lemma bin_succ_1: "bin_succ(w BIT 1) = bin_succ(w) BIT 0" |
|
279 |
by simp |
|
280 |
||
281 |
lemma bin_succ_0: "bin_succ(w BIT 0) = NCons(w,1)" |
|
282 |
by simp |
|
283 |
||
284 |
lemma bin_pred_1: "bin_pred(w BIT 1) = NCons(w,0)" |
|
285 |
by simp |
|
286 |
||
287 |
lemma bin_pred_0: "bin_pred(w BIT 0) = bin_pred(w) BIT 1" |
|
288 |
by simp |
|
289 |
||
290 |
(** extra rules for bin_minus **) |
|
291 |
||
292 |
lemma bin_minus_1: "bin_minus(w BIT 1) = bin_pred(NCons(bin_minus(w), 0))" |
|
293 |
by simp |
|
294 |
||
295 |
lemma bin_minus_0: "bin_minus(w BIT 0) = bin_minus(w) BIT 0" |
|
296 |
by simp |
|
297 |
||
298 |
(** extra rules for bin_add **) |
|
299 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
300 |
lemma bin_add_BIT_11: "w \<in> bin \<Longrightarrow> bin_add(v BIT 1, w BIT 1) = |
23146 | 301 |
NCons(bin_add(v, bin_succ(w)), 0)" |
302 |
by simp |
|
303 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
304 |
lemma bin_add_BIT_10: "w \<in> bin \<Longrightarrow> bin_add(v BIT 1, w BIT 0) = |
23146 | 305 |
NCons(bin_add(v,w), 1)" |
306 |
by simp |
|
307 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
308 |
lemma bin_add_BIT_0: "\<lbrakk>w \<in> bin; y \<in> bool\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
309 |
\<Longrightarrow> bin_add(v BIT 0, w BIT y) = NCons(bin_add(v,w), y)" |
23146 | 310 |
by simp |
311 |
||
312 |
(** extra rules for bin_mult **) |
|
313 |
||
314 |
lemma bin_mult_1: "bin_mult(v BIT 1, w) = bin_add(NCons(bin_mult(v,w),0), w)" |
|
315 |
by simp |
|
316 |
||
317 |
lemma bin_mult_0: "bin_mult(v BIT 0, w) = NCons(bin_mult(v,w),0)" |
|
318 |
by simp |
|
319 |
||
320 |
||
321 |
(** Simplification rules with integer constants **) |
|
322 |
||
323 |
lemma int_of_0: "$#0 = #0" |
|
324 |
by simp |
|
325 |
||
326 |
lemma int_of_succ: "$# succ(n) = #1 $+ $#n" |
|
327 |
by (simp add: int_of_add [symmetric] natify_succ) |
|
328 |
||
329 |
lemma zminus_0 [simp]: "$- #0 = #0" |
|
330 |
by simp |
|
331 |
||
332 |
lemma zadd_0_intify [simp]: "#0 $+ z = intify(z)" |
|
333 |
by simp |
|
334 |
||
335 |
lemma zadd_0_right_intify [simp]: "z $+ #0 = intify(z)" |
|
336 |
by simp |
|
337 |
||
338 |
lemma zmult_1_intify [simp]: "#1 $* z = intify(z)" |
|
339 |
by simp |
|
340 |
||
341 |
lemma zmult_1_right_intify [simp]: "z $* #1 = intify(z)" |
|
342 |
by (subst zmult_commute, simp) |
|
343 |
||
344 |
lemma zmult_0 [simp]: "#0 $* z = #0" |
|
345 |
by simp |
|
346 |
||
347 |
lemma zmult_0_right [simp]: "z $* #0 = #0" |
|
348 |
by (subst zmult_commute, simp) |
|
349 |
||
350 |
lemma zmult_minus1 [simp]: "#-1 $* z = $-z" |
|
351 |
by (simp add: zcompare_rls) |
|
352 |
||
353 |
lemma zmult_minus1_right [simp]: "z $* #-1 = $-z" |
|
354 |
apply (subst zmult_commute) |
|
355 |
apply (rule zmult_minus1) |
|
356 |
done |
|
357 |
||
358 |
||
60770 | 359 |
subsection\<open>Simplification Rules for Comparison of Binary Numbers\<close> |
360 |
text\<open>Thanks to Norbert Voelker\<close> |
|
23146 | 361 |
|
362 |
(** Equals (=) **) |
|
363 |
||
46820 | 364 |
lemma eq_integ_of_eq: |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
365 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
366 |
\<Longrightarrow> ((integ_of(v)) = integ_of(w)) \<longleftrightarrow> |
23146 | 367 |
iszero (integ_of (bin_add (v, bin_minus(w))))" |
76216
9fc34f76b4e8
getting rid of apply (unfold ...)
paulson <lp15@cam.ac.uk>
parents:
76215
diff
changeset
|
368 |
unfolding iszero_def |
23146 | 369 |
apply (simp add: zcompare_rls integ_of_add integ_of_minus) |
370 |
done |
|
371 |
||
372 |
lemma iszero_integ_of_Pls: "iszero (integ_of(Pls))" |
|
373 |
by (unfold iszero_def, simp) |
|
374 |
||
375 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
376 |
lemma nonzero_integ_of_Min: "\<not> iszero (integ_of(Min))" |
76216
9fc34f76b4e8
getting rid of apply (unfold ...)
paulson <lp15@cam.ac.uk>
parents:
76215
diff
changeset
|
377 |
unfolding iszero_def |
23146 | 378 |
apply (simp add: zminus_equation) |
379 |
done |
|
380 |
||
46820 | 381 |
lemma iszero_integ_of_BIT: |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
382 |
"\<lbrakk>w \<in> bin; x \<in> bool\<rbrakk> |
76214 | 383 |
\<Longrightarrow> iszero (integ_of (w BIT x)) \<longleftrightarrow> (x=0 \<and> iszero (integ_of(w)))" |
23146 | 384 |
apply (unfold iszero_def, simp) |
46820 | 385 |
apply (subgoal_tac "integ_of (w) \<in> int") |
23146 | 386 |
apply typecheck |
387 |
apply (drule int_cases) |
|
388 |
apply (safe elim!: boolE) |
|
389 |
apply (simp_all (asm_lr) add: zcompare_rls zminus_zadd_distrib [symmetric] |
|
390 |
int_of_add [symmetric]) |
|
391 |
done |
|
392 |
||
393 |
lemma iszero_integ_of_0: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
394 |
"w \<in> bin \<Longrightarrow> iszero (integ_of (w BIT 0)) \<longleftrightarrow> iszero (integ_of(w))" |
46820 | 395 |
by (simp only: iszero_integ_of_BIT, blast) |
23146 | 396 |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
397 |
lemma iszero_integ_of_1: "w \<in> bin \<Longrightarrow> \<not> iszero (integ_of (w BIT 1))" |
23146 | 398 |
by (simp only: iszero_integ_of_BIT, blast) |
399 |
||
400 |
||
401 |
||
402 |
(** Less-than (<) **) |
|
403 |
||
46820 | 404 |
lemma less_integ_of_eq_neg: |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
405 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
406 |
\<Longrightarrow> integ_of(v) $< integ_of(w) |
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
407 |
\<longleftrightarrow> znegative (integ_of (bin_add (v, bin_minus(w))))" |
76217 | 408 |
unfolding zless_def zdiff_def |
23146 | 409 |
apply (simp add: integ_of_minus integ_of_add) |
410 |
done |
|
411 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
412 |
lemma not_neg_integ_of_Pls: "\<not> znegative (integ_of(Pls))" |
23146 | 413 |
by simp |
414 |
||
415 |
lemma neg_integ_of_Min: "znegative (integ_of(Min))" |
|
416 |
by simp |
|
417 |
||
418 |
lemma neg_integ_of_BIT: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
419 |
"\<lbrakk>w \<in> bin; x \<in> bool\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
420 |
\<Longrightarrow> znegative (integ_of (w BIT x)) \<longleftrightarrow> znegative (integ_of(w))" |
23146 | 421 |
apply simp |
46820 | 422 |
apply (subgoal_tac "integ_of (w) \<in> int") |
23146 | 423 |
apply typecheck |
424 |
apply (drule int_cases) |
|
425 |
apply (auto elim!: boolE simp add: int_of_add [symmetric] zcompare_rls) |
|
46820 | 426 |
apply (simp_all add: zminus_zadd_distrib [symmetric] zdiff_def |
23146 | 427 |
int_of_add [symmetric]) |
428 |
apply (subgoal_tac "$#1 $- $# succ (succ (n #+ n)) = $- $# succ (n #+ n) ") |
|
429 |
apply (simp add: zdiff_def) |
|
430 |
apply (simp add: equation_zminus int_of_diff [symmetric]) |
|
431 |
done |
|
432 |
||
433 |
(** Less-than-or-equals (<=) **) |
|
434 |
||
435 |
lemma le_integ_of_eq_not_less: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
436 |
"(integ_of(x) $\<le> (integ_of(w))) \<longleftrightarrow> \<not> (integ_of(w) $< (integ_of(x)))" |
23146 | 437 |
by (simp add: not_zless_iff_zle [THEN iff_sym]) |
438 |
||
439 |
||
440 |
(*Delete the original rewrites, with their clumsy conditional expressions*) |
|
46820 | 441 |
declare bin_succ_BIT [simp del] |
442 |
bin_pred_BIT [simp del] |
|
23146 | 443 |
bin_minus_BIT [simp del] |
444 |
NCons_Pls [simp del] |
|
445 |
NCons_Min [simp del] |
|
446 |
bin_adder_BIT [simp del] |
|
447 |
bin_mult_BIT [simp del] |
|
448 |
||
449 |
(*Hide the binary representation of integer constants*) |
|
450 |
declare integ_of_Pls [simp del] integ_of_Min [simp del] integ_of_BIT [simp del] |
|
451 |
||
452 |
||
453 |
lemmas bin_arith_extra_simps = |
|
46820 | 454 |
integ_of_add [symmetric] |
455 |
integ_of_minus [symmetric] |
|
456 |
integ_of_mult [symmetric] |
|
457 |
bin_succ_1 bin_succ_0 |
|
458 |
bin_pred_1 bin_pred_0 |
|
459 |
bin_minus_1 bin_minus_0 |
|
23146 | 460 |
bin_add_Pls_right bin_add_Min_right |
461 |
bin_add_BIT_0 bin_add_BIT_10 bin_add_BIT_11 |
|
462 |
diff_integ_of_eq |
|
463 |
bin_mult_1 bin_mult_0 NCons_simps |
|
464 |
||
465 |
||
466 |
(*For making a minimal simpset, one must include these default simprules |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
467 |
of thy. Also include simp_thms, or at least (\<not>False)=True*) |
23146 | 468 |
lemmas bin_arith_simps = |
469 |
bin_pred_Pls bin_pred_Min |
|
470 |
bin_succ_Pls bin_succ_Min |
|
471 |
bin_add_Pls bin_add_Min |
|
472 |
bin_minus_Pls bin_minus_Min |
|
46820 | 473 |
bin_mult_Pls bin_mult_Min |
23146 | 474 |
bin_arith_extra_simps |
475 |
||
476 |
(*Simplification of relational operations*) |
|
477 |
lemmas bin_rel_simps = |
|
478 |
eq_integ_of_eq iszero_integ_of_Pls nonzero_integ_of_Min |
|
479 |
iszero_integ_of_0 iszero_integ_of_1 |
|
480 |
less_integ_of_eq_neg |
|
481 |
not_neg_integ_of_Pls neg_integ_of_Min neg_integ_of_BIT |
|
482 |
le_integ_of_eq_not_less |
|
483 |
||
484 |
declare bin_arith_simps [simp] |
|
485 |
declare bin_rel_simps [simp] |
|
486 |
||
487 |
||
488 |
(** Simplification of arithmetic when nested to the right **) |
|
489 |
||
490 |
lemma add_integ_of_left [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
491 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
492 |
\<Longrightarrow> integ_of(v) $+ (integ_of(w) $+ z) = (integ_of(bin_add(v,w)) $+ z)" |
23146 | 493 |
by (simp add: zadd_assoc [symmetric]) |
494 |
||
495 |
lemma mult_integ_of_left [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
496 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
497 |
\<Longrightarrow> integ_of(v) $* (integ_of(w) $* z) = (integ_of(bin_mult(v,w)) $* z)" |
23146 | 498 |
by (simp add: zmult_assoc [symmetric]) |
499 |
||
46820 | 500 |
lemma add_integ_of_diff1 [simp]: |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
501 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
502 |
\<Longrightarrow> integ_of(v) $+ (integ_of(w) $- c) = integ_of(bin_add(v,w)) $- (c)" |
76216
9fc34f76b4e8
getting rid of apply (unfold ...)
paulson <lp15@cam.ac.uk>
parents:
76215
diff
changeset
|
503 |
unfolding zdiff_def |
23146 | 504 |
apply (rule add_integ_of_left, auto) |
505 |
done |
|
506 |
||
507 |
lemma add_integ_of_diff2 [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
508 |
"\<lbrakk>v \<in> bin; w \<in> bin\<rbrakk> |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
509 |
\<Longrightarrow> integ_of(v) $+ (c $- integ_of(w)) = |
23146 | 510 |
integ_of (bin_add (v, bin_minus(w))) $+ (c)" |
511 |
apply (subst diff_integ_of_eq [symmetric]) |
|
512 |
apply (simp_all add: zdiff_def zadd_ac) |
|
513 |
done |
|
514 |
||
515 |
||
516 |
(** More for integer constants **) |
|
517 |
||
518 |
declare int_of_0 [simp] int_of_succ [simp] |
|
519 |
||
520 |
lemma zdiff0 [simp]: "#0 $- x = $-x" |
|
521 |
by (simp add: zdiff_def) |
|
522 |
||
523 |
lemma zdiff0_right [simp]: "x $- #0 = intify(x)" |
|
524 |
by (simp add: zdiff_def) |
|
525 |
||
526 |
lemma zdiff_self [simp]: "x $- x = #0" |
|
527 |
by (simp add: zdiff_def) |
|
528 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
529 |
lemma znegative_iff_zless_0: "k \<in> int \<Longrightarrow> znegative(k) \<longleftrightarrow> k $< #0" |
23146 | 530 |
by (simp add: zless_def) |
531 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
532 |
lemma zero_zless_imp_znegative_zminus: "\<lbrakk>#0 $< k; k \<in> int\<rbrakk> \<Longrightarrow> znegative($-k)" |
23146 | 533 |
by (simp add: zless_def) |
534 |
||
61395 | 535 |
lemma zero_zle_int_of [simp]: "#0 $\<le> $# n" |
23146 | 536 |
by (simp add: not_zless_iff_zle [THEN iff_sym] znegative_iff_zless_0 [THEN iff_sym]) |
537 |
||
538 |
lemma nat_of_0 [simp]: "nat_of(#0) = 0" |
|
539 |
by (simp only: natify_0 int_of_0 [symmetric] nat_of_int_of) |
|
540 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
541 |
lemma nat_le_int0_lemma: "\<lbrakk>z $\<le> $#0; z \<in> int\<rbrakk> \<Longrightarrow> nat_of(z) = 0" |
23146 | 542 |
by (auto simp add: znegative_iff_zless_0 [THEN iff_sym] zle_def zneg_nat_of) |
543 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
544 |
lemma nat_le_int0: "z $\<le> $#0 \<Longrightarrow> nat_of(z) = 0" |
23146 | 545 |
apply (subgoal_tac "nat_of (intify (z)) = 0") |
546 |
apply (rule_tac [2] nat_le_int0_lemma, auto) |
|
547 |
done |
|
548 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
549 |
lemma int_of_eq_0_imp_natify_eq_0: "$# n = #0 \<Longrightarrow> natify(n) = 0" |
23146 | 550 |
by (rule not_znegative_imp_zero, auto) |
551 |
||
552 |
lemma nat_of_zminus_int_of: "nat_of($- $# n) = 0" |
|
553 |
by (simp add: nat_of_def int_of_def raw_nat_of zminus image_intrel_int) |
|
554 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
555 |
lemma int_of_nat_of: "#0 $\<le> z \<Longrightarrow> $# nat_of(z) = intify(z)" |
23146 | 556 |
apply (rule not_zneg_nat_of_intify) |
557 |
apply (simp add: znegative_iff_zless_0 not_zless_iff_zle) |
|
558 |
done |
|
559 |
||
560 |
declare int_of_nat_of [simp] nat_of_zminus_int_of [simp] |
|
561 |
||
61395 | 562 |
lemma int_of_nat_of_if: "$# nat_of(z) = (if #0 $\<le> z then intify(z) else #0)" |
23146 | 563 |
by (simp add: int_of_nat_of znegative_iff_zless_0 not_zle_iff_zless) |
564 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
565 |
lemma zless_nat_iff_int_zless: "\<lbrakk>m \<in> nat; z \<in> int\<rbrakk> \<Longrightarrow> (m < nat_of(z)) \<longleftrightarrow> ($#m $< z)" |
23146 | 566 |
apply (case_tac "znegative (z) ") |
567 |
apply (erule_tac [2] not_zneg_nat_of [THEN subst]) |
|
568 |
apply (auto dest: zless_trans dest!: zero_zle_int_of [THEN zle_zless_trans] |
|
569 |
simp add: znegative_iff_zless_0) |
|
570 |
done |
|
571 |
||
572 |
||
573 |
(** nat_of and zless **) |
|
574 |
||
46820 | 575 |
(*An alternative condition is @{term"$#0 \<subseteq> w"} *) |
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
576 |
lemma zless_nat_conj_lemma: "$#0 $< z \<Longrightarrow> (nat_of(w) < nat_of(z)) \<longleftrightarrow> (w $< z)" |
23146 | 577 |
apply (rule iff_trans) |
578 |
apply (rule zless_int_of [THEN iff_sym]) |
|
579 |
apply (auto simp add: int_of_nat_of_if simp del: zless_int_of) |
|
580 |
apply (auto elim: zless_asym simp add: not_zle_iff_zless) |
|
581 |
apply (blast intro: zless_zle_trans) |
|
582 |
done |
|
583 |
||
76214 | 584 |
lemma zless_nat_conj: "(nat_of(w) < nat_of(z)) \<longleftrightarrow> ($#0 $< z \<and> w $< z)" |
23146 | 585 |
apply (case_tac "$#0 $< z") |
586 |
apply (auto simp add: zless_nat_conj_lemma nat_le_int0 not_zless_iff_zle) |
|
587 |
done |
|
588 |
||
589 |
(*This simprule cannot be added unless we can find a way to make eq_integ_of_eq |
|
590 |
unconditional! |
|
591 |
[The condition "True" is a hack to prevent looping. |
|
592 |
Conditional rewrite rules are tried after unconditional ones, so a rule |
|
593 |
like eq_nat_number_of will be tried first to eliminate #mm=#nn.] |
|
594 |
lemma integ_of_reorient [simp]: |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
595 |
"True \<Longrightarrow> (integ_of(w) = x) \<longleftrightarrow> (x = integ_of(w))" |
23146 | 596 |
by auto |
597 |
*) |
|
598 |
||
599 |
lemma integ_of_minus_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
600 |
"(integ_of(w) = $- x) \<longleftrightarrow> ($- x = integ_of(w))" |
23146 | 601 |
by auto |
602 |
||
603 |
lemma integ_of_add_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
604 |
"(integ_of(w) = x $+ y) \<longleftrightarrow> (x $+ y = integ_of(w))" |
23146 | 605 |
by auto |
606 |
||
607 |
lemma integ_of_diff_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
608 |
"(integ_of(w) = x $- y) \<longleftrightarrow> (x $- y = integ_of(w))" |
23146 | 609 |
by auto |
610 |
||
611 |
lemma integ_of_mult_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
612 |
"(integ_of(w) = x $* y) \<longleftrightarrow> (x $* y = integ_of(w))" |
23146 | 613 |
by auto |
614 |
||
58022 | 615 |
(** To simplify inequalities involving integer negation and literals, |
616 |
such as -x = #3 |
|
617 |
**) |
|
618 |
||
619 |
lemmas [simp] = |
|
620 |
zminus_equation [where y = "integ_of(w)"] |
|
621 |
equation_zminus [where x = "integ_of(w)"] |
|
622 |
for w |
|
623 |
||
624 |
lemmas [iff] = |
|
625 |
zminus_zless [where y = "integ_of(w)"] |
|
626 |
zless_zminus [where x = "integ_of(w)"] |
|
627 |
for w |
|
628 |
||
629 |
lemmas [iff] = |
|
630 |
zminus_zle [where y = "integ_of(w)"] |
|
631 |
zle_zminus [where x = "integ_of(w)"] |
|
632 |
for w |
|
633 |
||
634 |
lemmas [simp] = |
|
635 |
Let_def [where s = "integ_of(w)"] for w |
|
636 |
||
637 |
||
638 |
(*** Simprocs for numeric literals ***) |
|
639 |
||
640 |
(** Combining of literal coefficients in sums of products **) |
|
641 |
||
642 |
lemma zless_iff_zdiff_zless_0: "(x $< y) \<longleftrightarrow> (x$-y $< #0)" |
|
643 |
by (simp add: zcompare_rls) |
|
644 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
645 |
lemma eq_iff_zdiff_eq_0: "\<lbrakk>x \<in> int; y \<in> int\<rbrakk> \<Longrightarrow> (x = y) \<longleftrightarrow> (x$-y = #0)" |
58022 | 646 |
by (simp add: zcompare_rls) |
647 |
||
61395 | 648 |
lemma zle_iff_zdiff_zle_0: "(x $\<le> y) \<longleftrightarrow> (x$-y $\<le> #0)" |
58022 | 649 |
by (simp add: zcompare_rls) |
650 |
||
651 |
||
652 |
(** For combine_numerals **) |
|
653 |
||
654 |
lemma left_zadd_zmult_distrib: "i$*u $+ (j$*u $+ k) = (i$+j)$*u $+ k" |
|
655 |
by (simp add: zadd_zmult_distrib zadd_ac) |
|
656 |
||
657 |
||
658 |
(** For cancel_numerals **) |
|
659 |
||
660 |
lemma eq_add_iff1: "(i$*u $+ m = j$*u $+ n) \<longleftrightarrow> ((i$-j)$*u $+ m = intify(n))" |
|
661 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
|
662 |
apply (simp add: zcompare_rls) |
|
663 |
apply (simp add: zadd_ac) |
|
664 |
done |
|
665 |
||
666 |
lemma eq_add_iff2: "(i$*u $+ m = j$*u $+ n) \<longleftrightarrow> (intify(m) = (j$-i)$*u $+ n)" |
|
667 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
|
668 |
apply (simp add: zcompare_rls) |
|
669 |
apply (simp add: zadd_ac) |
|
670 |
done |
|
671 |
||
68233 | 672 |
context fixes n :: i |
673 |
begin |
|
674 |
||
675 |
lemmas rel_iff_rel_0_rls = |
|
676 |
zless_iff_zdiff_zless_0 [where y = "u $+ v"] |
|
677 |
eq_iff_zdiff_eq_0 [where y = "u $+ v"] |
|
678 |
zle_iff_zdiff_zle_0 [where y = "u $+ v"] |
|
679 |
zless_iff_zdiff_zless_0 [where y = n] |
|
680 |
eq_iff_zdiff_eq_0 [where y = n] |
|
681 |
zle_iff_zdiff_zle_0 [where y = n] |
|
682 |
for u v |
|
683 |
||
58022 | 684 |
lemma less_add_iff1: "(i$*u $+ m $< j$*u $+ n) \<longleftrightarrow> ((i$-j)$*u $+ m $< n)" |
685 |
apply (simp add: zdiff_def zadd_zmult_distrib zadd_ac rel_iff_rel_0_rls) |
|
686 |
done |
|
687 |
||
688 |
lemma less_add_iff2: "(i$*u $+ m $< j$*u $+ n) \<longleftrightarrow> (m $< (j$-i)$*u $+ n)" |
|
689 |
apply (simp add: zdiff_def zadd_zmult_distrib zadd_ac rel_iff_rel_0_rls) |
|
690 |
done |
|
691 |
||
68233 | 692 |
end |
693 |
||
61395 | 694 |
lemma le_add_iff1: "(i$*u $+ m $\<le> j$*u $+ n) \<longleftrightarrow> ((i$-j)$*u $+ m $\<le> n)" |
58022 | 695 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
696 |
apply (simp add: zcompare_rls) |
|
697 |
apply (simp add: zadd_ac) |
|
698 |
done |
|
699 |
||
61395 | 700 |
lemma le_add_iff2: "(i$*u $+ m $\<le> j$*u $+ n) \<longleftrightarrow> (m $\<le> (j$-i)$*u $+ n)" |
58022 | 701 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
702 |
apply (simp add: zcompare_rls) |
|
703 |
apply (simp add: zadd_ac) |
|
704 |
done |
|
705 |
||
69605 | 706 |
ML_file \<open>int_arith.ML\<close> |
58022 | 707 |
|
78791 | 708 |
simproc_setup inteq_cancel_numerals |
709 |
("l $+ m = n" | "l = m $+ n" | "l $- m = n" | "l = m $- n" | "l $* m = n" | "l = m $* n") = |
|
710 |
\<open>K Int_Numeral_Simprocs.inteq_cancel_numerals_proc\<close> |
|
711 |
||
712 |
simproc_setup intless_cancel_numerals |
|
713 |
("l $+ m $< n" | "l $< m $+ n" | "l $- m $< n" | "l $< m $- n" | "l $* m $< n" | "l $< m $* n") = |
|
714 |
\<open>K Int_Numeral_Simprocs.intless_cancel_numerals_proc\<close> |
|
715 |
||
716 |
simproc_setup intle_cancel_numerals |
|
717 |
("l $+ m $\<le> n" | "l $\<le> m $+ n" | "l $- m $\<le> n" | "l $\<le> m $- n" | "l $* m $\<le> n" | "l $\<le> m $* n") = |
|
718 |
\<open>K Int_Numeral_Simprocs.intle_cancel_numerals_proc\<close> |
|
719 |
||
720 |
simproc_setup int_combine_numerals ("i $+ j" | "i $- j") = |
|
721 |
\<open>K Int_Numeral_Simprocs.int_combine_numerals_proc\<close> |
|
722 |
||
723 |
simproc_setup int_combine_numerals_prod ("i $* j") = |
|
724 |
\<open>K Int_Numeral_Simprocs.int_combine_numerals_prod_proc\<close> |
|
725 |
||
726 |
||
727 |
subsubsection \<open>Examples\<close> |
|
59748 | 728 |
|
61798 | 729 |
text \<open>\<open>combine_numerals_prod\<close> (products of separate literals)\<close> |
59748 | 730 |
lemma "#5 $* x $* #3 = y" apply simp oops |
731 |
||
61337 | 732 |
schematic_goal "y2 $+ ?x42 = y $+ y2" apply simp oops |
59748 | 733 |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
734 |
lemma "oo : int \<Longrightarrow> l $+ (l $+ #2) $+ oo = oo" apply simp oops |
59748 | 735 |
|
736 |
lemma "#9$*x $+ y = x$*#23 $+ z" apply simp oops |
|
737 |
lemma "y $+ x = x $+ z" apply simp oops |
|
738 |
||
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
739 |
lemma "x : int \<Longrightarrow> x $+ y $+ z = x $+ z" apply simp oops |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
740 |
lemma "x : int \<Longrightarrow> y $+ (z $+ x) = z $+ x" apply simp oops |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
741 |
lemma "z : int \<Longrightarrow> x $+ y $+ z = (z $+ y) $+ (x $+ w)" apply simp oops |
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
742 |
lemma "z : int \<Longrightarrow> x$*y $+ z = (z $+ y) $+ (y$*x $+ w)" apply simp oops |
59748 | 743 |
|
61395 | 744 |
lemma "#-3 $* x $+ y $\<le> x $* #2 $+ z" apply simp oops |
745 |
lemma "y $+ x $\<le> x $+ z" apply simp oops |
|
746 |
lemma "x $+ y $+ z $\<le> x $+ z" apply simp oops |
|
59748 | 747 |
|
748 |
lemma "y $+ (z $+ x) $< z $+ x" apply simp oops |
|
749 |
lemma "x $+ y $+ z $< (z $+ y) $+ (x $+ w)" apply simp oops |
|
750 |
lemma "x$*y $+ z $< (z $+ y) $+ (y$*x $+ w)" apply simp oops |
|
751 |
||
752 |
lemma "l $+ #2 $+ #2 $+ #2 $+ (l $+ #2) $+ (oo $+ #2) = uu" apply simp oops |
|
76213
e44d86131648
Removal of obsolete ASCII syntax
paulson <lp15@cam.ac.uk>
parents:
71546
diff
changeset
|
753 |
lemma "u : int \<Longrightarrow> #2 $* u = u" apply simp oops |
59748 | 754 |
lemma "(i $+ j $+ #12 $+ k) $- #15 = y" apply simp oops |
755 |
lemma "(i $+ j $+ #12 $+ k) $- #5 = y" apply simp oops |
|
756 |
||
757 |
lemma "y $- b $< b" apply simp oops |
|
758 |
lemma "y $- (#3 $* b $+ c) $< b $- #2 $* c" apply simp oops |
|
759 |
||
760 |
lemma "(#2 $* x $- (u $* v) $+ y) $- v $* #3 $* u = w" apply simp oops |
|
761 |
lemma "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u $* #4 = w" apply simp oops |
|
762 |
lemma "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u = w" apply simp oops |
|
763 |
lemma "u $* v $- (x $* u $* v $+ (u $* v) $* #4 $+ y) = w" apply simp oops |
|
764 |
||
765 |
lemma "(i $+ j $+ #12 $+ k) = u $+ #15 $+ y" apply simp oops |
|
766 |
lemma "(i $+ j $* #2 $+ #12 $+ k) = j $+ #5 $+ y" apply simp oops |
|
767 |
||
768 |
lemma "#2 $* y $+ #3 $* z $+ #6 $* w $+ #2 $* y $+ #3 $* z $+ #2 $* u = #2 $* y' $+ #3 $* z' $+ #6 $* w' $+ #2 $* y' $+ #3 $* z' $+ u $+ vv" apply simp oops |
|
769 |
||
770 |
lemma "a $+ $-(b$+c) $+ b = d" apply simp oops |
|
771 |
lemma "a $+ $-(b$+c) $- b = d" apply simp oops |
|
772 |
||
60770 | 773 |
text \<open>negative numerals\<close> |
59748 | 774 |
lemma "(i $+ j $+ #-2 $+ k) $- (u $+ #5 $+ y) = zz" apply simp oops |
775 |
lemma "(i $+ j $+ #-3 $+ k) $< u $+ #5 $+ y" apply simp oops |
|
776 |
lemma "(i $+ j $+ #3 $+ k) $< u $+ #-6 $+ y" apply simp oops |
|
777 |
lemma "(i $+ j $+ #-12 $+ k) $- #15 = y" apply simp oops |
|
778 |
lemma "(i $+ j $+ #12 $+ k) $- #-15 = y" apply simp oops |
|
779 |
lemma "(i $+ j $+ #-12 $+ k) $- #-15 = y" apply simp oops |
|
780 |
||
60770 | 781 |
text \<open>Multiplying separated numerals\<close> |
59748 | 782 |
lemma "#6 $* ($# x $* #2) = uu" apply simp oops |
783 |
lemma "#4 $* ($# x $* $# x) $* (#2 $* $# x) = uu" apply simp oops |
|
784 |
||
23146 | 785 |
end |