Ocaml: Error - this expression has type x but is used with type x

849 Views Asked by At

This is my error:

Error: This expression has type nfa but is here used with type nfa

What could possibly be happening to cause this? I'm using emacs tuareg, and loading evaluating files one by one. Sometimes this happens, and other times it doesn't.

2

There are 2 best solutions below

0
On BEST ANSWER

There's a good description of this in the ocaml tutorial. What's happened is you have shadowed a type definition with a new definition:

type nfa = int
let f (x: nfa) = x

type nfa = int
let g (x: nfa) = x

Restarting the top-level will clear out the old definitions.

0
On

Update:

Since OCaml 4.01.0 (released Sept. 2013) the general problem is the same, but the error message adds a number to the type definition, to make evident the types are internally different.

Full example from the old OCaml FAQ in the toplevel:

type counter = Counter of int;; (* define a type *)
type counter = Counter of int
# let x = Counter 1;;           (* use the new type *)
val x : counter = Counter 1
type counter = Counter of int;; (* redefine the type, use it *)
type counter = Counter of int
# let incr_counter c = match c with Counter x -> Counter (x + 1);;
val incr_counter : counter -> counter = <fun>
# incr_counter x;;              (* now mix old and new defs *)
Error: This expression has type counter/1029
       but an expression was expected of type counter/1032
#