|
144 | 144 | # application, variables are matched with the bound variable and replaced
|
145 | 145 | # accordingly. This reduction strategy is known as 'call by name'.
|
146 | 146 | #
|
| 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 | +# |
147 | 152 | # Readings on lambda reduction:
|
148 | 153 | # - 'Types and Programming Languages', by Thomas Pierce
|
149 | 154 | # - http://www.cs.yale.edu/homes/hudak/CS201S08/lambda.pdf
|
150 | 155 | # - 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) |
169 | 168 |
|
170 | 169 | VARIABLE_TO_STRING = Y (
|
171 | 170 | lambda f:
|
|
200 | 199 | CHAR_IS_EOF (char)
|
201 | 200 | (lambda _: check_syntax (char)
|
202 | 201 | # TODO: reduce to normal form.
|
203 |
| - (lambda _: EXPRESSION_TO_STRING (parser)) |
| 202 | + (lambda _: EXPRESSION_TO_STRING (EXPRESSION_TO_NORMAL_FORM (parser))) |
204 | 203 | (lambda _: '<invalid program>')
|
205 | 204 | (NONE))
|
206 | 205 | (lambda _: f (check_syntax (char)) (parser (char)))
|
|
0 commit comments