Skip to content

does not work: newtype D = D Double deriving UniformRange #185

@jwaldmann

Description

@jwaldmann

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 
<interactive>: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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions