Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Last active March 15, 2017 11:41
Show Gist options
  • Select an option

  • Save MikeMKH/8575997fefeaf16c5fcf141bfb60ed02 to your computer and use it in GitHub Desktop.

Select an option

Save MikeMKH/8575997fefeaf16c5fcf141bfb60ed02 to your computer and use it in GitHub Desktop.
Basic example of S, K, and I in fantasy-combinators using mocha with JSVerify
const {constant, identity/*, substitution*/} = require('fantasy-combinators');
const substitution = f => g => x => f(x)(g(x));
const K = constant;
const I = identity;
const S = substitution;
const expect = require('expect.js');
const jsc = require('jsverify');
describe('verify jsverify is set up right', () => {
jsc.property('boolean function applied thrice', jsc.fn(jsc.bool), jsc.bool,
(f, b) => f(f(f(b))) === f(b)
);
jsc.property('(b && b) === b', jsc.bool,
b => (b && b) === b
);
});
describe('K', () => {
jsc.property('K x y === x', jsc.bool, jsc.bool,
(x, y) => K(x)(y) === x
);
jsc.property('K x y === x', jsc.string, jsc.number,
(x, y) => K(x)(y) === x
);
});
describe('I', () => {
jsc.property('I x === x', jsc.int32,
x => I(x) === x
);
});
describe('S', () => {
const add = x => y => x + y;
const inc = add(1);
jsc.property('S f g x = f x (g y)', jsc.number,
x => S(add)(inc)(x) === add(x)(inc(x))
);
jsc.property('SKK = I', jsc.fn(jsc.number, jsc.number), jsc.fn(jsc.number), jsc.number,
(f, g, x) => S(K)(K)(x) === I(x)
);
jsc.property('SKS = I', jsc.fn(jsc.number, jsc.number), jsc.fn(jsc.number), jsc.number,
(f, g, x) => S(K)(S)(x) === I(x)
);
});
describe('Booleans', () => {
jsc.property('True = K True False',
() => K(true)(false) === true
),
jsc.property('False = SK True False',
() => S(K)(() => true)(false) === false
)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment