The problem is that your definition of arbitrary for the Building instance isn’t indented under the ‘instance Arbitrary Building’, so it’s getting interpreted as two top-level statements: one that Building is an Arbitrary of Instance, and one that defines a value named ‘arbitrary’. Hence the error message. To fix this, just indent the definition of arbitrary like you did in the Glass definition.

On Tuesday, November 26, 2013 at 11:06 AM, Kees Bleijenberg wrote:

I have a Building with a field glass: Glass.

I want to use quickcheck. Therefor I wrote a TestGlass.hs with the Arbitrary instance declaration for Glass. I also want to make Building an instance of Arbitrary. For the field glass in the Building I need the arbitrary :: Gen Glass function out of TestGlass.hs.

How do I export and use the instance declarations from TestGlass.hs?

 

Glass.hs

module Glass(

    Glass(..))

where

data Glass = Glass Int

 

TestGlass.hs

module TestGlass()

where

import Test.QuickCheck

import Test.QuickCheck.Gen

import Glass

 

instance Arbitrary Glass where

    arbitrary = do

                    n <- choose (1,8) :: Gen Int

                    return $ Glass n

 

 

Building.hs

module Building ()

where

import Glass

import TestGlass

import Test.QuickCheck

import Test.QuickCheck.Gen

 

data  Building = Building {

                                glass :: Glass

                              }

instance Arbitrary Building where

arbitrary = do

                g <- arbitrary :: (Gen Glass)

                return Building { glass = g}

 

 

When I compile Building I get:

Building.hs:13:23:

    Ambiguous occurrence `arbitrary'

    It could refer to either `Building.arbitrary',

                             defined at Building.hs:12:1

                          or `Test.QuickCheck.arbitrary',

                             imported from `Test.QuickCheck' at Building.hs:5:1-22

                             (and originally defined in `Test.QuickCheck.Arbitrary')

 

It looks like the compiler doesn’t look at the instance declarations in the TestGlass module.

What is wrong in my code?

I want to split the testcode as much as possible from the non-test code and I don’t want to merge the modules Building and Glass (they are both already big).

 

Kees

 

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe