I’m currently learning about functors in my university course on OCaml.
Right now, I’m having trouble using an instance of a functor, here’s my code
(* Signature ELT (element) *) module type ELT = sig type t val compare : t -> t -> int end (* Signature S (set) *) module type S = sig type elt type t val vide : t val element_de : elt -> t -> bool val ajout : t -> elt -> t end (* Implementation A of signature ELT *) module A : ELT = struct type t = int let compare x y = x - y end (* Functor MakeL with signature argument E of type ELT that produces a module S with type elt = E.t (same element type) *) module MakeL(E : ELT) : S with type elt = E.t = struct type elt = E.t type t = elt list let vide =  let element_de elt t = List.exists (fun e -> (compare e elt) = 0) t let ajout t elt = elt :: t end module Test = MakeL(A) let l1 = [1;4;5] let l2 = Test.ajout l1 6
When I compile it with
ocamlc -c, it outputs
File "ensemble.ml", line 58, characters 20-22: 58 | let l2 = Test.ajout l1 6 ^^ Error: This expression has type int list but an expression was expected of type Test.t = MakeL(A).t
Am I missing something? I thought l1 is already of type A.t list since A.t = int as implemented.
On a further note, I want to know how to access these fonctors from another file. Let’s say I put the
Test module in
test.ml. I get the following error:
File "test.ml", line 2, characters 20-21: 2 | module Test = MakeL(A) ^ Error: Unbound module A
How do functors and modules differ from functions when they can’t be accessed from other source files in the same directory when the latter can?