Strictness (was: Is this tail recursive?)

Brian Huffman bhuffman@galois.com
Thu, 14 Mar 2002 15:59:39 -0800


At 22:47 13/03/02 -0600, Jay Cox wrote:
>Perhaps what could be done about this strictness business is to make a
>kind of strictness annotation.  Perhaps something that says (force the
>second argument of function F before every call to F (including any time F
>calls itself)).
>...

>here's a rough example.  !a mean !a will be forced at its application (for
>not knowing proper language to call it).
>
>strict_foldl :: (a -> b -> a) -> !a -> [b] -> a
>strict_foldl = foldl
>
>of course, there has to be a number of things that must be propagated
>whence you start adding these things. like for instance.
>....
>How about it?  Has there been any other proposals? (like maybe going as
>far as a "Strictness Type" system?)

In Haskell you can produce the desired behavior by using pattern guards. 
Since the pattern guards always get evaluated before the result does, they 
can be used to make things more strict. Here is the foldl example:

strict x = seq x True

foldl' :: (a -> b -> a) -> a -> [b] -> a
foldl' f z [] = z
foldl' f z (x:xs)
  | strict z = foldl' f (f z x) xs

You can make multiple arguments strict by using && or similar, e.g.:

f x y | strict x && strict y = ...
f x y | all strict [x,y] = ...

Of course, with the second example x and y must have the same type.
Hope this helps.

- Brian Huffman