

 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

 
[ 2 ]                                       is maybe      (     --> t )
 
[ table 2 0 1 ]                             is jiggle     (   t --> n )
 
[ jiggle 1+ ]this[ swap peek do ]done[ ]    is 1-trit     (   t --> t )
 
[ swap jiggle 1+ ]this[ 
  swap peek 
  swap jiggle peek do ]done[ ]              is 2-trits    ( t t --> t )
 
[ 1-trit $ "true" $ "maybe" $ "false" ]     is trit$      (   t --> $ )
 
[ trit$ echo$ ]                             is echotrit   (   t -->   )
 
[ dup echotrit true = if sp ]               is paddedtrit (   t -->   )
 
          ( true  maybe false )
[ 1-trit    false maybe true  ]             is t.not      (   t --> t )
 
                    ( true  maybe false )
[ 2-trits ( true  ) [ false maybe true  ]
          ( maybe ) [ maybe maybe true  ]
          ( false ) [ true  true  true  ] ] is t.nand     ( t t --> t )
 
                    ( true  maybe false )
[ 2-trits ( true  ) [ true  maybe false ]
          ( maybe ) [ maybe maybe false ]
          ( false ) [ false false false ] ] is t.and      ( t t --> t )
 
                    ( true  maybe false )
[ 2-trits ( true  ) [ true  true  true  ]
          ( maybe ) [ true  maybe maybe ]
          ( false ) [ true  maybe false ] ] is t.or       ( t t --> t )
 
                    ( true  maybe false )
[ 2-trits ( true  ) [ false maybe true  ]
          ( maybe ) [ maybe maybe maybe ]
          ( false ) [ true  maybe false ] ] is t.xor      ( t t --> t )
 
( Quackery does not have operators for material implication 
  (if a then b, a implies b) or material equivalence 
  (a ≣ b, A <=> b). However, The Book of Quackery notes that they
  can be defined as:  
    "[ swap not or ] is implies ( a b --> c )"
    "[ xor not ]     is <=>     ( a b --> c )"
  so we will adapt these to test some of the ternary operators. )
 
[ swap t.not t.or ]                         is t.implies  ( t t --> t )
 
[ t.xor t.not ]                             is t.<=>      ( t t --> t )
 
cr
say " t.not     | true  | maybe | false"             cr
say " ---------------------------------"             cr
say "           | " true  t.not paddedtrit           sp
say            "| " maybe t.not paddedtrit           sp
say            "| " false t.not paddedtrit           cr
cr
say " t.and     | true  | maybe | false"             cr
say " ---------------------------------"             cr
say "   true    | " true  true  t.and paddedtrit     sp
say            "| " true  maybe t.and paddedtrit     sp
say            "| " true  false t.and paddedtrit     cr
say "   maybe   | " maybe true  t.and paddedtrit     sp
say            "| " maybe maybe t.and paddedtrit     sp
say            "| " maybe false t.and paddedtrit     cr
say "   false   | " false true  t.and paddedtrit     sp
say            "| " false maybe t.and paddedtrit     sp
say            "| " false false t.and paddedtrit     cr
cr
say " t.or      | true  | maybe | false"             cr
say " ---------------------------------"             cr
say "   true    | " true  true   t.or paddedtrit     sp
say            "| " true  maybe  t.or paddedtrit     sp
say            "| " true  false  t.or paddedtrit     cr
say "   maybe   | " maybe true   t.or paddedtrit     sp
say            "| " maybe maybe  t.or paddedtrit     sp
say            "| " maybe false  t.or paddedtrit     cr
say "   false   | " false true   t.or paddedtrit     sp
say            "| " false maybe  t.or paddedtrit     sp
say            "| " false false  t.or paddedtrit     cr
cr
say " t.implies | true  | maybe | false"             cr
say " ---------------------------------"             cr
say "   true    | " true  true  t.implies paddedtrit sp
say            "| " true  maybe t.implies paddedtrit sp
say            "| " true  false t.implies paddedtrit cr
say "   maybe   | " maybe true  t.implies paddedtrit sp
say            "| " maybe maybe t.implies paddedtrit sp
say            "| " maybe false t.implies paddedtrit cr
say "   false   | " false true  t.implies paddedtrit sp
say            "| " false maybe t.implies paddedtrit sp
say            "| " false false t.implies paddedtrit cr
cr
say " t.<=>     | true  | maybe | false"             cr
say " ---------------------------------"             cr
say "   true    | " true  true  t.<=> paddedtrit     sp
say            "| " true  maybe t.<=> paddedtrit     sp
say            "| " true  false t.<=> paddedtrit     cr
say "   maybe   | " maybe true  t.<=> paddedtrit     sp
say            "| " maybe maybe t.<=> paddedtrit     sp
say            "| " maybe false t.<=> paddedtrit     cr
say "   false   | " false true  t.<=> paddedtrit     sp
say            "| " false maybe t.<=> paddedtrit     sp
say            "| " false false t.<=> paddedtrit     cr