No project description provided
Project description
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
x = Unknown
y = True
z = False
trinary works with the standard comparisons and bitwise operators.
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
print(weakly(True)) # True
print(weakly(False)) # True
print(weakly('')) # False
print(weakly(' ')) # True
Use trinary to represent the truth value of a statement with uncertain information.
from trinary import Trinary, Unknown, strictly, weakly
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.