caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: SEROT Jocelyn <Jocelyn.SEROT@univ-bpclermont.fr>
To: Leo White <lpw25@cam.ac.uk>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Question on functors (again...)
Date: Tue, 21 Jan 2014 12:09:54 +0100	[thread overview]
Message-ID: <20140121120954.Horde.QQKslk0F5ldpqozcxKyq3g1@wmail.univ-bpclermont.fr> (raw)
In-Reply-To: <87ppnm5q2x.fsf@study.localdomain>

Thanks for your answer, Leo.

Leo White <lpw25@cam.ac.uk> a écrit :
>

> You should add the constraints to the `SET` module type itself:
>
>     module type SET = sig
>       type t
>       type elt
>       module Elt : sig type t = elt end
>       val choose : t -> elt
>       val singleton : elt -> t
>     end

Unfortunately, this is not possible since the ELT signature is  
actually more complex than just 'sig type t end'.
To illustrate this, without showing the original code (which, as you  
guessed it, has some extra and maybe unrelated complexity), i've tried  
to reuse your example, by simply changing

module type ELT = sig type t end

by

module type ELT = sig type t val string_of: t -> string end

Here's my attempt :

module type ELT =
sig
   type t
   val string_of: t -> string
end

module type SET = sig
   type t
   type elt
   module Elt : ELT
   val choose: t -> elt
   val singleton: elt -> t
   val string_of: t -> string
end

module Make (E : ELT) : SET with module Elt = E and type elt = E.t = struct
   type elt = E.t
   type t = elt list
   module Elt = E
   let choose s = List.hd s
   let singleton e = [e]
   let string_of s = "{" ^ E.string_of (choose s) ^ "}"
end

module MakePair (E1: ELT) (E2: ELT) : sig
   include ELT with type t = E1.t * E2.t
   val product : E1.t -> E2.t -> t
end = struct
   type t = E1.t * E2.t
   let product x y = x,y
   let string_of (x,y) = "(" ^ E1.string_of x ^ "," ^ E2.string_of y ^ ")"
end

module MakeProduct (S1: SET) (S2: SET) : sig
   include SET with type elt = S1.elt * S2.elt
   val product : S1.t -> S2.t -> t
end = struct
   module P = MakePair(S1.Elt)(S2.Elt)
   module S = Make(P)
   include S
   let product s1 s2 = S.singleton (P.product (S1.choose s1) (S2.choose s2))
end

Again, the compiler chokes on the definition of MakeProduct.product,  
saying, for "S1.choose s1", that :

    "This expression has type S1.elt but an expression was expected of  
type S1.Elt.t"

I understand your comment :

> The compiler doesn't know that your `S1` and `S2` arguments have been
> made with the `Make` functor so it cannot assume those constraints.

so, let's add these constraints directly to the functor arguments :

module MakeProduct (S1: SET with type elt = Elt.t) (S2: SET with type  
elt = Elt.t) : sig

but then, we get :

"  module MakeProduct (S1: SET with type elt = Elt.t) (S2: SET) : sig
                                               ^^^^^ Error: Unbound module Elt"

??? The Elt module is part of the SET signature, isn't it ?

Jocelyn





  reply	other threads:[~2014-01-21 11:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-20 16:34 Jocelyn Sérot
2014-01-20 20:57 ` Leo White
2014-01-21 11:09   ` SEROT Jocelyn [this message]
2014-01-21 11:30     ` Josh Berdine
2014-01-21 14:30       ` SEROT Jocelyn
2014-01-21 13:32     ` Leo White

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140121120954.Horde.QQKslk0F5ldpqozcxKyq3g1@wmail.univ-bpclermont.fr \
    --to=jocelyn.serot@univ-bpclermont.fr \
    --cc=caml-list@inria.fr \
    --cc=lpw25@cam.ac.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).