ocaml-modular-implicits icon indicating copy to clipboard operation
ocaml-modular-implicits copied to clipboard

Support simple recursive functions without type annotations

Open yallop opened this issue 11 years ago • 3 comments

# 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.

yallop avatar Sep 04 '14 00:09 yallop

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) ();;

lpw25 avatar Sep 04 '14 06:09 lpw25

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>

yallop avatar Sep 04 '14 08:09 yallop

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.

lpw25 avatar Oct 07 '14 15:10 lpw25