|
|
Subscribe / Log in / New account

Perl6's ^^

Perl6's ^^

Posted Feb 17, 2015 20:22 UTC (Tue) by dlang (guest, #313)
In reply to: Perl6's ^^ by mbunkus
Parent article: Scalar typing in the PHP world

how can you possible short circuit xor? after you evaluate the first argument, you have no way of knowing what the result is going to be until you evaluate the second one.


to post comments

Perl6's ^^

Posted Feb 17, 2015 20:24 UTC (Tue) by marcH (subscriber, #57642) [Link]

> how can you possible short circuit xor?

By inventing a brand new type of short-circuit without bothering giving it a new name.

Who cares about confusion - it's only Perl after all.

Perl6's ^^

Posted Feb 17, 2015 20:32 UTC (Tue) by mbunkus (subscriber, #87248) [Link] (12 responses)

Of course you cannot short-circuit a two-operand xor. But you can short-circuit xor consisting of three or more operands. If, at any time, two of those operands are true then the result will always be false, no matter which value the other operands have.

It's been explained at least twice in this thread with links to Perl's man page giving examples for it.

Perl6's ^^

Posted Feb 17, 2015 21:04 UTC (Tue) by mathstuf (subscriber, #69389) [Link] (11 responses)

> If, at any time, two of those operands are true then the result will always be false, no matter which value the other operands have.

That doesn't match the diagrams on Wikipedia on xor[1]. "true xor true xor true" is "false xor true" is "true". I don't see any reasonable reduction of it such that it can be false.

[1]https://en.wikipedia.org/wiki/Xor

Perl6's ^^

Posted Feb 17, 2015 21:11 UTC (Tue) by mbunkus (subscriber, #87248) [Link] (10 responses)

You're correct, of course, and my answer was imprecise. I should not have used the word »xor« like that because it isn't the bit-wise xor. It's infix ^^. Sorry.

Perl6's ^^

Posted Feb 17, 2015 22:20 UTC (Tue) by mathstuf (subscriber, #69389) [Link] (1 responses)

Do you at least admit that there is potential for serious confusion around this operator then (whether popular or not)? IIRC, there's some law about features of programming languages always getting (ab)used.

Perl6's ^^

Posted Feb 17, 2015 23:15 UTC (Tue) by mbunkus (subscriber, #87248) [Link]

Of course there's potential for confusion. That's always a danger with a language you're not that familiar with, especially with ones that, just like Perl, aren't that readable in the first place. For example, in PHP the ternary operator is right-associative (unlike ternary operators in any other language).

For me (and I'm really only speaking about myself here) having ^^ short-circuit is completely logical as all the other logical operators are short-circuiting as well. I find it easy to remember. Other people's brains work quite differently from my own, though, so of course they may find it a lot less logical or highly confusing.

For me »serious confusion« means a completely different set of topics in Perl. Those are also the things I dislike most about the language. For example: functions returning different results depending on context (wantarray – or the whole notion of having different contexts in the first place); the subtle but incredibly dangerous distinction between lists and arrays; all the magic and cryptically-named special variables ($_ or _ or $[ or $< or $/ or…).

Perl6's ^^

Posted Feb 18, 2015 19:41 UTC (Wed) by bronson (subscriber, #4806) [Link] (7 responses)

Now I'm really confused. You're saying "Infix ^^" is some sort of logical operation that isn't xor but everyone will be confused because '^'?

The linked page says it, "returns the first true argument if there is only one, and Nil otherwise". How is that useful? Is it common enough to be worth making it an operator? I'd google but that would obviously be futile. :)

Perl6's ^^

Posted Feb 18, 2015 19:56 UTC (Wed) by marcH (subscriber, #57642) [Link] (6 responses)

Assuming ^^ is useful, it should be part of some "list" module and given a full name and obviously not re-using the ^ symbol which is the traditional symbol for XOR.

Having the logic of this new ^^ operator somewhat related to XOR... yet different makes the re-use of the ^ symbol even worse: even more confusing, even more error-prone.

The unusual short-circuit logic which triggers only with 3 operands or more and the confusing effect of parentheses are the icing on the cake of surprises.

This looks like a futile exercise in code obfuscation; let's just stop beating a dead horse.

Perl6's ^^

Posted Feb 18, 2015 20:05 UTC (Wed) by niner (subscriber, #26151) [Link] (5 responses)

In Perl 6 ^^ is the infix operator version of the ^ junction operator.
Junction operators are: none, one (^), any (|), all (&). The infix operators behave accordingly. As such ^^'s behavior is indeed consistent with the other operators.

From Wikipedia:

List Operators

List operators construct a junction as a list:

my $options = any(1, 2, 3, 4); # Any of these is good
my $requirements = all(5, 6, 7, 8); # All or nothing
my $forbidden = none(9, 10, 11); # None of these
my $onlyone = one(12, 13, 4); # One and only one

Infix Operators

Another way to specify a junction is to use infix operators like we have already seen:

my $options = 1 | 2 | 3 | 4; # Any of these is good
my $requirements = 5 & 6 & 7 & 8; # All or nothing
my $onlyone = 12 ^ 13 ^ 4; # One and only one

Perl6's ^^

Posted Feb 18, 2015 20:19 UTC (Wed) by marcH (subscriber, #57642) [Link] (2 responses)

> Another way to specify a junction is to use infix operators...

One of the most dramatic things Open-Source changed is the fact that code is read, reviewed, reworked and read again by many more random people from many more random places with many more random backgrounds and many more education and knowledge levels. From this specific perspective Perl (and I guess PHP too - dunno it enough) are missing the open source revolution, because of this completely off the mark "There is more than one way to do it" mantra.

No Larry, computer languages should not try to emulate the fuzziness of natural languages, absolutely not. Machines are machines and humans are humans; two very different things which complement each other and don't need to copy each other.

Perl6's ^^

Posted Feb 19, 2015 7:31 UTC (Thu) by niner (subscriber, #26151) [Link] (1 responses)

Exactly! Code is a means to communicate with the next programmer and is almost incidentally executable by a computer. As you say, machines are machines and humans are humans. And humans understand human languages far better than machine code. Since the major goal is to communicate the intent of a piece of code to the next programmer that happens to read it, the fullness of a natural language can only help. And as a bonus, even the computer understands better what you're trying to achieve and thus has for example a better chance at optimizing.

Perl6's ^^

Posted Feb 19, 2015 15:25 UTC (Thu) by marcH (subscriber, #57642) [Link]

High-level abstractions and fullness are good but "There is more than one way" goes too far beyond fullness. Fullness is not the point here.

Natural languages have redundancy, semantic overlap, fuzziness, assumptions,... These have pros and cons; one (limited) purpose: expressing nuances. In pure technical communications nuances are completely unnecessary; even harmful in programming languages and any specification. For instance a natural language like English is so poorly suited to protocol specification that readers typically complain that they feel like they are reading a new, foreign language.

For driving machines (including machines that could kill), a language model to follow is not natural languages but maths: high-level, expressive, very limited redundancy and most importantly of all: unambiguous. Who wants machines to do something... "maybe"? Merely changing the vocabulary is fine since this does not change the properties, however adding assumptions and/or redundancy only brings confusion with users constantly wondering about what they are reading.

[BTW you don't seriously think that a redundant and inconsistent ^^ operator brings Perl closer to natural languages, do you?]

Perl6's ^^

Posted Feb 18, 2015 21:33 UTC (Wed) by bronson (subscriber, #4806) [Link] (1 responses)

I still don't get it. In your example, you're using ^ as an infix operator. So why does ^^ exist?

I liked Perl5, and still have some modules on CPAN, but this sort of discussion is giving me the heebie jeebies... Perl6 seems to have a number of false friends (^ and xor) and subtle nuances to learn.

Perl6's ^^

Posted Feb 19, 2015 7:27 UTC (Thu) by niner (subscriber, #26151) [Link]

^ creates a Junction: http://doc.perl6.org/type/Junction
^^ returns the first true argument if there is only one, and Nil otherwise.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds