Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-27 Thread ROGER GRAYDON CHRISTMAN

On Tue, Sep 26, 2017 11:00 PM, Steve D'Aprano 
wrote:
>
The critical distinction here is whether the names refer to each other:
>
>a <---> b
>
>or whether they merely refer to the same value:
>
>a ---> [ value ] <--- b
>
>
>Python uses the second model. Var parameters in Pascal and references in C++
use
>the first. Since the term "aliasing" is well-established for the first, using
>it in Python *without making the difference clear* is wrong.
>
>
>

Aha!  There is the fundamental problem.
In your first diagram below, if a is a pointer to b, and b is a pointer to a,
then where is the value?

If the value of 1 (or 2) is in a, then a cannot point at b;

Or are you saying now that all variables in Pascal and C
are _both_ values and pointers, simultaneously?

Does that mean then that the value 1 (or 2) is in _both_  a and b?
So that an assignment to either must change both copies of the value?

No, I think it is more likely to be that the second diagram applies
in all cases.   Simply declaring a variable like A would lead to

a --> [ uninitialized ]

int& b creates an alias to a leads to

a -> [ uninitialized ] <- b

and then any assignment to a or b in the non-Python languages
under consideration would fill in that box, allowing the change
to be visible to both.

But in Python, _all_ assignments are aliases.

And I'll leave off further discussion by referring back to my
previous note about what happens when we do "b = c"
in the non-Python languages and in Python.

>From the ordering of the notes in this forum, I will just assume
you did not get a chance to read it before this post I am responding to.

Roger Christman
Pennsylvania State University

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-27 Thread Antoon Pardon
Op 27-09-17 om 09:38 schreef Steven D'Aprano:

No, the model that C++ and Pascal use is not different in this aspect.

> that Pascal var parameters and C++ reference variables operate the same 
> way as Python variable assignment, the *kindest* thing I can say is that 
> you are ignorant.

The kindest thing I can say about you is that you are very confused
at a specific level. A confusion that is illustrated by your 
contribution at 
https://mail.python.org/pipermail/python-list/2017-September/726513.html
and of which you seem to totally ignore my resonse at 
https://mail.python.org/pipermail/python-list/2017-September/726527.html

> Python does not have anything like C++ references and Pascal var 
> parameters, which is why you will never be able to write a swap() 
> function that operates like the classic Pascal swap procedure used as the 
> definitive test for pass-by-reference.

You keep repeating this and you keep ignoring my counter argument.
The fact that you can write a swap procedure in C++ and Pascal
is not because the parameter passing semantics are different but
because the assignment semantics are different.

I already explained that a working swap function depends on two
conditions:

1) Reference paramaters
2) A general way in which the object a name refers to can be
   modified/mutated (In a language like Pascal that is the
   assignment).

You keep asserting that not being able to write a swap means we
don't have condition (1), while ignoring we know we don't have
condition (2) in Python.

> Twice you have claimed to be able to write such a swap procedure for 
> lists. You can't. If you think you can, it is only because you have 
> misunderstood the problem and are writing something else that does 
> something different from what the Pascal version does.

I already posted code once, here it is again:

ls1 = [1, 3, 5, 7]
ls2 = [2, 4, 6]

def list_swap(lp1, lp2):

tmp = [] # pseudo declaration

tmp[:] = lp1 # 
lp1[:] = lp2 #  This is more or less how array assignments work in Pascal
lp2[:] = tmp #

print("ls1 =", ls1)
print("ls2 =", ls2)

list_swap(ls1, ls2)

print()
print("ls1 =", ls1)
print("ls2 =", ls2)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-27 Thread Antoon Pardon
Op 27-09-17 om 10:11 schreef Chris Angelico:
> On Wed, Sep 27, 2017 at 5:38 PM, Steven D'Aprano
>  wrote:
>> Twice you have claimed to be able to write such a swap procedure for
>> lists. You can't. If you think you can, it is only because you have
>> misunderstood the problem and are writing something else that does
>> something different from what the Pascal version does.
> I suspect what he's thinking of is a swap_contents() function, which
> gives the appearance that the lists have been swapped. That's entirely
> possible, but doesn't actually achieve what swap() does.

If you refer to languages like Pascal or C, for illustrating that you
can write a swap function in them, the swapping that happens there is
one of content. So claiming that swapping contend doesn't actually
achieves whar swap() does, seems wrong.

