Πέμπτη 5 Φεβρουαρίου 2026

Ternary Logic

https://rosettacode.org/wiki/Ternary_logic#M2000_Interpreter 

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.


module Ternary_logic {
class trit {
private:
variant val
function copy() {
m=this
m.trit
=m
}
public:
enum ternary {
True="True"
Maybe="Maybe"
False="False"
}
function true() {
=.copy(.True)
}
function maybe() {
=.copy(.Maybe)
}
function false() {
=.copy(.False)
}
operator "==" (k as trit) {
push .val=k.val
}
operator "^^" (k as trit) {
select enum .val
case .True
.val<=k.val
case .False
.val<=.False
case else
if k.val=.False then .val<=.False else .val<=.Maybe
end select
}
operator "^|" (k as trit) {
select enum .val
case .True
.val<=k.val
case .False
.val<=.True
case else
if k.val=.True then .val<=.True else .val<=.Maybe
end select
}
operator "||" (k as trit) {
select enum .val
case .False
.val<=k.val
case .True
.val<=.True
case else
if k.val=.True then .val<=.True else .val<=.Maybe
end select
}
operator "~~" (k as trit) {
select enum .val
case .True
.val<=k.val
case .False
if k.val=.True then .val<=.False else.if k.val=.False then .val<=.True else .val<=k.val
case else
.val<=.Maybe
end select
}
operator unary {
select enum .val
case .True
.val<=.False
case .False
.val<=.True
end select
}
group value {
value {
link parent val to val
=val
}
}
module trit {
if empty or not isnum then
read s as .ternary=.Maybe
.val<=s
else.if isnum then
read what
if what then
.val<=.True
else
.val<=.False
end if
end if
}
}
function enum2array(t) {
m=each(t)
while m {data eval(m)}
=array([])
}
string out, nl={
}
q=trit()
m=trit()
k=enum2array(q.ternary)
out ="not a" + nl
a=each(k)
while a
q=trit(array(a))
z=-q
out +="    ternary_not "+(q.value) + " = " + (z.value) + nl
end while
out +="a and b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q ^^ m
out += "    " + (q.value) + " ternary_and " + (m.value) + " = " + (z.value) + nl
end while
end while
out +="a or b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q || m
out += "    " + (q.value) + " ternary_or " + (m.value) + " = " + (z.value) + nl
end while
end while
out +="if a then b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q ^| m
out += "    if " + (q.value) + " then " + (m.value) + " = " + (z.value) + nl
end while
end while
out +="a is equivalent to b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q ~~ m
out += "    "+(q.value) + " is equivalent to " + (m.value) + " = " + (z.value) + nl
end while
end while
report out
clipboard out
}
Ternary_logic

Output:

not a

    ternary_not True = False

    ternary_not Maybe = Maybe

    ternary_not False = True

a and b

    True ternary_and True = True

    True ternary_and Maybe = Maybe

    True ternary_and False = False

    Maybe ternary_and True = Maybe

    Maybe ternary_and Maybe = Maybe

    Maybe ternary_and False = False

    False ternary_and True = False

    False ternary_and Maybe = False

    False ternary_and False = False

a or b

    True ternary_or True = True

    True ternary_or Maybe = True

    True ternary_or False = True

    Maybe ternary_or True = True

    Maybe ternary_or Maybe = Maybe

    Maybe ternary_or False = Maybe

    False ternary_or True = True

    False ternary_or Maybe = Maybe

    False ternary_or False = False

if a then b

    if True then True = True

    if True then Maybe = Maybe

    if True then False = False

    if Maybe then True = True

    if Maybe then Maybe = Maybe

    if Maybe then False = Maybe

    if False then True = True

    if False then Maybe = True

    if False then False = True

a is equivalent to b

    True is equivalent to True = True

    True is equivalent to Maybe = Maybe

    True is equivalent to False = False

    Maybe is equivalent to True = Maybe

    Maybe is equivalent to Maybe = Maybe

    Maybe is equivalent to False = Maybe

    False is equivalent to True = False

    False is equivalent to Maybe = Maybe

    False is equivalent to False = True



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.