Skip to main content

License Code style: black

trinary - A Python implementation of three-valued logic

trinary is a Python library for working with three-valued logic. It allows you to represent and manipulate statements with three possible truth values: true, false, and unknown. Unknown represents the possibility of true and false.

Usage

To use trinary, import Unknown into your Python project. You can then use Unknown alongside True and False.

from trinary import Unknown

# Logical AND
print(Unknown & True)      # Unknown
print(Unknown & False)     # False
print(Unknown & Unknown)   # Unknown

# Logical OR
print(Unknown | True)      # True
print(Unknown | False)     # Unknown
print(Unknown | Unknown)   # Unknown

# Logical XOR
print(Unknown ^ True)      # Unknown
print(Unknown ^ False)     # Unknown
print(Unknown | Unknown)   # Unknown

# Logical NOT
print(~Unknown)            # Unknown

# Comparisons
print(Unknown == True)     # Unknown
print(Unknown == False)    # Unknown
print(Unknown == Unknown)  # Unknown   
print(Unknown != True)     # Unknown
print(Unknown != False)    # Unknown
print(Unknown != Unknown)  # Unknown
print(Unknown < True)      # Unknown
print(Unknown < False)     # False
print(Unknown < Unknown)   # Unknown   
print(Unknown <= True)     # True
print(Unknown <= False)    # Unknown
print(Unknown <= Unknown)  # Unknown   
print(Unknown > True)      # False
print(Unknown > False)     # Unknown
print(Unknown > Unknown)   # Unknown   
print(Unknown >= True)     # Unknown
print(Unknown >= False)    # True
print(Unknown >= Unknown)  # Unknown

To cast to a bool, use strictly or weakly to decide how Unknown is cast.

from trinary import Unknown, strictly, weakly

correct = Unknown
print(strictly(correct))  # False
print(weakly(correct))    # True
# anything else is the same as calling bool()
print(weakly(''))         # False

Examples

Use trinary to represent the truth value of a statement with uncertain information.

from trinary import Trinary, Unknown, strictly, weakly

test_a = Unknown
test_b = True

passed_both = test_a & test_b
print(passed_both)            # Unknown
print(strictly(passed_both))  # False
passed_at_least_one = test_a | test_b
print(passed_at_least_one)    # True
maybe_failed_both = weakly(~test_a & ~test_b)
print(maybe_failed_both)      # True


# Example with functions and type hints
def hot_out(weather: str) -> Trinary:
    if weather == "sunny":
        return True
    elif weather == "cloudy":
        return Unknown
    else:
        return False


def going_to_the_beach(weather: str, off_work: Trinary) -> Trinary:
    return hot_out(weather) & off_work


monday_beach = going_to_the_beach(weather="cloudy", off_work=False)
print(monday_beach)              # False
saturday_beach = going_to_the_beach(weather="cloudy", off_work=True)
print(saturday_beach)            # Unknown
definitely_free_saturday = strictly(~saturday_beach)
print(definitely_free_saturday)  # False

Theory

trinary implements Stephen Cole Kleene's "strong logic of indeterminacy", also called K3. This is equivalent to SQL logic with NULL.

Truth Table

p q p&q p^q p⇒q ¬p
T T T F T F
F F F F T T
F ? F ? ? T
? T ? ? T ?
? F F ? ? ?
? ? ? ? ? ?

License

trinary is licensed under the MIT License.

Release files for trinary 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trinary 0.2.0
File Size Uploaded
trinary-0.2.0.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trinary 0.2.0
File Interpreter ABI Platform
trinary-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / trinary-0.2.0.tar.gz

Download URL trinary-0.2.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6665df50c1bd1a82ee29a5904e757af86b338b6e5dcbed2f0b207be63277a998
BLAKE2b-256 checksum
How to use checksums
951a81ded67b4de91af20f3457e7df81aef73d8c007781d26b7cafc89fcf5b09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.0 Darwin/22.1.0

Release files / trinary-0.2.0-py3-none-any.whl

Download URL trinary-0.2.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9231d7d33d6086fa28b53e486ee37362c4a23aff5baf35d6139ca4139f0c2b8b
BLAKE2b-256 checksum
How to use checksums
29d42daafadb42cecbe9e368b5bd314085e5b33ed03119677cf6353914834ba8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.0 Darwin/22.1.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page