++ed by:

1 non-PAUSE user.

Martin Becker
and 1 contributors

NAME

Math::Logic::Ternary::Trit - ternary logical information unit

VERSION

This documentation refers to version 0.004 of Math::Logic::Ternary::Trit.

SYNOPSIS

  use Math::Logic::Ternary::Trit;

  $a = Math::Logic::Ternary::Trit->true;
  $a = Math::Logic::Ternary::Trit->from_string('true'); # same as above
  $a = Math::Logic::Ternary::Trit->from_int(1);         # same as above
  $a = Math::Logic::Ternary::Trit->from_bool(1 == 1);   # same as above

  $b = Math::Logic::Ternary::Trit->false;
  $b = Math::Logic::Ternary::Trit->from_string('false'); # same as above
  $b = Math::Logic::Ternary::Trit->from_int(-1);         # same as above
  $b = Math::Logic::Ternary::Trit->from_bool(0 == 1);    # same as above

  $c = Math::Logic::Ternary::Trit->nil;
  $c = Math::Logic::Ternary::Trit->from_string('nil');  # same as above
  $c = Math::Logic::Ternary::Trit->from_int(0);         # same as above
  $c = Math::Logic::Ternary::Trit->from_bool(undef);    # same as above

  $d = $a->and($b)->or($a->not->and($c));

  print $d->as_string;                                  # print '$false'
  print $d->as_int;                                     # print -1

DESCRIPTION

Math::Logic::Ternary::Trit is the class for ternary logical values aka trits. This class has only three instances, representing ternary truth values: true, false, and nil. It also implements logical operators, conversions and the role defined by Math::Logic::Ternary::Object.

Exports

None.

Constants

nil

The nil trit.

true

The true trit.

false

The false trit.

Unary Operators

sn

Set to nil.

  +-------+-------+
  |   A   | sn A  |
  +-------+-------+
  | false | nil   |
  | nil   | nil   |
  | true  | nil   |
  +-------+-------+
st

Set to true.

  +-------+-------+
  |   A   | st A  |
  +-------+-------+
  | false | true  |
  | nil   | true  |
  | true  | true  |
  +-------+-------+
sf

Set to false.

  +-------+-------+
  |   A   | sf A  |
  +-------+-------+
  | false | false |
  | nil   | false |
  | true  | false |
  +-------+-------+
id

Identity.

  +-------+-------+
  |   A   | id A  |
  +-------+-------+
  | false | false |
  | nil   | nil   |
  | true  | true  |
  +-------+-------+
not

Not.

  +-------+-------+
  |   A   | not A |
  +-------+-------+
  | false | true  |
  | nil   | nil   |
  | true  | false |
  +-------+-------+
up

Up one: increment modulo 3.

  +-------+-------+
  |   A   | up A  |
  +-------+-------+
  | false | nil   |
  | nil   | true  |
  | true  | false |
  +-------+-------+
nup

Not up: swap nil/false.

  +-------+-------+
  |   A   | nup A |
  +-------+-------+
  | false | nil   |
  | nil   | false |
  | true  | true  |
  +-------+-------+
dn

Down one: decrement modulo 3.

  +-------+-------+
  |   A   | dn A  |
  +-------+-------+
  | false | true  |
  | nil   | false |
  | true  | nil   |
  +-------+-------+
ndn

Not down: swap nil/true.

  +-------+-------+
  |   A   | ndn A |
  +-------+-------+
  | false | false |
  | nil   | true  |
  | true  | nil   |
  +-------+-------+
eqn

Equal to nil.

  +-------+-------+
  |   A   | eqn A |
  +-------+-------+
  | false | false |
  | nil   | true  |
  | true  | false |
  +-------+-------+
eqt

Equal to true.

  +-------+-------+
  |   A   | eqt A |
  +-------+-------+
  | false | false |
  | nil   | false |
  | true  | true  |
  +-------+-------+
eqf

Equal to false.

  +-------+-------+
  |   A   | eqf A |
  +-------+-------+
  | false | true  |
  | nil   | false |
  | true  | false |
  +-------+-------+
nen

Not equal to nil.

  +-------+-------+
  |   A   | nen A |
  +-------+-------+
  | false | true  |
  | nil   | false |
  | true  | true  |
  +-------+-------+
net

Not equal to true.

  +-------+-------+
  |   A   | net A |
  +-------+-------+
  | false | true  |
  | nil   | true  |
  | true  | false |
  +-------+-------+
