[Haskell-cafe] using external instance declarations

Kees Bleijenberg k.bleijenberg at lijbrandt.nl
Tue Nov 26 16:06:32 UTC 2013


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

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131126/a44e9715/attachment.html>


More information about the Haskell-Cafe mailing list