0

No instance for (Random t0)

I have a haskell file that can produce random numbers for a tuple.  This is the file:

module RandomTuple where

import System.Random

instance (Random a, Random b) => Random (a, b) where
randomR ((x1, y1), (x2, y2)) gen1 = ((x, y), gen3)
where (x, gen2) = randomR (x1, x2) gen1
(y, gen3) = randomR (y1, y2) gen2
random g0 = ((x1,y1), g2)
where (x1, g1) = random g0
(y1, g2) = random g1

 

This code compiles and runs fine with cabal run.  But Haskell for Mac reports 2 errors with it:

No instance for (Random t0)
The type variable ‘t0’ is ambiguous
When checking that ‘g1’ has the inferred type
  g1 :: g
Probable cause: the inferred type is ambiguous
In an equation for ‘random’:
    random g0
      = ((x1, y1), g2)
      where
          (x1, g1) = random g0
          (y1, g2) = random g1
In the instance declaration for ‘Random (a, b)’

 

And then on the next line it reports this error:

Could not deduce (RandomGen t0)
from the context (Random t)
  bound by the inferred type for ‘y1’: Random t => t
  at /Users/Paul/Documents/Poisson.hsproj/RandomTuple.hs:11:11-30
The type variable ‘t0’ is ambiguous
When checking that ‘y1’ has the inferred type
  y1 :: forall t. Random t => t
Probable cause: the inferred type is ambiguous
In an equation for ‘random’:
    random g0
      = ((x1, y1), g2)
      where
          (x1, g1) = random g0
          (y1, g2) = random g1
In the instance declaration for ‘Random (a, b)’

Could not deduce (Random t0)
from the context (RandomGen t)
  bound by the inferred type for ‘g2’: RandomGen t => t
  at /Users/Paul/Documents/Poisson.hsproj/RandomTuple.hs:11:11-30
The type variable ‘t0’ is ambiguous
When checking that ‘g2’ has the inferred type
  g2 :: forall t. RandomGen t => t
Probable cause: the inferred type is ambiguous
In an equation for ‘random’:
    random g0
      = ((x1, y1), g2)
      where
          (x1, g1) = random g0
          (y1, g2) = random g1
In the instance declaration for ‘Random (a, b)’


No instance for (Random t0)
The type variable ‘t0’ is ambiguous
When checking that ‘g2’ has the inferred type
  g2 :: g
Probable cause: the inferred type is ambiguous
In an equation for ‘random’:
    random g0
      = ((x1, y1), g2)
      where
          (x1, g1) = random g0
          (y1, g2) = random g1
In the instance declaration for ‘Random (a, b)’

 

What the heck is going on?  why is this code fine when I compile it and run it but not when the IDE reads it?

2 comments

Please sign in to leave a comment.