Pointers in Haskell??

Tom Pledger Tom.Pledger@peace.com
Mon, 17 Dec 2001 12:42:02 +1300


Hamilton Richards writes:
 | In my previous example,
 | 
 | >  t = [0..]
 | >  b = 3 : t
 | >  c = 5 : t
 | 
 | lists b and c share t, but in
 | 
 | >  x = 3 : [0..]
 | >  y = 5 : [0..]
 | 
 | lists x and y share nothing. Extensionally, they have the same
 | values as b and c, but each has its own copy of [0..].
 |    Unless, that is, the compiler is clever enough to recognize the
 | subexpression [0..] which x and y have in common. I don't know
 | whether any Haskell compilers look for common subexpressions, but
 | it's certainly an option.
 |    So the question of whether names are "*the* (only) way" to
 | obtain sharing isn't really a language question-- it's more of a
 | compiler question.

Hi.

It's a difficult question for a compiler, especially as sharing isn't
always a good thing.

    let t = [0..]
        b = 3 : t
        c = 5 : t
     in b !! (c !! 1000000)    -- likely to have a space leak

    let x = 3 : [0..]
        y = 5 : [0..]
     in x !! (y !! 1000000)    -- compact, unless transformed to gain sharing

(The space leak arises because a million cells of t are allocated, but
the garbage collector can't free them because they're still reachable
via b.)

Because of examples like that, I prefer to stick with a simple
name-based sharing scheme.

Regards,
Tom