DeepSeq.lhs [was: Re: [Haskell] Force evaluation]
Esa Pulkkinen
esa.pulkkinen at kotiposti.net
Sat Dec 11 03:20:49 EST 2004
In message <41B72630.6000309 at cl.cam.ac.uk>, Ben Rudiak-Gould writes:
> >> instance DeepSeq (IO a) where deepSeq = seq
>
>This is an interesting instance (which is not to say I think it's
>wrong). It means the original poster's code won't work.
There is a bit similar issue with the instance for (a -> b). You
might suppose that deepSeq of a function would turn the function to a
representation that had the property that no function application
would cause any complex computation, instead every function
application would just look up from a precomputed (by deepSeq) table
of values mapping each element of the domain to the codomain of the
function. So I'm thinking the instance should be as follows:
>instance (Enum a, Bounded a, DeepSeq b) => DeepSeq (a -> b) where
> deepSeq f y = foldr deepSeq y [f i | i <- [minBound .. maxBound]]
Though I'm not sure this will work, it might also require that 'f' is
memoized to work correctly, such that recomputation of the thunks will
not occur if 'y' uses 'f'.
The theory behind this is the observation that functions can be
thought of as a collection of thunks of the codomain type indexed by
the values of the domain. For a practical application that would
require this kind of approach, see
http://haskell.org/hawiki/ControlOperation, in there, the use of
unsafePerformIO is premised on the assumption that everything has been
evaluated. So this would require the above kind of instances for
deepSeq to allow use of functions in conjunction with the control
operation.
There are obviously some performance issues with this approach.
--
Esa Pulkkinen
More information about the Haskell
mailing list