diff --git a/src/lib/lambda/churchPrimitives.ts b/src/lib/lambda/churchPrimitives.ts index 1b122d6..23396b6 100644 --- a/src/lib/lambda/churchPrimitives.ts +++ b/src/lib/lambda/churchPrimitives.ts @@ -1,6 +1,7 @@ import { equal } from './equality'; import { parseTerm } from './parser'; import { cannonize } from './cannonize'; +import { id } from './wellKnownFunctions'; // TODO: do the inverse of these -- generation of church primitives @@ -10,6 +11,10 @@ export function renderAsChurchNumeral(uncannonized) { if (expression.type !== 'function') { return undefined; } + // exception: identity fn under beta-eta reduction is church 1 + if (equal(expression, id)) { + return 1; + } const outerName = expression.argument; const inner = expression.body; if (inner.type !== 'function') { diff --git a/src/lib/lambda/normalize.ts b/src/lib/lambda/normalize.ts index a08e821..b3d9808 100644 --- a/src/lib/lambda/normalize.ts +++ b/src/lib/lambda/normalize.ts @@ -1,4 +1,4 @@ -import { bReducable, bReduce } from './operations'; +import { bReducable, bReduce, eReducable, eReduce } from './operations'; // Call by name eval strategy // Expression -> Expression (with a depth overflow) @@ -22,6 +22,9 @@ function leftmostOutermostRedex(expression){ if(bReducable(expression)) { return bReduce(expression); } + if (eReducable(expression)) { + eReduce(expression); + } if (expression.type === 'function'){ const res = leftmostOutermostRedex(expression.body); if (res === undefined) { diff --git a/src/lib/lambda/wellKnownFunctions.ts b/src/lib/lambda/wellKnownFunctions.ts new file mode 100644 index 0000000..1bc9d6e --- /dev/null +++ b/src/lib/lambda/wellKnownFunctions.ts @@ -0,0 +1 @@ +export const id = {"type":"function","argument":"a","body":{"type":"variable","name":"a"}};