Quantcast

Checking for WHNF (a horrible naughty thing)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Checking for WHNF (a horrible naughty thing)

Jan-Willem Maessen
I would like to do a horrible naughty thing (which I promise never to  
expose to the world).  I would like to tell whether a term is in  
WHNF, without forcing evaluation of that term.  Something like:

isWHNF :: a -> Bool

Is there a way of doing this?  I can fake it with an IORef and much  
unsafeness, but I'm wondering if there's a safe-but-ugly way of doing  
the test in GHC.

If you're curious, I'm trying to compact exactly the evaluated spine  
of a list without changing the list's laziness in any way.  It  
remains to be seen whether this is even vaguely a good idea. :-)

-Jan-Willem Maessen

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Checking for WHNF (a horrible naughty thing)

Sigbjorn Finne
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));
}

----- Original Message -----
From: "Jan-Willem Maessen" <[hidden email]>
To: "glasgow-haskell-users" <[hidden email]>
Sent: Wednesday, November 23, 2005 08:10
Subject: Checking for WHNF (a horrible naughty thing)


>I would like to do a horrible naughty thing (which I promise never to  
> expose to the world).  I would like to tell whether a term is in  
> WHNF, without forcing evaluation of that term.  Something like:
>
> isWHNF :: a -> Bool
>
> Is there a way of doing this?  I can fake it with an IORef and much  
> unsafeness, but I'm wondering if there's a safe-but-ugly way of doing  
> the test in GHC.
>
> If you're curious, I'm trying to compact exactly the evaluated spine  
> of a list without changing the list's laziness in any way.  It  
> remains to be seen whether this is even vaguely a good idea. :-)
>
> -Jan-Willem Maessen
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Checking for WHNF (a horrible naughty thing)

Simon Marlow
In reply to this post by Jan-Willem Maessen
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
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Loading...