Skip to content

Commit 3c0f6e1

Browse files
authored
Merge pull request #105 from JohanWiltink/main
negabinary-scott test updates
2 parents 9bc1db1 + 67121e8 commit 3c0f6e1

File tree

2 files changed

+21
-50
lines changed

2 files changed

+21
-50
lines changed

tests/negabinary-scott/solution.lc

+3-26
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ snd = \ xy . xy \ _x y . y
2323
bimap = \ fn xy . xy \ x y . Pair (fn x) (fn y)
2424
Y2 = B Y (C (B bimap T))
2525

26-
#import scott-triple.lc
27-
Triple = \ x y z f . f x y z
28-
fst3 = \ xyz . xyz \ x _y _z . x
29-
snd3 = \ xyz . xyz \ _x y _z . y
30-
thd3 = \ xyz . xyz \ _x _y z . z
31-
trimap = \ fn xyz . xyz \ x y z . Triple (fn x) (fn y) (fn z)
32-
Y3 = B Y (C (B trimap T))
33-
3426
#import scott-quad.lc
3527
Quad = \ w x y z f . f w x y z
3628
fst4 = \ wxyz . wxyz \ w _x _y _z . w
@@ -55,24 +47,9 @@ Enum = Y2 (Pair (T \ succ pred . \ m . m 1 Bit1 (B nega-dbl pred)) # succ
5547
succ = fst Enum
5648
pred = snd Enum
5749

58-
Num = Y3 (Triple (T \ add adc adb .
59-
\ m n . m n # add
60-
( \ zm . n m ( \ zn . nega-dbl (add zm zn) ) ( \ zn . Bit1 (add zm zn) ) )
61-
( \ zm . n m ( \ zn . Bit1 (add zm zn) ) ( \ zn . nega-dbl (adb zm zn) ) )
62-
)
63-
(T \ add adc adb .
64-
\ m n . m (succ n) # add-with-carry
65-
( \ zm . n (succ m) ( \ zn . Bit1 (add zm zn) ) ( \ zn . nega-dbl (adb zm zn) ) )
66-
( \ zm . n (succ m) ( \ zn . nega-dbl (adb zm zn) ) ( \ zn . Bit1 (adb zm zn) ) )
67-
)
68-
(T \ add adc adb .
69-
\ m n . m (pred n) # add-with-borrow
70-
( \ zm . n (pred m) ( \ zn . Bit1 (adc zm zn) ) ( \ zn . nega-dbl (add zm zn) ) )
71-
( \ zm . n (pred m) ( \ zn . nega-dbl (add zm zn) ) ( \ zn . Bit1 (add zm zn) ) )
72-
) )
73-
add = fst3 Num
74-
adc = snd3 Num
75-
adb = thd3 Num
50+
add = \ m n . m n
51+
( \ zm . n m ( \ zn . nega-dbl (add zm zn) ) ( \ zn . Bit1 (add zm zn) ) )
52+
( \ zm . n m ( \ zn . Bit1 (add zm zn) ) ( \ zn . nega-dbl (pred (add zm zn)) ) )
7653

7754
negate = \ n . add n (nega-dbl n)
7855
sub = \ m n . add m (negate n)

tests/negabinary-scott/test.js

+18-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ LC.configure({ purity: "LetRec", numEncoding: { fromInt, toInt } });
2020
const solutionText = readFileSync(new URL("./solution.lc", import.meta.url), {encoding: "utf8"});
2121
const solution = LC.compile(solutionText);
2222
const { succ,pred, add,negate,sub, zero, lt0,le0,ge0,gt0,compare } = solution;
23-
const { True,False, LT,EQ,GT } = solution;
2423

2524
const toBoolean = p => p (true) (false) ;
2625
const toOrdering = cmp => cmp ("LT") ("EQ") ("GT") ;
@@ -33,46 +32,41 @@ describe("NegaBinaryScott", () => {
3332
});
3433
it("succ", () => {
3534
for ( let n=-10; n<=10; n++ )
36-
assert.strictEqual( toInt(succ(fromInt(n))), n+1, `succ ${ n }` );
37-
// assert.strictEqual( toInt(pred(fromInt(n))), n-1, `pred ${ n }` );
35+
assert.strictEqual( toInt(succ(n)), n+1, `succ ${ n }` );
3836
});
3937
it("pred", () => {
4038
for ( let n=-10; n<=10; n++ )
41-
// assert.strictEqual( toInt(succ(fromInt(n))), n+1, `succ ${ n }` ),
42-
assert.strictEqual( toInt(pred(fromInt(n))), n-1, `pred ${ n }` );
39+
assert.strictEqual( toInt(pred(n)), n-1, `pred ${ n }` );
4340
});
4441
it("add", () => {
4542
for ( let m=-10; m<=10; m++ )
46-
for ( let n=-10; n<=10; n++ ) {
47-
const actual = toInt(add(fromInt(m))(fromInt(n)));
48-
assert.strictEqual(actual,m+n,`add ${ m } ${ n }`);
49-
}
43+
for ( let n=-10; n<=10; n++ )
44+
assert.strictEqual( toInt(add(m)(n)), m+n, `add ${ m } ${ n }` );
5045
});
5146
it("negate", () => {
5247
for ( let n=-10; n<=10; n++ )
53-
assert.strictEqual( toInt(negate(fromInt(n))), -n, `negate ${ n }` );
48+
assert.strictEqual( toInt(negate(n)), -n, `negate ${ n }` );
49+
});
50+
it("negate . negate", () => {
51+
for ( let n=-10; n<=10; n++ )
52+
assert.strictEqual( toInt(negate(negate(n))), n, `negate (negate ${ n })` );
5453
});
5554
it("sub", () => {
5655
for ( let m=-10; m<=10; m++ )
57-
for ( let n=-10; n<=10; n++ ) {
58-
const actual = toInt(sub(fromInt(m))(fromInt(n)));
59-
assert.strictEqual(actual,m-n,`sub ${ m } ${ n }`);
60-
}
56+
for ( let n=-10; n<=10; n++ )
57+
assert.strictEqual( toInt(sub(m)(n)), m-n, `sub ${ m } ${ n }` );
6158
});
6259
it("eq, uneq", () => {
6360
for ( let n=-10; n<=10; n++ )
64-
assert.equal(toBoolean(zero(fromInt(n))),n===0,`zero ${ n }`),
65-
assert.equal(toBoolean(lt0(fromInt(n))),n<0,`lt0 ${ n }`),
66-
assert.equal(toBoolean(le0(fromInt(n))),n<=0,`le0 ${ n }`),
67-
assert.equal(toBoolean(ge0(fromInt(n))),n>=0,`ge0 ${ n }`),
68-
assert.equal(toBoolean(gt0(fromInt(n))),n>0,`gt0 ${ n }`);
61+
assert.strictEqual(toBoolean(zero(n)),n===0,`zero ${ n }`),
62+
assert.strictEqual(toBoolean(lt0(n)),n<0,`lt0 ${ n }`),
63+
assert.strictEqual(toBoolean(le0(n)),n<=0,`le0 ${ n }`),
64+
assert.strictEqual(toBoolean(ge0(n)),n>=0,`ge0 ${ n }`),
65+
assert.strictEqual(toBoolean(gt0(n)),n>0,`gt0 ${ n }`);
6966
});
7067
it("compare", () => {
7168
for ( let m=-10; m<=10; m++ )
72-
for ( let n=-10; n<=10; n++ ) {
73-
const actual = toOrdering(compare(fromInt(m))(fromInt(n)));
74-
const expected = m > n ? "GT" : m < n ? "LT" : "EQ" ;
75-
assert.equal(actual,expected,`compare ${ m } ${ n }`);
76-
}
69+
for ( let n=-10; n<=10; n++ )
70+
assert.strictEqual( toOrdering(compare(m)(n)), m > n ? "GT" : m < n ? "LT" : "EQ" , `compare ${ m } ${ n }` );
7771
});
7872
});

0 commit comments

Comments
 (0)