"ocaml_beginners"::[] Help with functors, universe and everything

View: New views
3 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] Help with functors, universe and everything

by rixed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Let's say we have these modules types :

# module type VTYPE = sig type t val value : t end;;
module type VTYPE = sig type t val value : t end
# module type PTYPE = sig module V : VTYPE val foo : V.t -> int end;;
module type PTYPE = sig module V : VTYPE val foo : V.t -> int end

And a functor to build PTYPEs from VTYPEs :

# module MAKE_PTYPE (V_:VTYPE) : PTYPE = struct module V=V_ let foo v = 1 end;;
module MAKE_PTYPE : functor (V_ : VTYPE) -> PTYPE

Now build a simple V :

# module V : VTYPE = struct type t=string let value="bar" end;;
module V : VTYPE

And a simple P that uses it :

# module P = MAKE_PTYPE(V);;
module P :
  sig
    module V : sig type t = MAKE_PTYPE(V).V.t val value : t end
    val foo : V.t -> int
  end

So we do know that P.V.t is the same as V.t, but apparently OCaml don't :

# P.foo V.value;;
        ^^^^^^^
Error: This expression has type V.t but an expression was expected of type
         P.V.t = MAKE_PTYPE(V).V.t

Of course, 'P.foo P.V.value' works, but what I really would like to do is 'P.foo V.value',
or even better : 'P.foo "a_string"'

Is there a work around this ?


"ocaml_beginners"::[] Re: Help with functors, universe and everything

by Vincent Aravantinos :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



--- In ocaml_beginners@..., rixed@... wrote:
> # module MAKE_PTYPE (V_:VTYPE) : PTYPE = struct module V=V_ let foo v = 1 end;;

Try this one instead:
# module MAKE_PTYPE (V_:VTYPE) : PTYPE with type V.t = V_.t = struct module V=V_ let foo v = 1 end;;

Ocaml does not make the inference for you since in other cases you may want to abstract away from V_, and hide how P was created.  Thus you have to tell it explicitely.

V.


Re: "ocaml_beginners"::[] Re: Help with functors, universe and everything

by rixed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Try this one instead:
> # module MAKE_PTYPE (V_:VTYPE) : PTYPE with type V.t = V_.t = struct module V=V_ let foo v = 1 end;;
>
> Ocaml does not make the inference for you since in other cases you may want to abstract away from V_, and hide how P was created.  Thus you have to tell it explicitely.

It works and it makes sense.
Thank you !