clojure-numerics / expresso

Clojure library for symbolic computation
312 stars 20 forks source link

Parsing "inner-product" #2

Closed mikera closed 10 years ago

mikera commented 10 years ago

Currently expresso does not correctly parse expressions like:

(parse-expression "inner-product(x,y)")
mschuene commented 10 years ago

This fails because inner-product(x,y) is interpreted as symbol inner minus function-call product(x,y). This is an unfortunate byproduct of allowing 3-4 to be parsed and not requiring whitespaces between literals. I think this is also the reason why in many algol- derived languages like java '-' is not a valid character in a symbol. I am not sure what to do about that. There are multiple strategies:

  1. require spaces between literals. Has the downside that you then could not write "1+23+4" and instead had to write "1 + 2 \ 3 + 4"
  2. use camel case and convert that to normal clojure naming, so innerProduct -> inner-product.
  3. don't support it. (You can still work around that by using a valid symbol like dot instead of inner-product and after parsing substitute that)
  4. support this methods with other names, e.g. dot for inner-product and substitute automatically after succesful parse. Could also be extensible so the user could specify aliases for function symbols in the string which is parsed

I think allowing all valid symbol characters clojure allows would be nice to have. I don't see a way to have this without 1.) but introducing it would break many user's expectation that "x-y" can be parsed.

Otherwise 4. would be the strategy I am inclined to. The ability to specify aliases for symbols could also make the infix expression more concise. That would also allow to specify 'values' for some variables in the infix string e.g. Parse this expression with x equals 5.

What would be your favourite way it is handled?

mikera commented 10 years ago

I guess I'd prefer 4) plus some way of quoting symbols to be used literally?

Would also be worth thinking how this might interact with namespacing, ideally the same mechanism would support qualified symbols.

mschuene commented 10 years ago

I just pushed a fix to master. It is now supported to escape symbols. Inside the escaped symbols you can use the characters that are allowed in clojure symbols. So you can do the following now: (parse-expression "inner-product(a)") ;=> (inner-product a) please report if it is working for you so I can close the issue.

mikera commented 10 years ago

Looks good to me!

I merged this fix back into develop for you.