This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_relation_types.py
81 lines (63 loc) · 2.41 KB
/
detect_relation_types.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
from dataclasses import dataclass
from typing import NamedTuple
class Pair(NamedTuple):
x: int
y: int
@dataclass
class Relation:
is_belong_to_type: bool = True
message: str = ""
class WhichRelation(NamedTuple):
reflexive_relation: Relation
symmetric_relation: Relation
antisymmetric_relation: Relation
transitive_relation: Relation
def is_reflexive_relation(M: tuple, taus: set) -> Relation:
relation = Relation()
for x in M:
if Pair(x=x, y=x) not in taus:
relation.is_belong_to_type = False
relation.message = f"({x},{x}) not in taus"
break
return relation
def is_symmetric_relation(M: tuple, taus: set) -> Relation:
relation = Relation()
for x in M:
for y in M:
if Pair(x=x, y=y) in taus:
if Pair(x=y, y=x) not in taus:
relation.is_belong_to_type = False
relation.message = f"({x},{y}) in taus, but ({y},{x}) not in taus"
break
return relation
def is_antisymmetric_relation(M: tuple, taus: set) -> Relation:
relation = Relation()
for x in M:
for y in M:
if Pair(x=x, y=y) in taus and Pair(x=y, y=x) in taus:
if (x == y) == False:
relation.is_belong_to_type = False
relation.message = (
f"({x},{y}) in taus, and ({y},{x}) in taus, but {x} != {y}"
)
break
return relation
def is_transitive_relation(M: tuple, taus: set) -> Relation:
relation = Relation()
for x in M:
for y in M:
for z in M:
if Pair(x=x, y=y) in taus and Pair(x=y, y=z) in taus:
if Pair(x=x, y=z) not in taus:
relation.is_belong_to_type = False
relation.message = f"({x},{y}) in taus, and ({y},{z}) in taus, but ({x},{z}) not in taus"
break
return relation
def is_equivalence_relation(wr: WhichRelation) -> bool:
return wr.reflexive_relation.is_belong_to_type and \
wr.symmetric_relation.is_belong_to_type and \
wr.transitive_relation.is_belong_to_type
def is_order_relation(wr: WhichRelation) -> bool:
return wr.reflexive_relation.is_belong_to_type and \
wr.antisymmetric_relation.is_belong_to_type and \
wr.transitive_relation.is_belong_to_type