Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

The trit package provides a Go implementation of three-level logic, including logical operations such as NOT, AND, OR, XOR, NAND, NOR, and XNOR.

License

NotificationsYou must be signed in to change notification settings

goloop/trit

Repository files navigation

Go Report CardLicenseLicenseStay with Ukraine

trit

Package for working with three-valued (trinary) logic in Go.

Package defines the data type and basic operations of a ternary logic system, often referred to as trinary or ternary logic. It supports three states, namely False, Unknown, and True, with False represented by negative number (-1), Unknown represented by zero (0), and True represented by positive number (1).

The logic operations NOT, AND, OR, XOR, NAND, NOR, and XNOR are implemented as methods of the Trit type, with each method applying the logic operation on the Trit receiver and a Trit argument to produce a Trit result according to the trinary truth table.

There are also some useful methods provided for checking the state of a Trit value (IsFalse, IsUnknown, IsTrue), setting a Trit value from an integer (Set), returning the underlying int8 value of a Trit (Int), and returning a string representation of a Trit value (String).

Overall, this package can be beneficial in scenarios where a "maybe" or "unknown" state is needed, such as in database systems and logic circuits, and in the construction of trinary computers and balanced ternary systems.

Installation

go get github.com/goloop/trit

Features

  • Three-valued logic operations (True, False, Unknown)
  • Safe bool conversions with Unknown state handling
  • JSON marshaling (Unknown → null)
  • Thread-safe parallel operations for slices
  • Zero allocations for basic operations
  • Extensive test coverage
  • Comprehensive truth tables documentation

Quick Start

