ocaml-modular-implicits
ocaml-modular-implicits copied to clipboard
Support simple recursive functions without type annotations
# module type S = sig end;;
module type S = sig end
# let rec f (implicit X : S) () = f (implicit X) ();;
Characters 34-46:
let rec f (implicit X : S) () = f (implicit X) ();;
^^^^^^^^^^^^
Error: This expression has type 'a
This is not a function; it cannot be applied an implicit argument.
This one is actually the correct behaviour (although we could probably use a specific error message for this case). For now, all uses of recursive implicits must be annotated with their type, so your example should actually be:
let rec f : (implicit X : S) -> unit ->'a = fun (implicit X : S) () -> f (implicit X) ();;
The error message is rather confusing: 'a can be instantiated with a function type, and the location points to the argument rather than to the function.
Besides improving the error message, I'd like to leave this open to see if we can do better with respect to recursion. We obviously need signatures in some cases (e.g. polymorphic recursion), but in this example the signature doesn't add anything that we can't determine immediately from the syntax, as shown by the fact that a trivial partial signature is sufficient:
# let rec f : (implicit X : S) -> _ =
fun (implicit X : S) () -> f (implicit X) ();;
val f : (implicit X : S) -> unit -> 'a = <fun>
I've renamed this issue to reflect why it is still open. Note that any solution to this problem will be very difficult so it is unlikely to be addressed any time soon.