4

Suppose I have the following setup

module type FOO = sig type f val do_foo : f end
module type BAR = sig type b val do_bar : b end


module type FOOANDBAR =
  sig 
    include FOO
    include BAR
  end

Now I want to (in a nice way, aka, without copying the interface and so that FOO and BAR are still subtypes) enforce the restriction that the type f and the type b are the same.

Is there a nice way to do this in OCaml, possibly using some different approach that the include keyword?

thanks!! -Joseph

Joseph Victor
  • 819
  • 6
  • 16

1 Answers1

5
module type FOOANDBAR =
  sig 
    include FOO
    include (BAR with type b = f)
  end
gasche
  • 31,259
  • 3
  • 78
  • 100
  • Gracias! (Today is my second day with OCaml, and this is all very confusing, but that is super easy) – Joseph Victor Dec 16 '12 at 21:00
  • 1
    What is the difference between `include (BAR with type b = f)` and `include (BAR with type b := f)`? The latter one "brutally" substitutes `b` with `f`. – lukstafi Dec 17 '12 at 11:43
  • @lukstafi: w`:=` is a destructive substitution that changes the signature instead of only refining it with equalities. I think either one would be fine in this case, but I used `=` as it is simpler and has been available for a long time, while `:=` was only added in OCaml 3.12. – gasche Dec 17 '12 at 15:00