[Haskell-cafe] is value evaluated?

Ketil Malde ketil at malde.org
Fri May 8 03:06:59 EDT 2009


Nikhil Patil <patil.nikhil at gmail.com> writes:

> I am curious to know if there is a function in Haskell to find if a certain
> value has already been evaluated. The function I need would have the type:
>
>> (?!) :: a -> Bool

Well, obviously you can't do this, it would violate referential
transparency.  Except if you define it as

   (?!) :: a -> Bool
   (?!) x = x `seq` True

..but that's probably not what you meant? :-)

> And I expect it to be such that the following terminates after printing the
> first 101 fibonacci numbers.

>> fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
>>
>> main = do print $ fibs !! 100
>>           print $ takeWhile (?!) fibs

The obvious problem is that the pure function "takeWhile (?!) fibs"
would return different things depending on what has happened to "fibs"
earlier in the program.

The run-time system has information about evaluation status, so it
might be possible to query it - but you'd need to do so in IO.  I also
seem to remember a debugger being able to display evaluated and
unevaluated thunks?  Also, you might be able to inspect evaluation by
something like 

  fibs2 = sequence' [ print ("evaluating:"++show x)) >> return x | x <- fibs ]

where sequence' is a lazy version of sequence.  This is a bit clunky,
but occasionally useful.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list