[Haskell-cafe] isWHNF :: a -> IO Bool ?

Jan-Willem Maessen jmaessen at alum.mit.edu
Thu Sep 27 11:30:20 EDT 2007

On Sep 27, 2007, at 9:14 AM, Pepe Iborra wrote:

> Actually, in 6.8 we can build isWHNF on top of the GHC-API.
> First, you need to import the ghc package:
>> ghci -package ghc
>> GHCi, version 6.7: http://www.haskell.org/ghc/  :? for help
> Then, you can define the isWHNF function as follows:
>> Prelude> :m +RtClosureInspect
>> Prelude RtClosureInspect> let isWHNF = fmap (isConstr . tipe) .  
>> getClosureData
>> Prelude RtClosureInspect> :t isWHNF
>> isWHNF :: a -> IO Bool
> What the code above does is to inspect the info table associated to  
> the value given, and check if the closure is a Constructor closure,  
> i.e. in WHNF.

Very cool.  This is much nicer than when I asked much the same  
question a few years back (and I can think of all sorts of  
interesting things I can learn from the interface in that module).   
But what about indirection chasing?  Surely we want isWHNF to return  
True if we have an indirection to a WHNF.  Possibly one wants  
something a bit like this (untested, and rather depends on GHC's  
indirection semantics):

removingIndirections :: (forall c . c -> IO b) -> a -> IO b
removingIndirections k a = do
     closureData <- getClosureData a
     if isConstr (tipe closureData) then
         removingIndirections (ptrs closureData ! 0)
         k a

simpleIsWHNF :: a -> IO Boolean
simpleIsWHNF =  fmap (isConstr . tipe) . getClosureData

isWHNF = removingIndirections simpleIsWHNF

-Jan-Willem Maessen

More information about the Haskell-Cafe mailing list