RosettaCodeData/Task/Ternary-logic/Crystal/ternary-logic.cr
2026-04-30 12:34:36 -04:00

55 lines
1.1 KiB
Crystal
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

enum Trit
FALSE
MAYBE
TRUE
def ~ ()
case self
in .false? then TRUE
in .maybe? then MAYBE
in .true? then FALSE
end
end
def & (other : Trit)
Math.min(self, other)
end
def | (other : Trit)
Math.max(self, other)
end
# equivalent
def === (other : Trit)
case self
in .false? then ~other
in .maybe? then MAYBE
in .true? then other
end
end
# if-then
def > (other : Trit)
case self
in .false? then TRUE
in .maybe? then Math.max(other, MAYBE)
in .true? then other
end
end
end
FALSE = Trit::FALSE
MAYBE = Trit::MAYBE
TRUE = Trit::TRUE
puts "Testing some equivalences."
puts
puts "p ⊃ q ≡ ¬p q"
Indexable.each_cartesian([[FALSE, MAYBE, TRUE]] * 2) do |(p, q)|
puts "%5s ⊃ %-5s ≡ ¬%-5s %-5s : %5s ≡ %-5s : %-5s" % {p, q, p, q, (p > q), (~p | q), (p > q) === (~p | q) }
end
puts
puts "¬(p q) ≡ ¬p ¬q"
Indexable.each_cartesian([[FALSE, MAYBE, TRUE]] * 2) do |(p, q)|
puts "¬(%-5s %-5s) ≡ ¬%-5s ¬%-5s : %5s ≡ %-5s : %-5s" % {p, q, p, q, ~(p & q), ~p | ~q, ~(p & q) === ~p | ~q }
end