I want to generate stateful functions (C signature T f()) with QuickCheck as arguments for foreign functions. Preferably I also want to make them (or their inner s->(T,s)) showable.
I know that for stateless functions I can write something like
type Compare = CInt -> CInt -> CBool
foreign import ccall "wrapper"
mkCompare :: Compare -> IO (FunPtr Compare)
but I got stuck trying this approach for the stateful functions, as I don't see how I can translate the monad hiding the state in Haskell to the function hiding the state in C.
Example of a stateful function f
static int i = 0;
int f() {
return i++;
}
In Haskell I would represent this function as State (\s -> (s,s+1)).
What does QuickCheck have to do with it?
If I have a C function that takes a stateful function as argument, e.g.
int twice(int (*f)()) {
f();
return f();
}
then I can test the function with QuickCheck, where QuickCheck can generate different implementations for f, which would usually look similar to
prop_total (Fun f) xs = total $ g f xs
but these generated functions are stateless, not stateful like the example C function above.
type Compare = CInt -> CInt -> IO CBoolget you where you need to go? Then you can writewhatever :: IORef Whatever -> Compareto get a statefulCompare, and QuickCheck can generate aWhatever -> CInt -> CInt -> (Whatever, CBool)that you can wrap into aIORef Whatever -> Compare.Whatever -> CInt -> CInt -> (Whatever, CBool), which does not mentionIORef; then, after it's generated, you can wrap it up to do something to anIORefinstead of having explicit state.