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

Published on 12 May 2024 09:40 PM

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

Source code in the nim programming language

type Trit* = enum ttrue, tmaybe, tfalse

proc `$`*(a: Trit): string =
  case a
  of ttrue: "T"
  of tmaybe: "?"
  of tfalse: "F"

proc `not`*(a: Trit): Trit =
  case a
  of ttrue: tfalse
  of tmaybe: tmaybe
  of tfalse: ttrue

proc `and`*(a, b: Trit): Trit =
  const t: array[Trit, array[Trit, Trit]] =
    [ [ttrue,  tmaybe, tfalse]
    , [tmaybe, tmaybe, tfalse]
    , [tfalse, tfalse, tfalse] ]
  t[a][b]

proc `or`*(a, b: Trit): Trit =
  const t: array[Trit, array[Trit, Trit]] =
    [ [ttrue, ttrue,  ttrue]
    , [ttrue, tmaybe, tmaybe]
    , [ttrue, tmaybe, tfalse] ]
  t[a][b]

proc then*(a, b: Trit): Trit =
  const t: array[Trit, array[Trit, Trit]] =
    [ [ttrue, tmaybe, tfalse]
    , [ttrue, tmaybe, tmaybe]
    , [ttrue, ttrue,  ttrue] ]
  t[a][b]

proc equiv*(a, b: Trit): Trit =
  const t: array[Trit, array[Trit, Trit]] =
    [ [ttrue,  tmaybe, tfalse]
    , [tmaybe, tmaybe, tmaybe]
    , [tfalse, tmaybe, ttrue] ]
  t[a][b]


when isMainModule:

  import strutils

  for t in Trit:
    echo "Not ", t , ": ", not t

  for op1 in Trit:
    for op2 in Trit:
      echo "$# and   $#: $#".format(op1, op2, op1 and op2)
      echo "$# or    $#: $#".format(op1, op2, op1 or op2)
      echo "$# then  $#: $#".format(op1, op2, op1.then op2)
      echo "$# equiv $#: $#".format(op1, op2, op1.equiv op2)


  

You may also check:How to resolve the algorithm Hello world/Text step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Astro programming language
You may also check:How to resolve the algorithm Topological sort step by step in the Rust programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Arturo programming language
You may also check:How to resolve the algorithm Color quantization step by step in the Common Lisp programming language