microsoft / knossos-ksc

Compiler with automatic differentiation
Other
45 stars 10 forks source link

python parser silently ignores malformed 'lam (arg : Type) body extra extra' #911

Open acl33 opened 3 years ago

acl33 commented 3 years ago
>>> from ksc.parse_ks import parse_expr_string
>>> parse_expr_string("(lam (x : Integer) (y : Float) foo)")
<ksc.expr.Lam object at 0x7fbf0aff3910>
>>> e=parse_expr_string("(lam (x : Integer) (y : Float) foo)")
>>> print(e)
(Lam x : Integer (Call y [:, Float]))
>>> print(e.body)
(Call y [:, Float])
>>> print(e.body.args[0])
:
>>> print(e.body.args[1])
Float
>>> print(e.body.args[2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>>

So parsing ":" and "Float" as 'Var's is one thing, but also, what has happened to the "foo"? Here's a simpler example of the latter:

>>> e2 = parse_expr_string("(lam (x : Integer) (add x 1) hello world)")
>>> print(e2)
(Lam x : Integer (Call add [x, 1]))

where the result is plausible but the hello world has disappeared entirely.

toelli-msft commented 3 years ago

That's not a valid lam. There should only be three components to a lam:

  1. lam
  2. A variable with its type
  3. The body

In your example (add x 1) is the body. (Arguably the presence of hello and world should cause a parse failure.)

acl33 commented 3 years ago

Yes, I think a parse failure would be helpful. This hit me in an example when the first parsed successfully "by accident"...