swap by name

Wayne Throop throopw at dg_rtp.UUCP
Tue Jul 8 06:37:52 AEST 1986


> gwyn at BRL.ARPA (VLD/VMB)

> I'm not sure how one would implement a bind-by-reference
> operator in a language with call-by-name function interface,
> though.

Relatively simple.  It implies that the bind-by-reference operator must
not be a function, but this is no stranger than allowing a
bind-by-reference operation in C, where function arguments are
bind-by-value.

> Are the names to be forced through the bind or not?
> I think my original statement is correct no matter what else
> you add to the language, unless you break call-by-name.

I seem to have been unclear.  The bind-by-reference operator would not
affect the way values are passed to functions.  It would be an operator
that could be applied to (perhaps) any expression.  In a language with
lisp-like syntax but with call-by-name, we might implement swap
something like so:

    (define-function swap (a b)
        (declare t)
        (bind-by-reference ((a-alias a) (b-alias b))
            (assign t a-alias)
            (assign a-alias b-alias)
            (assign b-alias t) ) )

"define-function", "declare" and "bind-by-reference" must be special
forms, but "assign" and "swap" can be functions.  The bind-by-reference
form coins new names, which, when evaluated, always yield the same
instance.  Thus, swap only mentions "a" and "b" once.

I have glossed over detail (what type is "t", for example?), but I hope
everybody can see how this would work.  Casting it in C, it might work
something like this:

    void swap( a, b ) int *(*a)(), *(*b)(); {
        int t, *a_alias = (*a)(), *b_alias = (*b)();
        t = *a_alias;
        *a_alias = *b_alias;
        *b_alias = t;
    }

Of course, to be exactly proper, t, a_alias, and b_alias ought to be of
type (int *(*)()) also.  The t function would always return an address
in swap's local frame, a_alias would always return what a returned at
bind-time, and so on.  Can be done, but makes the function a *lot*
uglier.  I'll leave that as an excersize for the masochistic.

> In any case, I was just trying to illuminate one aspect of
> the problem; you have added some more light to that.  Who
> needs a universal swap macro anyway?

Well, the "weak form" of your original point (the meaning of "swap" in
the presense of by-name binding is a real problem) is still quite valid.
I just was trying to point out that implicit by-name binding doesn't
mean that the language can't provide explicit by-reference binding.
Just as in C, the fact that the language binds by-value implicitly,
doesn't mean that one can't bind by-reference or by-name explicitly.

And I don't know who needs a universal swap macro.  I guess I view these
"devise a swap such that..." challenges as finger excersizes.

--
It is possible by ingenuity and at the expense of clarity ...
[to do almost anything in any language].  However, the fact
that it is possible to push a pea up a mountain with your nose
does not mean that this is a sensible way of getting it there.
                                        --- Christopher Strachey
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list