in reply to $a++ allowed by $a-- is not ! why?

I'm going to register what I consider to be the most serious reason why -- isnt implemented this way. Reversability. Consider that the rules say any string matching /\A[A-Za-z]*[0-9]*\z/. So this means that 'Zz9' incremented becomes 'AAz0'. Now, what happens when we decrement? Presumbly we should go from 'AAz0' back to 'Zz9'. So we need decrement to remove the leading character. All fine and good. So we decrement a bunch more and eventually get to 'z9'. Now we increment one, and get 'aa0'. So we then end up in the odd situation where -- and ++ are not reversable (++(--'Aa0')!='Aa0') (what the correct name for this? commutable?). It gets even odder, because if we contine down this path we get to '9' which again if we reverse course and increment leaves us at "10" and in the same problem as before but with different colors. And if we contine to decrement we go to -1 and etc as per normal numbers. But what happens if we decrement "A"? If we go to undef as one poster suggested we get a contradiction (in that --0 produces -1 but --'a' produces undef), and after we decrement undef again we get -1 (with a warning thrown in) so now its all a big mess.

++ Zz9 => AAa0 -- AAa0 => Zz9 -- aa0 => z9 -- Aa0 => z9 ++ z9 => aa0 -- a0 => 9 -- 10 => 9 ++ 9 => 10 -- 0 => -1 -- a => undef -- undef => -1

Given all this I can totally understand why -- isnt implemented. I don't think I would have a problem if this behaviour did occur, but I think its strange enough that I can see why it doesn't.


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Replies are listed 'Best First'.
Re: Re: $a++ allowed by $a-- is not ! why?
by kabel (Chaplain) on Sep 01, 2003 at 08:43 UTC
    (what the correct name for this? commutable?).
    commutativity involves only one operation.
    $a + $b = $b + $a
    and at least two operands which get "permuted". thus, it is pointless to ask for commutativity of an unary operation.

    the problem is, ++ is not surjective.
    or can you find a value for $z so that ++ $z is "Aa0"? no, you can't:
    kabel@linux:~> perl my ($z1, $z2) = qw/ z9 Z9 /; print ++ $z1, ", ", ++ $z2, $/; aa0, AA0 kabel@linux:~>
    thus, ++ does not have any reverse mapping at all.
    but if we only allow either lower case or upper case (not both mixed), the problem nearly disappears:
    ++: /\A[A-Z]*[0-9]*\z/ -> /\A[A-Z]*[0-9]*\z/ ++: /\A[a-z]*[0-9]*\z/ -> /\A[a-z]*[0-9]*\z/
    a problem is that "0" has no preimage.
    or can you find a value for $z so that ++ $z is "0"? no, you can't. ;D

    HTH hehe, finally i was able to apply some knowledge of last years mathematics. hopefully, i am correct. :-)

      Modern algebra would say that although ++ is one-to-one, it is not onto. As such, it is not a permutation, since a permutation is defined as a one-to-one onto function from a set to itself.

      -- OTOH (as it is currently defined) is theoretically a permutation over the set of all finite numbers, provided you run Perl on an architecture capable of storing and representing all numbers in that set. (In practice I am not aware of any such architecture, but nevermind.)

      The logical conclusion is that if -- were made the inverse function of ++ it would no longer be one-to-one, because the inverse of a one-to-one function is onto and vice versa, but the inverse of a function that is not one-to-one is not onto and vice versa. Some people strongly prefer to avoid dealing with functions that are not one-to-one. Of course you could redefine the domain of -- so that it is one-to-one, but then you have a function on an apparently very arbitrary set (a set defined in terms of the range of ++ in fact), which could be considered "messy".

      I don't know how much of this went into the decision, but I know that Larry knows some set theory, so it is entirely possible he considered this issue. It's also entirely possible that he just "felt" that applying the magic string extension to -- would be messy, without ennumerating all these points; sometimes people who design software for a living have a pretty good feel for which features would result in thorny issues. Sometimes they use the term "well-defined" to refer to a feature that can be implemented and meet most reasonable expectations. Applying the magic extension to -- would probably not be considered well-defined.


      $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
        sorry, i did not mean that permutation ;) it was just meant to be a visual description of the commutitativity law.

        Modern algebra would say that although ++ is one-to-one, it is not onto.
        why? it is a mapping from N to N (if i understand you correctly).
        -- OTOH (as it is currently defined) is theoretically a permutation over the set of all finite numbers ...
        the association is not clear to me. why do you think -- is a permutation? (what is a permutation for you?).

        i think we make too much fuss about nothing. perhaps we should continue this by mail?

      but if we only allow either lower case or upper case (not both mixed), the problem nearly disappears:

      As long as you can mix numbers with letters the problem remains.

      or can you find a value for $z so that ++ $z is "0"? no, you can't.

      What about -1?

      But thanks, some good links there. :-)


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
        As long as you can mix numbers with letters the problem remains.
        no, the signature does not allow to mix letters with numbers. if we allowed this, that means:
        ++: /\A[a-z0-9]*\z/ -> /\A[a-z0-9]*\z/
        , what is the difference between this addition and the addition in a 36-ary number system? (have fun with this link)
        the only problem here is that the first character must not be a number, thus the awkward domains:
        kabel@linux:~> perl my $num = "1hiho"; print ++ $num, $/; 2 kabel@linux:~>
        What about "-1"?
        "-1" is an illegal input. harhar. ;-)