nef

Not equal to false.

  +-------+-------+
  |   A   | nef A |
  +-------+-------+
  | false | false |
  | nil   | true  |
  | true  | true  |
  +-------+-------+
hm

Hamlet: x or not x.

  +-------+-------+
  |   A   | hm A  |
  +-------+-------+
  | false | true  |
  | nil   | nil   |
  | true  | true  |
  +-------+-------+
uhm

Up & hamlet.

  +-------+-------+
  |   A   | uhm A |
  +-------+-------+
  | false | nil   |
  | nil   | true  |
  | true  | true  |
  +-------+-------+
dhm

Down & hamlet.

  +-------+-------+
  |   A   | dhm A |
  +-------+-------+
  | false | true  |
  | nil   | true  |
  | true  | nil   |
  +-------+-------+
orn

Or nil.

  +-------+-------+
  |   A   | orn A |
  +-------+-------+
  | false | nil   |
  | nil   | nil   |
  | true  | true  |
  +-------+-------+
uorn

Up & or nil.

  +-------+--------+
  |   A   | uorn A |
  +-------+--------+
  | false | nil    |
  | nil   | true   |
  | true  | nil    |
  +-------+--------+
dorn

Down & or nil.

  +-------+--------+
  |   A   | dorn A |
  +-------+--------+
  | false | true   |
  | nil   | nil    |
  | true  | nil    |
  +-------+--------+
qt

Quantum: x and not x.

  +-------+-------+
  |   A   | qt A  |
  +-------+-------+
  | false | false |
  | nil   | nil   |
  | true  | false |
  +-------+-------+
uqt

Up & quantum.

  +-------+-------+
  |   A   | uqt A |
  +-------+-------+
  | false | nil   |
  | nil   | false |
  | true  | false |
  +-------+-------+
dqt

Down & quantum.

  +-------+-------+
  |   A   | dqt A |
  +-------+-------+
  | false | false |
  | nil   | false |
  | true  | nil   |
  +-------+-------+
ann

And nil.

  +-------+-------+
  |   A   | ann A |
  +-------+-------+
  | false | false |
  | nil   | nil   |
  | true  | nil   |
  +-------+-------+
uann

Up & ann.

  +-------+--------+
  |   A   | uann A |
  +-------+--------+
  | false | nil    |
  | nil   | nil    |
  | true  | false  |
  +-------+--------+
dann

Down & ann.

  +-------+--------+
  |   A   | dann A |
  +-------+--------+
  | false | nil    |
  | nil   | false  |
  | true  | nil    |
  +-------+--------+

Binary Operators

and

And.

  A and B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   false   false |
  | nil   | false   nil     nil   |
  | true  | false   nil     true  |
  +-------+-----------------------+
or

Or.

  A or B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   nil     true  |
  | nil   | nil     nil     true  |
  | true  | true    true    true  |
  +-------+-----------------------+
xor

Exclusive or.

  A xor B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   nil     true  |
  | nil   | nil     nil     nil   |
  | true  | true    nil     false |
  +-------+-----------------------+
eqv

Equivalent.

  A eqv B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    nil     false |
  | nil   | nil     nil     nil   |
  | true  | false   nil     true  |
  +-------+-----------------------+
imp

Implication (x ==> y).

  A imp B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    true    true  |
  | nil   | nil     nil     true  |
  | true  | false   nil     true  |
  +-------+-----------------------+
rep

Replication (x <== y).

  A rep B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    nil     false |
  | nil   | true    nil     nil   |
  | true  | true    true    true  |
  +-------+-----------------------+
nand

Not and.

  A nand B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    true    true  |
  | nil   | true    nil     nil   |
  | true  | true    nil     false |
  +-------+-----------------------+
nor

Not or.

  A nor B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    nil     false |
  | nil   | nil     nil     false |
  | true  | false   false   false |
  +-------+-----------------------+
cmp

Compare (false < nil < true).

  A cmp B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | nil     false   false |
  | nil   | true    nil     false |
  | true  | true    true    nil   |
  +-------+-----------------------+
asc

Ascending (false < nil < true).

  A asc B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | nil     true    true  |
  | nil   | false   nil     true  |
  | true  | false   false   nil   |
  +-------+-----------------------+
tlr

The lesser (false < nil < true). Logically equivalent to and.

  A tlr B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   false   false |
  | nil   | false   nil     nil   |
  | true  | false   nil     true  |
  +-------+-----------------------+
