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

Fix for monad identity laws. #136

Closed
wants to merge 1 commit into from
Closed

Fix for monad identity laws. #136

wants to merge 1 commit into from

Conversation

jwoudenberg
Copy link

I believe there is an issue with the implementation of the identity monad laws. Specifically, the identity function is not a valid argument to chain, because it will not return a value of the same Monad (a requirement inherited from Chain).
The reason this has not caused an error so far is because is an implementation detail of the Monad it is tested against. I believe other valid monads can run into issues when testing them against the law in it's current state.

My proposed fix is passing in a monad-returning function f to test against instead of using identity. I realize this is not such a nice solution in the sense that it changes the API of these tests with regards of the laws of the other tests, so a better solution would be welcome.

The only trivial monad-returning function we could use is t.of, but using that would result in identitical leftIdentity and rightIdentity tests. Another approach is to build a simple f in the law file itself, but it would require knowledge of the type of x, which would compromise the generality of the test.

@@ -34,6 +34,9 @@ Sum.prototype[equals] = function(x) {
return this.v.equals ? this.v.equals(x.v) : this.v === x.v;
};

// Special function for monad identity laws.
const f = x => Id.of(x + x);

Copy link
Member

Choose a reason for hiding this comment

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

What's the + for?

Copy link
Author

@jwoudenberg jwoudenberg May 5, 2016

Choose a reason for hiding this comment

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

Without it, x => Id.of(x) is simply the same as Id.of. In that case, there is no difference between the leftIdentity and rightIdentity test cases. Any non-trivial function that returns an Id should do here, I just picked something simple :).

@jwoudenberg
Copy link
Author

I found another issue in that the Monoid laws currently assume the Monoid implements an of method, which is not required. Is there an interesting in fixing these? If so, I'll happily add a suggestion to this PR :).

@gabejohnson
Copy link
Member

@jwoudenberg you're linking to the Monad laws there.

@jwoudenberg
Copy link
Author

jwoudenberg commented Jun 15, 2016

@gabejohnson Thanks for pointing that out :). My remark stands though, see this new and improved link.

@SimonRichardson
Copy link
Member

PR would be amazing😉

On Wed, 15 Jun 2016, 19:04 Jasper Woudenberg, [email protected]
wrote:

@gabejohnson https://github.com/gabejohnson Thanks for pointing that
out to me. The point remains though, see this new and improved link
https://github.com/fantasyland/fantasy-land/blob/master/laws/monoid.js#L16
.


You are receiving this because you are subscribed to this thread.

Reply to this email directly, view it on GitHub
#136 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/ACcaGKfYuazAXPFLrCRg_0-Z-4ESKoyXks5qMD7EgaJpZM4IX9Ma
.

@jwoudenberg
Copy link
Author

I'll see what I can do :).

@SimonRichardson
Copy link
Member

This is fixed in #171

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants