user5357314
user5357314

Reputation: 31

Type checking a recursive single argument and single output function [Ocaml]

let rec loop (x:?) : ? =
  loop loop

Is there any types replacing the ? that will allow this function, loop, to type check?

Upvotes: 1

Views: 115

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66793

If you supply -rectypes to the interpreter it will tell you the type of your function:

$ ocaml -rectypes
        OCaml version 4.02.1

# let rec loop x = loop loop;;
val loop : 'a -> 'b as 'a = <fun>

You can declare this function using separate type variables for the parameter and return types, but this seems (to me) to be the same as not specifying the types.

# let rec loop (x: 'a) : 'b = loop loop;;
val loop : 'a -> 'b as 'a = <fun>

If you don't allow recursive types (with -rectypes), the function can't be made to typecheck because its type is recursive.

Upvotes: 4

Related Questions