tgr

The greater (false < nil < true). Logically equivalent to or.

  A tgr B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   nil     true  |
  | nil   | nil     nil     true  |
  | true  | true    true    true  |
  +-------+-----------------------+
eq

Equal to.

  A eq B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    false   false |
  | nil   | false   true    false |
  | true  | false   false   true  |
  +-------+-----------------------+
ne

Not equal to.

  A ne B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   true    true  |
  | nil   | true    false   true  |
  | true  | true    true    false |
  +-------+-----------------------+
lt

Less than (false < nil < true)

  A lt B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   true    true  |
  | nil   | false   false   true  |
  | true  | false   false   false |
  +-------+-----------------------+
ge

Greater or equal (false < nil < true).

  A ge B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    false   false |
  | nil   | true    true    false |
  | true  | true    true    true  |
  +-------+-----------------------+
gt

Greater than (false < nil < true).

  A gt B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   false   false |
  | nil   | true    false   false |
  | true  | true    true    false |
  +-------+-----------------------+
le

Less or equal (false < nil < true).

  A le B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    true    true  |
  | nil   | false   true    true  |
  | true  | false   false   true  |
  +-------+-----------------------+
cmpu

Compare (unbalanced, nil < true < false).

  A cmpu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     false   false |
  | true  | true    nil     false |
  | false | true    true    nil   |
  +-------+-----------------------+
ascu

Ascending (unbalanced, nil < true < false).

  A ascu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     true    true  |
  | true  | false   nil     true  |
  | false | false   false   nil   |
  +-------+-----------------------+
tlru

The lesser (unbalanced, nil < true < false).

  A tlru B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     nil     nil   |
  | true  | nil     true    true  |
  | false | nil     true    false |
  +-------+-----------------------+
tgru

The greater (unbalanced, nil < true < false).

  A tgru B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     true    false |
  | true  | true    true    false |
  | false | false   false   false |
  +-------+-----------------------+
ltu

Less than (unbalanced, nil < true < false).

  A ltu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | false   true    true  |
  | true  | false   false   true  |
  | false | false   false   false |
  +-------+-----------------------+
geu

Greater or equal (unbalanced, nil < true < false).

  A geu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | true    false   false |
  | true  | true    true    false |
  | false | true    true    true  |
  +-------+-----------------------+
gtu

Greater than (unbalanced, nil < true < false).

  A gtu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | false   false   false |
  | true  | true    false   false |
  | false | true    true    false |
  +-------+-----------------------+
leu

Less or equal (unbalanced, nil < true < false).

  A gtu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | true    true    true  |
  | true  | false   true    true  |
  | false | false   false   true  |
  +-------+-----------------------+
incr

Increment.

  A incr B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | true    false   nil   |
  | nil   | false   nil     true  |
  | true  | nil     true    false |
  +-------+-----------------------+
incc

Increment carry.

  A incc B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   nil     nil   |
  | nil   | nil     nil     nil   |
  | true  | nil     nil     true  |
  +-------+-----------------------+
inccu

Increment carry (unbalanced).

  A inccu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     nil     nil   |
  | true  | nil     nil     true  |
  | false | nil     true    true  |
  +-------+-----------------------+
inccv

Increment carry (negative base).

  A inccv B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     nil     true  |
  | true  | nil     nil     nil   |
  | false | nil     false   nil   |
  +-------+-----------------------+
decr

Decrement.

  A decr B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | nil     false   true  |
  | nil   | true    nil     false |
  | true  | false   true    nil   |
  +-------+-----------------------+
decc

Decrement carry.

  A decc B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | nil     nil     true  |
  | nil   | nil     nil     nil   |
  | true  | false   nil     nil   |
  +-------+-----------------------+
deccu

Decrement carry (unbalanced).

  A deccu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     true    true  |
  | true  | nil     nil     true  |
  | false | nil     nil     nil   |
  +-------+-----------------------+
deccv

Decrement carry (negative base)

  A deccv B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     false   nil   |
  | true  | nil     nil     nil   |
  | false | nil     nil     true  |
  +-------+-----------------------+
pty

Parity.

  A pty B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   true    nil   |
  | nil   | true    nil     false |
  | true  | nil     false   true  |
  +-------+-----------------------+
dpl

Duplicate.

  A dpl B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | nil     true    false |
  | nil   | false   nil     true  |
  | true  | true    false   nil   |
  +-------+-----------------------+
dplc