-- 
Antoon Pardon.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-27 Thread Chris Angelico
On Wed, Sep 27, 2017 at 5:38 PM, Steven D'Aprano
 wrote:
> Twice you have claimed to be able to write such a swap procedure for
> lists. You can't. If you think you can, it is only because you have
> misunderstood the problem and are writing something else that does
> something different from what the Pascal version does.

I suspect what he's thinking of is a swap_contents() function, which
gives the appearance that the lists have been swapped. That's entirely
possible, but doesn't actually achieve what swap() does.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-27 Thread Steven D'Aprano
On Wed, 27 Sep 2017 08:56:03 +0200, Antoon Pardon wrote:

>> But that's not enough for the variable b to be an alias for the
>> variable a.
> 
> Yes it is!


Since you seem to be intent on inventing your own meanings for well 
established words, for the confusion and misinformation of all, I can 
only follow in your footsteps and say:

"You are a fine fellow and your arguments make perfect sense."

Make if that what you will.


Antoon, there is no point in continuing this argument. You're entitled to 
your own opinions, but not your own facts, so when you insist:

> No, the model that C++ and Pascal use is not different in this aspect.

that Pascal var parameters and C++ reference variables operate the same 
way as Python variable assignment, the *kindest* thing I can say is that 
you are ignorant.

Python does not have anything like C++ references and Pascal var 
parameters, which is why you will never be able to write a swap() 
function that operates like the classic Pascal swap procedure used as the 
definitive test for pass-by-reference.

Twice you have claimed to be able to write such a swap procedure for 
lists. You can't. If you think you can, it is only because you have 
misunderstood the problem and are writing something else that does 
something different from what the Pascal version does.



-- 
Steven D'Aprano
“You are deluded if you think software engineers who can't write 
operating systems or applications without security holes, can write 
virtualization layers without security holes.” —Theo de Raadt
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-27 Thread Antoon Pardon
Op 27-09-17 om 04:58 schreef Steve D'Aprano:
> A pedantic difference that makes no difference to my argument.
>
> I see that you ignored the later assignment:
>
> b = 2;
>
> which also assigned to a. *That** is the fundamental point: b is certainly an
> alias for a, and assigning to b assigns to a.
>
> That's how aliases work in C++. That's how var parameters in Pascal work, and
> out parameters in Ada. That is what it means to say that "b is an alias to a".

The main problem is that you keep using assignment as if an assignment in
languages like Pascal and C++ has an similar effect like as assignment in
Python and thus that if an assignment has an effect in one language it should
have that effect in other languages.

In C++ and Pascal talking about an assignment means, that we have a name that
refers to an object whose value was overwritten.

In Python an assignment means that we have name that will now refer to an other
object.

If two names are aliases they refer to the same object and when the value of 
that
object is modified through one name, it is visible through the other name. How
one modified that object is immaterial. It doesn't matter whether the 
modification/mutation was done through an asignment or an other kind of 
mutation.

It also doesn't matter what the label is, we use for the operation. The label
"assignment" is not magic. If in one context an assignment mutates and in an
other context it doesn't you can't interfere conclusions about the assignment
in the second context based on the effects an assignment has in the first 
context
because we are talking about two differnt operations that just use the same 
lable.

-- 
Antoon Pardon.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-27 Thread Antoon Pardon
Op 27-09-17 om 04:58 schreef Steve D'Aprano:
> On Wed, 27 Sep 2017 02:03 am, Stefan Ram wrote:
>
>> Steve D'Aprano  writes:
>>> On Tue, 26 Sep 2017 03:26 am, Antoon Pardon wrote:
 at that moment, but it still needed correction. If the assignment is
 an alias operator then after the statements
