[Haskell-cafe] Newbie question: mutually exclusive strict / lazy

Loup Vaillant loup.vaillant at gmail.com
Mon Feb 11 04:51:34 EST 2008

2008/2/11, Peter Verswyvelen <bf3 at telenet.be>:
> Yes, sorry, GHC's strictness analyzer.
> What I meant with this email is that I guess that for a strictness analyzer,
> the information that a function is strict in an argument *independent from
> the other arguments* would not be good enough in itself for optimization, it
> would be better to also use the dependencies between the arguments (as in
> the case of the if…then…else).
> It seems one can indicate in GHC that an argument is strict using
> annotiations, but I don't see a way of specifying these dependencies (maybe
> this does not make sense, and this is all newbie nonsense). Of course, with
> whole program optimization this would not be necessary, but if the compiler
> just sees the function signature, he must assume that a lazy argument is
> always lazy, independent of the value of other strict arguments no?

It may not always be the case, but, here, for your particular example,
what you need is an inline followed by a reduction (dunno which).

> cond x y z = if x then y else z

The translation in core, is this:
cond x y z = case x of
  True  -> y
  False -> z

So, suppose we know at some call site that x is True. So, the call
cond x e1 e2 -- e1 and e2 are arbitrary expressions

is equivalent to:
cond True e1 e2

An inline replaces the call by this:
cond x e1 e2 = case True of
  True  -> e1
  False -> e2

In this case, the compiler can easily determine at compile time the
selected branch. Therefore, this "case" expression is replaced by the
correct branch:

I would be surprised if GHC doesn't already perform this kind of
optimization [1,2]. So, no need for a fancy strictness analyser for
this code. About more complicated cases, I'm clueless, thought.


[1] http://research.microsoft.com/~simonpj/Papers/inlining/
[2] http://citeseer.ist.psu.edu/jones91unboxed.html

More information about the Haskell-Cafe mailing list