haskell / haskell/random

does not work: newtype D = D Double deriving UniformRange

Open
#185 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
59
Forks
56
PR merge metrics
No merged PRs in 30d

Description

I have a newtype over Double, I want to use the built-in Double generator, but I get this error.

```
ghci> :set -package random
package flags have changed, resetting and loading new packages...
ghci> import System.Random
ghci> :set -XGeneralizedNewtypeDeriving
ghci> newtype D = D Double deriving Uniform
Uniform UniformRange
ghci> newtype D = D Double deriving UniformRange
:5:31: error: [GHC-18872]
• Couldn't match representation of type: m Double
with that of: m D
arising from the coercion of the method ‘System.Random.Internal.uniformRM’
from type ‘forall g (m :: * -> *).
System.Random.Internal.StatefulGen g m =>
(Double, Double) -> g -> m Double’
to type ‘forall g (m :: * -> *).
System.Random.Internal.StatefulGen g m =>
(D, D) -> g -> m D’
Note: We cannot know what roles the parameters to ‘m’ have;
we must assume that the role is nominal.
• When deriving the instance for (UniformRange D)

```
I can write the instance just fine
```
import qualified System.Random as R
import qualified System.Random.Stateful as RS

instance R.UniformRange F where
uniformRM (F l, F h) g = fmap F $ RS.uniformRM (l, h) g
isInRange (F l, F h) (F x) = RS.isInRange (l, h) x
```
but that's precisely what I wanted to avoid. It looks completely generic so the compiler should be able to provide/derive it?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.