Integrated Dynamics

Integrated Dynamics

63M Downloads

StackOverflowError from implementing Y combinator

narwhalfire opened this issue ยท 4 comments

commented

Issue type:

  • ๐Ÿ› Bug. Also sort of a feature request.

Short description:

I tried to implement the Y combinator to see if I could do some recursion. Recursion did indeed happen and a StackOverflowError resulted.

Steps to reproduce the problem:

Making the Y combinator is actually quite simple.

Y combinator: (\x -> x x) (\x -> x x)
Pipe 2 is used to duplicate an input into apply.
(\x -> x x) = pipe2 id id apply
Y comb = apply (\x -> x x) (\x -> x x)
Materializing (\x -> x x) is fine. Materializing the Y combinator dooms the stack and crashes.

Expected behaviour:

What happened was exactly what I expected, a StackOverflowError.


Versions:

  • This mod: 1.12.2-1.0.16
  • Minecraft: 1.12.2
  • Forge: 14.23.5.2838

Log file:

It's not the full log since that's just full of the same few methods over and over, but its the stack call just before Java decided it was done. https://pastebin.com/VNfX1KyA

###Notes:

I am actually glad this crashed the way it did since that means you can properly implement recursion in ID. I'd like this to stay in because you can do really cool stuff, and there shouldn't be a problem as long as infinite recursion is handled gracefully. I haven't dug around in the code base that much so I don't know how hard it would be to safely catch the overflow error, but if you catch it where you unwind the stack enough, you should be able to continue execution normally. If need be, maybe disallow creation of the Y combinator somehow, and implement a safe alternative operator. I think it would be better to handle infinite recursion in the general case instead as I have no doubt there can be other ways to achieve it, and you can throw an error to the player instead of having the game crash.

commented

You really seem to be pushing ID to its limits! :p
I'll look into fixing this soon, thanks for reporting.

commented

Oh Haskell Curry, please forgive this folley of mine.

commented

Pedantic note: that's not the Y combinator; it's the ฮฉ combinator, or equivalently, the result of applying the identity function to the Y combinator. The Y combinator itself would be (\f -> (\x -> f (x x)) (\x -> f (x x))).

And the ฮฉ combinator is supposed to blow up, so the bug is just that it crashes the whole game instead of just making the part error.

commented

Will be fixed in the next updated.

Tbh, I don't really like the fix, but as far as I know there is no proper way to detect recursive statements like this.