Pointers in Haskell??

Jeff Dalton jeff@aiai.ed.ac.uk
Sat, 8 Dec 2001 02:43:06 GMT

> >> ... 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.
> >
> >Are they the only way that's guaranteed to result in sharing, or is
> >even that not the case?
> Depends on what you mean by "guaranteed". Since in Haskell sharing vs. not
> sharing is not a semantic issue, you can't very well say that if sharing
> doesn't occur under such-and-such circumstances, the language specification
> is violated.

But the answers to the original question seemed to be saying you would
get sharing if you wrote code of a certain sort.  I take it that the
language semantics doesn't strictly require sharing, but it sounded
like it was something programmers could assume would happen in
practice.  If not, then shouldn't the answers to the original
question have been different?

What I was wondering was why the answers to the original question
about pointers involved giving a name to the thing you wanted shared.
Is there something less direct that would also work?  For instance,
could I instead name something that contained the structure I wanted
shared and then use an appropriate expression to refer to the part I
wanted shared - or do I have to give a name directly to that part.

The main point of my Java example was to give an example where
I didn't give a name to the very thing I wanted shared, but instead
gave a name to something that contained it.

> This is very different from languages like Java, where
>    Object[] x = new Object[](...)
>    Object[] y = x;
> means that x and y refer to the same array (not merely equivalent arrays),
> and if they don't, it's not Java. And what's crucial is that you can write
> a Java program that detects whether x and y refer to the same array, by
> updating the array via x and observing the effect via y.

You can just use ==.

Also, I'm not sure the Java language semantics really does require
that x and y in your example above refer to the same locations in
memory.  The update test doesn't necessarily show that either.
It just shows that if you change one, the "other" changes too.
Of course, programmers assume that assignment to array elements
is done in a fairly direct and simple way; but the language 
semantics might nonetheless allow something more expensive and

-- Jeff