On 8 May 1998, Carl R. Witty wrote:
> > > Is it necessary that the compiler do this? It strikes me that when the
> > > compiler encounters a function that requires multiple comprehensions of
> > > the same list, it should be smart enough to consolidate them into a single
> > > loop (which would also avoid the space leak). Is my intuition correct
> > > here?
> >
> > This optimization would probably be a win most of the time, so
> > yes, ideally compilers would do things like that. Currently however
> > those sort of optimizations are past the limits of what most Haskell
> > compilers will do.
>
> Watch out...this optimization is invalid. Consider the following
The problem is that something like (head (qsort [whatever])) is not strict
in the whole resulting sorted list, so merging the comprehensions is not a
win but a loss (a big loss: now we've got bottom!).
If the compiler could prove that the resulting list is needed strictly,
the merging is ok. This happens in (length (qsort [whatever])). This
sounds difficult...
The compiler might generate two versions for qsort, one to be used when
the result is needed strictly, and the other when it is not...
Do present compilers do this kind of things?
-- m
-----------------------------------------------------------------------
Mariano Suarez Alvarez The introduction of
Departamento de Matematica numbers as coordinates
Universidad Nacional de Rosario [...] is an act of violence
Pellegrini 250 A. Weyl
2000 Rosario - Argentina
e-mail: [EMAIL PROTECTED]
-----------------------------------------------------------------------