>>> Here's some C++ code that demonstrates it. Apologies in advance if it isn't
>>> the most idiomatic C++ code.
>>   In C++, assignments and initializations are different
>>   concepts.
>>
>>> int& b = a;  // reference variable or alias
>>   This is an initialization, not an assignment.
> A pedantic difference that makes no difference to my argument.
>
> I see that you ignored the later assignment:
>
> b = 2;
>
> which also assigned to a. *That** is the fundamental point: b is certainly an
> alias for a, and assigning to b assigns to a.
>
> That's how aliases work in C++. That's how var parameters in Pascal work, and
> out parameters in Ada. That is what it means to say that "b is an alias to a".
>
> b is another name for the *variable* a, not just whatever value a happens to
> hold now.
>
> I say that assignment in Python is NOT an aliasing operation. Antoon claims 
> I'm
> wrong, and his evidence is:
>
> a = []
> b = a  # Antoon says this is an alias operation
> b.append(1)
> assert a == [1]
>
>
> But that's not enough for the variable b to be an alias for the variable a.

Yes it is!

> Antoon is correct that a and b are two different names for the same list, but
> the two variables are not aliases to each other because assignments to b do 
> not
> affect a, and vice versa.

You are hitting your blindspot again and ignore that in languages like Pascal 
...
an assignment is a copy operation and thus mutates the variable that is assigned
to. Two variables are aliases for each other if they are the same object and so
when one mutates the object seen through one name, the mutation is visible 
through
the other.

Since Python in Python an assignent doesn't mutate the object but makes it an 
alias of an other object it is wrong to expect in python that an assignment
to one of an alias which would break the alias, would have the same effect
as in a language where an assignemt to one of an alias would mutate the 
variable. 

> A good test for aliasing is to take the source code and mechanically replace
> every occurrence of the alias (in the same scope of course) with the original,

No it is not. You forget the possibility that two names can be aliases at
one point but no longer are at an other point. 

> or vice versa, and see whether the meaning of the code changes.
>
> In C++, apart from the initial binding:
>
> int& b = a;
>
> ("initialisation") you could now randomly swap a for b or b for a and the
> meaning of the code will not change.

That is only true for as long a and b remain aliases. As soon as an operation
is excuted that makes a and b no longer aliases, your test fails. Sure in
a language like C++ such an alias can't be broken, but aliases are broken
in Python all the time because that is what assignment do, break some
aliases and forge new ones.

> But in Python, if we try the same trick, the code *does* change:
>
> a = 1
> b = a
> b = 2
>
> *is not* the same as:
>
> a = 1
> b = a
> a = 2
>
>
> (1) In Pascal, Ada, C++ etc using a reference variable (or var parameter, or
> whatever terminology they use) makes the two names aliases to EACH OTHER, not
> to the value they are bound to.

No using a reference variable makes the two names aliases to the same 
object/entity.
So that if you mutate it through one name, the mutation is visible through the
other name. The effect you see in those languages with assignments via one
name are explained by the fact that assignment is a mutating operation.

> (2) In Python, Javascript, Ruby etc assignment can give you two names for the
> same object, but the names do not alias each other.
>
> The critical distinction here is whether the names refer to each other:
>
> a <---> b

In languages like C++, Pascal, ... aliases don't mean the names refer to
each other. Names/Identifiers refering to each other is non sensical in
those languages.

>
> or whether they merely refer to the same value:
>
> a ---> [ value ] <--- b
>
>
> Python uses the second model. Var parameters in Pascal and references in C++ 
> use
> the first. Since the term "aliasing" is well-established for the first, using
> it in Python *without making the difference clear* is wrong.

No, the model that C++ and Pascal use is not different in this aspect.

-- 
Antoon Pardon.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-26 Thread Bill

Steve D'Aprano wrote:

On Wed, 27 Sep 2017 02:03 am, Stefan Ram wrote:


Steve D'Aprano  writes:

On Tue, 26 Sep 2017 03:26 am, Antoon Pardon wrote:

at that moment, but it still needed correction. If the assignment is
an alias operator then after the statements

Here's some C++ code that demonstrates it. Apologies in advance if it isn't
the most idiomatic C++ code.

   In C++, assignments and initializations are different
   concepts.


int& b = a;  // reference variable or alias

   This is an initialization, not an assignment.

A pedantic difference that makes no difference to my argument.

I see that you ignored the later assignment:

b = 2;

which also assigned to a. *That** is the fundamental point: b is certainly an
alias for a, and assigning to b assigns to a.

That's how aliases work in C++. That's how var parameters in Pascal work, and
out parameters in Ada. That is what it means to say that "b is an alias to a".

b is another name for the *variable* a, not just whatever value a happens to
hold now.

I say that assignment in Python is NOT an aliasing operation. Antoon claims I'm
wrong, and his evidence is:

