# Theory BinEx

theory BinEx
imports Complex_Main
(*  Title:      HOL/ex/BinEx.thy    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory    Copyright   1998  University of Cambridge*)header {* Binary arithmetic examples *}theory BinEximports Complex_Mainbeginsubsection {* Regression Testing for Cancellation Simprocs *}lemma "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)"apply simp  oopslemma "2*u = (u::int)"apply simp  oopslemma "(i + j + 12 + (k::int)) - 15 = y"apply simp  oopslemma "(i + j + 12 + (k::int)) - 5 = y"apply simp  oopslemma "y - b < (b::int)"apply simp  oopslemma "y - (3*b + c) < (b::int) - 2*c"apply simp  oopslemma "(2*x - (u*v) + y) - v*3*u = (w::int)"apply simp  oopslemma "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)"apply simp  oopslemma "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)"apply simp  oopslemma "u*v - (x*u*v + (u*v)*4 + y) = (w::int)"apply simp  oopslemma "(i + j + 12 + (k::int)) = u + 15 + y"apply simp  oopslemma "(i + j*2 + 12 + (k::int)) = j + 5 + y"apply simp  oopslemma "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = 2*y' + 3*z' + 6*w' + 2*y' + 3*z' + u + (vv::int)"apply simp  oopslemma "a + -(b+c) + b = (d::int)"apply simp  oopslemma "a + -(b+c) - b = (d::int)"apply simp  oops(*negative numerals*)lemma "(i + j + -2 + (k::int)) - (u + 5 + y) = zz"apply simp  oopslemma "(i + j + -3 + (k::int)) < u + 5 + y"apply simp  oopslemma "(i + j + 3 + (k::int)) < u + -6 + y"apply simp  oopslemma "(i + j + -12 + (k::int)) - 15 = y"apply simp  oopslemma "(i + j + 12 + (k::int)) - -15 = y"apply simp  oopslemma "(i + j + -12 + (k::int)) - -15 = y"apply simp  oopslemma "- (2*i) + 3  + (2*i + 4) = (0::int)"apply simp  oopssubsection {* Arithmetic Method Tests *}lemma "!!a::int. [| a <= b; c <= d; x+y<z |] ==> a+c <= b+d"by arithlemma "!!a::int. [| a < b; c < d |] ==> a-d+ 2 <= b+(-c)"by arithlemma "!!a::int. [| a < b; c < d |] ==> a+c+ 1 < b+d"by arithlemma "!!a::int. [| a <= b; b+b <= c |] ==> a+a <= c"by arithlemma "!!a::int. [| a+b <= i+j; a<=b; i<=j |] ==> a+a <= j+j"by arithlemma "!!a::int. [| a+b < i+j; a<b; i<j |] ==> a+a - - -1 < j+j - 3"by arithlemma "!!a::int. a+b+c <= i+j+k & a<=b & b<=c & i<=j & j<=k --> a+a+a <= k+k+k"by arithlemma "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |]      ==> a <= l"by arithlemma "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |]      ==> a+a+a+a <= l+l+l+l"by arithlemma "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |]      ==> a+a+a+a+a <= l+l+l+l+i"by arithlemma "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |]      ==> a+a+a+a+a+a <= l+l+l+l+i+l"by arithlemma "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |]      ==> 6*a <= 5*l+i"by arithsubsection {* The Integers *}text {* Addition *}lemma "(13::int) + 19 = 32"  by simplemma "(1234::int) + 5678 = 6912"  by simplemma "(1359::int) + -2468 = -1109"  by simplemma "(93746::int) + -46375 = 47371"  by simptext {* \medskip Negation *}lemma "- (65745::int) = -65745"  by simplemma "- (-54321::int) = 54321"  by simptext {* \medskip Multiplication *}lemma "(13::int) * 19 = 247"  by simplemma "(-84::int) * 51 = -4284"  by simplemma "(255::int) * 255 = 65025"  by simplemma "(1359::int) * -2468 = -3354012"  by simplemma "(89::int) * 10 ≠ 889"  by simplemma "(13::int) < 18 - 4"  by simplemma "(-345::int) < -242 + -100"  by simplemma "(13557456::int) < 18678654"  by simplemma "(999999::int) ≤ (1000001 + 1) - 2"  by simplemma "(1234567::int) ≤ 1234567"  by simptext{*No integer overflow!*}lemma "1234567 * (1234567::int) < 1234567*1234567*1234567"  by simptext {* \medskip Quotient and Remainder *}lemma "(10::int) div 3 = 3"  by simplemma "(10::int) mod 3 = 1"  by simptext {* A negative divisor *}lemma "(10::int) div -3 = -4"  by simplemma "(10::int) mod -3 = -2"  by simptext {*  A negative dividend\footnote{The definition agrees with mathematical  convention and with ML, but not with the hardware of most computers}*}lemma "(-10::int) div 3 = -4"  by simplemma "(-10::int) mod 3 = 2"  by simptext {* A negative dividend \emph{and} divisor *}lemma "(-10::int) div -3 = 3"  by simplemma "(-10::int) mod -3 = -1"  by simptext {* A few bigger examples *}lemma "(8452::int) mod 3 = 1"  by simplemma "(59485::int) div 434 = 137"  by simplemma "(1000006::int) mod 10 = 6"  by simptext {* \medskip Division by shifting *}lemma "10000000 div 2 = (5000000::int)"  by simplemma "10000001 mod 2 = (1::int)"  by simplemma "10000055 div 32 = (312501::int)"  by simplemma "10000055 mod 32 = (23::int)"  by simplemma "100094 div 144 = (695::int)"  by simplemma "100094 mod 144 = (14::int)"  by simptext {* \medskip Powers *}lemma "2 ^ 10 = (1024::int)"  by simplemma "-3 ^ 7 = (-2187::int)"  by simplemma "13 ^ 7 = (62748517::int)"  by simplemma "3 ^ 15 = (14348907::int)"  by simplemma "-5 ^ 11 = (-48828125::int)"  by simpsubsection {* The Natural Numbers *}text {* Successor *}lemma "Suc 99999 = 100000"  by simptext {* \medskip Addition *}lemma "(13::nat) + 19 = 32"  by simplemma "(1234::nat) + 5678 = 6912"  by simplemma "(973646::nat) + 6475 = 980121"  by simptext {* \medskip Subtraction *}lemma "(32::nat) - 14 = 18"  by simplemma "(14::nat) - 15 = 0"  by simplemma "(14::nat) - 1576644 = 0"  by simplemma "(48273776::nat) - 3873737 = 44400039"  by simptext {* \medskip Multiplication *}lemma "(12::nat) * 11 = 132"  by simplemma "(647::nat) * 3643 = 2357021"  by simptext {* \medskip Quotient and Remainder *}lemma "(10::nat) div 3 = 3"  by simplemma "(10::nat) mod 3 = 1"  by simplemma "(10000::nat) div 9 = 1111"  by simplemma "(10000::nat) mod 9 = 1"  by simplemma "(10000::nat) div 16 = 625"  by simplemma "(10000::nat) mod 16 = 0"  by simptext {* \medskip Powers *}lemma "2 ^ 12 = (4096::nat)"  by simplemma "3 ^ 10 = (59049::nat)"  by simplemma "12 ^ 7 = (35831808::nat)"  by simplemma "3 ^ 14 = (4782969::nat)"  by simplemma "5 ^ 11 = (48828125::nat)"  by simptext {* \medskip Testing the cancellation of complementary terms *}lemma "y + (x + -x) = (0::int) + y"  by simplemma "y + (-x + (- y + x)) = (0::int)"  by simplemma "-x + (y + (- y + x)) = (0::int)"  by simplemma "x + (x + (- x + (- x + (- y + - z)))) = (0::int) - y - z"  by simplemma "x + x - x - x - y - z = (0::int) - y - z"  by simplemma "x + y + z - (x + z) = y - (0::int)"  by simplemma "x + (y + (y + (y + (-x + -x)))) = (0::int) + y - x + y + y"  by simplemma "x + (y + (y + (y + (-y + -x)))) = y + (0::int) + y"  by simplemma "x + y - x + z - x - y - z + x < (1::int)"  by simpsubsection{*Real Arithmetic*}subsubsection {*Addition *}lemma "(1359::real) + -2468 = -1109"by simplemma "(93746::real) + -46375 = 47371"by simpsubsubsection {*Negation *}lemma "- (65745::real) = -65745"by simplemma "- (-54321::real) = 54321"by simpsubsubsection {*Multiplication *}lemma "(-84::real) * 51 = -4284"by simplemma "(255::real) * 255 = 65025"by simplemma "(1359::real) * -2468 = -3354012"by simpsubsubsection {*Inequalities *}lemma "(89::real) * 10 ≠ 889"by simplemma "(13::real) < 18 - 4"by simplemma "(-345::real) < -242 + -100"by simplemma "(13557456::real) < 18678654"by simplemma "(999999::real) ≤ (1000001 + 1) - 2"by simplemma "(1234567::real) ≤ 1234567"by simpsubsubsection {*Powers *}lemma "2 ^ 15 = (32768::real)"by simplemma "-3 ^ 7 = (-2187::real)"by simplemma "13 ^ 7 = (62748517::real)"by simplemma "3 ^ 15 = (14348907::real)"by simplemma "-5 ^ 11 = (-48828125::real)"by simpsubsubsection {*Tests *}lemma "(x + y = x) = (y = (0::real))"by arithlemma "(x + y = y) = (x = (0::real))"by arithlemma "(x + y = (0::real)) = (x = -y)"by arithlemma "(x + y = (0::real)) = (y = -x)"by arithlemma "((x + y) < (x + z)) = (y < (z::real))"by arithlemma "((x + z) < (y + z)) = (x < (y::real))"by arithlemma "(¬ x < y) = (y ≤ (x::real))"by arithlemma "¬ (x < y ∧ y < (x::real))"by arithlemma "(x::real) < y ==> ¬ y < x"by arithlemma "((x::real) ≠ y) = (x < y ∨ y < x)"by arithlemma "(¬ x ≤ y) = (y < (x::real))"by arithlemma "x ≤ y ∨ y ≤ (x::real)"by arithlemma "x ≤ y ∨ y < (x::real)"by arithlemma "x < y ∨ y ≤ (x::real)"by arithlemma "x ≤ (x::real)"by arithlemma "((x::real) ≤ y) = (x < y ∨ x = y)"by arithlemma "((x::real) ≤ y ∧ y ≤ x) = (x = y)"by arithlemma "¬(x < y ∧ y ≤ (x::real))"by arithlemma "¬(x ≤ y ∧ y < (x::real))"by arithlemma "(-x < (0::real)) = (0 < x)"by arithlemma "((0::real) < -x) = (x < 0)"by arithlemma "(-x ≤ (0::real)) = (0 ≤ x)"by arithlemma "((0::real) ≤ -x) = (x ≤ 0)"by arithlemma "(x::real) = y ∨ x < y ∨ y < x"by arithlemma "(x::real) = 0 ∨ 0 < x ∨ 0 < -x"by arithlemma "(0::real) ≤ x ∨ 0 ≤ -x"by arithlemma "((x::real) + y ≤ x + z) = (y ≤ z)"by arithlemma "((x::real) + z ≤ y + z) = (x ≤ y)"by arithlemma "(w::real) < x ∧ y < z ==> w + y < x + z"by arithlemma "(w::real) ≤ x ∧ y ≤ z ==> w + y ≤ x + z"by arithlemma "(0::real) ≤ x ∧ 0 ≤ y ==> 0 ≤ x + y"by arithlemma "(0::real) < x ∧ 0 < y ==> 0 < x + y"by arithlemma "(-x < y) = (0 < x + (y::real))"by arithlemma "(x < -y) = (x + y < (0::real))"by arithlemma "(y < x + -z) = (y + z < (x::real))"by arithlemma "(x + -y < z) = (x < z + (y::real))"by arithlemma "x ≤ y ==> x < y + (1::real)"by arithlemma "(x - y) + y = (x::real)"by arithlemma "y + (x - y) = (x::real)"by arithlemma "x - x = (0::real)"by arithlemma "(x - y = 0) = (x = (y::real))"by arithlemma "((0::real) ≤ x + x) = (0 ≤ x)"by arithlemma "(-x ≤ x) = ((0::real) ≤ x)"by arithlemma "(x ≤ -x) = (x ≤ (0::real))"by arithlemma "(-x = (0::real)) = (x = 0)"by arithlemma "-(x - y) = y - (x::real)"by arithlemma "((0::real) < x - y) = (y < x)"by arithlemma "((0::real) ≤ x - y) = (y ≤ x)"by arithlemma "(x + y) - x = (y::real)"by arithlemma "(-x = y) = (x = (-y::real))"by arithlemma "x < (y::real) ==> ¬(x = y)"by arithlemma "(x ≤ x + y) = ((0::real) ≤ y)"by arithlemma "(y ≤ x + y) = ((0::real) ≤ x)"by arithlemma "(x < x + y) = ((0::real) < y)"by arithlemma "(y < x + y) = ((0::real) < x)"by arithlemma "(x - y) - x = (-y::real)"by arithlemma "(x + y < z) = (x < z - (y::real))"by arithlemma "(x - y < z) = (x < z + (y::real))"by arithlemma "(x < y - z) = (x + z < (y::real))"by arithlemma "(x ≤ y - z) = (x + z ≤ (y::real))"by arithlemma "(x - y ≤ z) = (x ≤ z + (y::real))"by arithlemma "(-x < -y) = (y < (x::real))"by arithlemma "(-x ≤ -y) = (y ≤ (x::real))"by arithlemma "(a + b) - (c + d) = (a - c) + (b - (d::real))"by arithlemma "(0::real) - x = -x"by arithlemma "x - (0::real) = x"by arithlemma "w ≤ x ∧ y < z ==> w + y < x + (z::real)"by arithlemma "w < x ∧ y ≤ z ==> w + y < x + (z::real)"by arithlemma "(0::real) ≤ x ∧ 0 < y ==> 0 < x + (y::real)"by arithlemma "(0::real) < x ∧ 0 ≤ y ==> 0 < x + y"by arithlemma "-x - y = -(x + (y::real))"by arithlemma "x - (-y) = x + (y::real)"by arithlemma "-x - -y = y - (x::real)"by arithlemma "(a - b) + (b - c) = a - (c::real)"by arithlemma "(x = y - z) = (x + z = (y::real))"by arithlemma "(x - y = z) = (x = z + (y::real))"by arithlemma "x - (x - y) = (y::real)"by arithlemma "x - (x + y) = -(y::real)"by arithlemma "x = y ==> x ≤ (y::real)"by arithlemma "(0::real) < x ==> ¬(x = 0)"by arithlemma "(x + y) * (x - y) = (x * x) - (y * y)"  oopslemma "(-x = -y) = (x = (y::real))"by arithlemma "(-x < -y) = (y < (x::real))"by arithlemma "!!a::real. a ≤ b ==> c ≤ d ==> x + y < z ==> a + c ≤ b + d"by linarithlemma "!!a::real. a < b ==> c < d ==> a - d ≤ b + (-c)"by linarithlemma "!!a::real. a ≤ b ==> b + b ≤ c ==> a + a ≤ c"by linarithlemma "!!a::real. a + b ≤ i + j ==> a ≤ b ==> i ≤ j ==> a + a ≤ j + j"by linarithlemma "!!a::real. a + b < i + j ==> a < b ==> i < j ==> a + a < j + j"by linarithlemma "!!a::real. a + b + c ≤ i + j + k ∧ a ≤ b ∧ b ≤ c ∧ i ≤ j ∧ j ≤ k --> a + a + a ≤ k + k + k"by arithlemma "!!a::real. a + b + c + d ≤ i + j + k + l ==> a ≤ b ==> b ≤ c    ==> c ≤ d ==> i ≤ j ==> j ≤ k ==> k ≤ l ==> a ≤ l"by linarithlemma "!!a::real. a + b + c + d ≤ i + j + k + l ==> a ≤ b ==> b ≤ c    ==> c ≤ d ==> i ≤ j ==> j ≤ k ==> k ≤ l ==> a + a + a + a ≤ l + l + l + l"by linarithlemma "!!a::real. a + b + c + d ≤ i + j + k + l ==> a ≤ b ==> b ≤ c    ==> c ≤ d ==> i ≤ j ==> j ≤ k ==> k ≤ l ==> a + a + a + a + a ≤ l + l + l + l + i"by linarithlemma "!!a::real. a + b + c + d ≤ i + j + k + l ==> a ≤ b ==> b ≤ c    ==> c ≤ d ==> i ≤ j ==> j ≤ k ==> k ≤ l ==> a + a + a + a + a + a ≤ l + l + l + l + i + l"by linarithsubsection{*Complex Arithmetic*}lemma "(1359 + 93746*ii) - (2468 + 46375*ii) = -1109 + 47371*ii"by simplemma "- (65745 + -47371*ii) = -65745 + 47371*ii"by simptext{*Multiplication requires distributive laws.  Perhaps versions instantiatedto literal constants should be added to the simpset.*}lemma "(1 + ii) * (1 - ii) = 2"by (simp add: ring_distribs)lemma "(1 + 2*ii) * (1 + 3*ii) = -5 + 5*ii"by (simp add: ring_distribs)lemma "(-84 + 255*ii) + (51 * 255*ii) = -84 + 13260 * ii"by (simp add: ring_distribs)text{*No inequalities or linear arithmetic: the complex numbers are unordered!*}text{*No powers (not supported yet)*}end