[clean-list] Semantics of selectors and projection functions

John van Groningen johnvg at cs.ru.nl
Wed Mar 4 12:53:04 MET 2009


Vag <vag.vagoff at gmail.com> wrote:
>Addison-Wesley book and in Clean 2.1 Specification state that selector patterns are translated to projection functions. But (without strictness analysis and using small heap) test1 terminates ok but test2 fails with out of heap error. How it must be explained in semantics?

This happens because the garbage collector recognizes the thunks of
projection functions that are used for selector patterns, and if possible
performs the selection during the garbage collection. This prevents
space leaks.

Furthermore the compiler prevents some transformations of these projection
functions, for example thunk lifting or moving the projection inside
a then or else if a tuple is not computed inside the then or else.

The garbage collector does not recognize 'first' as a projection function,
and can therefore not do the selection during the garbage collection.
Because 'first' contains a reference to the whole list, the memory it uses
can only be freed at the end the program.

Kind regards,

John van Groningen

>Start = test4 [0..100*1000*1000]
>
>test1 xs = case ab of (_,_) -> n
>where
>    ab = decouple1 xs
>    (a,b) = ab
>    n = xsum 0 a xs
>
>test2 xs = case ab of (_,_) -> n
>where
>    ab = decouple1 xs
>    a = first ab
>    n = xsum 0 a xs
>
>decouple1 [x:xs] = (x,xs)
>
>first (a,b) = a
>
>xsum 0 a xs = xsum 1 a xs
>xsum n a [x:xs] = xsum (n+1) a xs
>xsum n a [] = a + n


More information about the clean-list mailing list