2

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.

1 Answers1

7

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

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • Thank you for your clarification. Can you please explain what is the "scope" that you mentioned of `int`? Also please take a look at my edits, in that added case, what is the scope of `int` then? Why is there a difference if I remove the `type a;;` command? Thanks! – user12448123 Mar 02 '20 at 16:26
  • 1
    Scope refers to the textual part of a program where a certain name is defined. For example in `let v = expr1 in expr2` the scope of `v` is `expr2`. After the end of `expr2`, `v` is no longer defined. The scope of a type identifier goes from where it appears to the end of the module (this includes the body of its definition, i.e., recursive uses). – Jeffrey Scofield Mar 02 '20 at 16:57