:: Binary Arithmetics. Addition
:: by Takaya Nishiyama and Yasuho Mizuhara
::
:: Received October 8, 1993
:: Copyright (c) 1993 Association of Mizar Users

:: BINARITH semantic presentation

definition
let i be Nat;
let D be non empty set ;
mode Tuple of i,D is Element of i -tuples_on D;
end;

theorem :: BINARITH:1
canceled;

theorem Th2: :: BINARITH:2
for i, n being Nat
for D being non empty set
for d being Element of D
for z being Tuple of n,D st i in Seg n holds
(z ^ <*d*>) /. i = z /. i
proof end;

theorem Th3: :: BINARITH:3
for n being Nat
for D being non empty set
for d being Element of D
for z being Tuple of n,D holds (z ^ <*d*>) /. (n + 1) = d
proof end;

registration
let x, y be boolean set ;
cluster x 'xor' y -> boolean ;
correctness
coherence
x 'xor' y is boolean
;
;
end;

definition
let x, y be Element of BOOLEAN ;
:: original: 'or'
redefine func x 'or' y -> Element of BOOLEAN ;
correctness
coherence
x 'or' y is Element of BOOLEAN
;
proof end;
:: original: 'xor'
redefine func x 'xor' y -> Element of BOOLEAN ;
correctness
coherence
x 'xor' y is Element of BOOLEAN
;
proof end;
end;

theorem :: BINARITH:4
canceled;

theorem :: BINARITH:5
canceled;

theorem :: BINARITH:6
canceled;

theorem :: BINARITH:7
for x being boolean set holds x 'or' FALSE = x ;

theorem :: BINARITH:8
canceled;

theorem :: BINARITH:9
for x, y being boolean set holds 'not' (x '&' y) = ('not' x) 'or' ('not' y) ;

theorem :: BINARITH:10
for x, y being boolean set holds 'not' (x 'or' y) = ('not' x) '&' ('not' y) ;

theorem :: BINARITH:11
canceled;

theorem :: BINARITH:12
for x, y being boolean set holds x '&' y = 'not' (('not' x) 'or' ('not' y)) ;

theorem :: BINARITH:13
for x being boolean set holds TRUE 'xor' x = 'not' x ;

theorem :: BINARITH:14
for x being boolean set holds FALSE 'xor' x = x ;

theorem :: BINARITH:15
canceled;

theorem :: BINARITH:16
for x being boolean set holds x '&' x = x ;

theorem :: BINARITH:17
canceled;

theorem :: BINARITH:18
canceled;

theorem :: BINARITH:19
for x being boolean set holds x 'or' TRUE = TRUE ;

theorem :: BINARITH:20
for x, y, z being boolean set holds (x 'or' y) 'or' z = x 'or' (y 'or' z) ;

theorem :: BINARITH:21
for x being boolean set holds x 'or' x = x ;

theorem :: BINARITH:22
canceled;

theorem :: BINARITH:23
canceled;

theorem :: BINARITH:24
canceled;

theorem :: BINARITH:25
canceled;

theorem :: BINARITH:26
canceled;

theorem :: BINARITH:27
canceled;

theorem :: BINARITH:28
canceled;

theorem :: BINARITH:29
canceled;

theorem :: BINARITH:30
canceled;

theorem :: BINARITH:31
canceled;

theorem :: BINARITH:32
canceled;

theorem :: BINARITH:33
TRUE 'xor' FALSE = TRUE ;

definition
let i, j be natural number ;
canceled;
canceled;
func i -' j -> Element of NAT equals :Def3: :: BINARITH:def 3
i - j if i - j >= 0
otherwise 0 ;
coherence
( ( i - j >= 0 implies i - j is Element of NAT ) & ( not i - j >= 0 implies 0 is Element of NAT ) )
by INT_1:16;
correctness
consistency
for b1 being Element of NAT holds verum
;
;
end;

:: deftheorem BINARITH:def 1 :
canceled;

