-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkleene.py
112 lines (86 loc) · 3.21 KB
/
kleene.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
##################################################################
#
# Kleene logic class library
#
##################################################################
# https://en.wikipedia.org/wiki/Three-valued_logic#Kleene_and_Priest_logics
import numpy as np
from enum import IntEnum, unique
@unique
class kleene(IntEnum) :
#
false = 0
true = 1
dunno = 2
def __str__(self) :
if self==kleene.false : return 'false'
if self==kleene.true : return 'true'
return 'dunno'
def __repr__(self):
return f'kleene.{str(self)}'
def __invert__(self) : # not
if self==kleene.false : return kleene.true
if self==kleene.true : return kleene.false
return kleene.dunno
def __and__(self,other) :
if (self==kleene.false) or (other==kleene.false) : return kleene.false
if (self==kleene.true) and (other==kleene.true) : return kleene.true
return kleene.dunno
def __or__(self,other) :
if self==kleene.true or other==kleene.true : return kleene.true
if self==kleene.false and other==kleene.false : return kleene.false
return kleene.dunno
def __xor__(self,other) : # is this correct?
if self==kleene.dunno or other==kleene.dunno : return kleene.dunno
if self==other : return kleene.false
return kleene.true
def negate(self) : # as a method
if self==kleene.false : return kleene.true
if self==kleene.true : return kleene.false
return kleene.dunno
def c(self) : # colored string
kolor = ['\x1b[33m\x1b[1m', '\x1b[32m\x1b[1m', '\x1b[37m\x1b[1m', '\x1b[0m']
return kolor[int(self)] + str(self) + kolor[-1]
# &= __IAND__(SELF, OTHER)
# |= __IOR__(SELF, OTHER)
# ^= __IXOR__(SELF, OTHER)
# def __bool__(self) :
# #return bool(self.value)
# if self==kleene.false : return False
# if self==kleene.true : return True
# return np.nan
###################################################################
# testing for the Kleene logic code
'''
a = kleene(0)
a = kleene(False)
b = kleene(1)
b = kleene(True)
c = kleene(2)
c = kleene(kleene.dunno)
#c = kleene()
#c = kleene((0,1))
bool(kleene(0)) # False
bool(kleene(1)) # True
bool(kleene(2)) # True
print(repr(a))
print(repr(b))
print(repr(c))
print(a.c())
print(b.c())
print(c.c())
print(kleene.c(a))
print(kleene.c(b))
print(kleene.c(c))
k = [a,c,b]
print('\nConjunction')
print('\t\t', False, '\t', 'Dunno', '\t', True,sep='')
i = k[0]; print(i, '\t',(i & k[0]).c(), '\t',(i & k[1]).c(), '\t',(i & k[2]).c(), sep='')
i = k[1]; print(i, '\t',(i & k[0]).c(), '\t',(i & k[1]).c(), '\t',(i & k[2]).c(), sep='')
i = k[2]; print(i, '\t',(i & k[0]).c(), '\t',(i & k[1]).c(), '\t',(i & k[2]).c(), sep='')
print('\nDisjunction')
print('\t\t', False, '\t', 'Dunno', '\t', True,sep='')
i = k[0]; print(i, '\t', (i | k[0]).c(), '\t', (i | k[1]).c(), '\t', (i | k[2]).c(), sep='')
i = k[1]; print(i, '\t', (i | k[0]).c(), '\t', (i | k[1]).c(), '\t', (i | k[2]).c(), sep='')
i = k[2]; print(i, '\t', (i | k[0]).c(), '\t', (i | k[1]).c(), '\t', (i | k[2]).c(), sep='')
'''