package mainimport ("fmt""github.com/goloop/trit")funcmain() {t1:=trit.Truet2:=trit.False// IsTruefmt.Println(t1.IsTrue())// Output: True// IsFalsefmt.Println(t2.IsFalse())// Output: True// Defaultt3:=trit.Unknownt3.Default(trit.True)fmt.Println(t3)// Output: True// TrueIfUnknownt4:=trit.Unknownt4.TrueIfUnknown()fmt.Println(t4)// Output: True// FalseIfUnknownt4=trit.Unknownt4.FalseIfUnknown()fmt.Println(t4)// Output: False// Andresult:=t1.And(t2)fmt.Println(result)// Output: False// Orresult=t1.Or(t2)fmt.Println(result)// Output: True// Xorresult=t1.Xor(t2)fmt.Println(result)// Output: True// Nandresult=t1.Nand(t2)fmt.Println(result)// Output: True// Norresult=t1.Nor(t2)fmt.Println(result)// Output: False// Xnorresult=t1.Nxor(t2)fmt.Println(result)// Output: False// Intfmt.Println(t1.Int())// Output: 1// Stringfmt.Println(t1.String())// Output: True}

Truth Tables

The truth table for the three-valued logic system is shown here. It's a mathematical table used in logic—specifically in connection with three-valued algebra, three-valued functions, and propositional calculus—which sets out the functional values of logical expressions on each of their functional arguments, that is, for each combination of values taken by their logical variables.

 Truth Tables of Three-valued logic (T=True, N=Unknown, F=False) NA   - Not MA   - Modus Ponens Absorption LA   - Law of Absorption IA   - Implication Absorption AND  - Logical AND OR   - Logical OR XOR  - Exclusive OR NAND - Logical not AND NOR  - Logical not OR NXOR - Logical not XOR IMP  - Implicationin Lukasevich's Logic EQ   - If and only if MIN  - Minimum NIMP - NOT IMP NEQ  - NOT EQ MAX  - Maximum  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  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  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  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

Explanation

Here's an explanation of some key parts of this package:

  • The Trit type is defined as an int8. This is a signed 8-bit integer, which means it can hold values between -128 and 127.

  • The Trit type is used to represent a "trinary digit," which can take on three states: False, Unknown, and True.

  • Package defineds various methods on the Trit type that allow you to perform operations on trinary digits, including determining if a trinary digit represents False, Unknown, or True, setting the value of a trinary digit based on an integer, normalizing a trinary digit, converting a trinary digit to an integer or a string.

  • The Trit type is defined as an alias for int8, and it can take one of three constants as its value: False, Unknown, or True. False corresponds to any negative number (including -1), Unknown corresponds to 0, and True corresponds to any positive number (including 1).

  • Provides methods default state (like Default, TrueIfUnknown, FalseIFUnknown etc.,) that check if the value of a Trit is Unknown and change its value based on the method called. For instance, TrueIfUnknown will set the Trit to True if its current value is Unknown.

  • There are some methods (like IsFalse, IsUnknown, IsTrue, etc.,) that check the state of a Trit and return a boolean indicating if the Trit is in the corresponding state.

  • Some methods perform various operations like assigning a value to a Trit (Set), returning the normalized value of a Trit (Val), normalizing the Trit in place (Norm), getting the integer representation of the Trit (Int), and getting the string representation of the Trit (String).

  • There are several methods for performing logic operations on Trit values including Not, And, Or, Xor, Nand, Nor etc. These methods implement trinary logic versions of their respective boolean logic operations.

  • The logic operation methods follow truth tables for ternary logic which are defined in the package comment section.

A real example of use

This package can be used, for example, to improve methods of updating information. Let's look at the following example:

package mainimport ("fmt")// The defaultEnabled variable is used to set the default value// for the Enabled field of the Config struct.constdefaultEnabled=true// Config is a some configuration struct.typeConfigstruct {NamestringPortintEanbledbool}// Service is a some service struct.typeServicestruct {namestringportintenabledbool}// Configure executes the service configuration without changing// the previously set parameters if the value for a specific// field is not specified.//// If the field is left blank, set the default value.func (s*Service)Configure(configConfig) {// We have no problem determining if the Name field was passed or// if it is empty and we have to set it as the default value.ifconfig.Name!="" {s.name=config.Name}ifs.name=="" {s.name="default"}// Everything is also fine for the Port parameter.ifconfig.Port!=0 {s.port=config.Port}ifs.port==0 {s.port=8080}// PROBLEM HERE:// But with bool the situation is more complicated.// Not sure if we passed False or just left the field blank.// Also, we can't figure out if the field is empty and should// be set by default, or if it's just set to False.ifconfig.Eanbled!=false {s.enabled=config.Eanbled}ifs.enabled==false {s.enabled=defaultEnabled}}funcmain() {// Default service.s:=&Service{}s.Configure(Config{Name:"default",Port:8081,Eanbled:true})fmt.Println(s)// &{default 8081 true}// Change port only.s.Configure(Config{Port:8082})fmt.Println(s)// &{default 8082 true}// PROBLEM HERE:// Change enabled status only.s.Configure(Config{Eanbled:false})fmt.Println(s)// &{default 8082 true}  --- not changed !!!// Note:// In this case, you need to write a configurator to which all// configuration fields (or at least bool fields) are always transferred.}

As you can see, Boolean logic is not always enough even in such simple tasks.

The following example solves this problem:

package mainimport ("fmt""github.com/goloop/trit")// The defaultEnabled variable is used to set the default value// for the Enabled field of the Config struct.constdefaultEnabled=true// Config is a some configuration struct.typeConfigstruct {NamestringPortintEanbled trit.Trit// use three-valued logic}// Service is a some service struct.typeServicestruct {namestringportintenabled trit.Trit// use three-valued logic}// Configure executes the service configuration without changing// the previously set parameters if the value for a specific// field is not specified.//// If the field is left blank, set the default value.func (s*Service)Configure(configConfig) {// ... some code ...// Now we can easily determine if a field was passed as// a configuration update and if it is defined.if!config.Eanbled.IsUnknown() {s.enabled=config.Eanbled}// It looks like this: if the trit object is not defined,// set it to the default value, otherwise - do not touch it.trit.Default(&s.enabled,defaultEnabled)}// IsEnabled returns the current status of the service.func (s*Service)IsEnabled()bool {returns.enabled.IsTrue()}funcmain() {// ... some code ...// Change enabled status only.s.Configure(Config{Eanbled:-1})fmt.Println(s)// &{default 8082 False} --- now it works !!!// Next code outputs: Service is disabledifs.IsEnabled() {fmt.Println("Service is enabled")}else {fmt.Println("Service is disabled")}}

This is a very simple and demonstrative example of using three-valued logic in everyday life.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see theLICENSE file for details.

About

The trit package provides a Go implementation of three-level logic, including logical operations such as NOT, AND, OR, XOR, NAND, NOR, and XNOR.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp