Checking for WHNF (a horrible naughty thing)
Simon Marlow
simonmar at microsoft.com
Thu Nov 24 06:41:39 EST 2005
On 23 November 2005 18:29, Sigbjorn Finne wrote:
> The appended snippet might help..
>
> --sigbjorn
>
> -- whnf.hs
> import Foreign.StablePtr
> import System.IO.Unsafe
>
> isWHNF :: a -> Bool
> isWHNF a = unsafePerformIO $ do
> stl <- newStablePtr a
> rc <- isWhnf stl
> freeStablePtr stl
> return (rc /= 0)
>
> foreign import ccall safe "isWhnf" isWhnf :: StablePtr a -> IO Int
>
> /* whnf.c */
> #include "Rts.h"
> int
> isWhnf(StgStablePtr st)
> {
> StgClosure* c = (StgClosure*)(stable_ptr_table[(StgWord)st].addr);
> return !(closure_THUNK(c));
> }
using deRefStablePtr() would be slightly better. Also, you should
consider whether you want indirections to be counted as WHNF or not.
Also, I think you can make that foreign import "unsafe" (safe will be
slow). For more speed you could implement it directly as a primitive.
Cheers,
Simon
More information about the Glasgow-haskell-users
mailing list