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

36 activations of term rewriting macro? #9288

Open
kaushalmodi opened this issue Oct 10, 2018 · 6 comments
Open

36 activations of term rewriting macro? #9288

kaushalmodi opened this issue Oct 10, 2018 · 6 comments

Comments

@kaushalmodi
Copy link
Contributor

I took an example from the Nim Manual's TRM section: https://nim-lang.org/docs/manual.html#term-rewriting-macros

The example is almost as-is except that I added a debugEcho so that I know if/when the TRM is getting activated:

template mulIsCommutative{`*`(a, b)}(a, b: int): int =
  debugEcho("-> Term rewriting activated!")
  b * a

let x = 3
echo "Calculating x * 2:"
echo x * 2

Surprisingly that prints:

Calculating x * 2:
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
6

Ref: https://gitter.im/nim-lang/Nim?at=5bbe2eb6bbdc0b25051a4d02

what happens is that diverging rewrite rules get cancelled at some point - Araq

/cc @Araq

@kaushalmodi
Copy link
Contributor Author

To add to the complication, if I add another multiplication after that first one, the TRM stops activating altogether:

template mulIsCommutative{`*`(a, b)}(a, b: int): int =
  debugEcho("-> Term rewriting activated!")
  b * a

let x = 3
echo "Calculating x * 2:"
echo x * 2

echo ""
echo "Calculating x * 2 again:"
echo x * 2

echo ""
echo "Calculating x * 3:"
echo x * 3

Output:

Calculating x * 2:
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
-> Term rewriting activated!
6

Calculating x * 2 again:
6

Calculating x * 3:
9

@andreaferretti
Copy link
Collaborator

Duplicate of #8376 and #2901

@andreaferretti
Copy link
Collaborator

The confusion stems from the fact that term rewriting macros are applied recursively (up to a limit). I think this should be documented in the manual and all 3 issues closed

@kaushalmodi
Copy link
Contributor Author

(up to a limit).

That limit is 36?

I think this should be documented in the manual and all 3 issues closed

I can understand that for the first multiplication overflow happened, and TRM stopped activating after 36 loops. But then why does it not get activated at all for the next 2 multiplications? Once the limit is reached, the TRM stops working altogether for the rest of the program?

@andreaferretti
Copy link
Collaborator

andreaferretti commented Oct 11, 2018

I presume so, according to your experiments

@kaushalmodi
Copy link
Contributor Author

It would be more ensuring to get a better answer. "presume so" sounds like you also don't know for sure if that's the case. I'd like to know the answers to my 3 questions based on spec or design choice rather than based on my toy example.

Araq pushed a commit that referenced this issue Oct 11, 2018
@Araq Araq reopened this Oct 11, 2018
narimiran pushed a commit that referenced this issue Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants