Re: '!' versus 'not' in boolean expression

2016-01-25 Thread Brandon Allbery
On Mon, Jan 25, 2016 at 2:42 PM, Carl Mäsak  wrote:

> >> my %h; say 'false' if !%h:exists;
> > Unexpected named parameter 'exists' passed


By the way, is it me or would it be a lot more appropriate and helpful if
this error said *what* it was passed to?

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: '!' versus 'not' in boolean expression

2016-01-25 Thread Carl Mäsak
Tom (>):
> In creating some new Perl 6 programs I've run across several instances
> I'm confused about, to wit:
>
> Example 1
> ---
>
>> my %h; say 'false' if !%h:exists;
> Unexpected named parameter 'exists' passed

I can explain this one. But it's the kind of explanation that makes a
person sadder, not happier.

Adverbs like `:exists` latch onto a preceding operator according to a
rule described below. The operator you want it to latch onto is the
postcircumfix `` indexing of `%h`. The operator it *does* latch on
to is the prefix `!`. That's not what a sane human reader expects, but
that's what happens.

How does it happen? Here's the rule for what an adverb latches onto:
it looks back at the preceding expression, views it as a tree, and
picks the "top" node, that is, whatever operator is holding the rest
of the expression.

Without the `!`, the top node is ``. With the `!`, the top node is `!`.

This is slightly easier to see if we convert the expression the adverb
is considering to Lisp form:

(prefix: (postcircumfix:<< <> >> %h "a"))

The fact that the "prefix:" ends up being first in Lisp form means
that it's the top node in the expression tree.

This also means that one way to make this problem go away is to add
extra parentheses, because that again puts `` at the top by
shutting out `!` from consideration:

$ perl6 -e 'my %h; say "false" if !(%h :exists)'
false

Personally, I find that while the "top node" rule always perfectly
explains the behavior of adverbs in retrospect, it's not a natural
rule to use, and it's downright refactoring-hostile. It sows distrust
in one's ability to just add a `!` somewhere and being able to predict
the result.

I think I would much prefer adverbs to latch onto the textually last
operator. (But in saying this, I realize that I might be unaware of
some terribly important use case for adverbs that this would
preclude.)

Of course, all this is moot if you just take lizmat++'s advice and do
:!exists. :)

// Carl


'!' versus 'not' in boolean expression

2016-01-18 Thread Tom Browder
In creating some new Perl 6 programs I've run across several instances
I'm confused about, to wit:

Example 1
---

> my %h; say 'false' if !%h:exists;
Unexpected named parameter 'exists' passed

Example 2
---

> my %h; say 'false' if not %h:exists;
false

It looks like '!' doesn't work as I thought it was supposed to.  But,
I just discovered that when I use parens, it works.

Example 3
---

> my %h; say 'false' if !(%h:exists);
false

I presume the parens would cure the similar things I've noticed with
other classes.

When I look at the docs on Operators I see this:


prefix !

multi sub prefix:(Mu) returns Bool:D

Negated boolean context operator.

Coerces the argument to Bool by calling the Bool method on it, and
returns the negation of the result. Note that this collapses
Junctions.



prefix not

multi sub prefix:(Mu $x) returns Bool:D

Evaluates its argument in boolean context (and thus collapses
Junctions), and negates the result.


Those two definitions look very similar to my eyes, but I think the
subtle difference is intentional.But they are not identical.

Is there some rule of thumb here that a Perl 6 wannabe can grasp in
Perl 5 terms (e.g., prefer 'not' over '!')?  Or am I going to have to
go deep early into the object class structure?

Many thanks.

-Tom


Re: '!' versus 'not' in boolean expression

2016-01-18 Thread yary
In Perl5, there's "&&" vs "and", "||" vs "or", "^" vs "xor", and "!"
vs "not", the difference being precedence. Perhaps it's the same with
Perl6...


Re: '!' versus 'not' in boolean expression

2016-01-18 Thread Elizabeth Mattijsen

> On 18 Jan 2016, at 19:55, Tom Browder  wrote:
> 
> In creating some new Perl 6 programs I've run across several instances
> I'm confused about, to wit:
> 
> Example 1
> ---
> 
>> my %h; say 'false' if !%h:exists;
> Unexpected named parameter 'exists’ passed

Yeah, this is an unexpected one.  However, there is a simple solution:

  my %h; say 'false' if %h:!exists;

In general, :foo is equivalent to foo => True, and :!foo is equivalent to foo 
=> False.


> Example 2
> ---
> 
>> my %h; say 'false' if not %h:exists;
> false
> 
> It looks like '!' doesn't work as I thought it was supposed to.  But,
> I just discovered that when I use parens, it works.

Yes, the ! binds closer, and eats the :exists, and then complains about it.



Liz

Re: '!' versus 'not' in boolean expression

2016-01-18 Thread James Ellis Osborne III
Too many Reimanns & Not enough role?

-jas

On 18 January 2016 at 11:37, Elizabeth Mattijsen  wrote:
>
>> On 18 Jan 2016, at 19:55, Tom Browder  wrote:
>>
>> In creating some new Perl 6 programs I've run across several instances
>> I'm confused about, to wit:
>>
>> Example 1
>> ---
>>
>>> my %h; say 'false' if !%h:exists;
>> Unexpected named parameter 'exists’ passed
>
> Yeah, this is an unexpected one.  However, there is a simple solution:
>
>   my %h; say 'false' if %h:!exists;
>
> In general, :foo is equivalent to foo => True, and :!foo is equivalent to foo 
> => False.
>
>
>> Example 2
>> ---
>>
>>> my %h; say 'false' if not %h:exists;
>> false
>>
>> It looks like '!' doesn't work as I thought it was supposed to.  But,
>> I just discovered that when I use parens, it works.
>
> Yes, the ! binds closer, and eats the :exists, and then complains about it.
>
>
>
> Liz


Re: '!' versus 'not' in boolean expression

2016-01-18 Thread Tom Browder
On Mon, Jan 18, 2016 at 1:37 PM, Elizabeth Mattijsen  wrote:
>> On 18 Jan 2016, at 19:55, Tom Browder  wrote:
>> In creating some new Perl 6 programs I've run across several instances
>> I'm confused about, to wit:
>>
>> Example 1
>> ---
>>
>>> my %h; say 'false' if !%h:exists;
>> Unexpected named parameter 'exists’ passed
>
> Yeah, this is an unexpected one.  However, there is a simple solution:
>
>   my %h; say 'false' if %h:!exists;
>
> In general, :foo is equivalent to foo => True, and :!foo is equivalent to foo 
> => False.

Okay, I'll just have to get used to it, then.

Thanks, Liz!

-Tom