Ternary logic: Difference between revisions
m
no edit summary
No edit summary |
MazenMokhtar (talk | contribs) mNo edit summary |
||
(19 intermediate revisions by 9 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}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,655 ⟶ 1,776:
sealed class Trit
{
{
var val2 := cast bool(b) \ back(nil);
if (val2 != nil && _value != nil)
{
^ _value.equal(val2)
};
^ nilValue;
}
}
Line 1,706 ⟶ 1,836:
{
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
console.printLine(left, " ≡ ", right, " = ", left.equivalent
}
}
Line 2,965 ⟶ 3,095:
L3.iff(a, 2 == 2) // undefined
</syntaxhighlight>
Here is a compact, highly readable solution that uses undefined for the third value of the trit
<syntaxhighlight lang="javascript">
nand = (a, b) => (a == false || b == false) ? true : (a == undefined || b == undefined) ? undefined : false
not = (a) => nand(a, a)
and = (a, b) => not(nand(a, b))
Line 2,979 ⟶ 3,108:
... to test it
<syntaxhighlight lang="javascript">
trit = [false, undefined, true]
functor = {nand, and, or, nor, implies, iff, xor}
display = {nand: '⊼', and: '∧', or: '∨', nor: '⊽', implies: '
log = 'NOT\n';
for (let a of
log += '\nNAND AND OR NOR IMPLIES
for (let a of
for (let b of
log += "\n"
for (let op in functor) log += `${display[a]} ${display[op]} ${display[b]} = ${display[functor[op](a, b)]} `
}
}
Line 2,998 ⟶ 3,127:
<syntaxhighlight lang="text">
NOT
¬F =
¬
¬T =
NAND AND OR NOR IMPLIES
F ⊼ F = T F ∧ F = F F ∨ F = F F ⊽ F = T F
F ⊼
F ⊼ T = T F ∧ T = F F ∨ T = T F ⊽ T = F F
T ⊼ F = T T ∧ F = F T ∨ F = T T ⊽ F = F T
T ⊼
T ⊼ T = F T ∧ T = T T ∨ T = T T ⊽ T = F T
</syntaxhighlight>
Line 3,334 ⟶ 3,463:
=={{header|langur}}==
{{trans|Go}}
<syntaxhighlight lang="langur"># borrowing null for "maybe"
val .trSet = [false, null, true]
val .and =
case true, null:
case null, true:
Line 3,346 ⟶ 3,473:
}
val .or =
case false, null:
case null, false:
Line 3,353 ⟶ 3,480:
}
val .imply =
# formatting function for the result values
# replacing null with "maybe"
# using left alignment of 5 code points
val .F =
writeln "a not a"
for .a in .trSet {
writeln
}
Line 3,368 ⟶ 3,495:
for .a in .trSet {
for .b in .trSet {
writeln
}
}
Line 3,375 ⟶ 3,502:
for .a in .trSet {
for .b in .trSet {
writeln
}
}
Line 3,382 ⟶ 3,509:
for .a in .trSet {
for .b in .trSet {
writeln
}
}
Line 3,389 ⟶ 3,516:
for .a in .trSet {
for .b in .trSet {
writeln
}
}
</syntaxhighlight>
{{out}}
Line 3,649 ⟶ 3,777:
q=trit()
m=trit()
k=enum2array(q.ternary)
out ="not a" + nl
Line 3,753 ⟶ 3,880:
False is equivalent to False = True
</pre>
=={{header|Maple}}==
Line 4,481 ⟶ 4,607:
=={{header|Perl}}==
<syntaxhighlight lang="perl">use v5.36;
package Trit;
use List::Util qw(min max);
our @ISA = qw(Exporter);
our @EXPORT = qw(
my %E = (true => 1, false => -1, maybe => 0);
use overload
'<=>'
'
'
'
'
'
'
'
'
'
'
'
'
'""' => sub ($a,$,$) { $
'0+' => sub ($a,$,$) { $
;
sub
$$a == $E{maybe} || $E{$b} == $E{maybe} ? $E{maybe} : # either arg 'maybe', return 'maybe'
$$a == $E{false} && $E{$b} == $E{false} ? $E{true} : # both args 'false', return 'true'
min $$a, $E{$b} # either arg 'false', return 'false', otherwise 'true'
}
# do tests in a manner that avoids overloaded operators
sub
my $value =
! defined $v ? $E{maybe} :
$v =~ /true/ ? $E{true} :
$v =~ /maybe/ ? $E{maybe} :
$v gt $E{maybe} ? $E{true} :
$v lt $E{maybe} ? $E{false} :
$E{maybe} ;
bless \$value, $class;
}
sub tostr ($a) { $
sub tonum ($a) { $
sub
sub
sub and
sub or
sub equiv ($a,$b) { Trit->new( eqv $a, $b ) }
package main;
Trit->import;
my @a = (
printf "Codes for logic values: %6s = %d %6s = %d %6s = %d\n", @a[0, 0, 1, 1, 2, 2];
# prefix ! (not) ['~' also can be used]
say "
print "$_\t".(!$_)."\n" for @a;
Line 4,557 ⟶ 4,678:
# infix | (or)
say "\nOR\t" . join("\t",@a);
for my $a (@a) { print $a; print "\t" . ($a | $_) for @a; say '' }
# infix eq (equivalence)
say "\nEQV\t" . join("\t",@a);
for my $a (@a) { print $a; print "\t" . ($a eq $_) for @a; say '' }
# infix == (equality)
say "\n==\t" . join("\t",@a);
for my $a (@a) { print $a; print "\t" . ($a == $_) for @a; say '' }</syntaxhighlight>
{{out}}
<pre>Codes for logic values: true = 1 maybe = 0 false = -1
a NOT a
true false
maybe maybe
Line 6,405 ⟶ 6,528:
end func;
const func trit: (in trit: aTrit1) xor (in trit: aTrit2) is
return tritImplies[succ(ord(aTrit1))][succ(ord(aTrit2))];
Line 6,412 ⟶ 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))];
# Begin of test code
Line 6,904 ⟶ 7,025:
=={{header|Wren}}==
<syntaxhighlight lang="
var Maybe = 0
var True = 1
|