How to resolve the algorithm Ternary logic step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ternary logic step by step in the Ada programming language

Table of Contents

Problem Statement

In logic, a three-valued logic (also trivalent, ternary, or trinary logic, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating true, false and some indeterminate third value.
This is contrasted with the more commonly known bivalent logics (such as classical sentential or boolean logic) which provide only for true and false. Conceptual form and basic ideas were initially created by Łukasiewicz, Lewis and Sulski. These were then re-formulated by Grigore Moisil in an axiomatic algebraic form, and also extended to n-valued logics in 1945.

Note:   Setun   (Сетунь) was a   balanced ternary   computer developed in 1958 at   Moscow State University.   The device was built under the lead of   Sergei Sobolev   and   Nikolay Brusentsov.   It was the only modern   ternary computer,   using three-valued ternary logic

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ternary logic step by step in the Ada programming language

Source code in the ada programming language

package Logic is
   type Ternary is (True, Unknown, False); 

   -- logic functions
   function "and"(Left, Right: Ternary) return Ternary;
   function "or"(Left, Right: Ternary) return Ternary;
   function "not"(T: Ternary) return Ternary;
   function Equivalent(Left, Right: Ternary) return Ternary;
   function Implies(Condition, Conclusion: Ternary) return Ternary;

   -- conversion functions
   function To_Bool(X: Ternary) return Boolean;
   function To_Ternary(B: Boolean) return Ternary;
   function Image(Value: Ternary) return Character;
end Logic;


package body Logic is
   -- type Ternary is (True, Unknown, False);

   function Image(Value: Ternary) return Character is
   begin
      case Value is
         when True    => return 'T';
         when False   => return 'F';
         when Unknown => return '?';
      end case;
   end Image;

   function "and"(Left, Right: Ternary) return Ternary is
   begin
      return Ternary'max(Left, Right);
   end "and";

   function "or"(Left, Right: Ternary) return Ternary is
   begin
      return Ternary'min(Left, Right);
   end "or";

   function "not"(T: Ternary) return Ternary is
   begin
      case T is
         when False   => return True;
         when Unknown => return Unknown;
         when True    => return False;
      end case;
   end "not";

   function To_Bool(X: Ternary) return Boolean is
   begin
      case X is
         when True  => return True;
         when False => return False;
         when Unknown => raise Constraint_Error;
      end case;
   end To_Bool;

   function To_Ternary(B: Boolean) return Ternary is
   begin
      if B then
         return True;
      else
         return False;
      end if;
   end To_Ternary;

   function Equivalent(Left, Right: Ternary) return Ternary is
   begin
      return To_Ternary(To_Bool(Left) = To_Bool(Right));
   exception
      when Constraint_Error => return Unknown;
   end Equivalent;

   function Implies(Condition, Conclusion: Ternary) return Ternary is
   begin
      return (not Condition) or Conclusion;
   end Implies;

end Logic;


with Ada.Text_IO, Logic;

procedure Test_Tri_Logic is

   use Logic;

   type F2 is access function(Left, Right: Ternary) return Ternary;
   type F1 is access function(Trit: Ternary) return Ternary;

   procedure Truth_Table(F: F1; Name: String) is
   begin
      Ada.Text_IO.Put_Line("X | " & Name & "(X)");
      for T in Ternary loop
         Ada.Text_IO.Put_Line(Image(T) & " |  " & Image(F(T)));
      end loop;
   end Truth_Table;

   procedure Truth_Table(F: F2; Name: String) is
   begin
      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put_Line("X | Y | " & Name & "(X,Y)");
      for X in Ternary loop
         for Y in Ternary loop
            Ada.Text_IO.Put_Line(Image(X) & " | " & Image(Y) & " |  " & Image(F(X,Y)));
         end loop;
      end loop;
   end Truth_Table;

begin
   Truth_Table(F => "not"'Access, Name => "Not");
   Truth_Table(F => "and"'Access, Name => "And");
   Truth_Table(F => "or"'Access, Name => "Or");
   Truth_Table(F => Equivalent'Access, Name => "Eq");
   Truth_Table(F => Implies'Access, Name => "Implies");
end Test_Tri_Logic;


  

You may also check:How to resolve the algorithm Identity matrix step by step in the FBSL programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Ada programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Forth programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Forth programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Elixir programming language