:: deftheorem BINARITH:def 2 :
canceled;

:: deftheorem Def3 defines -' BINARITH:def 3 :
for i, j being natural number holds
( ( i - j >= 0 implies i -' j = i - j ) & ( not i - j >= 0 implies i -' j = 0 ) );

theorem :: BINARITH:34
canceled;

theorem :: BINARITH:35
canceled;

theorem :: BINARITH:36
canceled;

theorem :: BINARITH:37
canceled;

theorem :: BINARITH:38
canceled;

theorem Th39: :: BINARITH:39
for i, j being natural number holds (i + j) -' j = i
proof end;

definition
let n be Nat;
let x be Tuple of n,BOOLEAN ;
func 'not' x -> Tuple of n,BOOLEAN means :: BINARITH:def 4
for i being Nat st i in Seg n holds
it /. i = 'not' (x /. i);
existence
ex b1 being Tuple of n,BOOLEAN st
for i being Nat st i in Seg n holds
b1 /. i = 'not' (x /. i)
proof end;
uniqueness
for b1, b2 being Tuple of n,BOOLEAN st ( for i being Nat st i in Seg n holds
b1 /. i = 'not' (x /. i) ) & ( for i being Nat st i in Seg n holds
b2 /. i = 'not' (x /. i) ) holds
b1 = b2
proof end;
end;

:: deftheorem defines 'not' BINARITH:def 4 :
for n being Nat
for x, b3 being Tuple of n,BOOLEAN holds
( b3 = 'not' x iff for i being Nat st i in Seg n holds
b3 /. i = 'not' (x /. i) );

definition
let n be non empty Nat;
let x, y be Tuple of n,BOOLEAN ;
func carry x,y -> Tuple of n,BOOLEAN means :Def5: :: BINARITH:def 5
( it /. 1 = FALSE & ( for i being Nat st 1 <= i & i < n holds
it /. (i + 1) = (((x /. i) '&' (y /. i)) 'or' ((x /. i) '&' (it /. i))) 'or' ((y /. i) '&' (it /. i)) ) );
existence
ex b1 being Tuple of n,BOOLEAN st
( b1 /. 1 = FALSE & ( for i being Nat st 1 <= i & i < n holds
b1 /. (i + 1) = (((x /. i) '&' (y /. i)) 'or' ((x /. i) '&' (b1 /. i))) 'or' ((y /. i) '&' (b1 /. i)) ) )
proof end;
uniqueness
for b1, b2 being Tuple of n,BOOLEAN st b1 /. 1 = FALSE & ( for i being Nat st 1 <= i & i < n holds
b1 /. (i + 1) = (((x /. i) '&' (y /. i)) 'or' ((x /. i) '&' (b1 /. i))) 'or' ((y /. i) '&' (b1 /. i)) ) & b2 /. 1 = FALSE & ( for i being Nat st 1 <= i & i < n holds
b2 /. (i + 1) = (((x /. i) '&' (y /. i)) 'or' ((x /. i) '&' (b2 /. i))) 'or' ((y /. i) '&' (b2 /. i)) ) holds
b1 = b2
proof end;
end;

:: deftheorem Def5 defines carry BINARITH:def 5 :
for n being non empty Nat
for x, y, b4 being Tuple of n,BOOLEAN holds
( b4 = carry x,y iff ( b4 /. 1 = FALSE & ( for i being Nat st 1 <= i & i < n holds
b4 /. (i + 1) = (((x /. i) '&' (y /. i)) 'or' ((x /. i) '&' (b4 /. i))) 'or' ((y /. i) '&' (b4 /. i)) ) ) );

