Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2f(n) parses as 2 * f * n #349

Open
byorgey opened this issue Apr 8, 2022 · 3 comments
Open

2f(n) parses as 2 * f * n #349

byorgey opened this issue Apr 8, 2022 · 3 comments
Labels
C-Moderate Effort Should take a moderate amount of time to address. S-Moderate Moderate importance U-Parsing U-Syntax Z-Bug

Comments

@byorgey
Copy link
Member

byorgey commented Apr 8, 2022

This definition yields a type error:

f : N -> N
f(0)
f(n+1) = 2f(n) + 3

It is accepted if we add a * sign in between the 2 and the f. Here's the problem:

Disco> :pretty 2f(n)
2 * f * n 
@byorgey byorgey added C-Moderate Effort Should take a moderate amount of time to address. U-Parsing S-Moderate Moderate importance Z-Bug labels Apr 11, 2022
@byorgey
Copy link
Member Author

byorgey commented Apr 11, 2022

So 2f(n) needs to parse as 2 * (f(n)) because function application has higher precedence than multiplication; however, the problem is that since function application and multiplication are both left-associative, this first parses as (2 f) n; then 2 f is resolved to 2 * f and then in turn (2 * f) n is resolved to (2 * f) * n. Indeed, if we had something like 2 x y that would be the correct thing to do. But in this case we need to take advantage of the fact that we still know there are parentheses around n to decide that f(n) is function application.

Off the top of my head I'm not sure of the best way to do this in general. Perhaps we should first turn any sequence of chained juxtapositions into a list, then go from there. Anything that looks like ident ( expr ) should be treated as a function call regardless of what comes to the left of the identifier?

@byorgey
Copy link
Member Author

byorgey commented Mar 22, 2023

#71 is related. In fact it seems like this might have worked correctly before 5c4cdf1 . We need both 2 f(n) to parse as 2 * (f @ n) and also (x+1)(x+2)(x+3) to parse as ((x+1) * (x+2)) * (x+3).

Might be simpler to change fixJuxtMul to work differently: first parse chained juxtapositions as a list. Then go through the list and turn anything that looks like <var> (<exp>) into function application; next find things that look syntactically like multiplication; then everything left is function application.

Hmm, could I just come up with an actual grammar for this? i.e. can I do it in a less ad-hoc way?

M = n | T op | T op T | '(' M ')' | M T
T = x '(' T ')' | M | T T

@byorgey
Copy link
Member Author

byorgey commented May 26, 2024

Note: I was worried that this would mean x(x+1) would no longer parse as multiplication. But it turns out it already does not parse as multiplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Moderate Effort Should take a moderate amount of time to address. S-Moderate Moderate importance U-Parsing U-Syntax Z-Bug
Projects
None yet
Development

No branches or pull requests

1 participant