Array Logical Operators

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Array Logical Operators

Antonio Scuri

  Hi,

 

  I created a class for arrays that use the arithmetic and relational operators. But I would like also to implement logical operators so I can write:

 

d = a<b and b<c

 

  I defined the result of the relational operators as another array of booleans with the result for each element. So naturally the logical operators would have to return also an array of booleans.

 

  I don’t need to define the meaning of “if (a<b)”, it can be an error.

 

  So, wouldn’t be interesting to Lua to have metamethods for Logical operators too?

 

Best Regards,

Antonio Scuri

 

Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Dirk Laurie-2
2011/10/21 Antonio Scuri <[hidden email]>:

>   So, wouldn’t be interesting to Lua to have metamethods for Logical operators too?
>

>From the Lua 5.2 manual (but I think it is older than that):

"eq": the == operation. The function _getequalhandler_ defines how Lua
chooses a metamethod for equality. A metamethod is selected only when
both values being compared have the same type and the same metamethod
for the selected operation.

Also "le" and "lt".

It is a rare pleasure to be able to assist someone with an e-mail
address like yours :-)

Dirk

Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

KHMan
On 10/21/2011 10:02 PM, Dirk Laurie wrote:

> 2011/10/21 Antonio Scuri:
>
>>    So, wouldn’t be interesting to Lua to have metamethods for Logical operators too?
>
>> From the Lua 5.2 manual (but I think it is older than that):
>
> "eq": the == operation. The function _getequalhandler_ defines how Lua
> chooses a metamethod for equality. A metamethod is selected only when
> both values being compared have the same type and the same metamethod
> for the selected operation.
>
> Also "le" and "lt".
>
> It is a rare pleasure to be able to assist someone with an e-mail
> address like yours :-)

Nope. He wrote "logical operators".

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Elias Barrionovo
In reply to this post by Dirk Laurie-2
Wouldn't those be metamethods for *relational* operators? I'd think
the logical operators are 'not', 'and' and 'or'.

On 10/21/11, Dirk Laurie <[hidden email]> wrote:

> 2011/10/21 Antonio Scuri <[hidden email]>:
>
>>   So, wouldn’t be interesting to Lua to have metamethods for Logical
>> operators too?
>>
>
> >From the Lua 5.2 manual (but I think it is older than that):
>
> "eq": the == operation. The function _getequalhandler_ defines how Lua
> chooses a metamethod for equality. A metamethod is selected only when
> both values being compared have the same type and the same metamethod
> for the selected operation.
>
> Also "le" and "lt".
>
> It is a rare pleasure to be able to assist someone with an e-mail
> address like yours :-)
>
> Dirk
>
>

--
Sent from my mobile device

NI!

Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Dirk Laurie-2
2011/10/21 Elias Barrionovo <[hidden email]>:
> Wouldn't those be metamethods for *relational* operators? I'd think
> the logical operators are 'not', 'and' and 'or'.
>
> On 10/21/11, Dirk Laurie <[hidden email]> wrote:
>> 2011/10/21 Antonio Scuri <[hidden email]>:
>>
>> It is a rare pleasure to be able to assist someone with an e-mail
>> address like yours :-)
>>
Down to earth with a bump :-(

Dirk

Reply | Threaded
Open this post in threaded view
|

RE: Array Logical Operators

Antonio Scuri
In reply to this post by Elias Barrionovo
  Yes, I meant 'not', 'and' and 'or'.

Best,
Scuri

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Elias Barrionovo
> Sent: sexta-feira, 21 de outubro de 2011 12:23
> To: Lua mailing list
> Subject: Re: Array Logical Operators
>
> Wouldn't those be metamethods for *relational* operators? I'd think
> the logical operators are 'not', 'and' and 'or'.
>
> On 10/21/11, Dirk Laurie <[hidden email]> wrote:
> > 2011/10/21 Antonio Scuri <[hidden email]>:
> >
> >>   So, wouldn’t be interesting to Lua to have metamethods for Logical
> >> operators too?
> >>
> >
> > >From the Lua 5.2 manual (but I think it is older than that):
> >
> > "eq": the == operation. The function _getequalhandler_ defines how
> Lua
> > chooses a metamethod for equality. A metamethod is selected only when
> > both values being compared have the same type and the same metamethod
> > for the selected operation.
> >
> > Also "le" and "lt".
> >
> > It is a rare pleasure to be able to assist someone with an e-mail
> > address like yours :-)
> >
> > Dirk
> >
> >
>
> --
> Sent from my mobile device
>
> NI!


Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Gé Weijers
In reply to this post by Antonio Scuri
One problem is that the logical operators 'and' and 'or' use short-circuit evaluation, which is not easy to implement efficiently for user defined objects.

-- Gé

On Oct 21, 2011, at 4:55, Antonio Scuri <[hidden email]> wrote:

  Hi,

 

  I created a class for arrays that use the arithmetic and relational operators. But I would like also to implement logical operators so I can write:

 

d = a<b and b<c

 

  I defined the result of the relational operators as another array of booleans with the result for each element. So naturally the logical operators would have to return also an array of booleans.

 

  I don’t need to define the meaning of “if (a<b)”, it can be an error.

 

  So, wouldn’t be interesting to Lua to have metamethods for Logical operators too?

 

Best Regards,

Antonio Scuri

 

Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Roberto Ierusalimschy
> One problem is that the logical operators 'and' and 'or' use short-circuit
> evaluation, which is not easy to implement efficiently for user defined
> objects.

