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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ternary logic step by step in the Seed7 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 Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const type: trit is new enum
    False, Maybe, True
  end enum;

# Enum types define comparisons (=, <, >, <=, >=, <>) and
# the conversions ord and conv.

const func string: str (in trit: aTrit) is
  return [] ("False", "Maybe", "True")[succ(ord(aTrit))];

enable_output(trit);  # Allow writing trit values

const array trit: tritNot is [] (True, Maybe, False);
const array array trit: tritAnd is [] (
    [] (False, False, False),
    [] (False, Maybe, Maybe),
    [] (False, Maybe, True ));
const array array trit: tritOr is [] (
    [] (False, Maybe, True ),
    [] (Maybe, Maybe, True ),
    [] (True,  True,  True ));
const array array trit: tritXor is [] (
    [] (False, Maybe, True ),
    [] (Maybe, Maybe, Maybe),
    [] (True,  Maybe, False));
const array array trit: tritImplies is [] (
    [] (True,  True,  True ),
    [] (Maybe, Maybe, True ),
    [] (False, Maybe, True ));
const array array trit: tritEquiv is [] (
    [] (True,  Maybe, False),
    [] (Maybe, Maybe, Maybe),
    [] (False, Maybe, True ));

const func trit: not (in trit: aTrit) is
  return tritNot[succ(ord(aTrit))];

const func trit: (in trit: aTrit1) and (in trit: aTrit2) is
  return tritAnd[succ(ord(aTrit1))][succ(ord(aTrit2))];
 
const func trit: (in trit: aTrit1) and (ref func trit: aTrit2) is func
  result
    var trit: res is False;
  begin
    if aTrit1 = True then
      res := aTrit2;
    elsif aTrit1 = Maybe and aTrit2 <> False then
      res := Maybe;
    end if;
  end func;

const func trit: (in trit: aTrit1) or (in trit: aTrit2) is
  return tritOr[succ(ord(aTrit1))][succ(ord(aTrit2))];
 
const func trit: (in trit: aTrit1) or (ref func trit: aTrit2) is func
  result
    var trit: res is True;
  begin
    if aTrit1 = False then
      res := aTrit2;
    elsif aTrit1 = Maybe and aTrit2 <> True then
      res := Maybe;
    end if;
  end func;

$ syntax expr: .().xor.() is -> 15;
const func trit: (in trit: aTrit1) xor (in trit: aTrit2) is
  return tritImplies[succ(ord(aTrit1))][succ(ord(aTrit2))];

const func trit: (in trit: aTrit1) -> (in trit: aTrit2) is
  return tritImplies[succ(ord(aTrit1))][succ(ord(aTrit2))];

const func trit: (in trit: aTrit1) == (in trit: aTrit2) is
  return tritEquiv[succ(ord(aTrit1))][succ(ord(aTrit2))];

const func trit: rand (in trit: low, in trit: high) is
  return trit conv (rand(ord(low), ord(high)));

# Begin of test code

var trit: operand1 is False;
var trit: operand2 is False;

const proc: writeTable (ref func trit: tritExpr, in string: name) is func
  begin
    writeln;
    writeln(" " <& name rpad 7 <& " | False  Maybe  True");
    writeln("---------+---------------------");
    for operand1 range False to True do
      write(" " <& operand1 rpad 7 <& " | ");
      for operand2 range False to True do
        write(tritExpr rpad 7);
      end for;
      writeln;
    end for;
  end func;

const proc: main is func
  begin
    writeln(" not" rpad 8 <& " | False  Maybe  True");
    writeln("---------+---------------------");
    write("         | ");
    for operand1 range False to True do
      write(not operand1 rpad 7);
    end for;
    writeln;
    writeTable(operand1 and operand2, "and");
    writeTable(operand1 or operand2,  "or");
    writeTable(operand1 xor operand2, "xor");
    writeTable(operand1 -> operand2,  "->");
    writeTable(operand1 == operand2,  "==");
  end func;

  

You may also check:How to resolve the algorithm String concatenation step by step in the VBScript programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Lua programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Maple programming language
You may also check:How to resolve the algorithm Digital root step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Nth root step by step in the J programming language