Ruler icon indicating copy to clipboard operation
Ruler copied to clipboard

Add native arithmetical expressions support

Open Hywan opened this issue 11 years ago • 26 comments

A trivial merge of Hoa\Math in this library to get native arithmetical expressions support.

It will introduce a change (so a break) in the grammar. Array declarations will be [1, 2, 3] and no longer (1, 2, 3). The library has the RC state, so we can still introduce BC breaks. I particulary ask @stephpy, @shouze and @osaris to get their feedbacks :-).

Note: we should not add new features in the grammar in the future. If we want more power, we should focus on Hoathis\Lua which is a much better scripting language (imperative, procedural, functional etc.).

Hywan avatar Mar 02 '14 10:03 Hywan

I don't understand, you want to merge ruler and math grammar to support thinkgs like (2 + user.nb_vote) > context.minimum_vote_to_win ?

stephpy avatar Mar 03 '14 08:03 stephpy

“merge” is not really appropriated. I just would like to see the same features, i.e. native arithmetical expressions. And to reply to your question: yes :-).

Hywan avatar Mar 03 '14 08:03 Hywan

Ok, that would be a good point. But is “Ruler“ a good name ? May be it would be appropriated to make something more global like symfony\ExpressionLanguage ?

stephpy avatar Mar 03 '14 09:03 stephpy

No, as I said, if we need more features, we should focus on Lua which is a real scripting language (more powerful than Symfony\ExpressionLanguage). But are arithmetical operations a need in rules? That's the real question I think.

Hywan avatar Mar 03 '14 09:03 Hywan

@Hywan I think that Ruler should stay a Rete rule algorithm like lib. I'm not very confortable with the fact you can assign a value to any variable in this lib. Should be immutable & read only system I think.

shouze avatar Mar 03 '14 09:03 shouze

@shouze Oh sure, it will be immutable. I propose the support of arithmetical expression, not assignation. The example of @stephpy is right: user.nb_vote + 2 > context.minimum_vote_to_win / 2 for example.

(thanks for the link)

Hywan avatar Mar 03 '14 09:03 Hywan

ok, so I'm fully ok with that :+1:

shouze avatar Mar 03 '14 09:03 shouze

Actually, it will not be so much difficult (I think). Arithmetical operators will be introduce in the grammar and treated as Hoa\Ruler\Model\Operator. Maybe we will need some tricks for the minus operator bu we will see. Anyone interesting for a PR?

Hywan avatar Mar 03 '14 09:03 Hywan

But what about the BC break it will introduce?

Hywan avatar Mar 03 '14 16:03 Hywan

Why (1,2,3) could not be supported ? The SQL like syntax of rule is interesting, if we can keep it, it would be better.

stephpy avatar Mar 03 '14 16:03 stephpy

Oh, due to conflicts with arythmetic operations (x + y) etc...

If we can't support it, let's go for []

stephpy avatar Mar 03 '14 16:03 stephpy

(7) is an array or just 7 ;-) ? This is ambiguous.

Hywan avatar Mar 03 '14 18:03 Hywan

This is ambigous indeed, but (7) in SQL is an array with only 7 value :)

stephpy avatar Mar 04 '14 08:03 stephpy

@stephpy Hmm interesting. So, since we postulate the ruler language is close to SQL, do we need to introduce this BC break? Also, (7) is very “uncommon” but this is language design error from my point of view :-p.

Hywan avatar Mar 04 '14 10:03 Hywan

Imoi, It would be nice to keep SQL syntax, but the question is “Is it possible ?".

stephpy avatar Mar 04 '14 11:03 stephpy

Normally, yes. I have to test, but the only ambiguous syntax is an unary list like (7) which will be considered as an array with one element instead of a “flat” integer. But if the user is aware of it, there is no problem.

Hywan avatar Mar 04 '14 11:03 Hywan

the difficulty is to supports 1 + (7) in (8) which should return true.

stephpy avatar Mar 04 '14 11:03 stephpy

In the same line what will happen when you do (2, 3) * (5, 6) matrix calculation?

flip111 avatar Mar 04 '14 12:03 flip111

@flip111 it's not valid, (2,3) is an array, it's like array(2, 3) * array(5,6)

stephpy avatar Mar 04 '14 12:03 stephpy

@stephpy, you said:

the difficulty is to supports 1 + (7) in (8) which should return true.

No it should not. 1 + (7) is “integer plus array”, which is not valid (PHP will try to cast anyway). So either (i) we introduce the BC break (an array declaration is surrounded by [ and ]), or (ii) the (7) will only represent an array. We have to know if the solution (ii) is restrictive for the user or not.

Hywan avatar Mar 04 '14 14:03 Hywan

Is it still relevant ?

stephpy avatar May 02 '16 07:05 stephpy

@Hywan just in case of BC break array notation support, the best thing should be indeed to adopt something similar to matlab notation no? So yes [ and ], so php array(1, 2)or [1, 2] should be represented by [1 2] notation. Parenthesis are so much essential in operation groups that they can't be used also for array notation.

shouze avatar May 02 '16 08:05 shouze

@shouze Arrays are represented by [ and ] since a long time. The comments above are outdated.

@stephpy This might be still relevant I guess but this is still hard to implement. This will duplicate code from Hoa\Math, except if we re-use the arithmetical visitor. Are you willing to do it?

Hywan avatar May 09 '16 07:05 Hywan

@Hywan Oops, yes indeed, sorry.

shouze avatar May 09 '16 08:05 shouze

Looking at the code and grammars, this seems pretty hard to implement. At least, it will require many code duplication:

  • part of the Arithmetic.pp grammar will have to be added here
  • some of the rules in Arithmetic.pp will have to change a bit: for example, Arithmetic.pp expects a number token and Ruler works with floats and integers
  • The visitor from Hoa\Math will also have to be duplicated
  • The visitor will also have to be adapted: for example, Math expects a #variable node but Ruler does not have such node defined in its grammar

I think this is not as easy as it seems.

jubianchi avatar Sep 09 '16 16:09 jubianchi

Yup, it's a very hard problem.

Hywan avatar Sep 12 '16 07:09 Hywan