Skip to content

Commit 4c72680

Browse files
author
Filippo Costa
committed
Rename REDUCTION -> EXPRESSION_TO_NORMAL_FORM
1 parent 0911779 commit 4c72680

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

main.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,27 @@
144144
# application, variables are matched with the bound variable and replaced
145145
# accordingly. This reduction strategy is known as 'call by name'.
146146
#
147+
# - If it's a variable, then leave it as it is.
148+
# - If it's an abstraction, then replace the bound term inside the body. At the
149+
# first call this doesn't happen!
150+
# - If it's an application, then the bound term becomes the argument.
151+
#
147152
# Readings on lambda reduction:
148153
# - 'Types and Programming Languages', by Thomas Pierce
149154
# - http://www.cs.yale.edu/homes/hudak/CS201S08/lambda.pdf
150155
# - https://en.wikipedia.org/wiki/Reduction_strategy_(lambda_calculus)
151-
REDUCTION = (
152-
lambda f:
153-
lambda expression:
154-
lambda term:
155-
expression
156-
(TRUE)
157-
(lambda variable: variable)
158-
(lambda abstraction: TUPLE
159-
(ABSTRACTION)
160-
# We increment the counter 'n' and keep searching for terms bound to 'x'.
161-
(f (expression (FALSE)) (term) (INCR (n))))
162-
(lambda application: TUPLE
163-
(APPLICATION)
164-
# We reduce both terms and then the second is applied to the first.
165-
(f (_1ST (expression (FALSE))) (x) (n))
166-
# The dummy argument g=ID is just to avoid strict evaluation.
167-
(f (_2ND (_2ND (e))) (x) (n)))
168-
)
156+
EXPRESSION_TO_NORMAL_FORM = ID #Y (
157+
# lambda f:
158+
# lambda current_index:
159+
# lambda term:
160+
# lambda expression:
161+
# expression (TRUE)
162+
# (ID)
163+
# (lambda abstraction: BUILD_ABSTRACTION
164+
# # We increment the counter 'n' and keep searching for terms bound to 'x'.
165+
# (f (expression (FALSE)) (INCR (current_index)) (term)))
166+
# (lambda application: f (INCR (current_index)) (application (FALSE) (FALSE)) (application (FALSE) (TRUE)))
167+
#) (ZERO) (NONE)
169168

170169
VARIABLE_TO_STRING = Y (
171170
lambda f:
@@ -200,7 +199,7 @@
200199
CHAR_IS_EOF (char)
201200
(lambda _: check_syntax (char)
202201
# TODO: reduce to normal form.
203-
(lambda _: EXPRESSION_TO_STRING (parser))
202+
(lambda _: EXPRESSION_TO_STRING (EXPRESSION_TO_NORMAL_FORM (parser)))
204203
(lambda _: '<invalid program>')
205204
(NONE))
206205
(lambda _: f (check_syntax (char)) (parser (char)))

0 commit comments

Comments
 (0)