Skip to main content

Trinary logic in Python

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trinary-0.2.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

trinary-0.2.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file trinary-0.2.0.tar.gz.

File metadata

  • Download URL: trinary-0.2.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.1.0

File hashes

Hashes for trinary-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6665df50c1bd1a82ee29a5904e757af86b338b6e5dcbed2f0b207be63277a998
MD5 320447a6f97dfd400fd721e5a73db54c
BLAKE2b-256 951a81ded67b4de91af20f3457e7df81aef73d8c007781d26b7cafc89fcf5b09

See more details on using hashes here.

File details

Details for the file trinary-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: trinary-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.1.0

File hashes

Hashes for trinary-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9231d7d33d6086fa28b53e486ee37362c4a23aff5baf35d6139ca4139f0c2b8b
MD5 c6926b1f39401c1eee7fb272dfeec783
BLAKE2b-256 29d42daafadb42cecbe9e368b5bd314085e5b33ed03119677cf6353914834ba8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page