-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
226 lines (190 loc) · 5.48 KB
/
Copy pathindex.ts
File metadata and controls
226 lines (190 loc) · 5.48 KB
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
type Stack = readonly unknown[];
// StackOp represents our primitive stack operations
type StackOp =
| readonly ["push", unknown]
| readonly ["pop"]
| readonly ["dup"]
| readonly ["swap"]
| readonly ["add"]
| readonly ["mul"]
| readonly ["apply", (stack: Stack) => Stack];
// Operation results mapped to their operations
// Define return types for operations
type OperationReturnType = {
push: Stack;
pop: Stack;
dup: Stack;
swap: Stack;
add: Stack;
mul: Stack;
apply: Stack;
};
// Map operation to its return type
type OperationResult<Op> = Op extends [infer T]
? T extends keyof OperationReturnType
? OperationReturnType[T]
: never
: never;
// Free monad implementation using tuples
type Pure<F, A> = ["pure", A];
type Impure<F, A> = ["impure", F, (x: OperationResult<F>) => Free<F, A>];
type Free<F, A> = Pure<F, A> | Impure<F, A>;
const pure = <F, A>(a: A): Free<F, A> => ["pure", a] as const;
const impure = <F, A>(
fa: F,
next: (x: OperationResult<F>) => Free<F, A>
): Free<F, A> => ["impure", fa, next] as const;
// Generic operation constructor
const makeOp =
<T extends StackOp[0]>(type: T) =>
(...args: T extends keyof OperationArgs ? [OperationArgs[T]] : []) =>
impure([type, ...args] as const, (stack: Stack) => pure(stack));
// Type helper for operations with arguments
type OperationArgs = {
push: unknown;
apply: (stack: Stack) => Stack;
};
// Create all operations using the generic constructor
const push = makeOp("push");
const pop = makeOp("pop");
const dup = makeOp("dup");
const swap = makeOp("swap");
const add = makeOp("add");
const mul = makeOp("mul");
const apply = makeOp("apply");
// Monadic operations
const map = <F, A, B>(fa: Free<F, A>, f: (a: A) => B): Free<F, B> => {
const [tag] = fa;
switch (tag) {
case "pure": {
const [, ...rest] = fa;
return pure(f(rest[0]));
}
case "impure": {
const [, ...rest] = fa;
const [value, next] = rest;
return impure(value, (x) => map(next(x), f));
}
}
};
const flatMap = <F, A, B>(
fa: Free<F, A>,
f: (a: A) => Free<F, B>
): Free<F, B> => {
const [tag] = fa;
switch (tag) {
case "pure": {
const [, ...rest] = fa;
return f(rest[0]);
}
case "impure": {
const [, ...rest] = fa;
const [value, next] = rest;
return impure(value, (x) => flatMap(next(x), f));
}
}
};
// Stack interpreter
const interpreter = (initialStack: Stack = []) => {
const interpret = async <A>(program: Free<StackOp, A>): Promise<A> => {
const [tag] = program;
switch (tag) {
case "pure": {
const [, result] = program;
return result;
}
case "impure": {
const [, ...rest] = program;
const [op, next] = rest;
let result: Stack;
const [opName] = op;
switch (opName) {
case "push": {
const [, item] = op;
result = [item, ...initialStack];
break;
}
case "pop": {
const [, ...rest] = initialStack;
result = rest;
break;
}
case "dup": {
const [top, ...restDup] = initialStack;
result = [top, top, ...restDup];
break;
}
case "swap": {
const [a, b, ...restSwap] = initialStack;
result = [b, a, ...restSwap];
break;
}
case "add": {
const [n1, n2, ...restAdd] = initialStack;
result = [Number(n1) + Number(n2), ...restAdd];
break;
}
case "mul": {
const [m1, m2, ...restMul] = initialStack;
result = [Number(m1) * Number(m2), ...restMul];
break;
}
case "apply": {
const [, f] = op;
result = f(initialStack);
break;
}
default: {
result = initialStack;
}
}
initialStack = result;
return interpret(next(result as OperationResult<typeof op>));
}
}
};
return interpret;
};
// Higher-level combinators
const sequence = (
...operations: Free<StackOp, Stack>[]
): Free<StackOp, Stack> =>
operations.reduce((acc, op) => flatMap(acc, (stack) => op), pure([]));
// Example: Define words (functions) as sequences of operations
const squared = sequence(dup(), mul());
const sum3 = sequence(add(), add());
// Example usage: Calculate (2 + 3) * 4
const program = sequence(push(2), push(3), add(), push(4), mul());
// Example: Factorial implementation
const factorial = (n: number): Free<StackOp, Stack> => {
if (n <= 1) {
return push(1);
}
return sequence(push(n), factorial(n - 1), mul());
};
// Examples showing composition
const complexProgram = sequence(push(5), squared, push(3), push(2), sum3);
// Custom word definitions
const define = (name: string, program: Free<StackOp, Stack>) => ({
[name]: program,
});
const words = {
...define("squared", squared),
...define("sum3", sum3),
};
// Example of using the interpreter
const runExample = async () => {
const interpret = interpreter();
const test = interpreter([2]);
console.log("squared:", await test(squared));
console.log("Running (2 + 3) * 4:");
const result1 = await interpret(program);
console.log(result1);
console.log("\nRunning 5 squared + 2 + 3:");
const result2 = await interpret(complexProgram);
console.log(result2);
console.log("\nCalculating factorial of 5:");
const result3 = await interpret(factorial(5));
console.log(result3);
};
runExample();