trit

package module
v2.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 7 Imported by: 0

README

Go Report Card License License Stay with Ukraine

trit

trit implements three-valued (ternary) logic in Go. A Trit is an int8 with three states - False (any negative number), Unknown (0) and True (any positive number) - and the full set of ternary logic operators (Not, And, Or, Xor, Nand, Nor, Nxor, implication, equivalence and more).

The key property is that the zero value is Unknown, so an uninitialized Trit is already meaningful. That is what makes it useful wherever a "maybe" or "not set" state matters - config merging, partial updates, nullable database columns, logic circuits - where a plain bool cannot tell "false" apart from "unset".

Features

  • Three-valued logic operations (True, False, Unknown); zero value is Unknown.
  • Safe bool conversions with explicit Unknown handling.
  • Serialization: JSON (Unknown → null), text, and database/sql (Unknown → NULL).
  • ParseTrit, Compare (ordering False < Unknown < True), and Default.
  • Slice aggregates: All, Any, None, Known, Consensus, Majority (plus iter.Seq forms).
  • Zero allocations for basic operations; property and fuzz tested.

Installation

go get github.com/goloop/trit/v2
import "github.com/goloop/trit/v2"

Requires Go 1.24 or newer.

Quick start

package main

import (
    "fmt"

    "github.com/goloop/trit/v2"
)

func main() {
    t1, t2 := trit.True, trit.False

    fmt.Println(t1.IsTrue())  // true
    fmt.Println(t1.And(t2))   // False
    fmt.Println(t1.Or(t2))    // True
    fmt.Println(t1.Xor(t2))   // True

    // Resolve the Unknown state.
    t3 := trit.Unknown
    t3.Default(trit.True)     // set only if currently Unknown
    fmt.Println(t3)           // True
}

Why three-valued logic? A bool config field cannot distinguish "explicitly false" from "not provided". A trit.Trit field can - an unset field stays Unknown, so you can apply a default without clobbering an explicit choice:

type Config struct {
    Enabled trit.Trit // Unknown until the caller sets it
}

func (s *Service) Configure(c Config) {
    if !c.Enabled.IsUnknown() {
        s.enabled = c.Enabled
    }
    trit.Default(&s.enabled, true) // default only if still unset
}

Documentation

Contributing

Contributions are welcome. Please run go test ./..., go vet ./... and gofmt -l . before submitting a pull request.

License

trit is released under the MIT License. See LICENSE.

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

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidTrit = errors.New("invalid trit value")

ErrInvalidTrit is returned when a textual value cannot be parsed as a Trit.

View Source
var ErrUnknownValue = errors.New("cannot convert Unknown to bool")

ErrUnknownValue is returned when we try to convert Unknown to bool.

Functions

func IsFalse

func IsFalse[T Logicable](t T) bool

IsFalse checks if the trit-object is False.

See Trit.IsFalse() for more information.

func IsTrue

func IsTrue[T Logicable](t T) bool

IsTrue checks if the trit-object is True.

See Trit.IsTrue() for more information.

func IsUnknown

func IsUnknown[T Logicable](t T) bool

IsUnknown checks if the trit-object is Unknown.

See Trit.IsUnknown() for more information.

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

func All[T Logicable](t ...T) Trit

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

func AllSeq[T Logicable](seq iter.Seq[T]) Trit

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

func And[T, U Logicable](a T, b U) Trit

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

func Any[T Logicable](t ...T) Trit

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

func AnySeq[T Logicable](seq iter.Seq[T]) Trit

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

func Consensus[T Logicable](trits ...T) Trit

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

func Convert[T Logicable](v ...T) []Trit

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

func Default[T Logicable](t *Trit, v T) Trit

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

func Define[T Logicable](v T) Trit

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

func Eq[T, U Logicable](a T, b U) Trit

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

func Ia[T Logicable](t T) Trit

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

func Imp[T, U Logicable](a T, b U) Trit

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

func Known[T Logicable](ts ...T) Trit

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

func KnownSeq[T Logicable](seq iter.Seq[T]) Trit

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

func La[T Logicable](t T) Trit

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

func Ma[T Logicable](t T) Trit

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

func Majority[T Logicable](trits ...T) Trit

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

func Max[T, U Logicable](a T, b U) Trit

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

func Min[T, U Logicable](a T, b U) Trit

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

func Nand[T, U Logicable](a T, b U) Trit

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

func Neq[T, U Logicable](a T, b U) Trit

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

func Nimp[T, U Logicable](a T, b U) Trit

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

func None[T Logicable](t ...T) Trit

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

func NoneSeq[T Logicable](seq iter.Seq[T]) Trit

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

func Nor[T, U Logicable](a T, b U) Trit

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

func Not[T Logicable](t T) Trit

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

func Nxor[T, U Logicable](a T, b U) Trit

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

func Or[T, U Logicable](a T, b U) Trit

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

func ParseTrit(s string) (Trit, error)

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

func Random(up ...uint8) Trit

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 Set

func Set[T Logicable](t *Trit, v T) Trit

Set sets the value of the trit-object.

See Trit.Set() for more information.

func Xor

func Xor[T, U Logicable](a T, b U) Trit

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

func (t Trit) And(trit Trit) Trit

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

func (t Trit) CanBeBool() bool

CanBeBool checks if the value can be converted to bool. Returns true only if the value is True or False.

func (*Trit) Clean

func (t *Trit) Clean() Trit

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

func (t Trit) Compare(o Trit) int

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

func (t *Trit) Default(trit Trit) Trit

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

func (t Trit) Eq(trit Trit) Trit

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

func (t *Trit) FalseIfUnknown() Trit

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

func (t Trit) Ia() Trit

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

func (t Trit) Imp(trit Trit) Trit

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

func (t Trit) Int() 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

func (t Trit) IsFalse() bool

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

func (t Trit) IsTrue() bool

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

func (t Trit) IsUnknown() bool

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

func (t Trit) La() Trit

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

func (t Trit) Ma() Trit

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

func (t Trit) MarshalJSON() ([]byte, error)

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

func (t Trit) MarshalText() ([]byte, error)

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

func (t Trit) Max(trit Trit) Trit

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

func (t Trit) Min(trit Trit) Trit

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

func (t Trit) Nand(trit Trit) Trit

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

func (t Trit) Neq(trit Trit) Trit

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

func (t Trit) Nimp(trit Trit) Trit

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

func (t Trit) Nor(trit Trit) Trit

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

func (t *Trit) Norm() Trit

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

func (t Trit) Not() Trit

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

func (t Trit) Nxor(trit Trit) Trit

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

func (t Trit) Or(trit Trit) Trit

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

func (t *Trit) Scan(src any) error

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

func (t *Trit) Set(v int) Trit

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

func (t Trit) String() 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

func (t Trit) ToBool() (bool, error)

ToBool converts Trit to bool. Returns (false, ErrUnknownValue) if the value is Unknown.

func (*Trit) TrueIfUnknown

func (t *Trit) TrueIfUnknown() Trit

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

func (t *Trit) UnmarshalJSON(data []byte) error

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

func (t *Trit) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. It accepts any value understood by ParseTrit (case-insensitive, whitespace-trimmed).

func (Trit) Val

func (t Trit) Val() Trit

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

func (t Trit) Value() (driver.Value, error)

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

func (t Trit) Xor(trit Trit) Trit

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

type Tritter

type Tritter interface {
	IsTrue() bool
	IsFalse() bool
	IsUnknown() bool
	Int() int
	String() string
}

Tritter is the behavioural contract of a three-valued digit. The concrete *Trit type satisfies it (see the compile-time assertion above).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL