Reputation: 61
I'm new to OCaml and I am a little confused about Modules.
I tried to implement a really simple test but I can't compile it...
Here are the files (I'm on Linux by the way) :
main.ml
let main () =
if ((Array.length Sys.argv) > 2 && int_of_string Sys.argv.(1) > 1 && int_of_string Sys.argv.(2) > 1)
then
begin
Printf.printf "Args = %d && %d\n" (int_of_string Sys.argv.(1)) (int_of_string Sys.argv.(2));
Laby.initLaby (int_of_string Sys.argv.(1)) (int_of_string Sys.argv.(2))
end
else
Printf.printf "Usage : ./test x y n"
let _ = main ()
Laby.ml
let initLaby (x : int) (y : int) =
let testCell = Cell.initCell 0 1 in
begin
Printf.printf "Init Laby with X(%d) / Y(%d)\n" x y;
Cell.printCell testCell;
end
Cell.ml
module type CELL =
sig
type t
val initCell : int -> int -> t
val printCell : t -> unit
end
module Cell : CELL =
struct
type t = (int * int)
let initCell (x : int) (y : int) =
(x, y)
let printCell (x, y) =
Printf.printf "Cell -> X(%d) / Y(%d)\n" x y
end
Cell.mli
module type CELL =
sig
type t
val initCell : int -> int -> t
val printCell : t -> unit
end
module Cell : CELL
And here is the Makefile :
NAME = test
ML = Cell.ml \
Laby.ml \
main.ml
MLI = Cell.mli
CMI = $(MLI:.mli=.cmi)
CMO = $(ML:.ml=.cmo)
CMX = $(ML:.ml=.cmx)
OCAMLDPE = ocamldep
CAMLFLAGS = -w Aelz -warn-error A
OCAMLC = ocamlc $(CAMLFLAGS)
OCAMLOPT = ocamlopt $(CAMLFLAGS)
OCAMLDOC = ocamldoc -html -d $(ROOT)/doc
all: .depend $(CMI) $(NAME)
byte: .depend $(CMI) $(NAME).byte
$(NAME): $(CMX)
@$(OCAMLOPT) -o $@ $(CMX)
@echo "[OK] $(NAME) linked"
$(NAME).byte: $(CMO)
@$(OCAMLC) -o $@ $(CMO)
@echo "[OK] $(NAME).byte linked"
%.cmx: %.ml
@$(OCAMLOPT) -c $<
@echo "[OK] [$<] builded"
%.cmo: %.ml
@$(OCAMLC) -c $<
@echo "[OK] [$<] builded"
%.cmi: %.mli
@$(OCAMLC) -c $<
@echo "[OK] [$<] builded"
documentation: $(CMI)
@$(OCAMLDOC) $(MLI)
@echo "[OK] Documentation"
re: fclean all
clean:
@/bin/rm -f *.cm* *.o .depend *~
@echo "[OK] clean"
fclean: clean
@/bin/rm -f $(NAME) $(NAME).byte
@echo "[OK] fclean"
.depend:
@/bin/rm -f .depend
@$(OCAMLDPE) $(MLI) $(ML) > .depend
@echo "[OK] dependencies"
Here is the output of the Makefile :
[OK] dependencies
[OK] [Cell.mli] builded
[OK] [Cell.ml] builded
File "Laby.ml", line 3, characters 17-30:
Error: Unbound value Cell.initCell
Makefile:47: recipe for target 'Laby.cmx' failed
make: *** [Laby.cmx] Error 2
I think it's a compilation error, since it seems to not found the Cell module, but I can't make it works... What am I doing wrong, and how can I fix it?
Upvotes: 0
Views: 1902
Reputation: 1252
Each .ml
file serves as its own module. You seem to have module Cell
inside cell.ml
which is double. You would have to address that function as Cell.Cell.initCell
. Or open Cell
in laby.ml
. Also, I think .ml
file names are conventionally lowercase? Aside: why does make
output wrong english?
Upvotes: 1