Ternary logic: Difference between revisions

No edit summary
(13 intermediate revisions by 8 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 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,655 ⟶ 1,776:
sealed class Trit
{
bool _value;
bool cast() = _value;
constructor(object v)
{
if (v != nil)
{
_value := cast bool(v);
}
}
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)
{
if (nil == _value)
{
^ b.and:(nil) \ back:(nilValue)
}
else
{
^ _value.and((lazy:cast bool(b))) \ back:(nilValue)
}
}
Trit or(b)
{
if (nil == _value)
{
^ b.or:(nilValue) \ back:(nilValue)
}
else
{
^ _value.or((lazy:cast bool(b))) \ back:(nilValue)
}
}
Trit implies(b)
= self.Inverted.or(b);
string toPrintable() = _value.toPrintable() \ back:("maybe");
}
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:(right));
console.printLine(left, " ≡ ", right, " = ", left.equivalent:(right))
}
}
Line 3,334 ⟶ 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,346 ⟶ 3,474:
}
 
val .or = ffn(.a, given.b) switch[and] .a, .b {
case false, null:
case null, false:
Line 3,353 ⟶ 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 3,649 ⟶ 3,777:
q=trit()
m=trit()
a=q.false
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;
Logic values are: -1 = false, 0 = maybe, 1 = true
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
package Trit;
use List::Util qw(min max);
 
our @ISA = qw(Exporter);
our @EXPORT = qw(TRUE FALSE MAYBE%E);
 
my %E = (true => 1, false => -1, maybe => 0);
use List::Util qw(min max);
 
use overload
'<=>' => sub ($a,$b) { $_[0]a->clonecmp($b) },
'<=>cmp' => sub ($a,$b) { $_[0]a->cmp($_[1]b) },
'cmp==' => sub ($a,$b,$) { $_[0]->cmp($_[1])a == $$b },
'==eq' => sub { (${a,$_[0]}b,$) == ${ $a->equiv($_[1]}b) },
'eq>' => sub ($a,$b,$) { $_[0]-$a >equiv( $_[1])E{$b} },
'><' => sub ($a,$b,$) { ${$_[0]}a < > $E{$_[1]b} },
'<>=' => sub ($a,$b,$) { ${$_[0]}a <>= ${$_[1]}b },
'><=' => sub ($a,$b,$) { ${$_[0]}a ><= ${$_[1]}b },
'<=|' => sub { (${a,$_[0]}b,$,$,$) <={ ${a->or($_[1]}b) },
'|&' => sub ($a,$b,$,$,$) { $_[0]a->orand($_[1]b) },
'&!' => sub ($a,$,$) { $_[0]a->andnot($_[1]) },
'!~' => sub ($a,$,$,$,$) { $_[0]a->not() },
'~neg' => sub ($a,$,$) { $_[0]->not()$$a },
'""' => sub ($a,$,$) { $_[0]a->tostr() },
'0+' => sub ($a,$,$) { $_[0]a->tonum() },
;
 
sub neweqv ($a,$b) {
$$a == $E{maybe} || $E{$b} == $E{maybe} ? $E{maybe} : # either arg 'maybe', return 'maybe'
my ($class, $v) = @_;
$$a == $E{false} && $E{$b} == $E{false} ? $E{true} : # both args 'false', return 'true'
my $ret =
min $$a, $E{$b} # either arg 'false', return 'false', otherwise 'true'
!defined($v) ? 0 :
$v eq 'true' ? 1 :
$v eq 'false'? -1 :
$v eq 'maybe'? 0 :
$v > 0 ? 1 :
$v < 0 ? -1 :
0;
return bless \$ret, $class;
}
 
# do tests in a manner that avoids overloaded operators
sub TRUE() { new Trit( 1) }
sub FALSE() { new Trit(-1$class, $v) }{
my $value =
sub MAYBE() { new Trit( 0) }
! defined $v ? $E{maybe} :
 
$v =~ /true/ ? $E{true} :
sub clone {
my $retv =~ /false/ ? $E{$_[0]false}; :
$v =~ /maybe/ ? $E{maybe} :
return bless \$ret, ref($_[0]);
$v gt $E{maybe} ? $E{true} :
$v lt $E{maybe} ? $E{false} :
$E{maybe} ;
bless \$value, $class;
}
 
sub tostr ($a) { ${$_[0]}a > 0$E{maybe} ? 'true' : ${$_[0]}a < 0$E{maybe} ? 'false' : 'maybe' }
sub tonum ($a) { ${$_[0]}a }
 
sub cmpnot { ($a) {$_[0]} <=Trit->new( -${$_[1]}a ) }
sub notcmp { new Trit(-$a,$b) { Trit->new( $_[0]}a <=> $b ) }
sub and { new ($a,$b) { Trit->new( min( ${$_[0]}a, ${$_[1]})b ) }
sub or { new ($a,$b) { Trit->new( max( ${$_[0]}a, ${$_[1]})b ) }
sub equiv ($a,$b) { Trit->new( eqv $a, $b ) }
 
sub equiv { new Trit( ${$_[0]} * ${$_[1]} ) }
 
package main;
Trit->import;
 
my @a = (TRUE Trit->new($E{true}), MAYBETrit->new($E{maybe}), FALSETrit->new($E{false}) );
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 "a\na\tNOT a";
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
<pre>a NOT a
 
a NOT a
true false
maybe maybe
Line 6,405 ⟶ 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,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))];
 
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,904 ⟶ 7,025:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var False = -1
var Maybe = 0
var True = 1
890

edits