Duplication carry.

  A dplc B
  +---+---------------------------+
  | A | B   false   nil     true  |
  |   +---+-----------------------+
  | false | false   false   nil   |
  | nil   | nil     nil     nil   |
  | true  | nil     true    true  |
  +-------+-----------------------+
dplcu

Duplication carry (unbalanced).

  A dplcu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     nil     nil   |
  | true  | nil     true    true  |
  | false | true    true    false |
  +-------+-----------------------+
dplcv

Duplication carry (negative base).

  A dplcv B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     nil     true  |
  | true  | nil     false   nil   |
  | false | false   false   false |
  +-------+-----------------------+
negcv

Negation carry (negative base).

  A negcv B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     nil     nil   |
  | true  | true    nil     nil   |
  | false | true    true    nil   |
  +-------+-----------------------+
mulcu

Multiplication carry (unbalanced).

  A mulcu B
  +---+---------------------------+
  | A | B   nil     true    false |
  |   +---+-----------------------+
  | nil   | nil     nil     nil   |
  | true  | nil     nil     nil   |
  | false | nil     nil     true  |
  +-------+-----------------------+

Ternary Operators

add

Addition.

  add A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | nil     true    false |
  | false | nil   | true    false   nil   |
  | false | true  | false   nil     true  |
  |       |       |                       |
  | nil   | false | true    false   nil   |
  | nil   | nil   | false   nil     true  |
  | nil   | true  | nil     true    false |
  |       |       |                       |
  | true  | false | false   nil     true  |
  | true  | nil   | nil     true    false |
  | true  | true  | true    false   nil   |
  +-------+-------+-----------------------+
addc

Addition carry.

  addc A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | false   false   nil   |
  | false | nil   | false   nil     nil   |
  | false | true  | nil     nil     nil   |
  |       |       |                       |
  | nil   | false | false   nil     nil   |
  | nil   | nil   | nil     nil     nil   |
  | nil   | true  | nil     nil     true  |
  |       |       |                       |
  | true  | false | nil     nil     nil   |
  | true  | nil   | nil     nil     true  |
  | true  | true  | nil     true    true  |
  +-------+-------+-----------------------+
addcu

Addition carry (unbalanced).

  addcu A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     nil     nil   |
  | nil   | true  | nil     nil     true  |
  | nil   | false | nil     true    true  |
  |       |       |                       |
  | true  | nil   | nil     nil     true  |
  | true  | true  | nil     true    true  |
  | true  | false | true    true    true  |
  |       |       |                       |
  | false | nil   | nil     true    true  |
  | false | true  | true    true    true  |
  | false | false | true    true    false |
  +-------+-------+-----------------------+
addcv

Addition carry (negative base).

  addcv A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     nil     true  |
  | nil   | true  | nil     nil     nil   |
  | nil   | false | nil     false   nil   |
  |       |       |                       |
  | true  | nil   | nil     nil     nil   |
  | true  | true  | nil     false   nil   |
  | true  | false | false   false   nil   |
  |       |       |                       |
  | false | nil   | nil     false   nil   |
  | false | true  | false   false   nil   |
  | false | false | false   false   false |
  +-------+-------+-----------------------+
addcx

Addition carry (mixed base).

For multiplication in base(-3), an addition of two trits 0..2 and one trit -1..1 can be useful. Addcx computes a signed carry trit for this kind of addition.

  addcx A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     nil     true  |
  | nil   | true  | nil     nil     nil   |
  | nil   | false | true    nil     true  |
  |       |       |                       |
  | true  | nil   | nil     nil     nil   |
  | true  | true  | nil     false   nil   |
  | true  | false | nil     nil     true  |
  |       |       |                       |
  | false | nil   | nil     false   nil   |
  | false | true  | false   false   nil   |
  | false | false | nil     nil     nil   |
  +-------+-------+-----------------------+
subt

Subtraction.

  subt A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | true    nil     false |
  | false | nil   | nil     false   true  |
  | false | true  | false   true    nil   |
  |       |       |                       |
  | nil   | false | false   true    nil   |
  | nil   | nil   | true    nil     false |
  | nil   | true  | nil     false   true  |
  |       |       |                       |
  | true  | false | nil     false   true  |
  | true  | nil   | false   true    nil   |
  | true  | true  | true    nil     false |
  +-------+-------+-----------------------+
subc

