ocamlutop

Why a named function is still displayed as anonymous in utop?


(I am learning the OCaml and this may be a naive question.)

I use below code to define a named function a:

utop # let a = fun x -> x+1;;
val a : int -> int = <fun>

Note that the leading val a clearly shows up as the name of the function.

Then I tried to see the type info of the function a:

utop # a;;
- : int -> int = <fun>

There's a leading -, which means anonymous.

But I have given it a name a.

Why is it not displayed?


Solution

  • The REPL reads the line

    a;;
    

    as an anonymous toplevel expression, which returns a value of type int->int. This is the same behaviour as you see with

    Array.iteri (fun n x -> a.(n) <- x + 1) a
    - : unit = ()
    

    with a slightly unusual return type for the expression.

    If you want to have information on a value, you can use the #show toplevel directive:

    #show a;;
    val a: int -> int