

 This page uses content from Wikipedia. The original article was at Ternary logic. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



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.


Example Ternary Logic Operators in Truth Tables:




not a


 ¬


 True 
 False


 Maybe 
 Maybe


 False 
 True




a and b


 ∧

 True 
 Maybe 
 False


 True 
 True 
 Maybe 
 False


 Maybe 
 Maybe 
 Maybe 
 False


 False 
 False 
 False 
 False




a or b


 ∨

 True 
 Maybe 
 False


 True 
 True 
 True 
 True


 Maybe 
 True 
 Maybe 
 Maybe


 False 
 True 
 Maybe 
 False





if a then b


 ⊃

 True 
 Maybe 
 False


 True 
 True 
 Maybe 
 False


 Maybe 
 True 
 Maybe 
 Maybe


 False 
 True 
 True 
 True




a is equivalent to b


 ≡

 True 
 Maybe 
 False


 True 
 True 
 Maybe 
 False


 Maybe 
 Maybe 
 Maybe 
 Maybe


 False 
 False 
 Maybe 
 True




Task

 Define a new type that emulates ternary logic by storing data trits.
 Given all the binary logic operators of the original programming language, reimplement these operators for the new Ternary logic type trit.
 Generate a sampling of results using trit variables.
 Kudos for actually thinking up a test case algorithm where ternary logic is intrinsically useful, optimises the test case algorithm and is preferable to binary logic.


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

DEFINE TERNARY="BYTE"
DEFINE FALSE="0"
DEFINE MAYBE="1"
DEFINE TRUE="2"
 
PROC PrintT(TERNARY a)
  IF a=FALSE THEN Print("F")
  ELSEIF a=MAYBE THEN Print("?")
  ELSE Print("T")
  FI
RETURN
 
TERNARY FUNC NotT(TERNARY a)
RETURN (TRUE-a)
 
TERNARY FUNC AndT(TERNARY a,b)
  IF a<b THEN RETURN (a) FI
RETURN (b)
 
TERNARY FUNC OrT(TERNARY a,b)
  IF a>b THEN RETURN (a) FI
RETURN (b)
 
TERNARY FUNC IfThenT(TERNARY a,b)
  IF a=TRUE THEN RETURN (b)
  ELSEIF a=FALSE THEN RETURN (TRUE)
  ELSEIF a+b>TRUE THEN RETURN (TRUE)
  FI
RETURN (MAYBE)
 
TERNARY FUNC EquivT(TERNARY a,b)
  IF a=b THEN RETURN (TRUE)
  ELSEIF a=TRUE THEN RETURN (b)
  ELSEIF b=TRUE THEN RETURN (a)
  FI
RETURN (MAYBE)
 
PROC Main()
  BYTE x,y,a,b,res
 
  x=2 y=1
  FOR a=FALSE TO TRUE
  DO
    res=NotT(a)
    Position(x,y) y==+1
    Print("not ") PrintT(a)
    Print(" = ") PrintT(res)
  OD
 
  y==+1
  FOR a=FALSE TO TRUE
  DO
    FOR b=FALSE TO TRUE
    DO
      res=AndT(a,b)
      Position(x,y) y==+1
      PrintT(a) Print(" and ") PrintT(b)
      Print(" = ") PrintT(res)
    OD
  OD
 
  y==+1
  FOR a=FALSE TO TRUE
  DO
    FOR b=FALSE TO TRUE
    DO
      res=OrT(a,b)
      Position(x,y) y==+1
      PrintT(a) Print(" or ") PrintT(b)
      Print(" = ") PrintT(res)
    OD
  OD
 
  x=20 y=5
  FOR a=FALSE TO TRUE
  DO
    FOR b=FALSE TO TRUE
    DO
      res=IfThenT(a,b)
      Position(x,y) y==+1
      Print("if ") PrintT(a)
      Print(" then ") PrintT(b)
      Print(" = ") PrintT(res)
    OD
  OD
 
  y==+1
  FOR a=FALSE TO TRUE
  DO
    FOR b=FALSE TO TRUE
    DO
      res=EquivT(a,b)
      Position(x,y) y==+1
      PrintT(a) Print(" equiv ") PrintT(b)
      Print(" = ") PrintT(res)
    OD
  OD
RETURN