[Haskell-beginners] Using state inside quickcheck generators

Alexander MacDonald alexmac at adobe.com
Tue Sep 15 17:56:08 EDT 2009


Hi guys, I¹ve been using quickcheck for a while and I¹m having no problem
making generators for my data types, however recently I stumbled upon a
situation where I need to maintain State inside the quickcheck generator
functions. Here's a simple example, imagine I've got a basic language which
only has two features, variable definitions and assignments:

type Script = [Statement]

data Name = Name String
    deriving (Show,Eq)

data Statement = Def Name Int | Assign Name Name
    deriving Show

Generating random scripts is easy enough, just make each type an instance of
Arbitrary with a suitable generator function, however I want to make sure
that the generated scripts are syntactically valid. In this case that means
no "Name" can be used in an "Assign" statement unless it has previously been
defined by a "Def" statement.

My knowledge of using monads is somewhat basic so I'm struggling to figure
out how to use the State monad with QuickCheck. I've pasted my current
effort on the web: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=9412

If anyone could spare a moment to glance at my code and give me a push in
the right direction that would be much appreciated!

Thanks,
Alex Mac



More information about the Beginners mailing list