Open Capital-EX opened 1 year ago
So, what remedy do you propose? Like most interesting languages, Scala does not guarantee termination of type checking.
This is less about termination of type checking, and more potential errors in Scala approach to type checking. Many languages have non-terminating type checking, however only scala has resulted in this result.
However, one solution could be Idris's approach. Idris has full dependent typing and theorem proving, but distinguishes between total and non-total function. And restricts type-level programming to function Idris can prove are total. This may prove benfitial to Scala if there are plans for further dependent typing features.
But, Idris's type checker still halts with the message:
Error: Can't solve constraint between: (?a, ?s) -> ?c and ?a [no locals in scope].
Similar errors will be generated by Haskell, OCaml and F#.
Rust can detect that the type of apply
would be cyclical and rejects it for having an infinite size:
error[E0308]: mismatched types
--> src/main.rs:18:19
|
18 | let _ = apply(dup(quote((1,()))));
| ----- ^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
| |
| arguments to this function are incorrect
|
note: function defined here
--> src/main.rs:13:4
|
13 | fn apply<R: Clone, S: Clone>(s: (Rc<dyn Fn(R) -> S>, S)) -> R {
| ^^^^^ -----------------------------
I've tested this expression in about a dozen languages so far, but Scala is the only one with this unusually behavior. I'm not sure if I could propose a fix without a deeper understanding of Scala's approach to type checking. Most languages with generics/templates/type-level programing/etc. will have non-terminating type checkers, however none fail while evaluating simple polymorphic types for generic functions.
However, a short term fix could be including a memory limit on top of the existing recursion. If this unique behavior is intended, I can close the this issue.
The Scala compiler can be made to run out of memory while attempting to type check code that uses generics. Even while using a heap size of 4GB this fairly short snippet of code will cause the type check to run (seemingly) forever. My hunch is that Scala is attempting to construct an infinite type for
quot[A, S0, S1]
.Compiler version
Scala 3.2.2
Minimized code
Output (click arrow to expand)