:: RADIX_3 semantic presentation

Lemma1: for b1 being Nat st 1 <= b1 holds
Radix b1 = (Radix (b1 -' 1)) + (Radix (b1 -' 1))
proof end;

Lemma2: for b1 being Nat st 1 <= b1 holds
(Radix b1) - (Radix (b1 -' 1)) = Radix (b1 -' 1)
proof end;

Lemma3: for b1 being Nat st 1 <= b1 holds
(- (Radix b1)) + (Radix (b1 -' 1)) = - (Radix (b1 -' 1))
proof end;

Lemma4: for b1 being Nat st 1 <= b1 holds
2 <= Radix b1
proof end;

Lemma5: for b1, b2, b3, b4 being Nat st b4 in Seg b1 holds
DigA (DecSD b2,(b1 + 1),b3),b4 = DigA (DecSD (b2 mod ((Radix b3) |^ b1)),b1,b3),b4
proof end;

definition
let c1 be Nat;
func c1 -SD_Sub_S -> set equals :: RADIX_3:def 1
{ b1 where B is Element of INT : ( b1 >= - (Radix (a1 -' 1)) & b1 <= (Radix (a1 -' 1)) - 1 ) } ;
correctness
coherence
{ b1 where B is Element of INT : ( b1 >= - (Radix (c1 -' 1)) & b1 <= (Radix (c1 -' 1)) - 1 ) } is set
;
;
end;

:: deftheorem Def1 defines -SD_Sub_S RADIX_3:def 1 :
for b1 being Nat holds b1 -SD_Sub_S = { b2 where B is Element of INT : ( b2 >= - (Radix (b1 -' 1)) & b2 <= (Radix (b1 -' 1)) - 1 ) } ;

definition
let c1 be Nat;
func c1 -SD_Sub -> set equals :: RADIX_3:def 2
{ b1 where B is Element of INT : ( b1 >= (- (Radix (a1 -' 1))) - 1 & b1 <= Radix (a1 -' 1) ) } ;
correctness
coherence
{ b1 where B is Element of INT : ( b1 >= (- (Radix (c1 -' 1))) - 1 & b1 <= Radix (c1 -' 1) ) } is set
;
;
end;

:: deftheorem Def2 defines -SD_Sub RADIX_3:def 2 :
for b1 being Nat holds b1 -SD_Sub = { b2 where B is Element of INT : ( b2 >= (- (Radix (b1 -' 1))) - 1 & b2 <= Radix (b1 -' 1) ) } ;

theorem Th1: :: RADIX_3:1
canceled;

theorem Th2: :: RADIX_3:2
for b1 being Nat
for b2 being Integer st b2 in b1 -SD_Sub holds
( (- (Radix (b1 -' 1))) - 1 <= b2 & b2 <= Radix (b1 -' 1) )
proof end;

theorem Th3: :: RADIX_3:3
for b1 being Nat holds b1 -SD_Sub_S c= b1 -SD_Sub
proof end;

theorem Th4: :: RADIX_3:4
for b1 being Nat holds b1 -SD_Sub_S c= (b1 + 1) -SD_Sub_S
proof end;

theorem Th5: :: RADIX_3:5
for b1 being Nat st 2 <= b1 holds
b1 -SD_Sub c= b1 -SD
proof end;

theorem Th6: :: RADIX_3:6
0 in 0 -SD_Sub_S
proof end;

theorem Th7: :: RADIX_3:7
for b1 being Nat holds 0 in b1 -SD_Sub_S
proof end;

theorem Th8: :: RADIX_3:8
for b1 being Nat holds 0 in b1 -SD_Sub
proof end;

theorem Th9: :: RADIX_3:9
for b1 being Nat
for b2 being set st b2 in b1 -SD_Sub holds
b2 is Integer
proof end;

theorem Th10: :: RADIX_3:10
for b1 being Nat holds b1 -SD_Sub c= INT
proof end;

theorem Th11: :: RADIX_3:11
for b1 being Nat holds b1 -SD_Sub_S c= INT
proof end;

registration
let c1 be Nat;
cluster a1 -SD_Sub_S -> non empty ;
coherence
not c1 -SD_Sub_S is empty
by Th7;
cluster a1 -SD_Sub -> non empty ;
coherence
not c1 -SD_Sub is empty
by Th8;
end;

definition
let c1 be Nat;
redefine func -SD_Sub_S as c1 -SD_Sub_S -> non empty Subset of INT ;
coherence
c1 -SD_Sub_S is non empty Subset of INT
by Th11;
end;

definition
let c1 be Nat;
redefine func -SD_Sub as c1 -SD_Sub -> non empty Subset of INT ;
coherence
c1 -SD_Sub is non empty Subset of INT
by Th10;
end;

theorem Th12: :: RADIX_3:12
for b1, b2, b3 being Nat
for b4 being Tuple of b2,(b3 -SD_Sub ) st b1 in Seg b2 holds
b4 . b1 is Element of b3 -SD_Sub
proof end;

definition
let c1 be Integer;
let c2 be Nat;
func SDSub_Add_Carry c1,c2 -> Integer equals :Def3: :: RADIX_3:def 3
1 if Radix (a2 -' 1) <= a1
- 1 if a1 < - (Radix (a2 -' 1))
otherwise 0;
coherence
( ( Radix (c2 -' 1) <= c1 implies 1 is Integer ) & ( c1 < - (Radix (c2 -' 1)) implies - 1 is Integer ) & ( not Radix (c2 -' 1) <= c1 & not c1 < - (Radix (c2 -' 1)) implies 0 is Integer ) )
;
consistency
for b1 being Integer st Radix (c2 -' 1) <= c1 & c1 < - (Radix (c2 -' 1)) holds
( b1 = 1 iff b1 = - 1 )
;
end;

:: deftheorem Def3 defines SDSub_Add_Carry RADIX_3:def 3 :
for b1 being Integer
for b2 being Nat holds
( ( Radix (b2 -' 1) <= b1 implies SDSub_Add_Carry b1,b2 = 1 ) & ( b1 < - (Radix (b2 -' 1)) implies SDSub_Add_Carry b1,b2 = - 1 ) & ( not Radix (b2 -' 1) <= b1 & not b1 < - (Radix (b2 -' 1)) implies SDSub_Add_Carry b1,b2 = 0 ) );

definition
let c1 be Integer;
let c2 be Nat;
func SDSub_Add_Data c1,c2 -> Integer equals :: RADIX_3:def 4
a1 - ((Radix a2) * (SDSub_Add_Carry a1,a2));
coherence
c1 - ((Radix c2) * (SDSub_Add_Carry c1,c2)) is Integer
;
end;

:: deftheorem Def4 defines SDSub_Add_Data RADIX_3:def 4 :
for b1 being Integer
for b2 being Nat holds SDSub_Add_Data b1,b2 = b1 - ((Radix b2) * (SDSub_Add_Carry b1,b2));

theorem Th13: :: RADIX_3:13
for b1 being Integer
for b2 being Nat st 2 <= b2 holds
( - 1 <= SDSub_Add_Carry b1,b2 & SDSub_Add_Carry b1,b2 <= 1 )
proof end;

theorem Th14: :: RADIX_3:14
for b1 being Nat
for b2 being Integer st 2 <= b1 & b2 in b1 -SD holds
( SDSub_Add_Data b2,b1 >= - (Radix (b1 -' 1)) & SDSub_Add_Data b2,b1 <= (Radix (b1 -' 1)) - 1 )
proof end;

theorem Th15: :: RADIX_3:15
for b1 being Integer
for b2 being Nat st 2 <= b2 holds
SDSub_Add_Carry b1,b2 in b2 -SD_Sub_S
proof end;

theorem Th16: :: RADIX_3:16
for b1 being Nat
for b2, b3 being Integer st 2 <= b1 & b2 in b1 -SD & b3 in b1 -SD holds
(SDSub_Add_Data b2,b1) + (SDSub_Add_Carry b3,b1) in b1 -SD_Sub
proof end;

theorem Th17: :: RADIX_3:17
for b1 being Nat st 2 <= b1 holds
SDSub_Add_Carry 0,b1 = 0
proof end;

definition
let c1, c2, c3 be Nat;
let c4 be Tuple of c3,(c2 -SD_Sub );
func DigA_SDSub c4,c1 -> Integer equals :Def5: :: RADIX_3:def 5
a4 . a1 if a1 in Seg a3
0 if a1 = 0
;
coherence
( ( c1 in Seg c3 implies c4 . c1 is Integer ) & ( c1 = 0 implies 0 is Integer ) )
proof end;
consistency
for b1 being Integer st c1 in Seg c3 & c1 = 0 holds
( b1 = c4 . c1 iff b1 = 0 )
by FINSEQ_1:3;
end;

:: deftheorem Def5 defines DigA_SDSub RADIX_3:def 5 :
for b1, b2, b3 being Nat
for b4 being Tuple of b3,(b2 -SD_Sub ) holds
( ( b1 in Seg b3 implies DigA_SDSub b4,b1 = b4 . b1 ) & ( b1 = 0 implies DigA_SDSub b4,b1 = 0 ) );

definition
let c1, c2, c3 be Nat;
let c4 be Tuple of c3,(c2 -SD );
func SD2SDSubDigit c4,c1,c2 -> Integer equals :Def6: :: RADIX_3:def 6
(SDSub_Add_Data (DigA a4,a1),a2) + (SDSub_Add_Carry (DigA a4,(a1 -' 1)),a2) if a1 in Seg a3
SDSub_Add_Carry (DigA a4,(a1 -' 1)),a2 if a1 = a3 + 1
otherwise 0;
coherence
( ( c1 in Seg c3 implies (SDSub_Add_Data (DigA c4,c1),c2) + (SDSub_Add_Carry (DigA c4,(c1 -' 1)),c2) is Integer ) & ( c1 = c3 + 1 implies SDSub_Add_Carry (DigA c4,(c1 -' 1)),c2 is Integer ) & ( not c1 in Seg c3 & not c1 = c3 + 1 implies 0 is Integer ) )
;
consistency
for b1 being Integer st c1 in Seg c3 & c1 = c3 + 1 holds
( b1 = (SDSub_Add_Data (DigA c4,c1),c2) + (SDSub_Add_Carry (DigA c4,(c1 -' 1)),c2) iff b1 = SDSub_Add_Carry (DigA c4,(c1 -' 1)),c2 )
proof end;
end;

:: deftheorem Def6 defines SD2SDSubDigit RADIX_3:def 6 :
for b1, b2, b3 being Nat
for b4 being Tuple of b3,(b2 -SD ) holds
( ( b1 in Seg b3 implies SD2SDSubDigit b4,b1,b2 = (SDSub_Add_Data (DigA b4,b1),b2) + (SDSub_Add_Carry (DigA b4,(b1 -' 1)),b2) ) & ( b1 = b3 + 1 implies SD2SDSubDigit b4,b1,b2 = SDSub_Add_Carry (DigA b4,(b1 -' 1)),b2 ) & ( not b1 in Seg b3 & not b1 = b3 + 1 implies SD2SDSubDigit b4,b1,b2 = 0 ) );

theorem Th18: :: RADIX_3:18
for b1, b2, b3 being Nat
for b4 being Tuple of b3,(b1 -SD ) st 2 <= b1 & b2 in Seg (b3 + 1) holds
SD2SDSubDigit b4,b2,b1 is Element of b1 -SD_Sub
proof end;

definition
let c1, c2, c3 be Nat;
let c4 be Tuple of c3,(c2 -SD );
assume E25: ( 2 <= c2 & c1 in Seg (c3 + 1) ) ;
func SD2SDSubDigitS c4,c1,c2 -> Element of a2 -SD_Sub equals :Def7: :: RADIX_3:def 7
SD2SDSubDigit a4,a1,a2;
coherence
SD2SDSubDigit c4,c1,c2 is Element of c2 -SD_Sub
by E25, Th18;
end;

:: deftheorem Def7 defines SD2SDSubDigitS RADIX_3:def 7 :
for b1, b2, b3 being Nat
for b4 being Tuple of b3,(b2 -SD ) st 2 <= b2 & b1 in Seg (b3 + 1) holds
SD2SDSubDigitS b4,b1,b2 = SD2SDSubDigit b4,b1,b2;

definition
let c1, c2 be Nat;
let c3 be Tuple of c1,(c2 -SD );
func SD2SDSub c3 -> Tuple of (a1 + 1),(a2 -SD_Sub ) means :Def8: :: RADIX_3:def 8
for b1 being Nat st b1 in Seg (a1 + 1) holds
DigA_SDSub a4,b1 = SD2SDSubDigitS a3,b1,a2;
existence
ex b1 being Tuple of (c1 + 1),(c2 -SD_Sub ) st
for b2 being Nat st b2 in Seg (c1 + 1) holds
DigA_SDSub b1,b2 = SD2SDSubDigitS c3,b2,c2
proof end;
uniqueness
for b1, b2 being Tuple of (c1 + 1),(c2 -SD_Sub ) st ( for b3 being Nat st b3 in Seg (c1 + 1) holds
DigA_SDSub b1,b3 = SD2SDSubDigitS c3,b3,c2 ) & ( for b3 being Nat st b3 in Seg (c1 + 1) holds
DigA_SDSub b2,b3 = SD2SDSubDigitS c3,b3,c2 ) holds
b1 = b2
proof end;
end;

:: deftheorem Def8 defines SD2SDSub RADIX_3:def 8 :
for b1, b2 being Nat
for b3 being Tuple of b1,(b2 -SD )
for b4 being Tuple of (b1 + 1),(b2 -SD_Sub ) holds
( b4 = SD2SDSub b3 iff for b5 being Nat st b5 in Seg (b1 + 1) holds
DigA_SDSub b4,b5 = SD2SDSubDigitS b3,b5,b2 );

theorem Th19: :: RADIX_3:19
for b1, b2, b3 being Nat
for b4 being Tuple of b2,(b3 -SD_Sub ) st b1 in Seg b2 holds
DigA_SDSub b4,b1 is Element of b3 -SD_Sub
proof end;

theorem Th20: :: RADIX_3:20
for b1 being Nat
for b2, b3 being Integer st 2 <= b1 & b2 in b1 -SD & b3 in b1 -SD_Sub holds
SDSub_Add_Data (b2 + b3),b1 in b1 -SD_Sub_S
proof end;

definition
let c1, c2, c3 be Nat;
let c4 be Tuple of c3,(c2 -SD_Sub );
func DigB_SDSub c4,c1 -> Element of INT equals :: RADIX_3:def 9
DigA_SDSub a4,a1;
coherence
DigA_SDSub c4,c1 is Element of INT
by INT_1:def 2;
end;

:: deftheorem Def9 defines DigB_SDSub RADIX_3:def 9 :
for b1, b2, b3 being Nat
for b4 being Tuple of b3,(b2 -SD_Sub ) holds DigB_SDSub b4,b1 = DigA_SDSub b4,b1;

definition
let c1, c2, c3 be Nat;
let c4 be Tuple of c3,(c2 -SD_Sub );
func SDSub2INTDigit c4,c1,c2 -> Element of INT equals :: RADIX_3:def 10
((Radix a2) |^ (a1 -' 1)) * (DigB_SDSub a4,a1);
coherence
((Radix c2) |^ (c1 -' 1)) * (DigB_SDSub c4,c1) is Element of INT
by INT_1:def 2;
end;

:: deftheorem Def10 defines SDSub2INTDigit RADIX_3:def 10 :
for b1, b2, b3 being Nat
for b4 being Tuple of b3,(b2 -SD_Sub ) holds SDSub2INTDigit b4,b1,b2 = ((Radix b2) |^ (b1 -' 1)) * (DigB_SDSub b4,b1);

definition
let c1, c2 be Nat;
let c3 be Tuple of c1,(c2 -SD_Sub );
func SDSub2INT c3 -> Tuple of a1,INT means :Def11: :: RADIX_3:def 11
for b1 being Nat st b1 in Seg a1 holds
a4 /. b1 = SDSub2INTDigit a3,b1,a2;
existence
ex b1 being Tuple of c1,INT st
for b2 being Nat st b2 in Seg c1 holds
b1 /. b2 = SDSub2INTDigit c3,b2,c2
proof end;
uniqueness
for b1, b2 being Tuple of c1,INT st ( for b3 being Nat st b3 in Seg c1 holds
b1 /. b3 = SDSub2INTDigit c3,b3,c2 ) & ( for b3 being Nat st b3 in Seg c1 holds
b2 /. b3 = SDSub2INTDigit c3,b3,c2 ) holds
b1 = b2
proof end;
end;

:: deftheorem Def11 defines SDSub2INT RADIX_3:def 11 :
for b1, b2 being Nat
for b3 being Tuple of b1,(b2 -SD_Sub )
for b4 being Tuple of b1,INT holds
( b4 = SDSub2INT b3 iff for b5 being Nat st b5 in Seg b1 holds
b4 /. b5 = SDSub2INTDigit b3,b5,b2 );

definition
let c1, c2 be Nat;
let c3 be Tuple of c1,(c2 -SD_Sub );
func SDSub2IntOut c3 -> Integer equals :: RADIX_3:def 12
Sum (SDSub2INT a3);
coherence
Sum (SDSub2INT c3) is Integer
;
end;

:: deftheorem Def12 defines SDSub2IntOut RADIX_3:def 12 :
for b1, b2 being Nat
for b3 being Tuple of b1,(b2 -SD_Sub ) holds SDSub2IntOut b3 = Sum (SDSub2INT b3);

theorem Th21: :: RADIX_3:21
for b1, b2, b3, b4 being Nat st b4 in Seg b1 & 2 <= b2 holds
DigA_SDSub (SD2SDSub (DecSD b3,(b1 + 1),b2)),b4 = DigA_SDSub (SD2SDSub (DecSD (b3 mod ((Radix b2) |^ b1)),b1,b2)),b4
proof end;

theorem Th22: :: RADIX_3:22
for b1 being Nat st b1 >= 1 holds
for b2, b3 being Nat st b2 >= 2 & b3 is_represented_by b1,b2 holds
b3 = SDSub2IntOut (SD2SDSub (DecSD b3,b1,b2))
proof end;