-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducers.py
72 lines (62 loc) · 1.92 KB
/
reducers.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
from terms import var, abs, app
def is_var(term):
return isinstance(term, var)
def is_abs(term):
return isinstance(term, abs)
def is_app(term):
return isinstance(term, app)
def lift(term, offset, depth=0):
'''Increment all indices that are free to a given depth'''
if is_var(term):
if term.index < depth:
# Bound variable
return term
else:
# Free variable
return var(term.index + offset)
elif is_abs(term):
return abs(lift(term.body, offset, depth + 1))
elif is_app(term):
return app(lift(term.function, offset, depth), lift(term.argument, offset, depth))
else:
pass # raise Exception('Invalid term: {}'.format(term))
def substitution(term, substitute, index=0):
'''Substitute the variable with the specified index'''
if is_var(term):
if term.index < index:
return term
elif term.index == index:
return lift(substitute, index)
else:
return var(term.index - 1)
elif is_abs(term):
return abs(substitution(term.body, substitute, index + 1))
elif is_app(term):
return app(substitution(term.function, substitute, index), substitution(term.argument, substitute, index))
else:
pass # raise Exception('Invalid term: {}'.format(term))
def is_head_reducible(term):
return is_app(term) and is_abs(term.function)
def head_reduction(term):
return substitution(term.function.body, term.argument)
def head_normal_form(term):
if is_head_reducible(term):
return head_normal_form(head_reduction(term))
else:
return term
def normal_form(term):
if is_var(term):
return term
elif is_abs(term):
return abs(normal_form(term.body))
elif is_app(term):
# Reduce function first
term.function = normal_form(term.function)
# See whether further reduction is necessary
if is_head_reducible(term):
return normal_form(head_reduction(term))
else:
return app(normal_form(term.function), normal_form(term.argument))
else:
pass # raise Exception('Invalid term: {}'.format(term))
# Implement eta reduction