Article BINARI_2, MML version 4.99.1005

:: BINARI_2:funcnot 1 => BINARI_2:func 1
definition
  let a1 be Element of NAT;
  func Bin1 A1 -> Element of a1 -tuples_on BOOLEAN means
    for b1 being Element of NAT
          st b1 in Seg a1
       holds it /. b1 = IFEQ(b1,1,TRUE,FALSE);
end;

:: BINARI_2:def 1
theorem
for b1 being Element of NAT
for b2 being Element of b1 -tuples_on BOOLEAN holds
      b2 = Bin1 b1
   iff
      for b3 being Element of NAT
            st b3 in Seg b1
         holds b2 /. b3 = IFEQ(b3,1,TRUE,FALSE);

:: BINARI_2:funcnot 2 => BINARI_2:func 2
definition
  let a1 be non empty Element of NAT;
  let a2 be Element of a1 -tuples_on BOOLEAN;
  func Neg2 A2 -> Element of a1 -tuples_on BOOLEAN equals
    ('not' a2) + Bin1 a1;
end;

:: BINARI_2:def 2
theorem
for b1 being non empty Element of NAT
for b2 being Element of b1 -tuples_on BOOLEAN holds
   Neg2 b2 = ('not' b2) + Bin1 b1;

:: BINARI_2:funcnot 3 => BINARI_2:func 3
definition
  let a1 be Element of NAT;
  let a2 be Element of a1 -tuples_on BOOLEAN;
  func Intval A2 -> integer set equals
    Absval a2
    if a2 /. a1 = FALSE
    otherwise (Absval a2) - (2 to_power a1);
end;

:: BINARI_2:def 3
theorem
for b1 being Element of NAT
for b2 being Element of b1 -tuples_on BOOLEAN holds
   (b2 /. b1 = FALSE implies Intval b2 = Absval b2) &
    (b2 /. b1 = FALSE or Intval b2 = (Absval b2) - (2 to_power b1));

:: BINARI_2:funcnot 4 => BINARI_2:func 4
definition
  let a1 be non empty Element of NAT;
  let a2, a3 be Element of a1 -tuples_on BOOLEAN;
  func Int_add_ovfl(A2,A3) -> Element of BOOLEAN equals
    (('not' (a2 /. a1)) '&' 'not' (a3 /. a1)) '&' ((carry(a2,a3)) /. a1);
end;

:: BINARI_2:def 4
theorem
for b1 being non empty Element of NAT
for b2, b3 being Element of b1 -tuples_on BOOLEAN holds
Int_add_ovfl(b2,b3) = (('not' (b2 /. b1)) '&' 'not' (b3 /. b1)) '&' ((carry(b2,b3)) /. b1);

:: BINARI_2:funcnot 5 => BINARI_2:func 5
definition
  let a1 be non empty Element of NAT;
  let a2, a3 be Element of a1 -tuples_on BOOLEAN;
  func Int_add_udfl(A2,A3) -> Element of BOOLEAN equals
    ((a2 /. a1) '&' (a3 /. a1)) '&' 'not' ((carry(a2,a3)) /. a1);
end;

:: BINARI_2:def 5
theorem
for b1 being non empty Element of NAT
for b2, b3 being Element of b1 -tuples_on BOOLEAN holds
Int_add_udfl(b2,b3) = ((b2 /. b1) '&' (b3 /. b1)) '&' 'not' ((carry(b2,b3)) /. b1);

:: BINARI_2:th 3
theorem
for b1 being Element of 2 -tuples_on BOOLEAN
      st b1 = <*FALSE*> ^ <*FALSE*>
   holds Intval b1 = 0;

:: BINARI_2:th 4
theorem
for b1 being Element of 2 -tuples_on BOOLEAN
      st b1 = <*TRUE*> ^ <*FALSE*>
   holds Intval b1 = 1;

:: BINARI_2:th 5
theorem
for b1 being Element of 2 -tuples_on BOOLEAN
      st b1 = <*FALSE*> ^ <*TRUE*>
   holds Intval b1 = - 2;

:: BINARI_2:th 6
theorem
for b1 being Element of 2 -tuples_on BOOLEAN
      st b1 = <*TRUE*> ^ <*TRUE*>
   holds Intval b1 = - 1;

:: BINARI_2:th 7
theorem
for b1, b2 being Element of NAT
      st b2 in Seg b1 & b2 = 1
   holds (Bin1 b1) /. b2 = TRUE;

:: BINARI_2:th 8
theorem
for b1, b2 being Element of NAT
      st b2 in Seg b1 & b2 <> 1
   holds (Bin1 b1) /. b2 = FALSE;

:: BINARI_2:th 9
theorem
for b1 being non empty Element of NAT holds
   Bin1 (b1 + 1) = (Bin1 b1) ^ <*FALSE*>;

:: BINARI_2:th 10
theorem
for b1 being non empty Element of NAT holds
   Intval ((Bin1 b1) ^ <*FALSE*>) = 1;

:: BINARI_2:th 11
theorem
for b1 being non empty Element of NAT
for b2 being Element of b1 -tuples_on BOOLEAN
for b3 being Element of BOOLEAN holds
   'not' (b2 ^ <*b3*>) = ('not' b2) ^ <*'not' b3*>;

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

:: BINARI_2:th 13
theorem
for b1 being non empty Element of NAT
for b2, b3 being Element of b1 -tuples_on 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*>);

:: BINARI_2:th 14
theorem
for b1 being non empty Element of NAT
for b2, b3 being Element of b1 -tuples_on 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));

:: BINARI_2:th 15
theorem
for b1 being non empty Element of NAT
for b2 being Element of b1 -tuples_on BOOLEAN holds
   Absval 'not' b2 = ((- Absval b2) + (2 to_power b1)) - 1;

:: BINARI_2:th 16
theorem
for b1 being non empty Element of NAT
for b2 being Element of b1 -tuples_on BOOLEAN
for b3 being Element of BOOLEAN holds
   Neg2 (b2 ^ <*b3*>) = (Neg2 b2) ^ <*('not' b3) 'xor' add_ovfl('not' b2,Bin1 b1)*>;

:: BINARI_2:th 17
theorem
for b1 being non empty Element of NAT
for b2 being Element of b1 -tuples_on 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*>);

:: BINARI_2:th 18
theorem
for b1 being non empty Element of NAT
for b2 being Element of b1 -tuples_on BOOLEAN
for b3 being Element of BOOLEAN holds
   Neg2 Neg2 (b2 ^ <*b3*>) = b2 ^ <*b3*>;

:: BINARI_2:funcnot 6 => BINARI_2:func 6
definition
  let a1 be non empty Element of NAT;
  let a2, a3 be Element of a1 -tuples_on BOOLEAN;
  func A2 - A3 -> Element of a1 -tuples_on BOOLEAN means
    for b1 being Element of NAT
          st b1 in Seg a1
       holds it /. b1 = ((a2 /. b1) 'xor' ((Neg2 a3) /. b1)) 'xor' ((carry(a2,Neg2 a3)) /. b1);
end;

:: BINARI_2:def 6
theorem
for b1 being non empty Element of NAT
for b2, b3, b4 being Element of b1 -tuples_on BOOLEAN holds
   b4 = b2 - b3
iff
   for b5 being Element of NAT
         st b5 in Seg b1
      holds b4 /. b5 = ((b2 /. b5) 'xor' ((Neg2 b3) /. b5)) 'xor' ((carry(b2,Neg2 b3)) /. b5);

:: BINARI_2:th 19
theorem
for b1 being non empty Element of NAT
for b2, b3 being Element of b1 -tuples_on BOOLEAN holds
b2 - b3 = b2 + Neg2 b3;

:: BINARI_2:th 20
theorem
for b1 being non empty Element of NAT
for b2, b3 being Element of b1 -tuples_on 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)*>;

:: BINARI_2:th 21
theorem
for b1 being non empty Element of NAT
for b2, b3 being Element of b1 -tuples_on 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*>);