definition
let n be Nat;
let x be Tuple of n,BOOLEAN ;
func Binary x -> Tuple of n,NAT means :Def6: :: BINARITH:def 6
for i being Nat st i in Seg n holds
it /. i = IFEQ (x /. i),FALSE ,0 ,(2 to_power (i -' 1));
existence
ex b1 being Tuple of n,NAT st
for i being Nat st i in Seg n holds
b1 /. i = IFEQ (x /. i),FALSE ,0 ,(2 to_power (i -' 1))
proof end;
uniqueness
for b1, b2 being Tuple of n,NAT st ( for i being Nat st i in Seg n holds
b1 /. i = IFEQ (x /. i),FALSE ,0 ,(2 to_power (i -' 1)) ) & ( for i being Nat st i in Seg n holds
b2 /. i = IFEQ (x /. i),FALSE ,0 ,(2 to_power (i -' 1)) ) holds
b1 = b2
proof end;
end;

:: deftheorem Def6 defines Binary BINARITH:def 6 :
for n being Nat
for x being Tuple of n,BOOLEAN
for b3 being Tuple of n,NAT holds
( b3 = Binary x iff for i being Nat st i in Seg n holds
b3 /. i = IFEQ (x /. i),FALSE ,0 ,(2 to_power (i -' 1)) );

definition
let n be Nat;
let x be Tuple of n,BOOLEAN ;
func Absval x -> Element of NAT equals :: BINARITH:def 7
addnat $$ (Binary x);
correctness
coherence
addnat $$ (Binary x) is Element of NAT
;
;
end;

:: deftheorem defines Absval BINARITH:def 7 :
for n being Nat
for x being Tuple of n,BOOLEAN holds Absval x = addnat $$ (Binary x);

definition
let n be non empty Nat;
let x, y be Tuple of n,BOOLEAN ;
func x + y -> Tuple of n,BOOLEAN means :Def8: :: BINARITH:def 8
for i being Nat st i in Seg n holds
it /. i = ((x /. i) 'xor' (y /. i)) 'xor' ((carry x,y) /. i);
existence
ex b1 being Tuple of n,BOOLEAN st
for i being Nat st i in Seg n holds
b1 /. i = ((x /. i) 'xor' (y /. i)) 'xor' ((carry x,y) /. i)
proof end;
uniqueness
for b1, b2 being Tuple of n,BOOLEAN st ( for i being Nat st i in Seg n holds
b1 /. i = ((x /. i) 'xor' (y /. i)) 'xor' ((carry x,y) /. i) ) & ( for i being Nat st i in Seg n holds
b2 /. i = ((x /. i) 'xor' (y /. i)) 'xor' ((carry x,y) /. i) ) holds
b1 = b2
proof end;
end;

:: deftheorem Def8 defines + BINARITH:def 8 :
for n being non empty Nat
for x, y, b4 being Tuple of n,BOOLEAN holds
( b4 = x + y iff for i being Nat st i in Seg n holds
b4 /. i = ((x /. i) 'xor' (y /. i)) 'xor' ((carry x,y) /. i) );

definition
let n be non empty Nat;
let z1, z2 be Tuple of n,BOOLEAN ;
func add_ovfl z1,z2 -> Element of BOOLEAN equals :: BINARITH:def 9
(((z1 /. n) '&' (z2 /. n)) 'or' ((z1 /. n) '&' ((carry z1,z2) /. n))) 'or' ((z2 /. n) '&' ((carry z1,z2) /. n));
correctness
coherence
(((z1 /. n) '&' (z2 /. n)) 'or' ((z1 /. n) '&' ((carry z1,z2) /. n))) 'or' ((z2 /. n) '&' ((carry z1,z2) /. n)) is Element of BOOLEAN
;
;
end;

:: deftheorem defines add_ovfl BINARITH:def 9 :
for n being non empty Nat
for z1, z2 being Tuple of n,BOOLEAN holds add_ovfl z1,z2 = (((z1 /. n) '&' (z2 /. n)) 'or' ((z1 /. n) '&' ((carry z1,z2) /. n))) 'or' ((z2 /. n) '&' ((carry z1,z2) /. n));

definition
let n be non empty Nat;
let z1, z2 be Tuple of n,BOOLEAN ;
pred z1,z2 are_summable means :Def10: :: BINARITH:def 10
add_ovfl z1,z2 = FALSE ;
end;

:: deftheorem Def10 defines are_summable BINARITH:def 10 :
for n being non empty Nat
for z1, z2 being Tuple of n,BOOLEAN holds
( z1,z2 are_summable iff add_ovfl z1,z2 = FALSE );

theorem Th40: :: BINARITH:40
for z1 being Tuple of 1,BOOLEAN holds
( z1 = <*FALSE *> or z1 = <*TRUE *> )
proof end;

theorem Th41: :: BINARITH:41
for z1 being Tuple of 1,BOOLEAN st z1 = <*FALSE *> holds
Absval z1 = 0
proof end;

theorem Th42: :: BINARITH:42
for z1 being Tuple of 1,BOOLEAN st z1 = <*TRUE *> holds
Absval z1 = 1
proof end;

definition
let n1, n2 be Nat;
let D be non empty set ;
let z1 be Tuple of n1,D;
let z2 be Tuple of n2,D;
:: original: ^
redefine func z1 ^ z2 -> Tuple of (n1 + n2),D;
coherence
z1 ^ z2 is Tuple of (n1 + n2),D
by FINSEQ_2:127;
end;

definition
let D be non empty set ;
let d be Element of D;
:: original: <*
redefine func <*d*> -> Tuple of 1,D;
coherence
<*d*> is Tuple of 1,D
by FINSEQ_2:118;
end;

theorem Th43: :: BINARITH:43
for n being non empty Nat
for z1, z2 being Tuple of n,BOOLEAN
for d1, d2 being Element of BOOLEAN
for i being Nat st i in Seg n holds
(carry (z1 ^ <*d1*>),(z2 ^ <*d2*>)) /. i = (carry z1,z2) /. i
proof end;

theorem Th44: :: BINARITH:44
for n being non empty Nat
for z1, z2 being Tuple of n,BOOLEAN
for d1, d2 being Element of BOOLEAN holds add_ovfl z1,z2 = (carry (z1 ^ <*d1*>),(z2 ^ <*d2*>)) /. (n + 1)
proof end;

theorem Th45: :: BINARITH:45
for n being non empty Nat
for z1, z2 being Tuple of n,BOOLEAN
for d1, d2 being Element of BOOLEAN holds (z1 ^ <*d1*>) + (z2 ^ <*d2*>) = (z1 + z2) ^ <*((d1 'xor' d2) 'xor' (add_ovfl z1,z2))*>
proof end;

theorem Th46: :: BINARITH:46
for n being non empty Nat
for z being Tuple of n,BOOLEAN
for d being Element of BOOLEAN holds Absval (z ^ <*d*>) = (Absval z) + (IFEQ d,FALSE ,0 ,(2 to_power n))
proof end;

theorem Th47: :: BINARITH:47
for n being non empty Nat
for z1, z2 being Tuple of n,BOOLEAN holds (Absval (z1 + z2)) + (IFEQ (add_ovfl z1,z2),FALSE ,0 ,(2 to_power n)) = (Absval z1) + (Absval z2)
proof end;

theorem :: BINARITH:48
for n being non empty Nat
for z1, z2 being Tuple of n,BOOLEAN st z1,z2 are_summable holds
Absval (z1 + z2) = (Absval z1) + (Absval z2)
proof end;

theorem :: BINARITH:49
for c, a, b being natural number st c <= a & c <= b & a -' c = b -' c holds
a = b
proof end;

theorem Th50: :: BINARITH:50
for b, a being natural number st b <= a holds
a -' b = a - b
proof end;

theorem Th51: :: BINARITH:51
for a being natural number holds a -' a = 0
proof end;

theorem Th52: :: BINARITH:52
for a, b being natural number holds a -' b <= a
proof end;

theorem Th53: :: BINARITH:53
for i, j being natural number st i >= j holds
(i -' j) + j = i
proof end;

theorem Th54: :: BINARITH:54
for n, i being Nat st n -' i = 0 holds
n <= i
proof end;

theorem :: BINARITH:55
for i, j, k being Nat st i <= j holds
(j + k) -' i = (j + k) - i by Th50, NAT_1:12;

theorem :: BINARITH:56
for i, j, k being Nat st i <= j holds
(j + k) -' i = (j -' i) + k
proof end;

theorem Th57: :: BINARITH:57
for i, i1 being natural number st ( i -' i1 >= 1 or i - i1 >= 1 ) holds
i -' i1 = i - i1
proof end;

theorem :: BINARITH:58
for n being natural number holds n -' 0 = n
proof end;

theorem :: BINARITH:59
for i1, i2, n being natural number st i1 <= i2 holds
n -' i2 <= n -' i1
proof end;

theorem Th60: :: BINARITH:60
for i1, i2, n being natural number st i1 <= i2 holds
i1 -' n <= i2 -' n
proof end;

theorem Th61: :: BINARITH:61
for i, i1 being natural number st ( i -' i1 >= 1 or i - i1 >= 1 ) holds
(i -' i1) + i1 = i
proof end;

theorem :: BINARITH:62
for i1, i2 being natural number st i1 <= i2 holds
i1 -' 1 <= i2
proof end;

theorem Th63: :: BINARITH:63
for i being natural number holds i -' 2 = (i -' 1) -' 1
proof end;

theorem Th64: :: BINARITH:64
for i1, i2 being natural number st i1 + 1 <= i2 holds
( i1 -' 1 < i2 & i1 -' 2 < i2 & i1 <= i2 )
proof end;

theorem :: BINARITH:65
for i1, i2 being natural number st ( i1 + 2 <= i2 or (i1 + 1) + 1 <= i2 ) holds
( i1 + 1 < i2 & (i1 + 1) -' 1 < i2 & (i1 + 1) -' 2 < i2 & i1 + 1 <= i2 & (i1 -' 1) + 1 < i2 & ((i1 -' 1) + 1) -' 1 < i2 & i1 < i2 & i1 -' 1 < i2 & i1 -' 2 < i2 & i1 <= i2 )
proof end;

theorem :: BINARITH:66
for i1, i2 being natural number st ( i1 <= i2 or i1 <= i2 -' 1 ) holds
( i1 < i2 + 1 & i1 <= i2 + 1 & i1 < (i2 + 1) + 1 & i1 <= (i2 + 1) + 1 & i1 < i2 + 2 & i1 <= i2 + 2 )
proof end;

theorem :: BINARITH:67
for i1, i2 being natural number st ( i1 < i2 or i1 + 1 <= i2 ) holds
i1 <= i2 -' 1
proof end;

theorem :: BINARITH:68
for i, i1, i2 being natural number st i >= i1 holds
i >= i1 -' i2
proof end;

theorem :: BINARITH:69
for i, i1 being natural number st 1 <= i & 1 <= i1 -' i holds
i1 -' i < i1
proof end;

theorem Th70: :: BINARITH:70
for i, k, j being natural number st i -' k <= j holds
i <= j + k
proof end;

theorem :: BINARITH:71
for i, j, k being natural number st i <= j + k holds
i -' k <= j
proof end;

theorem :: BINARITH:72
for i, j, k being natural number st i <= j -' k & k <= j holds
i + k <= j
proof end;

theorem :: BINARITH:73
for j, k, i being natural number st j + k <= i holds
k <= i -' j
proof end;

theorem :: BINARITH:74
for k, i, j being natural number st k <= i & i < j holds
i -' k < j -' k
proof end;

theorem :: BINARITH:75
for i, j, k being natural number st i < j & k < j holds
i -' k < j -' k
proof end;

theorem :: BINARITH:76
for D being non empty set
for f being FinSequence of D
for k being Nat holds len (f /^ k) = (len f) -' k
proof end;