[Haskell] factoring `if'
Jeremy Gibbons
Jeremy.Gibbons at comlab.ox.ac.uk
Mon Oct 11 06:39:53 EDT 2004
On Mon, 11 Oct 2004, Serge D. Mechveliani wrote:
> How do you think, is the program (1) equivalent to (2)
> in the meaning of Haskell-98 ?
Not at all. If foo is non-strict and p partial, (2) may yield a result
where (1) would not. You identify the possibility yourself: (2) is lazier.
> (1) (\ x -> (if p x then foo (g x) else foo (h x))
> where
> p ... g ... h ... foo ...
> )
>
> (2) (\ x -> foo ((if p x then g x else h x)
> where
> 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
> meaning.
>
> 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?
Jeremy.Gibbons at comlab.ox.ac.uk
Oxford University Computing Laboratory, TEL: +44 1865 283508
Wolfson Building, Parks Road, FAX: +44 1865 273839
Oxford OX1 3QD, UK.
URL: http://www.comlab.ox.ac.uk/oucl/people/jeremy.gibbons.html
More information about the Glasgow-haskell-users
mailing list