Ternary logic: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|Perl}}: a single file of runnable code)
Line 4,269: Line 4,269:


=={{header|Perl}}==
=={{header|Perl}}==
Logic values are: -1 = false, 0 = maybe, 1 = true
File <TT>Trit.pm</TT>:
<syntaxhighlight lang="perl">package Trit;
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';


package Trit;
# -1 = false ; 0 = maybe ; 1 = true


our @ISA = qw(Exporter);
use Exporter 'import';
our @EXPORT = qw(TRUE FALSE MAYBE);

our @EXPORT_OK = qw(TRUE FALSE MAYBE is_true is_false is_maybe);
our %EXPORT_TAGS = (
all => \@EXPORT_OK,
const => [qw(TRUE FALSE MAYBE)],
bool => [qw(is_true is_false is_maybe)],
);


use List::Util qw(min max);
use List::Util qw(min max);
Line 4,303: Line 4,299:
;
;


sub new
sub new {
my ($class, $v) = @_;
{
my ($class, $v) = @_;
my $ret =
!defined($v) ? 0 :
my $ret =
!defined($v) ? 0 :
$v eq 'true' ? 1 :
$v eq 'true' ? 1 :
$v eq 'false'? -1 :
$v eq 'false'? -1 :
$v eq 'maybe'? 0 :
$v eq 'maybe'? 0 :
$v > 0 ? 1 :
$v > 0 ? 1 :
$v < 0 ? -1 :
$v < 0 ? -1 :
0;
return bless \$ret, $class;
0;
return bless \$ret, $class;
}
}


Line 4,321: Line 4,316:
sub MAYBE() { new Trit( 0) }
sub MAYBE() { new Trit( 0) }


sub clone
sub clone {
my $ret = ${$_[0]};
{
my $ret = ${$_[0]};
return bless \$ret, ref($_[0]);
return bless \$ret, ref($_[0]);
}
}


sub tostr { ${$_[0]} > 0 ? "true" : ${$_[0]} < 0 ? "false" : "maybe" }
sub tostr { ${$_[0]} > 0 ? 'true' : ${$_[0]} < 0 ? 'false' : 'maybe' }
sub tonum { ${$_[0]} }
sub tonum { ${$_[0]} }

sub is_true { ${$_[0]} > 0 }
sub is_false { ${$_[0]} < 0 }
sub is_maybe { ${$_[0]} == 0 }


sub cmp { ${$_[0]} <=> ${$_[1]} }
sub cmp { ${$_[0]} <=> ${$_[1]} }
Line 4,339: Line 4,329:
sub or { new Trit(max(${$_[0]}, ${$_[1]}) ) }
sub or { new Trit(max(${$_[0]}, ${$_[1]}) ) }


sub equiv { new Trit( ${$_[0]} * ${$_[1]} ) }</syntaxhighlight>
sub equiv { new Trit( ${$_[0]} * ${$_[1]} ) }

File <TT>test.pl</TT>:
package main;
<syntaxhighlight lang="perl">use Trit ':all';
Trit->import;


my @a = (TRUE(), MAYBE(), FALSE());
my @a = (TRUE(), MAYBE(), FALSE());


# prefix ! (not) ['~' also can be used]
print "\na\tNOT a\n";
say "a\tNOT a";
print "$_\t".(!$_)."\n" for @a; # Example of use of prefix operator NOT. Tilde ~ also can be used.
print "$_\t".(!$_)."\n" for @a;


# infix & (and)
say "\nAND\t" . join("\t",@a);
for my $a (@a) { print $a; print "\t" . ($a & $_) for @a; say '' }


# infix | (or)
print "\nAND\t".join("\t",@a)."\n";
say "\nOR\t".join("\t",@a);
for my $a (@a) {
for my $a (@a) { print $a; print "\t" . ($a | $_) for @a; say '' }
print $a;
for my $b (@a) {
print "\t".($a & $b); # Example of use of infix & (and)
}
print "\n";
}


# infix eq (equivalence)
print "\nOR\t".join("\t",@a)."\n";
say "\nEQV\t".join("\t",@a);
for my $a (@a) {
for my $a (@a) { print $a; print "\t" . ($a eq $_) for @a; say '' }
print $a;
for my $b (@a) {
print "\t".($a | $b); # Example of use of infix | (or)
}
print "\n";
}

print "\nEQV\t".join("\t",@a)."\n";
for my $a (@a) {
print $a;
for my $b (@a) {
print "\t".($a eq $b); # Example of use of infix eq (equivalence)
}
print "\n";
}


# infix == (equality)
print "\n==\t".join("\t",@a)."\n";
say "\n==\t".join("\t",@a);
for my $a (@a) {
for my $a (@a) { print $a; print "\t" . ($a == $_) for @a; say '' }</syntaxhighlight>
print $a;
for my $b (@a) {
print "\t".($a == $b); # Example of use of infix == (equality)
}
print "\n";
}</syntaxhighlight>
{{out}}
{{out}}
<pre>a NOT a
<pre>a NOT a