in reply to Re^6: Precedence design question...'x' & arith
in thread Precedence design question...'x' & arith

Parens are a sign of either a complex statement OR insufficient strength in the grammar.

Now there's a false dilemma. Unless you enforce a strict left-to-right evaluation order like Smalltalk, you have to choose how to break ties between different operators at different precedence levels. Even when you have operators at the same precedence level, you have to decide which executes first, depending on associativity and arity and the like.

Without a single hard and fast expression evaluation order enforced by the grammar, you're going to have to make tradeoffs. Parentheses are also a sign that these tradeoffs aren't always right in every case.

  • Comment on Re^7: Precedence design question...'x' & arith

Replies are listed 'Best First'.
Re^8: Precedence design question...'x' & arith
by perl-diddler (Chaplain) on Jul 07, 2013 at 02:20 UTC
    You are right that you have to break ties.

    But we are talking about operators for 2 different data types.

    If one writes:

    " " x $indent*$spaces_p_tab . 2+3 ." + " . 4+6 . " == ".3*5
    Wouldn't it make sense to do the number operations first then combine their string representations with the strings?

    Since if you do the string operations first or give them equal precedence with the number operations, you end up with an invalid result. Why not do the evaluation that naturally gives you a valid result rather than choosing a rule to make it wrong?

    I would say that humans tend to group like with like and that a well designed parser would follow that premise over one that makes humans wrong.

      Wouldn't it make sense to do the number operations first then combine their string representations with the strings?

      If that's the only type of compound expression anyone ever writes which mixes string operations with numeric operations, perhaps. Unfortunately, it's not. Thus the possibilities are 1) to add even more precedence levels to add special cases things like the repetition operator, 2) to require disambiguating parentheses where the default precedence levels are wrong for special cases like this, or 3) to enforce a strict left to right evaluation order.

      I would say that humans tend to group like with like and that a well designed parser would follow that premise over one that makes humans wrong.

      You're poisoning the well.

        Among your possibilities... (2) AND (3) are required. There is a strict left to right evaluation among {x*/} and {.+=}, AND parenthesis are required in simple expressions. Adding the precedence levels described would eliminate default precedence levels being wrong (because in this case there is no right way to create the expression without them), and would allow strict left to right evaluation within the same types.

        In an expression like:

        ' ' x 2*3
        it is more efficient for the interpreter to do the numeric calculation first as 2*2 is 1 instruction while the other requires buffer allocation for a result. So perl has to do MORE work -- to end up with an expression that fails. If that's not a hint that the precedence rules could use some fine tuning, I don't know hat is.

        Currently, the operators, 'x', '*', '/' are at the same level. Below them are '.', '+', and '-'.

        But having the string operators at the same level as the numeric operators doesn't makes sense. If there is a logic in that choice, please explain it. In mathematics it is generally a given that an simple expression can be simplified by distributing operands and getting rid of parentheses.

        'x' doesn't belong in the same group as * and /. One, because it isn't communicative, 2) because technically it's a call to an internal API-function that requires its arguments in a specific order. It's even more rigorous in its requirements than ".".

        This is a case of the language breaking the principle of generalization. The only way you can intermix those operators is if all operands are numbers and then you end up with a very specialized usage that isn't intuitive at all.

        I'm proposing the idea that in separating the precedence, we make the ability to combine statements composed of numbers and strings a generalized possibility, whereas now, it's a special case in the language -- more so because the 'x' operator isn't commutative.

        As for your common on poisoning the well, I am not sure I follow, unless you mean by my persistence, in adherence to ideals I alienate people. That is unfortunate if that is the case.