* * *

Poll

I know the answer.

false
0 (0%)
unknown
2 (66.7%)
true
1 (33.3%)

Total Members Voted: 3

Author Topic: trinary logic with operator overloads  (Read 2725 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 6368
Re: trinary logic with operator overloads
« Reply #15 on: May 08, 2018, 09:15:49 am »
[Why doesn't the following work?
Code: Pascal  [Select]
  1. program op(input, output, stderr);
  2. type
  3.         enum = (zero, one, two, three = one + two);
  4. begin
  5.         writeLn(one + two); // ideally prints three
  6. end.
Well, this works:
Code: Pascal  [Select]
  1. type enum = (zero, one, two, three = one + two);
  2. begin
  3.   writeLn(enum(ord(one) + ord(two))); // actually prints three
  4. end.
If I only could do that with NOT.... :(

In this case even this works:
Code: Pascal  [Select]
  1. type enum = (zero = -5, one = 10, two = 7, three = one + two);
  2. begin
  3.   writeLn(enum(ord(one) + ord(two))); // actually prints three
  4.   writeln(Ord(Three)); // should be 17,right?
  5. end.
Ok, compiler has a note...

I still feel that negation, addition and subtraction should be legal as overloads. Maybe not for enums with assigned values, as the ordinal test shows.
That is a hornet's nest.
« Last Edit: May 08, 2018, 09:31:18 am by Thaddy »
I am enjoining wine, not whine....

Thaddy

  • Hero Member
  • *****
  • Posts: 6368
Re: trinary logic with operator overloads
« Reply #16 on: July 08, 2018, 09:04:42 am »
@Kays
Florian fixed it. In the latest trunk (39406) this now compiles:
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2. // Kleene and Priest three value logic
  3. // See https://en.wikipedia.org/wiki/Three-valued_logic
  4. type
  5.   ternary = (F, U, T);
  6.    
  7.   operator and (const a,b:ternary):ternary;inline;
  8.     const lookupAnd:array[ternary,ternary] of ternary =
  9.                     ((F,F,F),(F,U,U),(F,U,T));
  10.   begin
  11.     Result:= LookupAnd[a,b];
  12.   end;
  13.          
  14.   operator or (const a,b:ternary):ternary;inline;
  15.     const lookupOr:array[ternary,ternary] of ternary =
  16.                    ((F,U,T),(U,U,T),(T,T,T));
  17.   begin
  18.     Result := LookUpOr[a,b];
  19.   end;
  20.  
  21.   operator not (const a:ternary):ternary;inline;
  22.     const LookupNot:array[ternary] of ternary =(T,U,F);
  23.   begin
  24.      Result:= LookUpNot[a];
  25.   end;
  26.  
  27.  
  28. begin
  29.   // works as expected
  30.   writeln('AND');write(F and F);write(F and U);
  31.   writeln(F and T);write(U and F);write(U and U);
  32.   writeln(U and T);write(T and F);write(T and U);
  33.   writeln(T and T);
  34.   writeln;
  35.   //works as expected
  36.   writeln('OR');write(F or F);write(F or U);
  37.   writeln(F or T);write(U or F);write(U or U);
  38.   writeln(U or T);write(T or F);write(T or U);
  39.   writeln(T or T);
  40.   writeln;
  41.   // works as expected from rev 39406+
  42.   writeln('NOT');
  43.   writeln(not F);
  44.   writeln(not U);
  45.   writeln(not T);
  46.   writeln;
  47.   // works as expected too
  48.   // Material implication for Kleene logic is defined as not(a) or b
  49.   writeln('IMP -> not(a) or b');
  50.   write(not(T) or T);write(not(U) or T);writeln(not(F) or T);
  51.   write(not(T) or U);write(not(U) or U);writeln(not(F) or U);
  52.   write(not(T) or F);write(not(U) or F);writeln(not(F) or F);    
  53. end.
  54.  
[edit] added test for imp, which is not(a) or b
So here we have it.
« Last Edit: July 08, 2018, 12:36:49 pm by Thaddy »
I am enjoining wine, not whine....

Thaddy

  • Hero Member
  • *****
  • Posts: 6368
Re: trinary logic with operator overloads
« Reply #17 on: July 08, 2018, 01:52:44 pm »
I also tested a five way and a nine way logic system. Now also works.
Quite impressive that we can do such things and it has really good applicability in electronics and machine learning.
Tnx Florian!! (And Kays for bringing it up!!)
« Last Edit: July 08, 2018, 01:59:54 pm by Thaddy »
I am enjoining wine, not whine....

Thaddy

  • Hero Member
  • *****
  • Posts: 6368
Re: trinary logic with operator overloads
« Reply #18 on: July 09, 2018, 03:12:30 pm »
I have attached an implementation of a symmetrically balanced 5-way logic for those who are interested.
I am enjoining wine, not whine....

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus