

 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

 
tritValues = .array~of(.trit~true, .trit~false, .trit~maybe)
tab = '09'x
 
say "not operation (\)"
loop a over tritValues
    say "\"a":" (\a)
end
 
say
say "and operation (&)"
loop aa over tritValues
    loop bb over tritValues
        say (aa" & "bb":" (aa&bb))
    end
end
 
say
say "or operation (|)"
loop aa over tritValues
    loop bb over tritValues
        say (aa" | "bb":" (aa|bb))
    end
end
 
say
say "implies operation (&&)"
loop aa over tritValues
    loop bb over tritValues
        say (aa" && "bb":" (aa&&bb))
    end
end
 
say
say "equals operation (=)"
loop aa over tritValues
    loop bb over tritValues
        say (aa" = "bb":" (aa=bb))
    end
end
 
::class trit
-- making this a private method so we can control the creation
-- of these.  We only allow 3 instances to exist
::method new class private
  forward class(super)
 
::method init class
  expose true false maybe
  -- delayed creation
  true = .nil
  false = .nil
  maybe = .nil
 
-- read only attribute access to the instances.
-- these methods create the appropriate singleton on the first call
::attribute true class get
  expose true
  if true == .nil then true = self~new("True")
  return true
 
::attribute false class get
  expose false
  if false == .nil then false = self~new("False")
  return false
 
::attribute maybe class get
  expose maybe
  if maybe == .nil then maybe = self~new("Maybe")
  return maybe
 
-- create an instance
::method init
  expose value
  use arg value
 
-- string method to return the value of the instance
::method string
  expose value
  return value
 
-- "and" method using the operator overload
::method "&"
  use strict arg other
  if self == .trit~true then return other
  else if self == .trit~maybe then do
      if other == .trit~false then return .trit~false
      else return .trit~maybe
  end
  else return .trit~false
 
-- "or" method using the operator overload
::method "|"
  use strict arg other
  if self == .trit~true then return .trit~true
  else if self == .trit~maybe then do
      if other == .trit~true then return .trit~true
      else return .trit~maybe
  end
  else return other
 
-- implies method...using the XOR operator for this
::method "&&"
  use strict arg other
  if self == .trit~true then return other
  else if self == .trit~maybe then do
      if other == .trit~true then return .trit~true
      else return .trit~maybe
  end
  else return .trit~true
 
-- "not" method using the operator overload
::method "\"
  if self == .trit~true then return .trit~false
  else if self == .trit~maybe then return .trit~maybe
  else return .trit~true
 
-- "equals" using the "=" override.  This makes a distinction between
-- the "==" operator, which is real equality and the "=" operator, which
-- is trinary equality.
::method "="
  use strict arg other
  if self == .trit~true then return other
  else if self == .trit~maybe then return .trit~maybe
  else return \other
 