Functor signature mismatch

I am learning functor recently. There are some tutorials, and documentation. They are very helpful, but I cannot figure out how is functor signature specification works.

Suppose I want to provide a general Address module. We can use register, temporary or immediate to calculate it, so I am using a module Op with an abstract type i to denote address base, index and other field. I can use functor to generate a specific address module with corresponding underlying type. Like IntAddress, or RegAddress through this wrapper.

module type Op = sig
  type i
end

module type Sig = sig
  type i
  type t

  val of_bisp : i -> i option -> i option -> i option -> t
  val pp_exp : t -> string
end

module Wrapper (M : Op) : Sig = struct
  type i = M.i

  type t =
    { base : i
    ; index : i option 
    ; scale : i option
    ; disp : i option 
    }

  let of_bisp base index scale disp = { base; index; scale; disp }
end

I want to to generate my custom address module like the following.

module IntAddress : Sig with type i = int = Wrapper (struct
  type i = int
end)

However, it says

Signature mismatch:
Type declarations do not match: type i is not included in type i = int

It seems I have explicit specified my abstract type i, but why there is a mismatch?

I know this works

module IntAddress : Sig = Wrapper (struct
  type i = int
end)

The point is I want to expose my abstract type i to the outside of this module, so other function can expect this specific type. Without specifying at signature, it seems I cannot achieve it.

The OCaml reference manual comes with a tutorial/introduction on modules, that explains exactly this issue, in its section “4. Functors and type abstraction”. You should give that one a read!

2 Likes

Many big thanks for your reference! I am working on it and will return here and post my solution later!