a = []
b = a  # Antoon says this is an alias operation
b.append(1)
assert a == [1]


But that's not enough for the variable b to be an alias for the variable a.

Antoon is correct that a and b are two different names for the same list, but
the two variables are not aliases to each other because assignments to b do not
affect a, and vice versa.

A good test for aliasing is to take the source code and mechanically replace
every occurrence of the alias (in the same scope of course) with the original,
or vice versa, and see whether the meaning of the code changes.

In C++, apart from the initial binding:

 int& b = a;

("initialisation") you could now randomly swap a for b or b for a and the
meaning of the code will not change.

But in Python, if we try the same trick, the code *does* change:

a = 1
b = a
b = 2

*is not* the same as:

a = 1
b = a
a = 2


(1) In Pascal, Ada, C++ etc using a reference variable (or var parameter, or
whatever terminology they use) makes the two names aliases to EACH OTHER, not
to the value they are bound to.

(2) In Python, Javascript, Ruby etc assignment can give you two names for the
same object, but the names do not alias each other.

The critical distinction here is whether the names refer to each other:

a <---> b

or whether they merely refer to the same value:

a ---> [ value ] <--- b


Python uses the second model. Var parameters in Pascal and references in C++ use
the first. Since the term "aliasing" is well-established for the first, using
it in Python *without making the difference clear* is wrong.




That is a very nice argument!  : )
--
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-26 Thread Steve D'Aprano
On Wed, 27 Sep 2017 02:03 am, Stefan Ram wrote:

> Steve D'Aprano  writes:
>>On Tue, 26 Sep 2017 03:26 am, Antoon Pardon wrote:
>>>at that moment, but it still needed correction. If the assignment is
>>>an alias operator then after the statements
>>Here's some C++ code that demonstrates it. Apologies in advance if it isn't
>>the most idiomatic C++ code.
> 
>   In C++, assignments and initializations are different
>   concepts.
> 
>>int& b = a;  // reference variable or alias
> 
>   This is an initialization, not an assignment.

A pedantic difference that makes no difference to my argument.

I see that you ignored the later assignment:

b = 2;

which also assigned to a. *That** is the fundamental point: b is certainly an
alias for a, and assigning to b assigns to a.

That's how aliases work in C++. That's how var parameters in Pascal work, and
out parameters in Ada. That is what it means to say that "b is an alias to a".

b is another name for the *variable* a, not just whatever value a happens to
hold now.

I say that assignment in Python is NOT an aliasing operation. Antoon claims I'm
wrong, and his evidence is:

a = []
b = a  # Antoon says this is an alias operation
b.append(1)
assert a == [1]


But that's not enough for the variable b to be an alias for the variable a.

Antoon is correct that a and b are two different names for the same list, but
the two variables are not aliases to each other because assignments to b do not
affect a, and vice versa.

A good test for aliasing is to take the source code and mechanically replace
every occurrence of the alias (in the same scope of course) with the original,
or vice versa, and see whether the meaning of the code changes.

In C++, apart from the initial binding:

int& b = a;

("initialisation") you could now randomly swap a for b or b for a and the
meaning of the code will not change.

But in Python, if we try the same trick, the code *does* change:

a = 1
b = a
b = 2

*is not* the same as:

a = 1
b = a
a = 2


(1) In Pascal, Ada, C++ etc using a reference variable (or var parameter, or
whatever terminology they use) makes the two names aliases to EACH OTHER, not
to the value they are bound to.

(2) In Python, Javascript, Ruby etc assignment can give you two names for the
same object, but the names do not alias each other.

The critical distinction here is whether the names refer to each other:

a <---> b

or whether they merely refer to the same value:

a ---> [ value ] <--- b


Python uses the second model. Var parameters in Pascal and references in C++ use
the first. Since the term "aliasing" is well-established for the first, using
it in Python *without making the difference clear* is wrong.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-26 Thread ROGER GRAYDON CHRISTMAN

