Ternary logic: Difference between revisions

imported>Arakov
(10 intermediate revisions by 6 users not shown)
Line 933:
MAYBE EQV TRUE = MAYBE
TRUE EQV TRUE = TRUE
</pre>
 
=={{header|Bruijn}}==
Direct translations of the truth tables to lambda calculus. The operators could be golfed significantly. If you do so, please add them here!
 
For applications of Ternary logic, see bruijn's [[Balanced_ternary#Bruijn|balanced ternary]] implementation.
<syntaxhighlight lang="bruijn">
true [[[0]]]
 
maybe [[[1]]]
 
false [[[2]]]
 
¬‣ [0 true maybe false]
 
…⋀… [[1 (0 1 1 1) (0 0 0 1) (0 0 0 0)]]
 
…⋁… [[1 (0 0 0 0) (0 1 0 0) (0 1 1 1)]]
 
…⊃… [[1 (0 true 0 1) (0 true 1 1) (0 1 1 1)]]
 
…≡… [[1 (0 true 0 1) (0 1 1 1) (0 0 0 0)]]
 
# --- result samples ---
 
:import std/List .
 
main [[inp <> "=" <> !res ++ "\n"] <++> (cross3 ops trits trits)]
!‣ [0 "false" "maybe" "true"]
…<>… [[1 ++ " " ++ 0]]
inp 0 [[~1 <> (0 [[!1 <> (0 [[!1]])]])]]
res ^(^0) ^(~0) ^(~(~0))
ops (…⋀… : "and") : ((…⋁… : "or") : ((…⊃… : "if") : {}(…≡… : "equiv")))
trits true : (maybe : {}false)
</syntaxhighlight>
 
{{out}}
<pre>
and true true = true
and true maybe = maybe
and true false = false
and maybe true = maybe
and maybe maybe = maybe
and maybe false = false
and false true = false
and false maybe = false
and false false = false
or true true = true
or true maybe = true
or true false = true
or maybe true = true
or maybe maybe = maybe
or maybe false = maybe
or false true = true
or false maybe = maybe
or false false = false
if true true = true
if true maybe = true
if true false = true
if maybe true = maybe
if maybe maybe = maybe
if maybe false = true
if false true = false
if false maybe = maybe
if false false = true
equiv true true = true
equiv true maybe = maybe
equiv true false = false
equiv maybe true = maybe
equiv maybe maybe = maybe
equiv maybe false = maybe
equiv false true = false
equiv false maybe = maybe
equiv false false = true
</pre>
 
Line 1,646 ⟶ 1,720:
That's no real fun, but lookup can then be done with
<syntaxhighlight lang="delphi">Result := tvl_and[A, B];</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
sym$[] = [ "F" "?" "T" ]
arrbase sym$[] -1
#
func tnot x .
return -x
.
func tand x y .
if x > y
return tand y x
.
return x
.
func tor x y .
if x < y
return tor y x
.
return x
.
func teqv x y .
return x * y
.
func timp x y .
if -y > x
return -y
.
return x
.
print " (AND) ( OR) (EQV) (IMP) (NOT)"
print " F ? T F ? T F ? T F ? T "
print " -----------------------------------------"
for i = -1 to 1
o$ = " " & sym$[i] & " | "
o$ &= sym$[tand -1 i] & " " & sym$[tand 0 i] & " " & sym$[tand 1 i]
o$ &= " "
o$ &= sym$[tor -1 i] & " " & sym$[tor 0 i] & " " & sym$[tor 1 i]
o$ &= " "
o$ &= sym$[timp -1 i] & " " & sym$[timp 0 i] & " " & sym$[timp 1 i]
o$ &= " "
o$ &= sym$[timp -1 i] & " " & sym$[timp 0 i] & " " & sym$[timp 1 i]
o$ &= " " & sym$[tnot i]
print o$
.
</syntaxhighlight>
 
=={{header|Elena}}==
Line 1,668 ⟶ 1,789:
Trit equivalent(b)
{
= _value.equal(cast bool(b)) \ back:nilValue;
var val2 := cast bool(b) \ back(nil);
 
if (val2 != nil && _value != nil)
{
^ _value.equal(val2)
};
 
^ nilValue;
}
Trit Inverted
= _value.Inverted \ back:(nilValue);
Trit and(b)
Line 1,677 ⟶ 1,807:
if (nil == _value)
{
^ b.and:(nil) \ back:(nilValue)
}
else
{
^ _value.and($lazy cast bool(b)) \ back:(nilValue)
}
}
Line 1,689 ⟶ 1,819:
if (nil == _value)
{
^ b.or:(nilValue) \ back:(nilValue)
}
else
{
^ _value.or($lazy cast bool(b)) \ back:(nilValue)
}
}
Line 1,700 ⟶ 1,830:
= self.Inverted.or(b);
string toPrintable() = _value.toPrintable() \ back:("maybe");
}
public program()
{
Trit a := true;
Trit b := nilValue;
 
List<Trit> values := new Trit[]{true, nilValue, false};
values.forEach::(left)
{
console.printLine("¬",left," = ", left.Inverted);
values.forEach::(right)
{
console.printLine(left, " & ", right, " = ", left && right);
console.printLine(left, " | ", right, " = ", left || right);
console.printLine(left, " → ", right, " = ", left.implies:(right));
console.printLine(left, " ≡ ", right, " = ", left.equivalent:(right))
}
}
Line 3,337 ⟶ 3,464:
=={{header|langur}}==
{{trans|Go}}
{{works with|langur|0.6.12}}
 
<syntaxhighlight lang="langur"># borrowing null for "maybe"
val .trSet = [false, null, true]
 
val .and = ffn(.a, given.b) switch[and] .a, .b {
case true, null:
case null, true:
Line 3,349 ⟶ 3,474:
}
 
val .or = ffn(.a, given.b) switch[and] .a, .b {
case false, null:
case null, false:
Line 3,356 ⟶ 3,481:
}
 
val .imply = ffn(.a, .b) if(.a nor .b: not? .a; .b)
 
# formatting function for the result values
# replacing null with "maybe"
# using left alignment of 5 code points
val .F = ffn(.r) $"\{nn [.r, "maybe"]:-5}"
 
writeln "a not a"
Line 6,403 ⟶ 6,528:
end func;
 
$ syntax expr: .().xor.() is -> 15;
const func trit: (in trit: aTrit1) xor (in trit: aTrit2) is
return tritImplies[succ(ord(aTrit1))][succ(ord(aTrit2))];
Line 6,410 ⟶ 6,535:
return tritImplies[succ(ord(aTrit1))][succ(ord(aTrit2))];
 
syntax expr: .(). == .() is <-> 12;
const func trit: (in trit: aTrit1) == (in trit: aTrit2) is
return tritEquiv[succ(ord(aTrit1))][succ(ord(aTrit2))];
 
const func trit: rand (in trit: low, in trit: high) is
return trit conv (rand(ord(low), ord(high)));
 
# Begin of test code
Line 6,902 ⟶ 7,025:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var False = -1
var Maybe = 0
var True = 1
890

edits