brownplt / lambda-py

Other
58 stars 10 forks source link

mutable aliasing #41

Closed lijunsong closed 11 years ago

lijunsong commented 11 years ago

I write the mutable aliasing bug here for reference.

Problem1: list mutation

dict aliasing doesn't have problem, but list does.

l = [1,2,3,4]
l1 = l
l1[1] = 13
print(l)  # l will not be modified. and print [1,2,3,4]

This problem might be caused by the function's arguments binding.

l1[1] = 13 can be desugared to something like:

((CFunc (self, idx, val)
   (CAssign self (CBuitinPrim 'list-item (self idx val))))
 (list
   (CId 'l1 (GlobalId))
   (int 1)
   (int 13)))

The value of (CId 'l1) is bound to self in CFunc in a new-location as local bound, which means, the CAssign will set the value that maps from self's new-location, not the l1's location.

but this function's argument binding doesn't have any problem:

def f(x):
  x[1] = 13333
l = [1,2,3,4,5]
f(l)
print(l)
#=> [1, 13333, 3, 4, 5]

Problem2: pass dict as argument

BUG:

def f(x):
  x[1] = 13333
d = {1:33}
f(d)
print(d)
#=> {1: 13333, 1: 33}

which may not be related to the aliasing.

lijunsong commented 11 years ago

Problem2 seems to be solved in modules branch, which implements the MetaDict in a new way.

amtriathlon commented 11 years ago

Using the current master:

l = [1,2,3,4]
l1 = l
l1[1] = 13
print(l)  => [1,2,3,4]
print(l1) => [1, 13, 3, 4]

since l1 is correctly modified this problem seems to be one of (lack of) aliasing in the assignment l1=l, and not necessarily related to method invocation.

def f(x):
    x[1] = 13333
d = {1:33}
f(d)
print(d) => {1: 13333}

this doesn't seem to be a bug.

lijunsong commented 11 years ago

@amtriathlon I think the assignment has contained the alias:

l = [1,2,3,4]
l1 = l
l[1] = 13
print(l1)  => [1,13,3,4]

the store of the l1 is (VPointer 263), correctly pointing the the reference store.

amtriathlon commented 11 years ago

In the current master assign is giving a new location to l1, what version are you using?

2013/2/5 Junsong Li notifications@github.com

@amtriathlon https://github.com/amtriathlon I think the assignment has contained the alias:

l = [1,2,3,4] l1 = l l[1] = 13 print(l1) => [1,13,3,4]

the store of the l1 is (VPointer 263), correctly pointing the the reference store.

— Reply to this email directly or view it on GitHubhttps://github.com/brownplt/lambda-py/issues/41#issuecomment-13133669.

Alejandro.

lijunsong commented 11 years ago

@amtriathlon in the scope-rewrite branch.

amtriathlon commented 11 years ago

So, if assignment aliasing is working on scope-rewrite and self aliasing is working in master, probably this bug will be solved with the merge, let's see.

On a related note, IMHO MetaDict representation should be changed to an immutable hash since it is the only use of of mutable data left and it may be hiding other aliasing bugs.

lijunsong commented 11 years ago

Just wait. Since the module branch has changed the Dict implementation, and the new implementation uses the feature of the mutable hash, it really should consider more about it.

On 2013-2-6, at 下午9:27, Alejandro Martinez notifications@github.com wrote:

So, if assignment aliasing is working on scope-rewrite and self aliasing is working in master, probably this bug will be solved with the merge, let's see.

On a related note, IMHO MetaDict representation should be changed to an immutable hash since it is the only use of of mutable data left and it may be hiding other aliasing bugs.

— Reply to this email directly or view it on GitHub.

lijunsong commented 11 years ago

resolving the aliasing bug may be helpful to help bootstrap the sys module.

aliasing should also be considered in the interpreting CDict.

PS: And I like the ZEN MODE when editing in github:D

amtriathlon commented 11 years ago

Agree, since aliasing is closely related to scope, waiting for the scope-rewrite merge before making other changes seems the best course of action.

My point is: being that we are using immutable representations for most of mutable Python objects (lists, user defined classes, instances of user defined classes), aliasing is essential for almost everything and, IMHO, dict representation should also be made immutable.

OTOH, if mutable representations are allowed, would be a lot easier to have all mutable Python objects represented using mutable data structures and let racket handle aliasing.

PS: I don't, I prefer to have the context visible.

amtriathlon commented 11 years ago

This problem was solved by the new addresses regime, both examples are giving correct results.