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

apfelmus apfelmus at quantentunnel.de
Thu Sep 27 16:57:36 EDT 2007

Tristan Allwood wrote:
>>> Does anyone know if there is a function that tells you if a haskell
>>> value has been forced or not?  e.g. isWHNF :: a -> IO Bool
> apfelmus wrote:
>> Note that this function [isWHNF :: a -> Bool] is not
>> referentially transparent
> Indeed.  Does it still mess up with the result in IO Bool (as was my
> intent)? 

It depends, but I think yes. I mean, given extensional equality,

   isWHNF 2
   isWHNF (1+1)

still have to be the same IO actions, in the sense that there cannot be 
a guarantee that the first always returns  True  during execution 
without the second returning always  True , too. That is, you may not 
depend on such a property for proving that your program is correct, 
although you may use it for performance (memory & time) characteristics 
(I don't know how you would use  isWHNF  to do /that/, but it's a 
hypothetical possibility). In other words, if your program output is 
correct with a fake nondeterministic replacement like

   isWHNF x = do
     b' <- getMemoizedValueFor x
     if b' then return True else do
       b <- randomIO
       when b $ setMemoizedValueFor x True
       return b

then it's safe, otherwise it's not. But similarly to a memoization 
function implemented with unsafePerformIO

   memoize :: Ord a => (a -> b) -> (a -> b)

you may well use the "not so nondeterministic" property of  isWHNF  to 
achieve a time & space improvement.


More information about the Haskell-Cafe mailing list