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

Remove orphan instance for MonadRandom #91

Closed
pjones opened this issue Jan 4, 2020 · 11 comments
Closed

Remove orphan instance for MonadRandom #91

pjones opened this issue Jan 4, 2020 · 11 comments

Comments

@pjones
Copy link

pjones commented Jan 4, 2020

I have a MonadRandom instance in my application that looks like this:

instance (MonadIO m) => MonadRandom (MyTransfomer m) where
  getRandomBytes = ...

This generates an overlapping instance error due to the vagueness of the instance in hs-jose.

I'm not sure why this package needs the orphan instance but is there a way to remove it or use a newtype wrapper?

@frasertweedale
Copy link
Owner

It is needed by the test suite. I don't object to removing it (or rather, moving it to the test suite where it is actually used). However I don't plan on cutting a new release any time soon. So in the mean time, you can probably replace your MonadRandom instance with an instance of MonadTrans MyTransformer and the MonadRandom instance exported by jose will be used.

@pjones
Copy link
Author

pjones commented Feb 12, 2020

@frasertweedale Is there anything I can do to help? I can submit a PR and prepare a release if that's helpful.

@frasertweedale
Copy link
Owner

@pjones the change is already committed on master. I just don't think there's enough to release (removing a type class instance warrants more than a patch-level release).

Per my previous comment, you should be able to instance MonadTrans for MyTransformer instead of MonadRandom, and then be able to use the MonadRandom instance from jose.

@frasertweedale
Copy link
Owner

@pjones sorry for misleading you - I'm actually going to revert the change I made yesterday. The example programs and typical application code rely on MonadRandom. Ideally we could see the instance (MonadRandom m, MonadTrans t) => MonadRandom (t m) in cryptonite itself (where MonadRandom is defined), so that we can avoid orphan instances anywhere. I'll investigate whether that is possible.

frasertweedale added a commit that referenced this issue Feb 13, 2020
This reverts commit cfad613.

See discussion:
#91 (comment)

See also cryptonite pull request:
haskell-crypto/cryptonite#310
@pjones
Copy link
Author

pjones commented Feb 13, 2020

Thank you for trying to find a solution to this.

The reason I can't use the MonadRandom instance from jose is because the the transformer I want to take it from has a concrete implementation via Cryptonite. If I used the instance from jose it would delegate back to the base monad which in my case doesn't have a MonadRandom instance.

For now I have a workaround, but it would be nice if I didn't have to deal with orphan instances.

@hdgarrood
Copy link

I realise this is old but I’d really appreciate if we could reconsider removing this instance from the library, for the same reason as the one given upstream in cryptonite: lifting is not the only valid implementation. Plus this is an orphan, which adds a new level of potential frustration as previously working code can stop working due to the orphan coming into scope.

@frasertweedale
Copy link
Owner

Reopening. Gonna deal with this properly, at long last.

@frasertweedale frasertweedale reopened this Apr 7, 2022
frasertweedale added a commit that referenced this issue Apr 8, 2022
To avoid problems with orphan instances, introduce

    newtype JOSE e m a

Which behaves like `Except e m a` but also has

    instance (MonadRandom m) => MonadRandom (JOSE e m)

This is an API breaking change and therefore requires a major
version bump before release.

Fixes: #91
@frasertweedale
Copy link
Owner

frasertweedale commented Apr 8, 2022

Seeking feedback on proposed solution: b163ce2. The diff in the test/ and example/ subdirectories gives an idea of what consumers will need to do to migrate. In most cases, it is sufficient to replace runExceptT with runJOSE.

@hdgarrood
Copy link

Looks good to me! I’d suggest getting rid of the -fno-warn-orphans pragmas in src/ so that you get warnings if there are any other orphans in there.

frasertweedale added a commit that referenced this issue Apr 9, 2022
To avoid problems with orphan instances, introduce

    newtype JOSE e m a

Which behaves like `Except e m a` but also has

    instance (MonadRandom m) => MonadRandom (JOSE e m)

This is an API breaking change and therefore requires a major
version bump before release.

Fixes: #91
@frasertweedale
Copy link
Owner

Looks good to me! I’d suggest getting rid of the -fno-warn-orphans pragmas in src/ so that you get warnings if there are any other orphans in there.

Thanks, I updated the commit. Some of the -fno-warn-orphans pragmas are still required to suppress warnings about overlapping instances. I may try to address that in subsequent commits.

@frasertweedale
Copy link
Owner

I merged the change in commit b163ce2. I plan to cut a new release in about one week. In the meantime please share any more feedback about the change.

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

3 participants