Skip to content

Commit 751795e

Browse files
author
Johan Wiltink
committed
2 parents 848dccd + 4a5a892 commit 751795e

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@codewars/lambda-calculus",
33
"type": "module",
4-
"version": "1.0.0-rc.5",
4+
"version": "1.0.2",
55
"description": "Lambda Calculus evaluator for Codewars",
66
"main": "src/lambda-calculus.js",
77
"files": [

src/lambda-calculus.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ export function fromInt(n) {
133133
export function toInt(term) {
134134
try {
135135
if ( config.numEncoding === "Church" ) {
136-
return term ( x => x+1 ) ( Primitive(0) );
136+
const succ = x => x+1 ;
137+
const result = term ( succ ) ;
138+
return result ( result === succ ? 0 : Primitive(0) );
137139
} else if ( config.numEncoding === "Scott" ) {
138140
let result = 0, evaluating = true;
139141
while ( evaluating )
@@ -229,8 +231,13 @@ function parse(code) {
229231
function v(i) {
230232
const r = name(i);
231233
if ( r ) {
232-
const [j,name] = r;
233-
return [j,new V(name)];
234+
const [j,termName] = r;
235+
if ( termName==="_" ) {
236+
const undef = new V("()");
237+
undef.defName = name(0)[1];
238+
return [j,undef];
239+
} else
240+
return [j,new V(termName)];
234241
} else
235242
return null;
236243
}
@@ -323,7 +330,10 @@ function evalLC(term) {
323330
let argEnv;
324331
if ( arg?.term && arg?.env ) ({ term: arg, env: argEnv } = arg); // if callback is passed another callback, or a term
325332
const termVal = new Tuple( typeof arg === 'number' ? fromInt(arg) : arg , new Env(argEnv) );
326-
return runEval( new Tuple(term.body, new Env(env).setThunk(term.name, termVal)), stack );
333+
if ( term.name==="_" )
334+
return runEval( new Tuple(term.body, new Env(env)), stack );
335+
else
336+
return runEval( new Tuple(term.body, new Env(env).setThunk(term.name, termVal)), stack );
327337
}
328338
return Object.assign( result, {term,env} );
329339
}
@@ -364,7 +374,7 @@ function evalLC(term) {
364374
env = new Env(env).setThunk(term.name, new Tuple(lastTerm, lastEnv));
365375
term = term.body;
366376
} else { // Pass the function some other function.
367-
term = lastTerm(awaitArg(term, stack, env));
377+
term = lastTerm(awaitArg(term, [], env));
368378
}
369379
} else if ( term instanceof Tuple ) {
370380
// for primitives
@@ -404,9 +414,6 @@ function printStackTrace(error, term, stack) {
404414

405415
if ( config.verbosity >= "Loquacious" )
406416
console.error( stack.slice(stackCutoff).reverse().map( v => `\twhile evaluating ${ v }`).join('\n') );
407-
408-
if ( config.verbosity >= "Verbose" )
409-
console.error( stack.slice().reverse().map( v => `\twhile evaluating ${ v }`).join('\n') );
410417
}
411418

412-
Object.defineProperty( Function.prototype, "valueOf", { value: function valueOf() { return toInt(this); } } );
419+
Object.defineProperty( Function.prototype, "valueOf", { value: function valueOf() { if (this.term) return toInt(this); else return this; } } );

tests/basics-church/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ describe("Church tests",function(){
3636
assert.equal( times(1e2)(1e1), 1e3 );
3737
assert.equal( pow(10)(3), 1e3 );
3838
assert.equal( pred(pow(10)(3)), 1e3-1 );
39+
assert.equal( pow(0)(0), 1);
3940
});
4041
});

workspace/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workspace/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"timeout": 0
1212
},
1313
"dependencies": {
14-
"@codewars/lambda-calculus": "^1.0.0-rc.5"
14+
"@codewars/lambda-calculus": "^1.0.2"
1515
},
1616
"devDependencies": {
1717
"@codewars/mocha-reporter": "^1.0.0",

workspace/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const F = _ => f => f;
88

99
describe("counter", () => {
1010
it("fixed tests", () => {
11-
assert.equal(counter(T)(T)(T)(F), 3);
12-
assert.equal(counter(T)(F), 1);
13-
assert.equal(counter(T)(T)(T)(T)(T)(T)(T)(F), 7);
11+
assert.numEql(counter(T)(T)(T)(F), 3);
12+
assert.numEql(counter(T)(F), 1);
13+
assert.numEql(counter(T)(T)(T)(T)(T)(T)(T)(F), 7);
1414
});
1515
});

0 commit comments

Comments
 (0)