[Haskell] factoring `if'

Serge D. Mechveliani mechvel at botik.ru
Mon Oct 11 06:19:45 EDT 2004

Dear Haskell implementors,

How do you think, is the program (1) equivalent to (2) 
in the meaning of Haskell-98 ?

(1)   (\ x -> (if p x then  foo (g x)  else  foo (h x))
              p ... g ... h ... foo ...

(2)   (\ x -> foo  ((if p x then  g x  else  h x)
                    p ... g ... h ... foo ...

If it is equivalent, then does it make sense for a compiler to 
convert (1) to (2):  to separate a common `factor' of the if-branches
The reason for this may be, for example, that the result printing
of  (f x)  is more `lazy' in (2) than in (1):
the part of  foo  may print immediately and  (g x) or (h x)  may print
long after.
This is a difference in behavior, it does not effect the computation

I have a large program which is easily written in the style of (1),
(and in many places it sets `case' instead of `if').  
Annoyingly, it prints out in a not a lazy manner.
It can be rewritten in the form of (2), but with effort, and it will
look less plain.
So, maybe, this is a business of a compiler?

Copy, please, the possible answer to  mechvel at botik.ru

Serge Mechveliani
mechvel at botik.ru

More information about the Haskell mailing list