-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.dart
290 lines (253 loc) · 9.89 KB
/
example.dart
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// ignore_for_file: avoid_print
import 'package:lambda_calculus/lambda_calculus.dart';
void main(List<String> arguments) {
// This main function is a walkthrough for this lambda calculus interpreter,
// assuming you already know about lambda calculus.
// Check out each function for more details.
print('PART I: UNTYPED LAMBDA CALCULUS\n');
_printExamples();
_parseLambda();
_countFreeVars();
_evaluationsByValue();
_fullEvaluations();
_evaluationsByName();
_factorial();
print('');
print('PART II: TYPED LAMBDA CALCULUS\n');
final l = r"\f. \g. \x. f (g x)".toLambda()!;
print("The type for $l is ${l.findType()}");
final m = r"\a. \b. \c. a c (b c)".toLambda()!;
print("The type for $m is ${m.findType()}");
final n = r"\a. (\b.\c. b c) (\b.\c. c ) a".toLambda()!;
print("The type for $n is ${n.findType()}");
print("The type for ${Lambda.constants.and()} is "
"${Lambda.constants.and().findType()}");
print("The type for ${Lambda.constants.yCombinator()} does not exist: "
"${Lambda.constants.yCombinator().findType()}");
print('');
}
void _printExamples() {
// This function prints out several useful lambda expressions.
print('Some lambda expressions with minimal brackets: ');
print('Lambda Id: ${Lambda.constants.identity()}');
print('Lambda True: ${Lambda.constants.lambdaTrue()}');
print('Lambda False: ${Lambda.constants.lambdaFalse()}');
print('Lambda Test: ${Lambda.constants.test()}');
print('Lambda And: ${Lambda.constants.and()}');
print('Lambda Pair: ${Lambda.constants.pair()}');
print('Lambda Not: ${Lambda.constants.not()}');
print('Lambda Succ: ${Lambda.constants.succ()}');
print('Lambda Times: ${Lambda.constants.times()}');
print('Lambda Plus: ${Lambda.constants.plus()}');
print('Lambda Seven: ${Lambda.constants.seven()}');
print('Omega: ${Lambda.constants.omega()}');
print('Y-Combinator: ${Lambda.constants.yCombinator()}');
print('');
}
void _parseLambda() {
// This function parses several lambda expressions from String.
String str;
Lambda? temp;
print('Lambda parser: ');
// The names of the variables are usually preserved.
// Nameless variables are converted to _x1, _x2 etc., but otherwise they are
// syntactically identical.
print("1. Print the 'succ' expression:");
str = 'λa. λb . λc. b (a b c)';
temp = str.toLambda();
print(' original: $str');
print(' parsed: $temp');
// We can also use slash and backslash to replace the lambda letter, as well
// as "->" to replace the ".".
print('2. Print the Y-Combinator:');
str = r'/x. (\y -> x (\z. y y z)) (/y. x (/z. y y z))';
temp = str.toLambda();
print(' original: $str');
print(' parsed: $temp');
print('3. Print an invalid lambda expression:');
str = 'λx. ';
temp = str.toLambda();
print(' original: $str');
print(' parsed: $temp');
// We can omit the variable after 'λ' if it is not used. In this case, a name
// in the form of _x{n} will be generated.
print('4. Print a lambda expression with an unused variable:');
str = 'λx. λ. x';
temp = str.toLambda();
print(' original: $str');
print(' parsed: $temp');
// We can also parse lambda expression written in De Bruijn Indices. Again,
// names in the form of _x{n} will be generated.
print('5. Print a lambda expression with De Bruijn Indices:');
str = 'λ. λ. 1 (1 0)';
temp = str.toLambda();
print(' original: $str');
print(' parsed: $temp');
// If the same variable name has been defined more than once, each usage is
// bounded to the closest definition.
print('6. λx. λx. x is the same as λx. λy. y');
str = 'λx. λx. x';
temp = str.toLambda();
print(' original: $str');
print(' parsed without names: ${temp!.toStringNameless()}');
/// For nested abstractions, we can omit the lambda symbol for the inner
/// abstractions.
print('7. λx y z. x y z is the same as λx. λy. λz. x y z');
str = 'λx y z. x y z';
temp = str.toLambda();
print(' original: $str');
print(' parsed without names: $temp');
print('');
}
void _countFreeVars() {
Lambda temp;
temp = r'(\x. \y. x c) (\a. \b. c a (\d. \c. a c d))'.toLambda()!;
print('Lambda: $temp');
print('Number of free vars: ${temp.freeCount(countDistinct: true)}');
print('');
}
void _evaluationsByValue() {
// This function demonstrates the evaluation of lambda expressions through
// beta-reduction with the "call by value" scheme.
Lambda temp;
print('Evaluate lambda expressions with the "call by value" scheme: ');
temp = LambdaBuilder.applyAll([
LambdaBuilder.constants.test(),
LambdaBuilder.constants.lambdaTrue(),
LambdaBuilder.constants.two(),
LambdaBuilder.constants.one(),
]).build();
// We use the .eval1() method to evaluate a lambda expression by one step.
print("1. Evaluate 'test true 2 1' step-by-step: ");
print(' $temp');
print(' = ${temp.eval1()}');
print(' = ${temp.eval1()!.eval1()}');
print(' = ${temp.eval1()!.eval1()!.eval1()}');
print(' = ${temp.eval1()!.eval1()!.eval1()!.eval1()}');
print(' = ${temp.eval1()!.eval1()!.eval1()!.eval1()!.eval1()}');
// We use the .eval() method to evaluate a lambda expression fully.
print("2. Evaluate 'test false 2 1' directly to its simplest form: ");
temp = LambdaBuilder.applyAll([
LambdaBuilder.constants.test(),
LambdaBuilder.constants.lambdaFalse(),
LambdaBuilder.constants.two(),
LambdaBuilder.constants.one(),
]).build();
print(' $temp\n = ${temp.eval()}');
// Demonstration of the "call by value" scheme.
print('3. An application within an abstraction is not reduced: ');
temp = Lambda(
form: LambdaForm.abstraction,
exp1: LambdaBuilder.applyAll(
[
LambdaBuilder.constants.identity(),
LambdaBuilder.constants.lambdaFalse(),
],
).build(),
);
print(' $temp\n = ${temp.eval()}');
// Another example: 'succ 2' results an expression behaviourally equivalent to
// but syntactically distinct from 3.
print("4. Evaluate 'succ 2', but the result is not the same as '3': ");
temp = LambdaBuilder.applyAll([
LambdaBuilder.constants.succ(),
LambdaBuilder.constants.two(),
]).build();
print(' $temp\n = ${temp.eval()}');
print("5. Evaluate 'succ 2', converting it to a natural number: ");
print(' $temp\n = ${temp.toInt()}');
print('');
}
void _fullEvaluations() {
// This function demonstrates the evaluation of lambda expressions through
// full beta-reduction.
Lambda temp;
print('Evaluate lambda expressions using full beta-reduction: ');
// We pass 'LambdaEvaluationType.FULL_REDUCTION' to the 'evalType' parameter
// in .eval() method to evaluate a lambda expression through full
// beta-reduction.
print("1. Evaluate '2 + 3' directly to its simplest form: ");
temp = LambdaBuilder.applyAll([
LambdaBuilder.constants.plus(),
LambdaBuilder.constants.two(),
LambdaBuilder.constants.three(),
]).build();
print(' $temp');
print(' = ${temp.eval(evalType: LambdaEvaluationType.fullReduction)}');
print("2. Evaluate '2 * 3' directly to its simplest form: ");
temp = LambdaBuilder.applyAll([
LambdaBuilder.constants.times(),
LambdaBuilder.constants.two(),
LambdaBuilder.constants.three(),
]).build();
print(' $temp');
print(' = ${temp.eval(evalType: LambdaEvaluationType.fullReduction)}');
print("3. Evaluate '2 ^ 3' directly to its simplest form: ");
temp = LambdaBuilder.applyAll([
LambdaBuilder.constants.power(),
LambdaBuilder.constants.two(),
LambdaBuilder.constants.three(),
]).build();
print(' $temp');
print(' = ${temp.eval(evalType: LambdaEvaluationType.fullReduction)}');
print('');
}
void _evaluationsByName() {
// This function demonstrates the evaluation of lambda expressions through
// beta-reduction with the "call by name" scheme.
Lambda temp;
print('Compare "call by name" with "call by value": ');
// We pass 'LambdaEvaluationType.CALL_BY_NAME' to the 'evalType' parameter
// in .eval1() method to evaluate a lambda expression through the
// "call-by-name" scheme.
print("1. Evaluate 'true 1 omega' lazily (call by name): ");
temp = LambdaBuilder.applyAll([
LambdaBuilder.constants.lambdaTrue(),
LambdaBuilder.constants.one(),
LambdaBuilder.constants.omega(),
]).build();
print(' $temp');
print(' = ${temp.eval1(evalType: LambdaEvaluationType.callByName)}');
print(
' = ${temp.eval1(evalType: LambdaEvaluationType.callByName)!.eval1(evalType: LambdaEvaluationType.callByName)}',
);
// In contrast, the expression diverges in a "call by value" scheme.
print("2. Evaluate 'true 1 omega' strictly (call by value): ");
print(' $temp');
print(' = ${temp.eval1()}');
print(' = ${temp.eval1()!.eval1()}');
print(' = ${temp.eval1()!.eval1()!.eval1()}');
print(' = ${temp.eval1()!.eval1()!.eval1()!.eval1()}');
print(' = ${temp.eval1()!.eval1()!.eval1()!.eval1()!.eval1()}');
print(' ...');
print('');
}
void _factorial() {
// The factorial function.
Lambda result;
print('Recursive factorial function with the Y-Combinator: ');
final factorial = LambdaBuilder.applyAll([
Lambda.constants.yCombinator(),
r'''
\a.\b.(\.\c.\.2(c)(0))((\.0(\.\.\.0)(\.\.1))b)(\.\.\.1(0))(\.(\.\d.\.2(d(0)))b(a((\c.(\.0(\.\.1))
(c(\d.(\a.\b.\.0(a)(b))((\.0(\.\.0))d)((\a.\.\.1(a(1)(0)))((\.0(\.\.0))d)))((\d.\a.\.0(d)
(a))(\.\.0)(\.\.0))))b)))(\.0)
'''
.toLambda()!,
]).build();
print('The factorial lamdba expression: ');
print(' $factorial');
print('1. Evaluate 0!: ');
result = LambdaBuilder.applyAll([factorial, LambdaBuilder.constants.zero()])
.build()
.eval();
print(' $result');
print(' = ${result.toInt()}');
print('2. Evaluate 3!: ');
result = LambdaBuilder.applyAll([factorial, LambdaBuilder.constants.three()])
.build()
.eval();
print(' $result');
print(' = ${result.toInt()}');
print('');
}