[Haskell-cafe] QuickCheck: Arbitrary for a complex type

Joel Reymont joelr1 at gmail.com
Wed Apr 4 10:44:16 EDT 2007


Suppose I have a type describing a statement and that I'm trying to  
make it an instance of arbitrary. The type looks like this:

data Statement
     = InputDecs [InputDecl]
     | VarDecs [VarDecl]
     | ArrayDecs [ArrayDecl]
     | Compound [Statement]
     | Assign (VarIdent, Expr)
     | ArrayAssign (VarIdent, [Expr], Expr)

Assuming that other types involved were instances of arbitrary, how  
do I write arbitrary for Statement?

Poking around various bits of source code I think that for a type  
like the following

data Style
     = StyleValue Expr
     | Solid
     | Dashed
     | Dotted
     | Dashed2
     | Dashed3
     deriving Show

I can write

instance Arbitrary Style where
     arbitrary = oneOf [ StyleValue . arbitrary,
                         elements [ Solid
                                  , Dashed
                                  , Dotted
                                  , Dashed2
                                  , Dashed3
                                  ]
                       ]

I'm not sure if this is correct, though, so any help is appreciated!

	Thanks in advance, Joel

--
http://wagerlabs.com/







More information about the Haskell-Cafe mailing list