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

How could monadIOLaws ever be used? #12

Open
ocharles opened this issue Feb 11, 2019 · 6 comments
Open

How could monadIOLaws ever be used? #12

ocharles opened this issue Feb 11, 2019 · 6 comments

Comments

@ocharles
Copy link
Contributor

To have a law-abiding MonadIO means that your monad must be at least as powerful as IO - ie, be a transformer over IO. Such a monad won't have a meaningful Eq instance, because IO has no Eq instance. Can monadIOLaws actually ever be used?

@chessai
Copy link
Collaborator

chessai commented Feb 11, 2019

This is true. However, orphan instances are often common in test suites. That, or a newtype around the transformer which is given an Eq instance. One could use unsafePerformIO here, which is safe here (not generally, but in the case of running tests with hedgehog-classes it should be)

@ocharles
Copy link
Contributor Author

But my point is you can't write an Eq instance because there's no way to compare IO. So even if you newtype a transformer over IO, there's still not going to be an Eq instance.

@chessai
Copy link
Collaborator

chessai commented Feb 11, 2019

newtype EqIO a = EqIO (IO a)

instance Eq a => Eq (EqIO a) where
  EqIO x == EqIO y = unsafePerformIO $ do
  xVal <- x
  yVal <- y
  pure $ x == y

This is what I was suggesting

@ocharles
Copy link
Contributor Author

ocharles commented Feb 11, 2019

Ok, but now your Eq instance doesn't pass the Eq laws (tryReadTMVar foo where foo is non-empty will reduce to Just x == Nothing, so reflexivity doesn't hold). Something just feels off here, I'm just trying to see what the actual value in this is.

@chessai
Copy link
Collaborator

chessai commented Feb 11, 2019

That makes sense. That Eq instance is pretty lousy in lots of concurrent/asynchronous/transactional situations. I'm not really sure if it (monadIOLaws) should be removed, because you could be testing types that don't include any such computations. At the very least this should be documented, at worst monadIOLaws should be removed.

@chessai
Copy link
Collaborator

chessai commented Dec 17, 2022

I think it should be removed.

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

No branches or pull requests

2 participants