Path: utzoo!attcan!uunet!mcvax!ukc!dcl-cs!nott-cs!anw
From: anw@nott-cs.UUCP
Newsgroups: comp.lang.misc
Subject: Re: Alternatives to refs
Message-ID: <594@tuck.nott-cs.UUCP>
Date: 29 Nov 88 12:30:40 GMT
References: <388@ubbpc.UUCP> <16187@agate.BERKELEY.EDU> <599@quintus.UUCP> <591@tuck.nott-cs.UUCP> <404@ubbpc.UUCP> <7774@aw.sei.cmu.edu> <
Reply-To: anw@maths.nott.ac.uk (Dr A. N. Walker)
Organization: Department of Mathematics, The University, NOTTINGHAM, NG7 2RD, UK.
Lines: 71

[Clay Phipps (>>) and Steven Ryan (>) are surprised that:
	>>	  begin int I := 0, K := 1;
	>>	  ref int Ptr := I;
	>>	  Ptr := K;
	>>	  Print (I);
	>>	  end
prints 0 rather than 1.]

	Perhaps I'm being dense, but I would have been totally astonished
had it been otherwise.  Let's take it steadily:

	int I := 0, K := 1;
		# find me some local places suitable for holding two ints,
		  label them I and K, and initialise to 0 and 1 #
	ref int Ptr := I;
		# find me some local place suitable for holding a ref int,
		  (ie, for holding a place suitable for holding an int),
		  label it Ptr, and initialise to the place (the ref int!)
		  that we labelled I #
	Ptr := K;
		# make the place that we labelled Ptr hold instead the
		  place that we labelled K #
	print (I);
		# print what I holds -- still 0, naturally.  "print (Ptr)"
		  would, of course, print 1 #

>>The reason has to do with implicit conversions involving "ref" modes

	Not really -- the only conversion in the fragment shown is that
I is implicitly converted from "a place suitable for holding an int" into
"an array [of size 1] of printable objects" before being passed to the
print procedure.

	If you want to make the "place for holding an int" that the place
labelled Ptr contains hold what the place labelled K contains, then (of
course!) you should have written "(ref int) Ptr := K".  Well, of course
that's not so clear to the uninitiated;  but nor was the idea being
expressed, and you can't expect to read arbitrarily complex Algol 68
without learning it first.  There are some excellent textbooks around
which explain all this ....

>The confusion is that at times ref is treated as pointer and other times
>as a variable. Dereferencing can run away and confuse the program as to
>which is meant.

	Neither the program nor the compiler will be confused!  Nor will
you, if you first read one of the excellent textbooks ....  Ref is neither
a pointer nor a variable;  it is a keyword used [only] in constructing
modes, and "ref somemode" always means "place suitable for holding an
object of mode somemode".  You may find the incantations used to declare
identifiers or to generate such places misleading, and if so some experts
would agree with you;  but ref is blameless!

> [suggestion for "var" and "ref" ... example including:
>           var ref int Ptr = loc ref int := address I;

	But what is "variable" about Ptr?  [Algol 68 experts:  please
don't read the next sentence!]  It is a fixed label attached to a fixed
place in the computer of a fixed "shape" ("suitable for holding a ref int").
Perhaps you mean "ref var ref int Ptr = ..."?  And what do you mean by an
"address" if "I" isn't one already?  One of the major benefits of Algol 68
was that it cleared up this confusion.  Pity that more recent languages
managed to re-introduce it.

	There was a (sensible) proposal to extend Algol 68 to allow places
to be marked "read only", "write only", etc., but that seems to be not what
you have in mind.

-- 
Andy Walker, Maths Dept., Nott'm Univ., UK.
anw@maths.nott.ac.uk