Subtraction carry.

  subc A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | nil     nil     nil   |
  | false | nil   | nil     nil     true  |
  | false | true  | nil     true    true  |
  |       |       |                       |
  | nil   | false | false   nil     nil   |
  | nil   | nil   | nil     nil     nil   |
  | nil   | true  | nil     nil     true  |
  |       |       |                       |
  | true  | false | false   false   nil   |
  | true  | nil   | false   nil     nil   |
  | true  | true  | nil     nil     nil   |
  +-------+-------+-----------------------+
subcu

Subtraction carry (unbalanced).

  subcu A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     true    true  |
  | nil   | true  | true    true    true  |
  | nil   | false | true    true    false |
  |       |       |                       |
  | true  | nil   | nil     nil     true  |
  | true  | true  | nil     true    true  |
  | true  | false | true    true    true  |
  |       |       |                       |
  | false | nil   | nil     nil     nil   |
  | false | true  | nil     nil     true  |
  | false | false | nil     true    true  |
  +-------+-------+-----------------------+
subcv

Subtraction carry (negative base).

  subcv A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     false   nil   |
  | nil   | true  | false   false   nil   |
  | nil   | false | false   false   false |
  |       |       |                       |
  | true  | nil   | nil     nil     nil   |
  | true  | true  | nil     false   nil   |
  | true  | false | false   false   nil   |
  |       |       |                       |
  | false | nil   | nil     nil     true  |
  | false | true  | nil     nil     nil   |
  | false | false | nil     false   nil   |
  +-------+-------+-----------------------+
cmin

Ternary comparison to minimum.

  cmin A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | nil     nil     nil   |
  | false | nil   | nil     nil     nil   |
  | false | true  | nil     false   false |
  |       |       |                       |
  | nil   | false | nil     nil     nil   |
  | nil   | nil   | nil     nil     nil   |
  | nil   | true  | true    false   false |
  |       |       |                       |
  | true  | false | true    true    false |
  | true  | nil   | true    true    false |
  | true  | true  | true    true    false |
  +-------+-------+-----------------------+
cmed

Ternary comparison to median.

  cmed A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | true    true    false |
  | false | nil   | true    true    false |
  | false | true  | true    nil     nil   |
  |       |       |                       |
  | nil   | false | true    true    false |
  | nil   | nil   | true    true    false |
  | nil   | true  | false   true    nil   |
  |       |       |                       |
  | true  | false | nil     nil     true  |
  | true  | nil   | nil     nil     true  |
  | true  | true  | false   false   true  |
  +-------+-------+-----------------------+
cmax

Ternary comparison to maximum.

  cmax A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | false   false   true  |
  | false | nil   | false   false   true  |
  | false | true  | false   true    true  |
  |       |       |                       |
  | nil   | false | false   false   true  |
  | nil   | nil   | false   false   true  |
  | nil   | true  | nil     nil     true  |
  |       |       |                       |
  | true  | false | false   false   nil   |
  | true  | nil   | false   false   nil   |
  | true  | true  | nil     nil     nil   |
  +-------+-------+-----------------------+
cvld

Ternary comparison validation.

  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | true    true    true  |
  | false | nil   | nil     nil     true  |
  | false | true  | false   nil     true  |
  |       |       |                       |
  | nil   | false | true    nil     nil   |
  | nil   | nil   | nil     true    nil   |
  | nil   | true  | nil     nil     true  |
  |       |       |                       |
  | true  | false | true    nil     false |
  | true  | nil   | true    nil     nil   |
  | true  | true  | true    true    true  |
  +-------+-------+-----------------------+
min

Minimum of three.

  min A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | false   false   false |
  | false | nil   | false   false   false |
  | false | true  | false   false   false |
  |       |       |                       |
  | nil   | false | false   false   false |
  | nil   | nil   | false   nil     nil   |
  | nil   | true  | false   nil     nil   |
  |       |       |                       |
  | true  | false | false   false   false |
  | true  | nil   | false   nil     nil   |
  | true  | true  | false   nil     true  |
  +-------+-------+-----------------------+
med

Median of three.

  med A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | false   false   false |
  | false | nil   | false   nil     nil   |
  | false | true  | false   nil     true  |
  |       |       |                       |
  | nil   | false | false   nil     nil   |
  | nil   | nil   | nil     nil     nil   |
  | nil   | true  | nil     nil     true  |
  |       |       |                       |
  | true  | false | false   nil     true  |
  | true  | nil   | nil     nil     true  |
  | true  | true  | true    true    true  |
  +-------+-------+-----------------------+
