-
Notifications
You must be signed in to change notification settings - Fork 1.1k
rewrite rules mean implicit function variables cannot be safely passed as arguments #10889
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
Comments
The "obvious" fix to the spec is:
and from my (admittedly fast) read through of the Simplicitly paper, I didn't find a theoretical reason not to do this (and the quote above suggests that the version I propose is in fact the actual intent of the paper) However the next sentence from the docs:
makes it sound like there are some implementation difficulties to doing this naively. I've implemented my fair share of interpreters and type checkers for toy languages, but I am by no means a compiler engineer (and certainly not familiar with the dotty code base) so I'm not sure where a reasonable patch would even begin (perhaps conditionally type-checking |
This behavior is deeply ingrained in both the theory and implementation. The only possible improvement I see is if we could apply an optimization that shortcuts closure creation and application pairs. That would be a worthwhile project to undertake. |
I feel like if it's implemented as an optimization, it would need an annotation like |
@elfprince13 where would you put the annotation? I think it would just make everything more confusing, as it's quite a specialized case. An optimization like this sounds rather straightforward to implement, and from your example it looks like it could be useful to guarantee that the optimization is always performed. There was a similar story with |
I definitely have no objection to not having to add annotations as long as there is a guarantee somewhere. |
Okay, a couple questions.
|
Correct.
Yes, that seems to be the best way forward. We do something similar for call-by-name parameters.
You can't special case the original rewrite since at that point you don't have the type of any tree. |
Understood.
Okay, so it looks like the three relevant classes are Presumably the starting point would be to base a class off of Presumably if |
… contextual closures if they wrap a context function of the expected type. Add a runtime test to probe for stackoverflow if this optimization is not performed.
Fixes scala#10889. Alternative to scala#13750, from which the test is taken.
Fixes scala#10889. Alternative to scala#13750, from which the test is taken.
(discussion carried over from #10825 at @LPTK's suggestion).
Minimized code
Output
StackOverflowError originating at
Explode$.atomic$$anonfun$1(Explode.scala:19)
:Expectation
STMs and other optimistic approaches to speculative execution of composite atomic operations typically rely on an abort/retry loop, and are a pretty standard example for the benefits of contextual abstractions (see, e.g, ScalaSTM).
Unfortunately, the current version of dotty uses the following rule for context functions passed as arguments:
This means that even if
E
is already a variable of type(T_1, ..., T_n) ?=> U
, the wrapper is still produced.In the abort/retry loop handler structure above, this means that (a) a potentially very large number of transient objects are invisibly produced in a performance-sensitive context, (b) the stack space consumed by invoking
txnOp
grows linearly with the number of abort/retry cycles, and, in the pathological case, potentially side-effecting control flow with a StackOverflowError.At a more basic level it breaks one of the fundamental assumptions that I think most programmers have about type systems, which is that if I have a variable
x:S
and functionf:S=>T
, then invokingf(x)
will actually pass the valuex
tof
.We can work around this by falling back to something which is closer to the Scala 2 style of "implicit functions":
But this is clearly a lot less elegant and feels like it should not be necessary. In fact it runs counter to the stated goal of implicit functions:
[ Odersky, Blanvillain, Liu, Biboudis, Miller, and Stucki. 2018. Simplicitly: Foundations and Applications of Implicit Function Types ]
The text was updated successfully, but these errors were encountered: