-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathterms.py
104 lines (75 loc) · 2.88 KB
/
terms.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
class Variable (object):
def __init__ (self, uName):
self._name = uName
def __str__ (self):
return str (self._name)
def __eq__ (self, other):
if isinstance (other, Variable):
if other._name == self._name:
return True
return False
def iswhnf(self):
return True
# def __hash__ (self):
# return hash (self._name)
# def unify (self, uTerm):
# if not isinstance (uTerm, Variable):
# return False
# return (Variable (self._name), Variable (uTerm._name))
class Application (object):
def __init__ (self, uFirst, uSecond):
self._first = uFirst
self._second = uSecond
def __str__ (self):
#return '(' + str (self._first) + ')' + '(' + str (self._second) + ')'
return str (self._first) + str (self._second)
def __eq__ (self, other):
if isinstance (other, Application):
return (other._first == self._first) and \
(other._second == self._second)
return False
def iswhnf(self):
if isinstance(self._first,Abstraction) :
return False
elif isinstance(self._first,Application) :
return self._first.iswhnf()
else :
return True
# def __hash__ (self):
# return hash ((self._first, self._second))
class Abstraction (object):
def __init__ (self, uVariable, uBody):
self._variable = uVariable
self._body = uBody
def __str__ (self):
return '(\\' + str (self._variable) + '.' + str (self._body) + ')'
def __eq__ (self, other):
if isinstance (other, Abstraction):
return (other._variable == self._variable) and \
(other._body == self._body)
return False
def iswhnf(self):
return True
# def __hash__ (self):
# return hash ((self._variable, self._body))
# def unify (self, uTerm):
# if not isinstance (uTerm, Abstraction):
# return False
# ## calculate the substitutions for the body of the abstraction
# body_substitutions = self._body.unify (uTerm._body)
# ## then do the same for the head
# head_substitutions = self._variable.unify (uTerm._variable)
# ## check, whether a clash exists, that the substitution
# ## to perform is the same
# # for sub in head_substitutions:
# # if sub in body_substitutions:
# # (to_subtitute, new_term) = sub
# # try:
# # new_term = body_substitutions [sub]
# # if new_term == head_substitutions [sub]: continue
# # else: return False
# # except: continue
## return the merged dict of substitutions
# for s in head_substitutions:
# body_substitutions.append (s)
# return body_substitutions