max

Maximum of three.

  max A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | false   nil     true  |
  | false | nil   | nil     nil     true  |
  | false | true  | true    true    true  |
  |       |       |                       |
  | nil   | false | nil     nil     true  |
  | nil   | nil   | nil     nil     true  |
  | nil   | true  | true    true    true  |
  |       |       |                       |
  | true  | false | true    true    true  |
  | true  | nil   | true    true    true  |
  | true  | true  | true    true    true  |
  +-------+-------+-----------------------+
minu

Minimum of three (unbalanced).

  minu A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     nil     nil   |
  | nil   | true  | nil     nil     nil   |
  | nil   | false | nil     nil     nil   |
  |       |       |                       |
  | true  | nil   | nil     nil     nil   |
  | true  | true  | nil     true    true  |
  | true  | false | nil     true    true  |
  |       |       |                       |
  | false | nil   | nil     nil     nil   |
  | false | true  | nil     true    true  |
  | false | false | nil     true    false |
  +-------+-------+-----------------------+
medu

Median of three (unbalanced).

  medu A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     nil     nil   |
  | nil   | true  | nil     true    true  |
  | nil   | false | nil     true    false |
  |       |       |                       |
  | true  | nil   | nil     true    true  |
  | true  | true  | true    true    true  |
  | true  | false | true    true    false |
  |       |       |                       |
  | false | nil   | nil     true    false |
  | false | true  | true    true    false |
  | false | false | false   false   false |
  +-------+-------+-----------------------+
maxu

Maximum of three (unbalanced).

  maxu A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   nil     true    false |
  |       |   +---+-----------------------+
  | nil   | nil   | nil     true    false |
  | nil   | true  | true    true    false |
  | nil   | false | false   false   false |
  |       |       |                       |
  | true  | nil   | true    true    false |
  | true  | true  | true    true    false |
  | true  | false | false   false   false |
  |       |       |                       |
  | false | nil   | false   false   false |
  | false | true  | false   false   false |
  | false | false | false   false   false |
  +-------+-------+-----------------------+
ipqc

Interpolation quadratic coefficient.

In modulo 3 arithmetic, a polynomial with values A, B, C at positions 0, 1, 2 can be computed as -(A + B + C) * x * x + (C - B) * x + A. For that reason, the negated sum of three trits modulo 3 has got the funny name ipqc.

  ipqc A, B, C
  +-------+---+---------------------------+
  |   A   | B | C   false   nil     true  |
  |       |   +---+-----------------------+
  | false | false | nil     false   true  |
  | false | nil   | false   true    nil   |
  | false | true  | true    nil     false |
  |       |       |                       |
  | nil   | false | false   true    nil   |
  | nil   | nil   | true    nil     false |
  | nil   | true  | nil     false   true  |
  |       |       |                       |
  | true  | false | true    nil     false |
  | true  | nil   | nil     false   true  |
  | true  | true  | false   true    nil   |
  +-------+-------+-----------------------+

Quarternary Operators

sum

Summation.

A result trit and a carry trit can hold the sum of three operand trits and a carry trit. Thus, addition of three numerical values can be implemented efficiently in ternary arithmetic based on this super addition operator with four trit arguments.

  sum A, B, C, D
  +-------+---+---------------------------+
  |   A   | B | C   f f f   n n n   t t t |
  |       |   +---------------------------+
  |       |   | D   f n t   f n t   f n t |
  |       |   +---+-----------------------+
  | false | false | f n t   n t f   t f n |
  | false | nil   | n t f   t f n   f n t |
  | false | true  | t f n   f n t   n t f |
  |       |       |                       |
  | nil   | false | n t f   t f n   f n t |
  | nil   | nil   | t f n   f n t   n t f |
  | nil   | true  | f n t   n t f   t f n |
  |       |       |                       |
  | true  | false | t f n   f n t   n t f |
  | true  | nil   | f n t   n t f   t f n |
  | true  | true  | n t f   t f n   f n t |
  +-------+-------+-----------------------+
sumc

Summation carry.

  sumc A, B, C, D
  +-------+---+---------------------------+
  |   A   | B | C   f f f   n n n   t t t |
  |       |   +---------------------------+
  |       |   | D   f n t   f n t   f n t |
  |       |   +---+-----------------------+
  | false | false | f f f   f f n   f n n |
  | false | nil   | f f n   f n n   n n n |
  | false | true  | f n n   n n n   n n t |
  |       |       |                       |
  | nil   | false | f f n   f n n   n n n |
  | nil   | nil   | f n n   n n n   n n t |
  | nil   | true  | n n n   n n t   n t t |
  |       |       |                       |
  | true  | false | f n n   n n n   n n t |
  | true  | nil   | n n n   n n t   n t t |
  | true  | true  | n n t   n t t   t t t |
  +-------+-------+-----------------------+
