Search code examples
ocamlutop

What is the type "int/2" in OCaml


When I executed the following commands in the interaction environment (either OCaml or utop), all the expressions of "int" type turned out to be "int/2" type. This behaviour can be replicated as follow.

# 3;;
- : int = 3

# type int;;
type int

# type a;;
type a

# 3;;
- : int/2 = 3

Does anyone have any idea why this happens? Thanks!

Edit on Mar 2 2020:

I found that the "int/2" won't appear if I do the following. Can anyone please explain what happened here?

# 3;;
- : int = 3

# type int;;
type int

# 3;;
- : int = 3

Update: OCaml version 4.08.1 was used in the above cases.


Solution

  • The constant 3 is of the built-in type int, which is not the type int that is in scope. So the toplevel appends a number to signal this fact. Otherwise things can get very confusing. I.e., you can get messages like "a value was expected of type int but this value is of type int". With the /n tag, it more clearly says "a value was expected of type int/n (one kind of int) but this value is of type int/m (another different kind of int)"

    It appears this behavior was added in OCaml 4.08.0. You can find discussion of the feature here: https://github.com/ocaml/ocaml/pull/1120

    Update

    You should show your OCaml version. This feature was added recently enough that you may encounter toplevels both with and without the feature.

    At any rate, both of your examples use the int/n notation in my tests using OCaml 4.10.0

    $ ocaml
            OCaml version 4.10.0
    # 3;;
    - : int = 3
    # type int;;
    type int
    # type a;;
    type a
    # 3;;
    - : int/2 = 3
    
    $ ocaml
            OCaml version 4.10.0
    # 3;;
    - : int = 3
    # type int;;
    type int
    # 3;;
    - : int/2 = 3
    

    (It's also possible that this behavior was made a little more consistent in OCaml 4.10.0.)