detect (some) terms without a normal form.

This commit is contained in:
trans_soup 2023-11-11 17:45:31 +01:00
parent 1001dfb152
commit 7e928a7a5f
1 changed files with 11 additions and 0 deletions

View File

@ -1,12 +1,23 @@
function serialize (expr) {
return Array.isArray(expr)
? "(" + expr.map(serialize).join(",") + ")"
: expr;
}
export function reduce (expr, max_steps = 1000) {
if (!is_application(expr)) {
return expr;
}
let result = reduce_single(expr);
const visited = new Map();
// trampoline
for (let step = 0; step < max_steps; step++) {
const key = serialize(result);
if (visited.has(key)) return result;
visited.set(key, true);
result = reduce_single(result);
}