-
Notifications
You must be signed in to change notification settings - Fork 0
/
trinary.py
156 lines (119 loc) · 3.42 KB
/
trinary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import annotations
import sys
import threading
from abc import ABC
from functools import wraps
from typing import Callable, Final, Optional, final, Union
class Trinary(ABC):
pass
Trinary.register(bool)
def _only_bool(
method: Callable[[UnknownClass, bool], Trinary]
) -> Callable[[UnknownClass, Trinary], Trinary]:
"""
Handle non-bools before passing to the decorated method, a binary operator.
"""
@wraps(method)
def inner(self: UnknownClass, other: Trinary) -> Trinary:
if isinstance(other, bool):
return method(self, other)
# Any binary operator with Unknown as both inputs
# except T or F returns Unknown
if other is self:
return self
return NotImplemented
return inner
@final
class UnknownClass(Trinary):
"""
Trinary logic. Unknown represents both True and False and is a singleton.
https://en.wikipedia.org/wiki/Three-valued_logic
>>> Unknown & True
Unknown
>>> Unknown | True
True
>>> Unknown == False
Unknown
>>> Unknown == Unknown
Unknown
>>> Unknown is False
False
>>> Unknown is Unknown
True
>>> Unknown <= False
Unknown
>>> ~Unknown
Unknown
>>> isinstance(True, Trinary)
True
Unknown can't be cast with bool since it could be either.
Choose with strictly or weakly.
>>> correct = Unknown
>>> strictly(correct)
False
>>> weakly(correct)
True
>>> weakly(False)
False
"""
__slots__ = ()
_instance: Optional[UnknownClass] = None
_lock: threading.Lock = threading.Lock()
def __new__(cls) -> UnknownClass:
if cls._instance is None:
with cls._lock:
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
def __repr__(self) -> str:
return "Unknown"
@_only_bool
def __and__(self, other: Trinary) -> Trinary:
return other and self
__rand__ = __and__
@_only_bool
def __or__(self, other: Trinary) -> Trinary:
return other or self
__ror__ = __or__
@_only_bool
def __eq__(self, other: Trinary) -> Trinary:
return self
__xor__ = __rxor__ = __ne__ = __eq__
def __invert__(self) -> Trinary:
return self
@_only_bool
def __gt__(self, other: Trinary) -> Trinary:
return False if other is True else self
@_only_bool
def __ge__(self, other: Trinary) -> Trinary:
return True if other is False else self
@_only_bool
def __lt__(self, other: Trinary) -> Trinary:
return other and self
@_only_bool
def __le__(self, other: Trinary) -> Trinary:
return other or self
def __hash__(self) -> int:
return hash(UnknownClass)
def __bool__(self):
raise TypeError("Unknown can't cast to a bool. Use strongly() or weakly().")
Unknown: Final[UnknownClass] = UnknownClass()
if sys.version_info > (3, 10):
Trinary = Union[bool, UnknownClass]
def strictly(val) -> bool:
"""
Unknown -> False
"""
return val is not Unknown and bool(val)
def weakly(val) -> bool:
"""
Unknown -> True
"""
return val is Unknown or bool(val)
def contains(container, item) -> bool:
"""
Unknowns will break containment checks. Use this instead.
"""
if item is Unknown:
return any(item is Unknown for item in container)
return item in container