sumcu

Summation carry (unbalanced).

  sumcu A, B, C, D
  +-------+---+---------------------------+
  |   A   | B | C   n n n   t t t   f f f |
  |       |   +---------------------------+
  |       |   | D   n t f   n t f   n t f |
  |       |   +---+-----------------------+
  | nil   | nil   | n n n   n n t   n t t |
  | nil   | true  | n n t   n t t   t t t |
  | nil   | false | n t t   t t t   t t f |
  |       |       |                       |
  | true  | nil   | n n t   n t t   t t t |
  | true  | true  | n t t   t t t   t t f |
  | true  | false | t t t   t t f   t f f |
  |       |       |                       |
  | false | nil   | n t t   t t t   t t f |
  | false | true  | t t t   t t f   t f f |
  | false | false | t t f   t f f   f f f |
  +-------+-------+-----------------------+
mpx

Multiplex. The first argument determines which one of the other arguments to return.

  mpx A, B, C, D
  +-------+---+---------------------------+
  |   A   | B | C   n n n   t t t   f f f |
  |       |   +---------------------------+
  |       |   | D   n t f   n t f   n t f |
  |       |   +---+-----------------------+
  | nil   | nil   | n n n   n n n   n n n |
  | nil   | true  | t t t   t t t   t t t |
  | nil   | false | f f f   f f f   f f f |
  |       |       |                       |
  | true  | nil   | n n n   t t t   f f f |
  | true  | true  | n n n   t t t   f f f |
  | true  | false | n n n   t t t   f f f |
  |       |       |                       |
  | false | nil   | n t f   n t f   n t f |
  | false | true  | n t f   n t f   n t f |
  | false | false | n t f   n t f   n t f |
  +-------+-------+-----------------------+
generic

$trit->generic($op_name, @args) evaluates a generic operator with the given arguments.

Generic operator names consist of a letter and a number of digits from 0 to 2. Each letter stands for an operator signature and determines the number of digits completely describing the operator. The digits are mapped to trits nil, true, false, as with from_int_u.

c - constant, 1 digit designating the constant. Example: c1 is equivalent to true.

u - unary operator, 3 digits designating the result from nil, true, false. Example: u021 is equivalent to not.

b - binary operator, 9 digits designating the result from 9 possible pairs of input trits. Example: b000021012 is equivalent to xor.

s - symmetric ternary operator, 10 digits designating the result from 10 possible combinations of input trits (only enumerating the lexically first instance of permutations). Example: s0122010120 is equivalent to add.

t - ternary operator, 27 digits designating the result from 27 possible triples of input trits (lexically sorted). Example: t000010002002000202010110000 is equivalent to subc.

q - symmetric quaternary operator, 15 digits designating the result from 15 possible combinations of input trits (only enumerating the lexically first instance of permutations). Example: q000102100211022 is equivalent to sumc.

Q - quaternary operator, 81 digits designating the result from 81 possible quadruples of input trits (lexically sorted).

Math::Logic::Ternary::Object Role Methods

Trit

Trit inspection: $trit->Trit($n) returns $trit if $n is 0, otherwise nil.

Trits

Trit inspection: $trit->Trits returns ($trit), a list with one element.

Rtrits

$trit->Rtrits returns an empty list if $trit is nil, otherwise ($trit), a list with one element.

Sign

$trit->Sign returns $trit.

as_int

$trit->as_int returns an integer number, 0 for nil, 1 for true, and -1 for false.

as_int_u
as_int_v
res_mod3

$trit->as_int_u returns an integer number, 0 for nil, 1 for true, and 2 for false. $trit->as_int_v does the same. $trit->res_mod3 does the same.

as_string

$trit->as_string returns a string, '$nil' for nil, '$true' for true, and '$false' for false.

is_equal

$trit->is_equal($obj) returns true if $trit->Rtrits and $obj->Rtrits are identical lists, otherwise false. This means a single trit is regarded as equal to itself and any word with the same least significant trit and no other non-nil trits.

Other Object Methods

Mpx