On Tue, Sep 26, 2017 12:00 PM, Steve D'Aprano 
wrote:
>
> a = 1
>> b = a
>> b = 2
>> 
>> a is not 2.
>
< snip >
int main () {
>   int a;
>   int& b = a;  // reference variable or alias
>
>   a = 1;
>   printf("a: %d, alias b: %d\n", a, b);
>   b = 2;
>   printf("a: %d, alias b: %d\n", a, b);
>   return 0;
>}
>
>
>And here is the output:
>
>
>a: 1, alias b: 1
>a: 2, alias b: 2
>
>
>So I stand by what I said: assignment in Python is NOT an alias operation. If
it
>were an alias operation, as you claim, then we could write this:
>
>a = 1
>b = a  # alias to a
>b = 2  # assigning to be is like assigning to a (as in the C++
>example)
>assert a == 2
>
>But Python is not like that, assignment is not an alias operation, and the
>assert will fail.
>

I think you are being a bit choosy in your analysis of the examples.
You essentially claim that "assignment is not an alias operation" by
assuming that some assignments are not an alias operations,
and then conclude that it is inconsistent for the rest to be.

Here is a discussion of the code in light of the claim that assignment
is an alias operation, meaning _all_ assignments are aliases,
and not just the ones you choose to prove your point.

a = 1 # 'a' is an alias to the value 1
b = a # 'b' is an alias to the value named by a, which is also an alias to 1
b = 2 # 'b' is now an alias to the value of 2, and no longer an alias to
'a' or 1

Your C++ counterexample is invalid, because, indeed, only the second
of the three assignments is an alias operation, and only because you
specifically declare variable b to be a reference variable.  You are not
really 'assigning' to b -- you are initializing the reference variable named
b..  

If we were to add an additional statement into both examples.

b = c

In Python, this would be another aliasing operation, causing b to
refer to the same thing as c refers to (assuming c has been assigned).

But in C++, that statement would be completely invalid -- once you
associate a reference to a reference variable, you cannot change
the binding.This further emphasizes that the statement "int  = a"
is not an assignment statement, which means it is rather irrelevant
to the semantics of assignment statements.

Roger Christman
Pennsylvania State University

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Aliasing [was Re: [Tutor] beginning to code]

2017-09-26 Thread Antoon Pardon
On 26-09-17 14:28, Steve D'Aprano wrote:
> On Tue, 26 Sep 2017 03:26 am, Antoon Pardon wrote:
> 
>> Sorry, what he wrote contradicts that. Maybe he was just really confused
>> at that moment, but it still needed correction. If the assignment is
>> an alias operator then after the statements
>>
>> a = 1
>> b = a
>> b = 2
>>
>> a is not 2.
> 
> How do you get that conclusion? I think you are mistaken.
> 
> If assignment were an alias operation, then a would be 2, because b is an 
> alias
> to a. That's how var parameters in Pascal work, and out parameters in Ada, and
> both are described as aliasing.

They work like that because the assignment in Pascal is *not* an alias 
operation.

> Its also how reference variables ("aliases") in C++ work.
> 
> https://www.tutorialspoint.com/cplusplus/cpp_references.htm
> 
> https://stackoverflow.com/a/17025902
> 
> 
> Here's some C++ code that demonstrates it. Apologies in advance if it isn't 
> the
> most idiomatic C++ code.

No that C++ code doesn't demonstrate your assertion. You ignore the fact that
only the 7th line is an alias operation, all the assignments (later) are copy
operations.

> #include 
> #include
> using namespace std;
> 
> int main () {
>int a;
>int& b = a;  // reference variable or alias
> 
>a = 1;
>printf("a: %d, alias b: %d\n", a, b);
>b = 2;
>printf("a: %d, alias b: %d\n", a, b);
>return 0;
> }

Because similarity of notation may be causing some confusion, I'll introduce two
new symbols.

:= This is the symbol I use for an assignment as copy operation, like Pascal.
<- This is the symbol I use for an assignment as alias operation.

So if the assignment is an alias operation, the code I was making an assertion
about, rewritten using these symbols would be:

a <- 1
b <- a
b <- 2

The code you use in support of your assertion instead behaves more like.

b <- a   # The order of the first two statements
a := 1   # doesn't matter, my criticism doesn't depend on that.
b := 2

If you want to make a statement about the effects of assignemt as an
alias operation, you will have to support that with code that actually
behaves like an alias operation each time an assignment is made. Not
code like your C++ example which only executes one alias operation and
where the rest of the assignments are copy operations.

-- 
Antoon Pardon.
-- 
https://mail.python.org/mailman/listinfo/python-list