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.

Regards,
apfelmus

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to