Multiplex. $trit->Mpx($case_n, $case_t, $case_f) with three arbitrary arguments returns $case_n if $trit is nil, $case_t if $trit is true, or $case_f if $trit is false.

is_nil

$trit->is_nil returns boolean true if $trit is nil, otherwise false.

is_true

$trit->is_true returns boolean true if $trit is true, otherwise false.

is_false

$trit->is_false returns boolean true if $trit is false, otherwise false.

as_bool

$trit->as_bool returns undef if $trit is nil, 1 (boolean true) if $trit is true, or an empty string (boolean false) if $trit is false.

as_modint

$trit->as_modint converts $trit to a Math::ModInt object, mapping nil to mod(0, 3), true to mod(1, 3), and false to mod(2, 3).

The Perl extension Math::ModInt (available on CPAN) must be installed for this to work, otherwise as_modint will raise a run-time exception.

Constructors

All constructors are class methods. They may raise an exception when called with unexpected arguments.

from_bool

Math::Logic::Ternary->from_bool($arg) returns nil if $arg is undefined, false if $arg is defined but false, or true if $arg is true.

from_sign

Math::Logic::Ternary->from_sign($int) returns nil if $int is zero, true if $int is positive, or false if $int is negative.

from_remainder

Math::Logic::Ternary->from_remainder($int) returns nil if $int is equivalent to 0 (modulo 3), true if $int is equivalent to 1 (modulo 3), or false if $int is equivalent to 2 (modulo 3).

from_int

Math::Logic::Ternary->from_int($int) returns nil if $int is 0, true if $int is 1, or false if $int is -1.

from_int_u

Math::Logic::Ternary->from_int_u($int) returns nil if $int is 0, true if $int is 1, or false if $int is 2.

from_string

Math::Logic::Ternary->from_string($str) returns nil if $str is 'nil' or '$nil', true if $str is 'true' or '$true', and false if $str is 'false' or '$false'.

from_modint

Math::Logic::Ternary->from_modint($obj) returns nil or true or false if $obj is a Math::ModInt object and $obj->modulus is 3. A residue of 0, 1, or 2 maps to nil, true, or false, respectively.

from_various

Math::Logic::Ternary->from_various($arg) guesses the type of its argument and calls one of the more specific constructors (but not from_remainder).

Other Class Methods

make_generic

Math::Logic::Ternary->make_generic($op_name) returns an operator coderef defined by a generic operator name. It can subsequently be called as a method on trits.

Example:

  $foo = Math::Logic::Ternary::Trit->make_generic('b021210102');
  $bar = true->$foo(false);     # $bar = nil
  $bar = $foo->(true, false);   # $bar = nil

On the syntax of generic operator names, see "generic".

trit_operators

Math::Logic::Ternary->trit_operators returns a list of listrefs with all currently implemented named trit operators with these contents:

  [$name, $min_args, $var_args, $ret_vals, $arithmetic]

$name is the name of the operator, $min_args is the minimum number of arguments, $var_args is the number of optional arguments, $ret_vals is the number of return values, $arithmetic is undefined unless the operator is an arithmetic operator belonging to balanced (= 0), unbalanced (= 1), or negative base (= 2) arithmetic.

Currently, $var_args will be 0 and $ret_vals will be 1 for all list entries. This may change in future releases.

DIAGNOSTICS

unknown operator name "%s"

"generic" or "make_generic" was called with an invalid operator name.

too few arguments, expected %d more

An operator was called with too few operands.

integer "%d" out of range %d..%d

A constructor taking an integer argument was called with an argument outside its defined range.

unknown trit name "%s"

"from_string" was called with an invalid trit name.

modular integer with modulus 3 expected

"from_modint" was called with an invalid argument, either a modular integer with a modulus other than 3 or not a Math::ModInt object at all.

cannot convert %s to a trit

"from_various" was called with an invalid argument.

perl extension Math::ModInt is not available

"as_modint" was called on a platform where Math::ModInt can not be loaded. Installing Math::ModInt from CPAN should resolve this problem.

SEE ALSO

Math::Logic::Ternary
Math::Logic::Ternary::Object
Math::Logic::Ternary::Word
Math::Logic::Ternary::Calculator

AUTHOR

Martin Becker <becker-cpan-mp@cozap.com>

COPYRIGHT AND LICENSE

Copyright (c) 2012-2017 by Martin Becker, Blaubeuren. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.0 or, at your option, any later version of Perl 5 you may have available.