Exaclty. In Lua, as in many other languages, 'and' and 'or' are not
exactly operators; they are more like control structures. They do not
generate a single opcode, but a sequence of tests and jumps.

In statically typed languages, the compiler knows in advance what to
do. For a dynamic language, once the control code is generated, it is
hard to change it.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

RE: Array Logical Operators

Antonio Scuri
  Ok.

Thanks,
Scuri

> Exaclty. In Lua, as in many other languages, 'and' and 'or' are not
> exactly operators; they are more like control structures. They do not
> generate a single opcode, but a sequence of tests and jumps.
>
> In statically typed languages, the compiler knows in advance what to
> do. For a dynamic language, once the control code is generated, it is
> hard to change it.
>
> -- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Geoff Leyland
In reply to this post by Antonio Scuri

On 22/10/2011, at 12:54 AM, Antonio Scuri wrote:

>   Hi,
>  
>   I created a class for arrays that use the arithmetic and relational operators. But I would like also to implement logical operators so I can write:
>  
> d = a<b and b<c
>  
>   I defined the result of the relational operators as another array of booleans with the result for each element.

How did you manage to return something other than a boolean from a relational operator?


Reply | Threaded
Open this post in threaded view
|

RE: Array Logical Operators

Antonio Scuri
> How did you manage to return something other than a boolean from a
> relational operator?

 Isn't just pushing it to the stack? I didn't tested yet.

Scuri



Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Geoff Leyland
On 22/10/2011, at 3:05 PM, Antonio Scuri wrote:

>> How did you manage to return something other than a boolean from a
>> relational operator?
>
> Isn't just pushing it to the stack? I didn't tested yet.


The return values get converted to boolean:

http://lua-users.org/lists/lua-l/2010-02/msg00616.html

I thought this was documented somewhere, but I can't find it from a quick look at the 5.1 or 5.2 LRM
Reply | Threaded
Open this post in threaded view
|

RE: Array Logical Operators

Antonio Scuri
  Thanks for pointing that message. Indeed I did not find anything in the
documentation about that.

Best,
Scuri

> >> How did you manage to return something other than a boolean from a
> >> relational operator?
> >
> > Isn't just pushing it to the stack? I didn't tested yet.
>
>
> The return values get converted to boolean:
>
> http://lua-users.org/lists/lua-l/2010-02/msg00616.html
>
> I thought this was documented somewhere, but I can't find it from a quick
> look at the 5.1 or 5.2 LRM


Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Dirk Laurie-2
In reply to this post by Roberto Ierusalimschy
>> One problem is that the logical operators 'and' and 'or' use short-circuit
>> evaluation, which is not easy to implement efficiently for user defined
>> objects.
>
> Exaclty. In Lua, as in many other languages, 'and' and 'or' are not
> exactly operators; they are more like control structures. They do not
> generate a single opcode, but a sequence of tests and jumps.
>
> In statically typed languages, the compiler knows in advance what to
> do. For a dynamic language, once the control code is generated, it is
> hard to change it.
>

>From the original post:

> I created a class for arrays that use the arithmetic and relational operators.
> But I would like also to implement logical operators so I can write:

 d = a<b and b<c

> I defined the result of the relational operators as another array of booleans
> with the result for each element. So naturally the logical operators would have
> to return also an array of booleans.

What you need metamethods for is not `and`, `or` and `not`.  It's metamethods
for `&`, `|` and `~` (or `!`), which are not in Lua yet.  They'd be
nice to have,
being predefined for boolean and number (i.e. bit32.band, bit32,bor
and bit32.bnot)
why not binary `#` for bit32.bxor while we're about it; one day when we get an
arbitrary length `bit` library they could be defined for strings too.

Dirk

Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

steve donovan
On Sun, Oct 23, 2011 at 7:31 PM, Dirk Laurie <[hidden email]> wrote:
> What you need metamethods for is not `and`, `or` and `not`.  It's metamethods
> for `&`, `|` and `~` (or `!`), which are not in Lua yet.  They'd be
> nice to have,
> being predefined for boolean and number (i.e. bit32.band, bit32,bor
> and bit32.bnot)

Nice one, Dirk!  Not only useful in themselves, but full of
entertaining overloading possibilities.

E.g. the pipe operator | (assuming the correct associativity) for
process chains.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Tony Finch
In reply to this post by Dirk Laurie-2
On 23 Oct 2011, at 18:31, Dirk Laurie <[hidden email]> wrote:
>
> What you need metamethods for is not `and`, `or` and `not`.  It's metamethods
> for `&`, `|` and `~` (or `!`), which are not in Lua yet.  They'd be
> nice to have,
> being predefined for boolean and number (i.e. bit32.band, bit32,bor
> and bit32.bnot)
> why not binary `#` for bit32.bxor while we're about it; one day when we get an
> arbitrary length `bit` library they could be defined for strings too.

Good plan. I recommend using the same operator for unary NOT and binary XOR, by analogy with negation and subtraction. ~ is a good choice given its use in the ~= operator and in C.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Reply | Threaded
Open this post in threaded view
|

Re: Array Logical Operators

Dirk Laurie-2
2011/10/25 Tony Finch <[hidden email]>:

> I recommend using the same operator for unary NOT and binary XOR,
> by analogy with negation and subtraction.
> ~ is a good choice given its use in the ~= operator and in C.

Mnemonic: "A NOT B" means either "(A NOT) B" or "A (NOT B)".
Yes!

Dirk