program ternarytests;
{$mode objfpc}
{
* Evaluated, added XOR, and tested against other languages after seeing code on rosettacode.
* They used almost all similar code. So I decided to finish it
*
Currently the equality operator can not be overridden for equal types but
1. equality is the same as: NOT (x XOR y)
2. IMP is the same as: (NOT x) OR y
So these two demonstrate the logic works!
Enjoy,
Thaddy
}
type
{ ternary type }
trit = (F, U, T);
operator and (const a,b:trit):trit;inline;
const lookupAnd:array[trit,trit] of trit =
((F,F,F),(F,U,U),(F,U,T));
begin
Result:= LookupAnd[a,b];
end;
operator or (const a,b:trit):trit;inline;
const lookupOr:array[trit,trit] of trit =
((F,U,T),(U,U,T),(T,T,T));
begin
Result := LookUpOr[a,b];
end;
operator not (const a:trit):trit;inline;
const LookupNot:array[trit] of trit =(T,U,F);
begin
Result:= LookUpNot[a];
end;
operator xor (const a,b:trit):trit;
const LookupXor:array[trit,trit] of trit =
((F,U,T),(U,U,U),(T,U,F));
begin
Result := LookupXor[a,b];
end;
// just tests...
begin
writeln(' a AND b');
writeln('__FUT');
writeln('F|',F and F,F and U,F and T);
writeln('U|',U and F,U and U,U and T);
writeln('T|',T and F,T and U,T and T);
writeln;
writeln(' a OR b');
writeln('__FUT');
writeln('F|',F or F,F or U,F or T);
writeln('U|',U or F,U or U,U or T);
writeln('T|',T or F,T or U,T or T);
writeln;
writeln(' NOT a');
writeln('__FUT');
writeln('F|',not F);
writeln('U|',not U);
writeln('T|',not T);
writeln;
writeln(' a XOR b');
writeln('__FUT');
writeln('F|',F xor F,F xor U,F xor T);
writeln('U|',U xor F,U xor U,U xor T);
writeln('T|',T xor F,T xor U,T xor T);
writeln;
writeln(' NOT (a XOR b) -> equality/equivalence');
writeln('__FUT');
writeln('F|',not(F xor F),not(F xor U),not(F xor T));
writeln('U|',not(U xor F),not(U xor U),not(U xor T));
writeln('T|',not(T xor F),not(T xor U),not(T xor T));
writeln;
writeln('IMP. a.k.a. IfThen -> not(a) or b');
writeln('T|',(not T) or T,(not T) or U,(not T) or F);
writeln('U|',(not U) or T,(not U) or U,(not U) or F);
writeln('F|',(not F) or T,(not F) or U,(not F) or F);
end.