I have a quick question regarding type abstraction when dealing with functors. Why is it that we have to repeat ‘endpoint = Endpoint.t’ in the following example (taken from the real world ocaml book)? I understand the use of the ‘with type’, but once we have written it, why is it also necessary to say ‘type endpoint = Endpoint.t’ in the struct body? Is it only to satisfy the fact that we have declared a ‘type endpoint’ in the ‘Interval_intf’ which then must be satisfied by the functor?
module Make_interval(Endpoint : Comparable) : (Interval_intf with type endpoint = Endpoint.t) = struct type endpoint = Endpoint.t (* Why is this necessary? *) type t = | Interval of Endpoint.t * Endpoint.t | Empty (* Skipping the rest of the implementation*) end ;; module type Interval_intf = sig type t type endpoint val create : endpoint -> endpoint -> t val is_empty : t -> bool val contains : t -> endpoint -> bool val intersect : t -> t -> t end ;;
I had a look at the reference manual, but I still can’t understand. I know that destructive substitution gets rid of this duplication, but I just wanted to know exactly why the duplication is necessary.