Documentation
¶
Overview ¶
Package trit implements three-valued logic operations with False, Unknown, and True states.
Overview ¶
Trit (short for "trinary digit") is an information unit that can take three states: False (-1), Unknown (0), and True (1). It serves as the fundamental unit of trinary/ternary logic systems, with applications in:
- Database systems (SQL NULL handling)
- Logic circuits and digital design
- Decision systems with uncertainty
- Artificial intelligence and expert systems
- Configuration management
Key Features
- Basic type conversion (from/to bool, int, float)
- Fundamental unary operations (NOT, MA, LA, IA)
- Binary logic operations (AND, OR, XOR, etc.)
- Extended operations (IMP, EQ, MIN, MAX)
- Slice aggregates (All, Any, None, Known, Consensus, Majority) with iterator forms (AllSeq, AnySeq, NoneSeq, KnownSeq) over iter.Seq
- Serialization: JSON, text, and database/sql (Unknown maps to NULL)
- Full set of comparison and testing methods
Quick Start ¶
Basic usage example:
t1 := trit.True t2 := trit.Unknown result := t1.And(t2) // Unknown isTrue := result.IsTrue() // false
Type System ¶
The package implements type Trit as int8 with three main states:
- False: Any negative value (-1 and below)
- Unknown: Zero (0)
- True: Any positive value (1 and above)
Truth Tables ¶
The package implements the following truth tables for three-valued logic:
1. Unary Operations:
NA (Not): Logical negation
MA (Modus Ponens Absorption)
LA (Law of Absorption)
IA (Implication Absorption)
A | NA A | MA A | LA A | IA ----+---- ----+---- ----+---- ----+---- F | T F | F F | F F | F U | U U | T U | F U | T T | F T | T T | T T | F
2. Basic Binary Operations:
AND: Logical conjunction
OR: Logical disjunction
XOR: Exclusive OR
A | B | AND A | B | OR A | B | XOR ---+---+------ ---+---+------ ---+---+------ F | F | F F | F | F F | F | F F | U | F F | U | U F | U | U F | T | F F | T | T F | T | T U | F | F U | F | U U | F | U U | U | U U | U | U U | U | U U | T | U U | T | T U | T | U T | F | F T | F | T T | F | T T | U | U T | U | T T | U | U T | T | T T | T | T T | T | F
3. Negative Binary Operations:
NAND: Logical NAND (NOT AND)
NOR: Logical NOR (NOT OR)
NXOR: Logical XNOR (NOT XOR)
A | B | NAND A | B | NOR A | B | NXOR ---+---+------ ---+---+------ ---+---+------ F | F | T F | F | T F | F | T F | U | T F | U | U F | U | U F | T | T F | T | F F | T | F U | F | T U | F | U U | F | U U | U | U U | U | U U | U | U U | T | U U | T | F U | T | U T | F | T T | F | F T | F | F T | U | U T | U | F T | U | U T | T | F T | T | F T | T | T
4. Extended Operations:
IMP: Implication (Lukasiewicz logic)
EQ: Equivalence (If and only if)
Note the two operators deliberately follow different systems: IMP is Lukasiewicz implication (Imp(U, U) = True), while EQ is Kleene equivalence (Eq(U, U) = Unknown). As a result the material-implication and mutual-implication identities do not hold at (Unknown, Unknown): Eq is not And(Imp(a, b), Imp(b, a)), and Imp is not Or(Not(a), b) there. Compose these operators with that difference in mind.
MIN: Minimum value
NIMP: Inverse implication
NEQ: Non-equivalence
MAX: Maximum value
A | B | IMP A | B | EQ A | B | MIN ---+---+------ ---+---+------ ---+---+------ F | F | T F | F | T F | F | F F | U | T F | U | U F | U | F F | T | T F | T | F F | T | F U | F | U U | F | U U | F | F U | U | T U | U | U U | U | U U | T | T U | T | U U | T | U T | F | F T | F | F T | F | F T | U | U T | U | U T | U | U T | T | T T | T | T T | T | T
A | B | NIMP A | B | NEQ A | B | MAX ---+---+------ ---+---+------ ---+---+------ F | F | F F | F | F F | F | F F | U | F F | U | U F | U | U F | T | F F | T | T F | T | T U | F | U U | F | U U | F | U U | U | F U | U | U U | U | U U | T | F U | T | U U | T | T T | F | T T | F | T T | F | T T | U | U T | U | U T | U | T T | T | F T | T | F T | T | T
Concurrency ¶
A Trit is a plain int8 value and every operation is a pure function of its inputs. The package holds no shared mutable state, so distinct Trit values may be used concurrently without synchronization. As with any value, a single Trit that is mutated (via the pointer-receiver helpers) while being read from another goroutine still requires external synchronization.
Performance Considerations ¶
The package optimizes performance by:
- Using int8 as the underlying type
- Converting generic inputs with a reflection-free type switch
- Providing direct value access methods
- Minimizing memory allocations (basic operations do not allocate)
For more examples and detailed API documentation, see the individual method documentation and the package examples.
Index ¶
- Variables
- func IsFalse[T Logicable](t T) bool
- func IsTrue[T Logicable](t T) bool
- func IsUnknown[T Logicable](t T) bool
- type Logicable
- type Trit
- func All[T Logicable](t ...T) Trit
- func AllSeq[T Logicable](seq iter.Seq[T]) Trit
- func And[T, U Logicable](a T, b U) Trit
- func Any[T Logicable](t ...T) Trit
- func AnySeq[T Logicable](seq iter.Seq[T]) Trit
- func Consensus[T Logicable](trits ...T) Trit
- func Convert[T Logicable](v ...T) []Trit
- func Default[T Logicable](t *Trit, v T) Trit
- func Define[T Logicable](v T) Trit
- func Eq[T, U Logicable](a T, b U) Trit
- func Ia[T Logicable](t T) Trit
- func Imp[T, U Logicable](a T, b U) Trit
- func Known[T Logicable](ts ...T) Trit
- func KnownSeq[T Logicable](seq iter.Seq[T]) Trit
- func La[T Logicable](t T) Trit
- func Ma[T Logicable](t T) Trit
- func Majority[T Logicable](trits ...T) Trit
- func Max[T, U Logicable](a T, b U) Trit
- func Min[T, U Logicable](a T, b U) Trit
- func Nand[T, U Logicable](a T, b U) Trit
- func Neq[T, U Logicable](a T, b U) Trit
- func Nimp[T, U Logicable](a T, b U) Trit
- func None[T Logicable](t ...T) Trit
- func NoneSeq[T Logicable](seq iter.Seq[T]) Trit
- func Nor[T, U Logicable](a T, b U) Trit
- func Not[T Logicable](t T) Trit
- func Nxor[T, U Logicable](a T, b U) Trit
- func Or[T, U Logicable](a T, b U) Trit
- func ParseTrit(s string) (Trit, error)
- func Random(up ...uint8) Trit
- func Set[T Logicable](t *Trit, v T) Trit
- func Xor[T, U Logicable](a T, b U) Trit
- func (t Trit) And(trit Trit) Trit
- func (t Trit) CanBeBool() bool
- func (t *Trit) Clean() Trit
- func (t Trit) Compare(o Trit) int
- func (t *Trit) Default(trit Trit) Trit
- func (t Trit) Eq(trit Trit) Trit
- func (t *Trit) FalseIfUnknown() Trit
- func (t Trit) Ia() Trit
- func (t Trit) Imp(trit Trit) Trit
- func (t Trit) Int() int
- func (t Trit) IsFalse() bool
- func (t Trit) IsTrue() bool
- func (t Trit) IsUnknown() bool
- func (t Trit) La() Trit
- func (t Trit) Ma() Trit
- func (t Trit) MarshalJSON() ([]byte, error)
- func (t Trit) MarshalText() ([]byte, error)
- func (t Trit) Max(trit Trit) Trit
- func (t Trit) Min(trit Trit) Trit
- func (t Trit) Nand(trit Trit) Trit
- func (t Trit) Neq(trit Trit) Trit
- func (t Trit) Nimp(trit Trit) Trit
- func (t Trit) Nor(trit Trit) Trit
- func (t *Trit) Norm() Trit
- func (t Trit) Not() Trit
- func (t Trit) Nxor(trit Trit) Trit
- func (t Trit) Or(trit Trit) Trit
- func (t *Trit) Scan(src any) error
- func (t *Trit) Set(v int) Trit
- func (t Trit) String() string
- func (t Trit) ToBool() (bool, error)
- func (t *Trit) TrueIfUnknown() Trit
- func (t *Trit) UnmarshalJSON(data []byte) error
- func (t *Trit) UnmarshalText(data []byte) error
- func (t Trit) Val() Trit
- func (t Trit) Value() (driver.Value, error)
- func (t Trit) Xor(trit Trit) Trit
- type Tritter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidTrit = errors.New("invalid trit value")
ErrInvalidTrit is returned when a textual value cannot be parsed as a Trit.
var ErrUnknownValue = errors.New("cannot convert Unknown to bool")
ErrUnknownValue is returned when we try to convert Unknown to bool.
Functions ¶
Types ¶
type Logicable ¶
type Logicable interface {
bool | Trit |
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 |
float32 | float64
}
Logicable is a special data type from which to determine the state of Trit in the context of three-valued logic.
Signed and floating-point values map by sign: any negative value is False, any positive value is True, and zero is Unknown. Unsigned values are True when non-zero and Unknown when zero (they can never be negative). A bool maps true to True and false to False.
type Trit ¶
type Trit int8
Trit represents a trinary digit, which can take on three distinct states: False, Unknown, or True. This type is a fundamental unit of trinary or ternary logic systems, including trinary computers and balanced ternary systems.
const ( // False represents the Trit state equivalent to 'false' in binary // logic. It is represented by a negative value (-1) in the underlying // int8 type. // // *Any negative numbers are also considered False. False Trit = -1 // Unknown represents an unknown or indeterminate Trit state. It is // beneficial in scenarios where a "maybe" state is required. // Unknown is represented by a zero value in the underlying int8 type. Unknown Trit = 0 // True represents the Trit state equivalent to 'true' in binary // logic. It is represented by a positive value (1) in the underlying // int8 type. // // *Any positive numbers are also considered True. True Trit = 1 )
func All ¶
All returns True if every value is True, and False as soon as any value is False or Unknown.
It follows the vacuous-truth convention of universal quantification: with no arguments the predicate holds trivially, so All() returns True.
Example usage:
t := trit.All(trit.True, trit.True, trit.True) fmt.Println(t.String()) // Output: True
func AllSeq ¶ added in v2.1.0
AllSeq is the iterator form of All. It returns True if every value produced by seq is True, and False as soon as a False or Unknown value appears. It stops pulling from seq at the first decisive value, so it can short-circuit over lazy or infinite sequences. An empty sequence yields True (vacuous truth), matching All.
Example usage:
t := trit.AllSeq(slices.Values([]trit.Trit{trit.True, trit.True}))
fmt.Println(t.String()) // Output: True
Example ¶
package main
import (
"fmt"
"slices"
"github.com/goloop/trit/v2"
)
func main() {
values := []trit.Trit{trit.True, trit.True, trit.False, trit.True}
// AllSeq stops at the first non-True value without draining the rest.
fmt.Println(trit.AllSeq(slices.Values(values)))
}
Output: False
func And ¶
And performs a logical AND operation between two Trit-Like values and returns the result as Trit.
See Trit.And() for more information.
func Any ¶
Any returns True as soon as any value is True, and False otherwise.
It follows the convention of existential quantification: with no arguments there is nothing that can be True, so Any() returns False.
Example usage:
t := trit.Any(trit.True, trit.False, trit.False) fmt.Println(t.String()) // Output: True
func AnySeq ¶ added in v2.1.0
AnySeq is the iterator form of Any. It returns True as soon as any value produced by seq is True, and False otherwise. It stops pulling from seq at the first True. An empty sequence yields False, matching Any.
func Consensus ¶
Consensus returns True if all input trits are True, False if all are False, and Unknown otherwise (including when any trit is Unknown).
With no arguments there is no shared position to agree on, so Consensus() returns Unknown.
Example usage:
t1, t2, t3 := trit.True, trit.True, trit.Unknown result := trit.Consensus(t1, t2, t3) // result will be Unknown, as not all trits are the same
Example ¶
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
fmt.Println(trit.Consensus(trit.True, trit.True, trit.True))
fmt.Println(trit.Consensus(trit.True, trit.True, trit.Unknown))
}
Output: True Unknown
func Convert ¶
Convert converts the any Logicable types to Trit.
Example usage:
tuf := trit.Convert(true, 0, -1) fmt.Println(tuf[0].String()) // Output: True fmt.Println(tuf[1].String()) // Output: Unknown fmt.Println(tuf[2].String()) // Output: False
func Default ¶
Default sets the default value for the trit-object if this one has a Unknown state.
Example usage:
t := trit.Unknown trit.Default(&t, trit.True) fmt.Println(t.String()) // Output: True
func Define ¶
Define converts the any Logicable type to Trit.
Example usage:
t := trit.Define(true) fmt.Println(t.String()) // Output: True
Example ¶
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
fmt.Println(trit.Define(5), trit.Define(0), trit.Define(-3))
}
Output: True Unknown False
func Eq ¶
Eq performs a logical EQ operation between two Trit-Like values and returns the result as Trit.
See Trit.Eq() for more information.
func Ia ¶
Ia performs a logical IA (Implication Absorption) operation on a Trit-Like value and returns the result as Trit.
See Trit.Ia() for more information.
func Imp ¶
Imp performs a logical IMP operation between two Trit-Like values and returns the result as Trit.
See Trit.Imp() for more information.
func Known ¶
Known returns True if every value is definite (True or False), and False as soon as any value is Unknown.
It follows the vacuous-truth convention: with no arguments there is no Unknown value, so Known() returns True.
Example usage:
a := trit.Known(trit.True, trit.False, trit.Unknown) fmt.Println(a.String()) // Output: False b := trit.Known(trit.True, trit.True, trit.False) fmt.Println(b.String()) // Output: True
func KnownSeq ¶ added in v2.1.0
KnownSeq is the iterator form of Known. It returns True if every value produced by seq is definite (True or False), and False as soon as an Unknown appears. It stops pulling from seq at the first Unknown. An empty sequence yields True (nothing is unknown), matching Known.
func La ¶
La performs a logical LA (Law of Absorption) operation on a Trit-Like value and returns the result as Trit.
See Trit.La() for more information.
func Ma ¶
Ma performs a logical MA (Modus Ponens Absorption) operation on a Trit-Like values and returns the result as Trit.
See Trit.Ma() for more information.
func Majority ¶
Majority returns True if more than half of the input trits are True, False if more than half are False, and Unknown otherwise.
With no arguments there is no majority, so Majority() returns Unknown.
Example usage:
t1, t2, t3, t4 := trit.True, trit.True, trit.False, trit.Unknown result := trit.Majority(t1, t2, t3, t4) // result will be True, as more than half of the trits are True
Example ¶
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
fmt.Println(trit.Majority(trit.True, trit.True, trit.False))
}
Output: True
func Max ¶
Max performs a logical MAX operation between two Trit-Like values and returns the result as Trit. MAX is an alias of OR.
See Trit.Max() for more information.
func Min ¶
Min performs a logical MIN operation between two Trit-Like values and returns the result as Trit. MIN is an alias of AND.
See Trit.Min() for more information.
func Nand ¶
Nand performs a logical NAND operation between two Trit-Like values and returns the result as Trit.
See Trit.Nand() for more information.
func Neq ¶
Neq performs a logical NEQ operation between two Trit-Like values and returns the result as Trit.
See Trit.Neq() for more information.
func Nimp ¶
Nimp performs a logical NIMP operation between two Trit-Like values and returns the result as Trit.
See Trit.Nimp() for more information.
func None ¶
None returns True if none of the values are True, and False as soon as any value is True.
It is the negation of Any: None() returns True for an empty input.
Example usage:
t := trit.None(trit.False, trit.False, trit.False) fmt.Println(t.String()) // Output: True
func NoneSeq ¶ added in v2.1.0
NoneSeq is the iterator form of None. It returns True if none of the values produced by seq are True, and False as soon as a True appears. An empty sequence yields True, matching None.
func Nor ¶
Nor performs a logical NOR operation between two Trit-Like values and returns the result as Trit.
See Trit.Nor() for more information.
func Not ¶
Not performs a logical NOT operation on a Trit-Like value and returns the result as Trit.
See Trit.Not() for more information.
func Nxor ¶
Nxor performs a logical NXOR operation between two Trit-Like values and returns the result as Trit.
See Trit.Nxor() for more information.
func Or ¶
Or performs a logical OR operation between two Trit-Like values and returns the result as Trit.
See Trit.Or() for more information.
func ParseTrit ¶
ParseTrit parses a textual representation of a Trit. Parsing is case-insensitive and ignores surrounding whitespace. The following tokens are recognized:
- True: "true", "t", "yes", "y", "on", "1"
- False: "false", "f", "no", "n", "off", "-1"
- Unknown: "unknown", "u", "maybe", "null", "nil", "none", "0", ""
Any other value returns ErrInvalidTrit.
Example usage:
v, _ := trit.ParseTrit("maybe")
fmt.Println(v.String()) // Output: Unknown
Example ¶
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
for _, s := range []string{"yes", "maybe", "off"} {
v, _ := trit.ParseTrit(s)
fmt.Println(v)
}
}
Output: True Unknown False
func Random ¶
Random returns a random Trit value.
The optional argument sets the probability, in percent, of the Unknown outcome; the remaining probability is split evenly between True and False. Multiple arguments are summed and the total is clamped to the range [0, 100]. With no argument the probability of Unknown is 33%.
Example usage:
a := trit.Random() fmt.Println(a.String()) // Output: True, False or Unknown b := trit.Random(0) fmt.Println(b.String()) // Output: True or False (never Unknown) c := trit.Random(50) fmt.Println(c.String()) // Output: Unknown with a probability of 50%
func Xor ¶
Xor performs a logical XOR operation between two Trit-Like values and returns the result as Trit.
See Trit.Xor() for more information.
func (Trit) And ¶
And performs a logical AND operation between two Trit values and returns the result. This function applies the following rules based on the truth table for AND:
- And(False, False) => False
- And(False, Unknown) => False
- And(False, True) => False
- And(Unknown, False) => False
- And(Unknown, Unknown) => Unknown
- And(Unknown, True) => Unknown
- And(True, False) => False
- And(True, Unknown) => Unknown
- And(True, True) => True
Example usage:
a := trit.True b := trit.Unknown result := a.And(b) fmt.Println(result.String()) // Output: Unknown
Example ¶
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
a := trit.True
b := trit.Unknown
fmt.Println(a.And(b)) // conjunction with an unknown is unknown
}
Output: Unknown
func (Trit) CanBeBool ¶
CanBeBool checks if the value can be converted to bool. Returns true only if the value is True or False.
func (*Trit) Clean ¶
Clean resets the Trit to Unknown regardless of its current state and returns the updated Trit. It is the counterpart of Set/Default: use it to discard a previously determined value and fall back to the indeterminate zero state.
Example usage:
t := trit.True t.Clean() fmt.Println(t.String()) // Output: Unknown
func (Trit) Compare ¶
Compare compares two Trit values using the natural ordering False < Unknown < True. It returns -1 if t sorts before o, +1 if it sorts after, and 0 if they are equal. Non-canonical values are normalized first, so Compare is consistent with Val. The signature matches the cmp.Compare convention, so it can drive slices.SortFunc over a []Trit.
Example ¶
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
fmt.Println(trit.False.Compare(trit.True))
fmt.Println(trit.True.Compare(trit.True))
fmt.Println(trit.True.Compare(trit.Unknown))
}
Output: -1 0 1
func (*Trit) Default ¶
Default is a method that checks if the value of the Trit is Unknown. If it is, it sets the Trit to the given Trit argument.
Example usage:
t := trit.Unknown t.Default(trit.True) fmt.Println(t.String()) // Output: True
func (Trit) Eq ¶
Eq performs a logical EQ operation between two Trit values and returns the result. This function applies the following rules based on the truth table for EQ:
- Eq(False, False) => True
- Eq(False, Unknown) => Unknown
- Eq(False, True) => False
- Eq(Unknown, False) => Unknown
- Eq(Unknown, Unknown) => Unknown
- Eq(Unknown, True) => Unknown
- Eq(True, False) => False
- Eq(True, Unknown) => Unknown
- Eq(True, True) => True
Example usage:
a := trit.True b := trit.False result := a.Eq(b) fmt.Println(result.String()) // Output: False
Equivalence (EQ): Also known as "if and only if" or "iff". It's True if both Trit are the same (either both True or both False), and False if they are different.
func (*Trit) FalseIfUnknown ¶
FalseIfUnknown is a method that checks if the value of the Trit is Unknown. If it is, it sets the Trit to False. It then returns the updated Trit.
Example usage:
t := trit.Unknown t.FalseIfUnknown() fmt.Println(t.String()) // Output: False
func (Trit) Ia ¶
Ia performs a logical IA (Implication Absorption) operation on a Trit values and returns the result. This function applies the following rules based on the truth table for IA:
- Ia(False) => False
- Ia(Unknown) => True
- Ia(True) => False
Example usage:
a := trit.True result := a.Ia() fmt.Println(result.String()) // Output: False
func (Trit) Imp ¶
Imp performs a logical IMP operation between two Trit values and returns the result. This function applies the following rules based on the truth table for IMP:
- Imp(False, False) => True
- Imp(False, Unknown) => True
- Imp(False, True) => True
- Imp(Unknown, False) => Unknown
- Imp(Unknown, Unknown) => True
- Imp(Unknown, True) => True
- Imp(True, False) => False
- Imp(True, Unknown) => Unknown
- Imp(True, True) => True
Example usage:
a := trit.True b := trit.False result := a.Imp(b) fmt.Println(result.String()) // Output: False
func (Trit) Int ¶
Int returns the underlying int8 value of a Trit in the form of an int. It first normalizes the Trit value and then returns it as an int.
Example usage:
t := trit.Trit(7) fmt.Println(t.Int()) // Output: 1
func (Trit) IsFalse ¶
IsFalse returns true if the Trit value represents a False state, which is any negative number in the underlying int8 type.
Example usage:
t := trit.Trit(-2) fmt.Println(t.IsFalse()) // Output: true
func (Trit) IsTrue ¶
IsTrue returns true if the Trit value represents a True state, which is any positive number in the underlying int8 type.
Example usage:
t := trit.Trit(2) fmt.Println(t.IsTrue()) // Output: true
func (Trit) IsUnknown ¶
IsUnknown returns true if the Trit value represents a Unknown state, which is zero in the underlying int8 type.
Example usage:
t := trit.Trit(0) fmt.Println(t.IsUnknown()) // Output: true
func (Trit) La ¶
La performs a logical LA (Law of Absorption) operation on a Trit value and returns the result. This function applies the following rules based on the truth table for LA:
- La(False) => False
- La(Unknown) => False
- La(True) => True
Example usage:
a := trit.True result := a.La() fmt.Println(result.String()) // Output: True
func (Trit) Ma ¶
Ma performs a logical MA (Modus Ponens Absorption) operation on a Trit value and returns the result. This function applies the following rules based on the truth table for MA:
- Ma(False) => False
- Ma(Unknown) => True
- Ma(True) => True
Example usage:
a := trit.True result := a.Ma() fmt.Println(result.String()) // Output: True
func (Trit) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. Unknown is converted to null, True to true, False to false.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
type record struct {
Active trit.Trit `json:"active"`
}
b, _ := json.Marshal(record{Active: trit.Unknown})
fmt.Println(string(b))
}
Output: {"active":null}
func (Trit) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface, producing "True", "False" or "Unknown". This enables Trit values to be used with text-based encoders (flags, YAML, TOML, CSV, map keys, etc.).
func (Trit) Max ¶
Max performs a logical MAX operation between two Trit values and returns the result. This function applies the following rules based on the truth table for MAX:
- Max(False, False) => False
- Max(False, Unknown) => Unknown
- Max(False, True) => True
- Max(Unknown, False) => Unknown
- Max(Unknown, Unknown) => Unknown
- Max(Unknown, True) => True
- Max(True, False) => True
- Max(True, Unknown) => True
- Max(True, True) => True
Example usage:
a := trit.True b := trit.False result := a.Max(b) fmt.Println(result.String()) // Output: True
func (Trit) Min ¶
Min performs a logical MIN operation between two Trit values and returns the result. This function applies the following rules based on the truth table for MIN:
- Min(False, False) => False
- Min(False, Unknown) => False
- Min(False, True) => False
- Min(Unknown, False) => False
- Min(Unknown, Unknown) => Unknown
- Min(Unknown, True) => Unknown
- Min(True, False) => False
- Min(True, Unknown) => Unknown
- Min(True, True) => True
Example usage:
a := trit.True b := trit.False result := a.Min(b) fmt.Println(result.String()) // Output: False
func (Trit) Nand ¶
Nand performs a logical NAND operation between two Trit values and returns the result. This function applies the following rules based on the truth table for NAND:
- Nand(False, False) => True
- Nand(False, Unknown) => True
- Nand(False, True) => True
- Nand(Unknown, False) => True
- Nand(Unknown, Unknown) => Unknown
- Nand(Unknown, True) => Unknown
- Nand(True, False) => True
- Nand(True, Unknown) => Unknown
- Nand(True, True) => False
Example usage:
a := trit.True b := trit.Unknown result := a.Nand(b) fmt.Println(result.String()) // Output: True
func (Trit) Neq ¶
Neq performs a logical NEQ operation between two Trit values and returns the result. This function applies the following rules based on the truth table for NEQ:
- Neq(False, False) => False
- Neq(False, Unknown) => Unknown
- Neq(False, True) => True
- Neq(Unknown, False) => Unknown
- Neq(Unknown, Unknown) => Unknown
- Neq(Unknown, True) => Unknown
- Neq(True, False) => True
- Neq(True, Unknown) => Unknown
- Neq(True, True) => False
Example usage:
a := trit.True b := trit.False result := a.Neq(b) fmt.Println(result.String()) // Output: True
func (Trit) Nimp ¶
Nimp performs a logical NIMP operation between two Trit values and returns the result. This function applies the following rules based on the truth table for NIMP:
- Nimp(False, False) => False
- Nimp(False, Unknown) => False
- Nimp(False, True) => False
- Nimp(Unknown, False) => Unknown
- Nimp(Unknown, Unknown) => False
- Nimp(Unknown, True) => False
- Nimp(True, False) => True
- Nimp(True, Unknown) => Unknown
- Nimp(True, True) => False
Example usage:
a := trit.True b := trit.False result := a.Nimp(b) fmt.Println(result.String()) // Output: True
func (Trit) Nor ¶
Nor performs a logical NOR operation between two Trit values and returns the result. This function applies the following rules based on the truth table for NOR:
- Nor(False, False) => True
- Nor(False, Unknown) => Unknown
- Nor(False, True) => False
- Nor(Unknown, False) => Unknown
- Nor(Unknown, Unknown) => Unknown
- Nor(Unknown, True) => False
- Nor(True, False) => False
- Nor(True, Unknown) => False
- Nor(True, True) => False
Example usage:
a := trit.True b := trit.False result := a.Nor(b) fmt.Println(result.String()) // Output: False
func (*Trit) Norm ¶
Norm normalizes the Trit value. If a Trit was set using an int8 value other than -1, 0, or 1, Norm maps it to the closest Trit value.
Example usage:
t := trit.Trit(7) t.Norm() fmt.Println(t.Int()) // Output: 1
func (Trit) Not ¶
Not performs a logical NOT operation on a Trit value and returns the result. This function applies the following rules based on the truth table for NOT:
- Not(False) => True
- Not(Unknown) => Unknown
- Not(True) => False
Example usage:
t := trit.True result := t.Not() fmt.Println(result.String()) // Output: False
Example ¶
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
fmt.Println(trit.False.Not(), trit.Unknown.Not(), trit.True.Not())
}
Output: True Unknown False
func (Trit) Nxor ¶
Nxor performs a logical XNOR operation between two Trit values and returns the result. This function applies the following rules based on the truth table for XNOR:
- Nxor(False, False) => True
- Nxor(False, Unknown) => Unknown
- Nxor(False, True) => False
- Nxor(Unknown, False) => Unknown
- Nxor(Unknown, Unknown) => Unknown
- Nxor(Unknown, True) => Unknown
- Nxor(True, False) => False
- Nxor(True, Unknown) => Unknown
- Nxor(True, True) => True
Example usage:
a := trit.True b := trit.False result := a.Nxor(b) fmt.Println(result.String()) // Output: False
func (Trit) Or ¶
Or performs a logical OR operation between two Trit values and returns the result. This function applies the following rules based on the truth table for OR:
- Or(False, False) => False
- Or(False, Unknown) => Unknown
- Or(False, True) => True
- Or(Unknown, False) => Unknown
- Or(Unknown, Unknown) => Unknown
- Or(Unknown, True) => True
- Or(True, False) => True
- Or(True, Unknown) => True
- Or(True, True) => True
Example usage:
a := trit.True b := trit.False result := a.Or(b) fmt.Println(result.String()) // Output: True
func (*Trit) Scan ¶
Scan implements the sql.Scanner interface for database/sql. It accepts the driver value kinds and maps them onto a Trit:
- nil -> Unknown (SQL NULL)
- bool -> True / False
- integer/float -> sign-based (see Set)
- string/[]byte -> parsed via ParseTrit
Unrecognized source types return ErrInvalidTrit.
func (*Trit) Set ¶
Set assigns a Trit value based on the given integer. Negative values are interpreted as False, zero as Unknown, and positive values as True.
Example usage:
var t trit.Trit t.Set(-2) fmt.Println(t.String()) // Output: False
func (Trit) String ¶
String returns a string representation of a Trit value. The possible return values are "False", "Unknown", and "True".
Example usage:
t := trit.True fmt.Println(t.String()) // Output: True
func (Trit) ToBool ¶
ToBool converts Trit to bool. Returns (false, ErrUnknownValue) if the value is Unknown.
func (*Trit) TrueIfUnknown ¶
TrueIfUnknown is a method that checks if the value of the Trit is Unknown. If it is, it sets the Trit to True. It then returns the updated Trit.
Example usage:
t := trit.Unknown t.TrueIfUnknown() fmt.Println(t.String()) // Output: True
func (*Trit) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
It accepts three JSON shapes, mirroring the "tolerant" nature of the type:
- null -> Unknown
- true / false -> True / False
- a JSON number -> sign-based: positive -> True, negative -> False, zero -> Unknown
func (*Trit) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It accepts any value understood by ParseTrit (case-insensitive, whitespace-trimmed).
func (Trit) Val ¶
Val returns the normalized Trit value if a Trit was not properly initialized using the predefined constants (False, Unknown, True). If a Trit was set using an int8 value other than -1, 0, or 1, Val maps it to the closest Trit value.
Example usage:
t := trit.Trit(7) fmt.Println(t.Val().String()) // Output: True
func (Trit) Value ¶
Value implements the driver.Valuer interface for database/sql. Unknown maps to a SQL NULL, while True and False map to the boolean true and false. This makes Trit a natural fit for nullable boolean columns.
func (Trit) Xor ¶
Xor performs a logical XOR operation between two Trit values and returns the result. This function applies the following rules based on the truth table for XOR:
- Xor(False, False) => False
- Xor(False, Unknown) => Unknown
- Xor(False, True) => True
- Xor(Unknown, False) => Unknown
- Xor(Unknown, Unknown) => Unknown
- Xor(Unknown, True) => Unknown
- Xor(True, False) => True
- Xor(True, Unknown) => Unknown
- Xor(True, True) => False
Example usage:
a := trit.True b := trit.False result := a.Xor(b) fmt.Println(result.String()) // Output: True