Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior of »-- #5057

Closed
frithnanth opened this issue Sep 10, 2022 · 10 comments
Closed

Strange behavior of »-- #5057

frithnanth opened this issue Sep 10, 2022 · 10 comments

Comments

@frithnanth
Copy link

I was toying with the code shown in this blog post Assuming optionality and I realized that running the following one-liner repeatedly:

$ raku -e'my BagHash $a=‘Perl Weekly Challenge’.comb.BagHash»--;$a.keys.sort.say'

got me different results, such as:

(  e l)
(  W e l)
(  C e l)

OTOH, the following:

$ raku -e'my BagHash $a=‘Perl Weekly Challenge’.comb.BagHash;$a.keys.sort.say'

always returns the same result, so I guess that the problem lays with the '»--'.

I'm running Rakudo v2022.07 on Debian sid.

@lizmat
Copy link
Contributor

lizmat commented Sep 10, 2022

Good catch! This is indeed weird.

But the >>-- appears to only be an issue when used when creating the BagHash. This appears to always produce the same results (at least for me):

$ raku -e 'my $a="Perl Weekly Challenge".comb.BagHash; say $a; $a>>--; say $a'
BagHash( (2) C P W a e(5) g h k l(4) n r y)
BagHash(  e(4) l(3))

@frithnanth
Copy link
Author

Yes, your version works fine here too!

@lizmat
Copy link
Contributor

lizmat commented Sep 10, 2022

$ raku -e '(my $a=‘Perl Weekly Challenge’.comb.BagHash)»--; say $a'
BagHash(  e(4) l(3))

also does the right thing.

@lizmat
Copy link
Contributor

lizmat commented Sep 10, 2022

Some progress: It turns out that in the case of the error, it is always the first key that is being decremented, that loses its decrement. If that is one of the higher valued keys, then you won't notice it unless you actually look at the value. If it is one the keys that has a value of 1, it won't get removed.

@lizmat
Copy link
Contributor

lizmat commented Sep 10, 2022

FWIW, this is the golf I'm working on ATM:

$ raku -e 'my $a:=‘Perl Weekly Challenge’.comb.BagHash.deepmap(&postfix:<-->); say $a'

as the >>-- effectively codegens to a call to deepmap.

@lizmat
Copy link
Contributor

lizmat commented Sep 10, 2022

Another datapoint: .MixHash has the same issue.

@lizmat
Copy link
Contributor

lizmat commented Sep 10, 2022

$ raku -e 'my $a:=‘Perl Weekly Challenge’.comb.MixHash.deepmap(&postfix:<-->); say $a'

@lizmat
Copy link
Contributor

lizmat commented Sep 11, 2022

Another Datapoint:

$ raku -e 'my $a:=‘Perl Weekly Challenge’.comb.BagHash.deepmap(&postfix:<++>); say $a'
BagHash( (2) C P W a e(5) g h k l(4) n r y)

Note that the increments somehow got lost :-(

lizmat added a commit that referenced this issue Sep 13, 2022
Spotted in #5057 .

The above basically runs QuantHash.new(1,2,3,4,1,2).deepmap(&postfix:<-->)

The problem was caused by there being an Associative candidate
for deepmap, and QuantHashes being Associative.  Since .deepmap is
supposed to *return* an object of the *same* type, this effectively
means a new object needs to be created.  In the case of the code in
the issue, essentially:

    $ raku -e 'my $a=‘abcdeabc’.comb.BagHash»--; $a.keys.sort.say'

there would be a BagHash object created, but then the deepmap logic
for Associatives would do some magic that would do the wrong thing
for QuantHashes (and possibly for Hash and Map as well, but that
requires further investigation).  The result was that the order in
which updates where being done, got confused, and the new BagHash
object got mutilated.

This commit adds specific Setty, Baggy and Mixy candidates for
deepmap, that actually uses information about the internal structure
of the QuantHashes to allow a fast creation of the new, updated
object.  This allows >>-- to even work on immutable QuantHashes (as
we create a new immutable object under the hood).  So this now works:

    $ raku -e 'say ‘abcdeabc’.comb.Bag»--'
    Bag(a b c)
@lizmat
Copy link
Contributor

lizmat commented Sep 13, 2022

Ok, finally got to the bottom of this: 63d03eb899

@lizmat
Copy link
Contributor

lizmat commented Sep 14, 2022

d0ec99a861 changed the example:

$ raku -e 'my $a=‘Perl Weekly Challenge’.comb.BagHash»--;$a.keys.sort.say'
(  C P W a e g h k l n r y)

and this is correct, as the increments happen on the original ‘Perl Weekly Challenge’.comb.BagHash and the result of >>-- on that is stored in $a. Since this has been specced as an object of the same type, but built on the return value of the operator, you effectively get the original BagHash and the decrements were done on the original BagHash object, which is not saved anywhere and therefore lost.

Adding parentheses to make sure that $a is bound before running >>--, makes the example run reliably now with the expected result:

$ raku -e '(my $a=‘Perl Weekly Challenge’.comb.BagHash)»--;$a.keys.sort.say'
(  e l)

So I'm now closing this issue, now that we hopefully have full clarity on the issue at hand, and its resolution.

@lizmat lizmat closed this as completed Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants