From c04bfcd1ed49a59df4cfc5b70ba4464b37799670 Mon Sep 17 00:00:00 2001 From: Evin Sellin Date: Sun, 1 Oct 2017 10:13:01 -0500 Subject: [PATCH] allow beta/eta reduction --- src/lib/lambda/churchPrimitives.ts | 5 +++++ src/lib/lambda/normalize.ts | 5 ++++- src/lib/lambda/wellKnownFunctions.ts | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/lib/lambda/wellKnownFunctions.ts 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"}};