Most likely there is something obvious I'm missing as I don't know a lot about rust, but unless the compiler isn't doing what is essentially a type level equivalent of tail call elimination, which it almost certainly does, finding the type of that function should be trivial unless the return type can become recursive with unbounded depth.
It doesn't, why would it? That's a pretty niche thing to have.
> finding the type of that function should be trivial unless the return type can become recursive with unbounded depth.
The return type is recursive with unbounded depth. The typechecker doesn't know the value of `n`, so to it this returns an infinitely recursive type.
Closures are statically dispatched. If you want something like this to work, you should box them as a trait object so that they get dynamically dispatched.
(The problem is solved by using a trait object to dynamically dispatch the inner call. https://play.rust-lang.org/?gist=0d3c0a5f1291eb34ba61bd39449...)
Although I'm pretty sure you can overload the Fn* traits to implement currying in Rust. (Fn traits are unstable). Which is neat.
// Something like this:
impl<F, A, B, R> (FnOnce(A) -> Curry<F, A, B>) for F
where F: FnOnce(A, B) -> R,
{ ... }
// and
impl<F, A, B, R> (FnOnce(B) -> R) for Curry<F, A, B>
where F: FnOnce(A, B) -> R
I think I got it working... but you would need some work for more arguments. (And it worked different on different versions of rust-nightly :/)
What happens in rust closures have a unique type[1]. So the recursive call in cps_factorial, instantiates a new cps_factorial which contains a new unique closure type which instantiates a new cps_factorial...
[1] This is done so that closure calls can be determined statically reducing run-time overhead and helps optimisation.