:: BINARI_2 semantic presentation

definition
let c1 be non empty set ;
let c2 be non empty Subset of c1;
let c3, c4 be set ;
let c5, c6 be Element of c2;
redefine func IFEQ as IFEQ c3,c4,c5,c6 -> Element of a2;
coherence
IFEQ c3,c4,c5,c6 is Element of c2
proof end;
end;

Lemma1: for b1 being Nat
for b2, b3 being Tuple of b1,BOOLEAN st len b2 = b1 & len b3 = b1 & ( for b4 being Nat st b4 in Seg b1 holds
b2 /. b4 = b3 /. b4 ) holds
b2 = b3
proof end;

definition
let c1 be Nat;
func Bin1 c1 -> Tuple of a1,BOOLEAN means :Def1: :: BINARI_2:def 1
for b1 being Nat st b1 in Seg a1 holds
a2 /. b1 = IFEQ b1,1,TRUE ,FALSE ;
existence
ex b1 being Tuple of c1,BOOLEAN st
for b2 being Nat st b2 in Seg c1 holds
b1 /. b2 = IFEQ b2,1,TRUE ,FALSE
proof end;
uniqueness
for b1, b2 being Tuple of c1,BOOLEAN st ( for b3 being Nat st b3 in Seg c1 holds
b1 /. b3 = IFEQ b3,1,TRUE ,FALSE ) & ( for b3 being Nat st b3 in Seg c1 holds
b2 /. b3 = IFEQ b3,1,TRUE ,FALSE ) holds
b1 = b2
proof end;
end;

:: deftheorem Def1 defines Bin1 BINARI_2:def 1 :
for b1 being Nat
for b2 being Tuple of b1,BOOLEAN holds
( b2 = Bin1 b1 iff for b3 being Nat st b3 in Seg b1 holds
b2 /. b3 = IFEQ b3,1,TRUE ,FALSE );

definition
let c1 be non empty Nat;
let c2 be Tuple of c1,BOOLEAN ;
func Neg2 c2 -> Tuple of a1,BOOLEAN equals :: BINARI_2:def 2
('not' a2) + (Bin1 a1);
correctness
coherence
('not' c2) + (Bin1 c1) is Tuple of c1,BOOLEAN
;
;
end;

:: deftheorem Def2 defines Neg2 BINARI_2:def 2 :
for b1 being non empty Nat
for b2 being Tuple of b1,BOOLEAN holds Neg2 b2 = ('not' b2) + (Bin1 b1);

definition
let c1 be Nat;
let c2 be Tuple of c1,BOOLEAN ;
func Intval c2 -> Integer equals :Def3: :: BINARI_2:def 3
Absval a2 if a2 /. a1 = FALSE
otherwise (Absval a2) - (2 to_power a1);
correctness
coherence
( ( c2 /. c1 = FALSE implies Absval c2 is Integer ) & ( not c2 /. c1 = FALSE implies (Absval c2) - (2 to_power c1) is Integer ) )
;
consistency
for b1 being Integer holds verum
;
;
end;

:: deftheorem Def3 defines Intval BINARI_2:def 3 :
for b1 being Nat
for b2 being Tuple of b1,BOOLEAN holds
( ( b2 /. b1 = FALSE implies Intval b2 = Absval b2 ) & ( not b2 /. b1 = FALSE implies Intval b2 = (Absval b2) - (2 to_power b1) ) );

definition
let c1 be non empty Nat;
let c2, c3 be Tuple of c1,BOOLEAN ;
func Int_add_ovfl c2,c3 -> Element of BOOLEAN equals :: BINARI_2:def 4
(('not' (a2 /. a1)) '&' ('not' (a3 /. a1))) '&' ((carry a2,a3) /. a1);
correctness
coherence
(('not' (c2 /. c1)) '&' ('not' (c3 /. c1))) '&' ((carry c2,c3) /. c1) is Element of BOOLEAN
;
;
end;

:: deftheorem Def4 defines Int_add_ovfl BINARI_2:def 4 :
for b1 being non empty Nat
for b2, b3 being Tuple of b1,BOOLEAN holds Int_add_ovfl b2,b3 = (('not' (b2 /. b1)) '&' ('not' (b3 /. b1))) '&' ((carry b2,b3) /. b1);

definition
let c1 be non empty Nat;
let c2, c3 be Tuple of c1,BOOLEAN ;
func Int_add_udfl c2,c3 -> Element of BOOLEAN equals :: BINARI_2:def 5
((a2 /. a1) '&' (a3 /. a1)) '&' ('not' ((carry a2,a3) /. a1));
correctness
coherence
((c2 /. c1) '&' (c3 /. c1)) '&' ('not' ((carry c2,c3) /. c1)) is Element of BOOLEAN
;
;
end;

:: deftheorem Def5 defines Int_add_udfl BINARI_2:def 5 :
for b1 being non empty Nat
for b2, b3 being Tuple of b1,BOOLEAN holds Int_add_udfl b2,b3 = ((b2 /. b1) '&' (b3 /. b1)) '&' ('not' ((carry b2,b3) /. b1));

theorem Th1: :: BINARI_2:1
canceled;

theorem Th2: :: BINARI_2:2
canceled;

theorem Th3: :: BINARI_2:3
for b1 being Tuple of 2,BOOLEAN st b1 = <*FALSE *> ^ <*FALSE *> holds
Intval b1 = 0
proof end;

theorem Th4: :: BINARI_2:4
for b1 being Tuple of 2,BOOLEAN st b1 = <*TRUE *> ^ <*FALSE *> holds
Intval b1 = 1
proof end;

theorem Th5: :: BINARI_2:5
for b1 being Tuple of 2,BOOLEAN st b1 = <*FALSE *> ^ <*TRUE *> holds
Intval b1 = - 2
proof end;

theorem Th6: :: BINARI_2:6
for b1 being Tuple of 2,BOOLEAN st b1 = <*TRUE *> ^ <*TRUE *> holds
Intval b1 = - 1
proof end;

theorem Th7: :: BINARI_2:7
for b1, b2 being Nat st b2 in Seg b1 & b2 = 1 holds
(Bin1 b1) /. b2 = TRUE
proof end;

theorem Th8: :: BINARI_2:8
for b1, b2 being Nat st b2 in Seg b1 & b2 <> 1 holds
(Bin1 b1) /. b2 = FALSE
proof end;

theorem Th9: :: BINARI_2:9
for b1 being non empty Nat holds Bin1 (b1 + 1) = (Bin1 b1) ^ <*FALSE *>
proof end;

theorem Th10: :: BINARI_2:10
for b1 being non empty Nat holds Intval ((Bin1 b1) ^ <*FALSE *>) = 1
proof end;

theorem Th11: :: BINARI_2:11
for b1 being non empty Nat
for b2 being Tuple of b1,BOOLEAN
for b3 being Element of BOOLEAN holds 'not' (b2 ^ <*b3*>) = ('not' b2) ^ <*('not' b3)*>
proof end;

theorem Th12: :: BINARI_2:12
for b1 being non empty Nat
for b2 being Tuple of b1,BOOLEAN
for b3 being Element of BOOLEAN holds Intval (b2 ^ <*b3*>) = (Absval b2) - (IFEQ b3,FALSE ,0,(2 to_power b1))
proof end;

theorem Th13: :: BINARI_2:13
for b1 being non empty Nat
for b2, b3 being Tuple of b1,BOOLEAN
for b4, b5 being Element of BOOLEAN holds ((Intval ((b2 ^ <*b4*>) + (b3 ^ <*b5*>))) + (IFEQ (Int_add_ovfl (b2 ^ <*b4*>),(b3 ^ <*b5*>)),FALSE ,0,(2 to_power (b1 + 1)))) - (IFEQ (Int_add_udfl (b2 ^ <*b4*>),(b3 ^ <*b5*>)),FALSE ,0,(2 to_power (b1 + 1))) = (Intval (b2 ^ <*b4*>)) + (Intval (b3 ^ <*b5*>))
proof end;

theorem Th14: :: BINARI_2:14
for b1 being non empty Nat
for b2, b3 being Tuple of b1,BOOLEAN
for b4, b5 being Element of BOOLEAN holds Intval ((b2 ^ <*b4*>) + (b3 ^ <*b5*>)) = (((Intval (b2 ^ <*b4*>)) + (Intval (b3 ^ <*b5*>))) - (IFEQ (Int_add_ovfl (b2 ^ <*b4*>),(b3 ^ <*b5*>)),FALSE ,0,(2 to_power (b1 + 1)))) + (IFEQ (Int_add_udfl (b2 ^ <*b4*>),(b3 ^ <*b5*>)),FALSE ,0,(2 to_power (b1 + 1)))
proof end;

theorem Th15: :: BINARI_2:15
for b1 being non empty Nat
for b2 being Tuple of b1,BOOLEAN holds Absval ('not' b2) = ((- (Absval b2)) + (2 to_power b1)) - 1
proof end;

theorem Th16: :: BINARI_2:16
for b1 being non empty Nat
for b2 being Tuple of b1,BOOLEAN
for b3 being Element of BOOLEAN holds Neg2 (b2 ^ <*b3*>) = (Neg2 b2) ^ <*(('not' b3) 'xor' (add_ovfl ('not' b2),(Bin1 b1)))*>
proof end;

theorem Th17: :: BINARI_2:17
for b1 being non empty Nat
for b2 being Tuple of b1,BOOLEAN
for b3 being Element of BOOLEAN holds (Intval (Neg2 (b2 ^ <*b3*>))) + (IFEQ (Int_add_ovfl ('not' (b2 ^ <*b3*>)),(Bin1 (b1 + 1))),FALSE ,0,(2 to_power (b1 + 1))) = - (Intval (b2 ^ <*b3*>))
proof end;

theorem Th18: :: BINARI_2:18
for b1 being non empty Nat
for b2 being Tuple of b1,BOOLEAN
for b3 being Element of BOOLEAN holds Neg2 (Neg2 (b2 ^ <*b3*>)) = b2 ^ <*b3*>
proof end;

definition
let c1 be non empty Nat;
let c2, c3 be Tuple of c1,BOOLEAN ;
func c2 - c3 -> Tuple of a1,BOOLEAN means :Def6: :: BINARI_2:def 6
for b1 being Nat st b1 in Seg a1 holds
a4 /. b1 = ((a2 /. b1) 'xor' ((Neg2 a3) /. b1)) 'xor' ((carry a2,(Neg2 a3)) /. b1);
existence
ex b1 being Tuple of c1,BOOLEAN st
for b2 being Nat st b2 in Seg c1 holds
b1 /. b2 = ((c2 /. b2) 'xor' ((Neg2 c3) /. b2)) 'xor' ((carry c2,(Neg2 c3)) /. b2)
proof end;
uniqueness
for b1, b2 being Tuple of c1,BOOLEAN st ( for b3 being Nat st b3 in Seg c1 holds
b1 /. b3 = ((c2 /. b3) 'xor' ((Neg2 c3) /. b3)) 'xor' ((carry c2,(Neg2 c3)) /. b3) ) & ( for b3 being Nat st b3 in Seg c1 holds
b2 /. b3 = ((c2 /. b3) 'xor' ((Neg2 c3) /. b3)) 'xor' ((carry c2,(Neg2 c3)) /. b3) ) holds
b1 = b2
proof end;
end;

:: deftheorem Def6 defines - BINARI_2:def 6 :
for b1 being non empty Nat
for b2, b3, b4 being Tuple of b1,BOOLEAN holds
( b4 = b2 - b3 iff for b5 being Nat st b5 in Seg b1 holds
b4 /. b5 = ((b2 /. b5) 'xor' ((Neg2 b3) /. b5)) 'xor' ((carry b2,(Neg2 b3)) /. b5) );

theorem Th19: :: BINARI_2:19
for b1 being non empty Nat
for b2, b3 being Tuple of b1,BOOLEAN holds b2 - b3 = b2 + (Neg2 b3)
proof end;

theorem Th20: :: BINARI_2:20
for b1 being non empty Nat
for b2, b3 being Tuple of b1,BOOLEAN
for b4, b5 being Element of BOOLEAN holds (b2 ^ <*b4*>) - (b3 ^ <*b5*>) = (b2 + (Neg2 b3)) ^ <*(((b4 'xor' ('not' b5)) 'xor' (add_ovfl ('not' b3),(Bin1 b1))) 'xor' (add_ovfl b2,(Neg2 b3)))*>
proof end;

theorem Th21: :: BINARI_2:21
for b1 being non empty Nat
for b2, b3 being Tuple of b1,BOOLEAN
for b4, b5 being Element of BOOLEAN holds (((Intval ((b2 ^ <*b4*>) - (b3 ^ <*b5*>))) + (IFEQ (Int_add_ovfl (b2 ^ <*b4*>),(Neg2 (b3 ^ <*b5*>))),FALSE ,0,(2 to_power (b1 + 1)))) - (IFEQ (Int_add_udfl (b2 ^ <*b4*>),(Neg2 (b3 ^ <*b5*>))),FALSE ,0,(2 to_power (b1 + 1)))) + (IFEQ (Int_add_ovfl ('not' (b3 ^ <*b5*>)),(Bin1 (b1 + 1))),FALSE ,0,(2 to_power (b1 + 1))) = (Intval (b2 ^ <*b4*>)) - (Intval (b3 ^ <*b5*>))
proof end;