-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoperator.py
131 lines (114 loc) · 4.73 KB
/
operator.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
# Author: Bohua Zhan
from kernel.type import BoolType
from kernel.term import Term
LEFT, RIGHT = range(2)
CONST, UNARY, BINARY = range(3)
class OperatorData():
"""Represents information for one operator.
fun_name -- name of the corresponding function.
priority -- operators with higher number will be evaluated first.
assoc -- association to left (LEFT_ASSOC) or right (RIGHT_ASSOC).
arity -- one of CONST, UNARY, or BINARY.
ascii_op -- ascii text of the operator.
unicode_op -- unicode text of the operator.
"""
def __init__(self, fun_name, priority, *, assoc=None, arity=BINARY,
ascii_op, unicode_op=None, key=None):
self.fun_name = fun_name
self.priority = priority
self.assoc = assoc
self.arity = arity
self.ascii_op = ascii_op
if unicode_op is None:
self.unicode_op = ascii_op
else:
self.unicode_op = unicode_op
if key is None:
self.key = self.fun_name
else:
self.key = key
class BinderData():
"""Represents information for one binder.
fun_name -- name of the corresponding function.
ascii_op -- ascii text of the operator.
unicode_op -- unicode text of the operator.
"""
def __init__(self, fun_name, *, ascii_op, unicode_op=None, key=None):
self.fun_name = fun_name
self.ascii_op = ascii_op
if unicode_op is None:
self.unicode_op = ascii_op
else:
self.unicode_op = unicode_op
if key is None:
self.key = self.fun_name
else:
self.key = key
op_data_raw = [
OperatorData("equals", 50, assoc=LEFT, ascii_op="="),
OperatorData("equals", 25, assoc=RIGHT, ascii_op="<-->", unicode_op="⟷", key="iff"),
OperatorData("implies", 20, assoc=RIGHT, ascii_op="-->", unicode_op="⟶"),
OperatorData("conj", 35, assoc=RIGHT, ascii_op="&", unicode_op="∧"),
OperatorData("disj", 30, assoc=RIGHT, ascii_op="|", unicode_op="∨"),
OperatorData("neg", 95, arity=UNARY, ascii_op="~", unicode_op="¬"),
OperatorData("plus", 65, assoc=LEFT, ascii_op="+"),
OperatorData("minus", 65, assoc=LEFT, ascii_op="-"),
OperatorData("uminus", 95, arity=UNARY, ascii_op="-"),
OperatorData("power", 81, assoc=LEFT, ascii_op="^"),
OperatorData("times", 70, assoc=LEFT, ascii_op="*"),
OperatorData("real_divide", 70, assoc=LEFT, ascii_op="/"),
OperatorData("nat_divide", 70, assoc=LEFT, ascii_op="DIV"),
OperatorData("nat_modulus", 70, assoc=LEFT, ascii_op="MOD"),
OperatorData("less_eq", 50, assoc=LEFT, ascii_op="<=", unicode_op="≤"),
OperatorData("less", 50, assoc=LEFT, ascii_op="<"),
OperatorData("greater_eq", 50, assoc=LEFT, ascii_op=">=", unicode_op="≥"),
OperatorData("greater", 50, assoc=LEFT, ascii_op=">"),
OperatorData("zero", 0, arity=CONST, ascii_op="0"),
OperatorData("append", 65, assoc=RIGHT, ascii_op="@"),
OperatorData("cons", 65, assoc=RIGHT, ascii_op="#"),
OperatorData("member", 50, assoc=LEFT, ascii_op="Mem", unicode_op="∈"),
OperatorData("subset", 50, assoc=LEFT, ascii_op="Sub", unicode_op="⊆"),
OperatorData("inter", 70, assoc=LEFT, ascii_op="Int", unicode_op="∩"),
OperatorData("union", 65, assoc=LEFT, ascii_op="Un", unicode_op="∪"),
OperatorData("empty_set", 0, arity=CONST, ascii_op="{}", unicode_op="∅"),
OperatorData("Union", 95, arity=UNARY, ascii_op="UN ", unicode_op="⋃"),
OperatorData("Inter", 95, arity=UNARY, ascii_op="INT ", unicode_op="⋂"),
OperatorData("comp_fun", 60, assoc=RIGHT, ascii_op="O", unicode_op="∘"),
]
binder_data_raw = [
BinderData("all", ascii_op="!", unicode_op="∀"),
BinderData("exists", ascii_op="?", unicode_op="∃"),
BinderData("exists1", ascii_op="?!", unicode_op="∃!"),
BinderData("The", ascii_op="THE "),
BinderData("Some", ascii_op="SOME "),
]
op_data = dict()
for entry in op_data_raw:
op_data[entry.key] = entry
binder_data = dict()
for entry in binder_data_raw:
binder_data[entry.key] = entry
def get_info_for_fun(t):
"""Returns data associated to a function term. The result is None
if the function is not found.
"""
if t.is_const():
if t.name == 'equals':
Targs, _ = t.T.strip_type()
if Targs[0] == BoolType:
return op_data['iff']
else:
return op_data['equals']
else:
if t.name in op_data:
return op_data[t.name]
else:
return None
else:
return None
def get_binder_info_for_fun(t):
"""Returns data associated to a binder."""
if t.is_const() and t.name in binder_data:
return binder_data[t.name]
else:
return None