• Subject: Re: Relational metamethods and mixed types
• From: Geoff Leyland <geoff_leyland@...>
• Date: Tue, 24 Mar 2009 15:02:55 +1300

```Hi David,

Thanks for you thoughts!

On 24/03/2009, at 2:19 PM, David Manura wrote:

```
```On Mon, Mar 23, 2009 at 8:03 PM, Geoff Leyland wrote:
```
PiL says: "Unlike arithmetic metamethods, relational metamethods do not
```support mixed types...".  Supporting mixed types would be very handy
for defining constraints in Linear Programs, where you could go:
a_constraint = 2 * x1 + 3 * x2 + 4 * x3 <= 6
```
...The pure Lua alternative I can think of is to offer easy constructors
```for term lists:
a_constraint = 2 * x1 + 3 * x2 + 4 * x3 <= make_term_list(6)
```
```
Even that won't work because, moreover, "these operators always result
in false or true." [1]  Therefore, a_constraint always assumes the
value true or false, not some expression object.
```
```
```
Thanks, I didn't read the docs far enough earlier, and that one just bit me as your mail arrived. Oh well...
```
```
```Your approach is fairly similar to [2], which has some limitations as
you have shown.
```
```
```
Thanks - that provides a reply to my earlier mail about Short Anonymous Functions - I figured it had to be a common approach. I note you mention Todd Veldhuizen on the wiki page - I corresponded with him a bit back in the days when template expressions were new...
```
```
```Here's a few alternatives:

a_constraint = le(2 * x1 + 3 * x2 + 4 * x3, 6)
a_constraint = (2 * x1 + 3 * x2 + 4 * x3):le(6)
a_constraint = Constraint[[2 * x1 + 3 * x2 + 4 * x3 <= 6]]
```
```
```
I tend to like the second at the moment, though I couldn't say why. I considered the third initially, and then went off it when I realised template expressions might get me there. Your hints about only parsing lightly could be useful though. I'm just not sure I have the time to develop more than one approach very far, so I'm gonna have to guess the right one at the start.
```
```
```I tend to like the last one since putting the expression into a string
you parse yourself allows much flexibility, few gotchas, and a
familiar syntax.  If you need to substitute Lua variables into those
expressions, it's a bit more cumbersome, but you might use the
approaches in [3,4] and take care about security concerns similar to
SQL injection.
```
```
```
Yes, well, list comprehensions are on the list of things I'll have to tackle at some stage, so that's very helpful (I had found it before). I think I'll probably mostly be doing conditional sums over late-bound sets though, so once again, Template Expressions would be a really nice way of building them up
```
```
```Metalua[5], token filters, or patching Lua are other alternatives if
you want a more seamless syntax.
```
```
```
I'd really like to do as much in as pure Lua as possible if I can (not counting bindings to an LP solver). Pulp[1] and pyomo[2] manage quite nicely in Python, and Lua is so close...
```
Cheers,
Geoff