bstpierre
bstpierre

Reputation: 31256

ocaml nested modules: the implementation ml does not match the interface cmi

Using ocaml 4.01.0 and core 111.17.00 as installed by opam.

I have two, probably related, problems that I've boiled down to the following simplified modules.

I have an outer module (Zee here) that contains an inner module (Foo). My main program is in xx.ml.

The first problem is that when I build using corebuild xx.byte, I get the error message:

File "zee.ml", line 1:
Error: The implementation zee.ml does not match the interface zee.cmi:
       The field `Foo' is required but not provided

The second problem, which may stem from whatever I'm doing wrong to cause that error, is that if I uncomment the commented-out code in xx.ml below, I get:

File "xx.ml", line 3, characters 23-32:
Error: Unbound module Zee.Foo

I'm new to ocaml, but I have other code with nested modules that seems to be working. I haven't been able to figure out what is different about this situation that causes these errors. Is there something about declaring module signatures inside of other modules that I'm missing?

Here is the code (separate files as indicated by headers):

(**** zee.mli ****)
module type Foo = sig
  val x : int
end

val bar : int

(**** zee.ml ****)
module Foo = struct
  let x = 10
end

let bar = 20

(**** xx.ml ****)
open Core.Std

(*
let () = printf "%d\n" Zee.Foo.x
*)

let () =
  printf "%d\n" Zee.bar

The full output from corebuild with the code exactly as above:

bash# rm -rf _build && corebuild xx.byte
ocamlfind ocamldep -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -modules xx.ml > xx.ml.depends
ocamlfind ocamldep -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -modules zee.mli > zee.mli.depends
ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -annot -bin-annot -short-paths -thread -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -o zee.cmi zee.mli
ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -annot -bin-annot -short-paths -thread -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -o xx.cmo xx.ml
ocamlfind ocamldep -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -modules zee.ml > zee.ml.depends
ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -annot -bin-annot -short-paths -thread -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -o zee.cmo zee.ml
+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -annot -bin-annot -short-paths -thread -syntax camlp4o -package bin_prot.syntax -package sexplib.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax -package core -o zee.cmo zee.ml
File "zee.ml", line 1:
Error: The implementation zee.ml does not match the interface zee.cmi:
       The field `Foo' is required but not provided
Command exited with code 2.

Upvotes: 2

Views: 2801

Answers (1)

Michaël Le Barbier
Michaël Le Barbier

Reputation: 6478

This is because you said

module type Foo =
sig
  val x : int
end

while you meant

module Foo :
sig
  val x : int
end

The first statement declares a module type Foo and specifies (note the use of the = sign reminiscent of a binding) the signature for this module type. The module type can then be used to specify the expected signature of arguments to a functor or to filter the signature of a module implementing a superset of Foo.

The second statement declares a module Foo and specify (note the use of the : character reminiscent of a type constraint) the signature of this module.

File "zee.ml", line 1:
Error: The implementation zee.ml does not match the interface zee.cmi:
       The field `Foo' is required but not provided

This is because you promised a module type Foo in the interface file which you did not implement in the implementation file — instead you implemented a module with this interface.

File "xx.ml", line 3, characters 23-32:
Error: Unbound module Zee.Foo

This is because the module definition is hidden by your interface file.

Upvotes: 5

Related Questions