Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test-suite validating Id is spec conformant #61

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.npm-debug.log
node_modules/
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ implement the Apply specification.
A value which satisfies the specification of a Chain does not
need to implement:

* Apply's `ap`; derivable as `m.chain(function(f) { return m.map(f); })`
* Apply's `ap`; derivable as `function(m) { return this.chain(function(f) { return m.map(f); }); }`

1. `m.chain(f).chain(g)` is equivalent to `m.chain(function(x) { return f(x).chain(g); })` (associativity)

Expand All @@ -179,7 +179,6 @@ the Applicative and Chain specifications.
A value which satisfies the specification of a Monad does not need to
implement:

* Apply's `ap`; derivable as `function(m) { return this.chain(function(f) { return m.map(f); }); }`
* Functor's `map`; derivable as `function(f) { var m = this; return m.chain(function(a) { return m.of(f(a)); })}`

1. `m.of(a).chain(f)` is equivalent to `f(a)` (left identity)
Expand Down
54 changes: 33 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
{
"name": "fantasy-land",
"author": "Brian McKenna",
"version": "0.0.1",
"description": "Specification for interoperability of common algebraic structures in JavaScript",
"license": "XXX",
"homepage": "https://github.com/pufuwozu/fantasy-land",
"keywords": [
"algebraic",
"monad",
"functor",
"monoid",
"semigroup"
],
"issues": {
"url": "https://github.com/pufuwozu/fantasy-land/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/pufuwozu/fantasy-land.git"
},
"files": ["id.js"]
"name": "fantasy-land",
"author": "Brian McKenna",
"version": "0.1.0",
"description": "Specification for interoperability of common algebraic structures in JavaScript",
"license": "XXX",
"homepage": "https://github.com/fantasyland/fantasy-land",
"keywords": [
"algebraic",
"monad",
"applicative",
"functor",
"monoid",
"semigroup",
"chain",
"apply"
],
"issues": {
"url": "https://github.com/fantasyland/fantasy-land/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/fantasyland/fantasy-land.git"
},
"scripts": {
"test": "mocha --reporter=spec test"
},
"files": [
"id.js"
],
"devDependencies": {
"jsverify": "~0.3.1",
"mocha": "~1.18.2"
}
}
306 changes: 306 additions & 0 deletions test/id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
"use strict";

var assert = require("assert");
var jsc = require("jsverify");
var Id = require("../id.js");

// functional basics
function identity(x) {
return x;
}

function compose(f, g) {
return function (x) {
return f(g(x));
};
}

// Any, simple monoid
function Any(b) {
assert(b === true || b === false);

this.value = b;
}

Any.prototype.concat = function (other) {
assert(other instanceof Any);

return new Any(this.value || other.value);
};

Any.empty = Any.prototype.empty = function () {
return new Any(false);
};

function eqAny(a, b) {
assert(a instanceof Any && b instanceof Any);

return a.value === b.value;
}

describe("Any", function () {
describe("Semigroup", function () {
it("associativity", function () {
var prop = jsc.forall(jsc.bool(), jsc.bool(), jsc.bool(), function (a, b, c) {
a = new Any(a);
b = new Any(b);
c = new Any(c);

var left = a.concat(b).concat(c);
var right = a.concat(b.concat(c));

return eqAny(left, right);
});

jsc.assert(prop);
});
});

describe("Monoid", function () {
it("right identity", function () {
var prop = jsc.forall(jsc.bool(), function (m) {
m = new Any(m);

var left = m.concat(m.empty());
var right = m;

return eqAny(left, right);
});

jsc.assert(prop);
});

it("left identity", function () {
var prop = jsc.forall(jsc.bool(), function (m) {
m = new Any(m);

var left = m.empty().concat(m);
var right = m;

return eqAny(left, right);
});

jsc.assert(prop);
});
});
});

// we will compare only nat values, so shallow equality is enough
function eqIdNat(a, b) {
assert(a instanceof Id && b instanceof Id);

return a.value === b.value;
}

function eqIdAny(a, b) {
assert(a instanceof Id && b instanceof Id);
assert(a.value instanceof Any && b.value instanceof Any);

return a.value.value === b.value.value;
}


describe("Id", function () {
describe("Semigroup", function () {
it("associativity", function () {
var prop = jsc.forall(jsc.bool(), jsc.bool(), jsc.bool(), function (a, b, c) {
a = Id.of(new Any(a));
b = Id.of(new Any(b));
c = Id.of(new Any(c));

var left = a.concat(b).concat(c);
var right = a.concat(b.concat(c));

return eqIdAny(left, right);
});

jsc.assert(prop);
});
});

describe("Monoid", function () {
it("right identity", function () {
var prop = jsc.forall(jsc.bool(), function (m) {
m = Id.of(new Any(m));

var left = m.concat(m.empty());
var right = m;

return eqIdAny(left, right);
});

jsc.assert(prop);
});

it("left identity", function () {
var prop = jsc.forall(jsc.bool(), function (m) {
m = Id.of(new Any(m));

var left = m.empty().concat(m);
var right = m;

return eqIdAny(left, right);
});

jsc.assert(prop);
});
});

describe("Functor", function () {
it("identity", function () {
var prop = jsc.forall(jsc.nat(), function (n) {
var u = Id.of(n);

var left = u.map(identity);
var right = u;

return eqIdNat(left, right);
});

jsc.assert(prop);
});

it("composition", function () {
var prop = jsc.forall(jsc.nat(), jsc.fun(jsc.nat()), jsc.fun(jsc.nat()), function (n, f, g) {
var u = Id.of(n);

var left = u.map(compose(f, g));
var right = u.map(g).map(f);

return eqIdNat(left, right);
});

jsc.assert(prop);
})
});

describe("Apply", function () {
it("composition", function () {
var prop = jsc.forall(jsc.fun(jsc.nat()), jsc.fun(jsc.nat()), jsc.nat(), function (f, g, n) {
var a = Id.of(f);
var u = Id.of(g);
var v = Id.of(n);

var left = a.map(function(f) { return function(g) { return function(x) { return f(g(x))}; }; }).ap(u).ap(v);
var right = a.ap(u.ap(v));

return eqIdNat(left, right);
});

jsc.assert(prop);
});
});

describe("Applicative", function () {
it("Functor", function () {
var prop = jsc.forall(jsc.nat(), jsc.fun(jsc.nat()), function (n, f) {
var u = Id.of(n);

var left = u.map(f);
var right = Id.of(f).ap(u);

return eqIdNat(left, right);
});

jsc.assert(prop);
});

it("identity", function () {
var prop = jsc.forall(jsc.nat(), function (n) {
var v = Id.of(n);

var left = Id.of(identity).ap(v);
var right = v;

return eqIdNat(left, right);
});

jsc.assert(prop);
});

it("homomorphism", function () {
var prop = jsc.forall(jsc.fun(jsc.nat()), jsc.nat(), function (f, x) {
var left = Id.of(f).ap(Id.of(x));
var right = Id.of(f(x));

return eqIdNat(left, right);
});

jsc.assert(prop);
});

it("interchange", function () {
var prop = jsc.forall(jsc.fun(jsc.nat()), jsc.nat(), function (f, y) {
var u = Id.of(f);

var left = u.ap(Id.of(y));
var right = Id.of(function(f) { return f(y); }).ap(u);

return eqIdNat(left, right);
});

jsc.assert(prop);
});
});

describe("Chain", function () {
it("Apply", function () {
var prop = jsc.forall(jsc.fun(jsc.nat()), jsc.nat(), function (f, n) {
var u = Id.of(f);
var v = Id.of(n);

var left = u.ap(v);
var right = u.chain(function (f) { return v.map(f); });

return eqIdNat(left, right);
});

jsc.assert(prop);
});

it("associativity", function () {
var prop = jsc.forall(jsc.fun(jsc.nat()), jsc.fun(jsc.nat()), jsc.nat(), function (f, g, n) {
f = compose(Id.of, f);
g = compose(Id.of, g);
var m = Id.of(n);

var left = m.chain(f).chain(g);
var right = m.chain(function(x) { return f(x).chain(g); });

return eqIdNat(left, right);
});

jsc.assert(prop);
});
});

describe("Monad", function () {
// we don't test for functor, as from `chain` we can derive `ap` and from `ap` we can device `map`.

it("left identity", function () {
var prop = jsc.forall(jsc.fun(jsc.nat()), jsc.nat(), function (f, a) {
f = compose(Id.of, f);

var left = Id.of(a).chain(f);
var right = f(a);

return eqIdNat(left, right);
});

jsc.assert(prop);
});

it("right identity", function () {
var prop = jsc.forall(jsc.nat(), function (n) {
var m = Id.of(n);

var left = m.chain(Id.of); // m.of in spec!
var right = m;

return eqIdNat(left, right);
});

jsc.assert(prop);
});
});
});