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

static ‘empty’ value #164

Closed
wants to merge 1 commit 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
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,21 @@ A value which has a Semigroup must provide a `concat` method. The
A value that implements the Monoid specification must also implement
the Semigroup specification.

1. `m.concat(m.empty())` is equivalent to `m` (right identity)
2. `m.empty().concat(m)` is equivalent to `m` (left identity)
1. `m.concat(m.constructor.empty)` is equivalent to `m` (right identity)
2. `m.constructor.empty.concat(m)` is equivalent to `m` (left identity)

#### `empty` method
#### `empty` value

```hs
empty :: Monoid m => () -> m
empty :: Monoid m => m
```

A value which has a Monoid must provide an `empty` method on itself or
its `constructor` object. The `empty` method takes no arguments:
A value which has a Monoid must provide an `empty` value on its
`constructor` object:

m.empty()
m.constructor.empty()
m.constructor.empty

1. `empty` must return a value of the same Monoid
1. `empty` must be a value of the same Monoid

### Functor

Expand Down Expand Up @@ -563,7 +562,7 @@ be equivalent to that of the derivation (or derivations).
[`bimap`]: #bimap-method
[`chain`]: #chain-method
[`concat`]: #concat-method
[`empty`]: #empty-method
[`empty`]: #empty-value
[`equals`]: #equals-method
[`extend`]: #extend-method
[`extract`]: #extract-method
Expand Down
6 changes: 1 addition & 5 deletions id.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ Id.prototype[fl.concat] = function(b) {
return new Id(this.value[fl.concat](b.value));
};

// Monoid (value must also be a Monoid)
Id[fl.empty] = function() {
return new Id(this.value[fl.empty] ? this.value[fl.empty]() : this.value.constructor[fl.empty]());
};
Id.prototype[fl.empty] = Id[fl.empty];
// Monoid is not satisfiable since the type lacks a universal empty value

// Foldable
Id.prototype[fl.reduce] = function(f, acc) {
Expand Down
13 changes: 7 additions & 6 deletions id_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ const semigroup = require('./laws/semigroup');
const setoid = require('./laws/setoid');
const traversable = require('./laws/traversable');

const {tagged} = require('daggy');
const {of, empty, concat, equals, map} = require('.');

const Id = require('./id');

// Special type of sum for the type of string.
const Sum = tagged('v');
const Sum = function Sum(v) {
if (!(this instanceof Sum)) return new Sum(v);
this.v = v;
};
Sum[of] = (x) => Sum(x);
Sum[empty] = () => Sum('');
Sum[empty] = Sum('');
Sum.prototype[of] = Sum[of];
Sum.prototype[empty] = Sum[empty];
Sum.prototype[map] = function(f) {
return Sum(f(this.v));
};
Expand Down Expand Up @@ -90,8 +91,8 @@ exports.monad = {
};

exports.monoid = {
leftIdentity: test((x) => monoid.leftIdentity(Id[of](Sum[empty]()))(equality)(Sum[of](x))),
rightIdentity: test((x) => monoid.rightIdentity(Id[of](Sum[empty]()))(equality)(Sum[of](x)))
leftIdentity: test((x) => monoid.leftIdentity(Sum)(equality)(x)),
rightIdentity: test((x) => monoid.rightIdentity(Sum)(equality)(x))
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could someone review this change?

It's a bit strange for code in id_test.js to test the Sum type, but the Id type can no longer satisfy Monoid.


// Semigroup tests are broken otherwise for this.
Expand Down
12 changes: 6 additions & 6 deletions laws/monoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ const {of, empty, concat} = require('..');

### Monoid

1. `m.concat(m.empty())` is equivalent to `m` (right identity)
2. `m.empty().concat(m)` is equivalent to `m` (left identity)
1. `m.concat(m.constructor.empty)` is equivalent to `m` (right identity)
2. `m.constructor.empty.concat(m)` is equivalent to `m` (left identity)

**/

const rightIdentity = t => eq => x => {
const a = t[of](x)[concat](t[empty]());
const a = t[of](x)[concat](t[empty]);
const b = t[of](x);
return eq(a, b);
};

const leftIdentity = t => eq => x => {
const a = t[empty]()[concat](t[of](x));
const a = t[empty][concat](t[of](x));
const b = t[of](x);
return eq(a, b);
};

module.exports = { rightIdentity
, leftIdentity
};
, leftIdentity
};