Rosetta Code

Ternary logic

Build a small truth table for three-valued logic with true, maybe, and false states.

Medium View source
Source rosettacode/popular/ternary_logic.vibe
# title: Ternary logic
# source: https://rosettacode.org/wiki/Ternary_logic
# category: Rosetta Code
# difficulty: Medium
# summary: Build a small truth table for three-valued logic with true, maybe, and false states.
# tags: popular, logic, booleans, truth-tables
# vibe: 0.2

def logic_not(value)
  if value == "T"
    "F"
  elsif value == "F"
    "T"
  else
    "M"
  end
end

def logic_and(left, right)
  if left == "F" || right == "F"
    "F"
  elsif left == "T" && right == "T"
    "T"
  else
    "M"
  end
end

def logic_or(left, right)
  if left == "T" || right == "T"
    "T"
  elsif left == "F" && right == "F"
    "F"
  else
    "M"
  end
end

def truth_rows
  values = ["T", "M", "F"]
  rows = []
  left_index = 0

  while left_index < values.size
    right_index = 0
    while right_index < values.size
      left = values[left_index]
      right = values[right_index]
      rows = rows.push([
        left,
        right,
        logic_and(left, right),
        logic_or(left, right),
        logic_not(left)
      ])
      right_index = right_index + 1
    end
    left_index = left_index + 1
  end

  rows
end

def run
  rows = []
  rows = rows.push(["left", "right", "and", "or", "not_left"])
  rows + truth_rows
end
Output
Press run to execute run from this example.
rosetta-code popular logic booleans truth-tables browser-runner