caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] When functor yields many types - include a module with extra type equality?
@ 2012-04-03 11:23 Dawid Toton
  2012-04-03 12:40 ` Gabriel Scherer
  2012-04-03 17:54 ` Hezekiah M. Carty
  0 siblings, 2 replies; 3+ messages in thread
From: Dawid Toton @ 2012-04-03 11:23 UTC (permalink / raw)
  To: Caml

Consider the following:

--- a.mli
module type B = sig
   type t
end

module type S = sig
   type t
   type other_lenghty_definitions
   val not_much_code : t
end

module Make (B : B) : S
   with type t = B.t
---
--- a.ml
module type B = sig
   type t
end

module type S = sig
   type t
   type other_lenghty_definitions
   val not_much_code : t
end

module Make (B : B) = struct
   type t = B.t
   type other_lenghty_definitions
   let not_much_code = assert false
end
---

So we have to keep 3 copies of other_lenghty_definitions. I can have it 
isolated in a separate file and insert into a.ml and a.mli in an extra 
preprocessing step. But I believe there should be some clean solution, 
something like the following:

--- c_sig.ml
module S = struct
   type t
   type other_lenghty_definitions
end

module type S = sig
   include (module type of S) (* 1 *)
   val not_much_code : t
end
---
--- c.mli
module type B = sig
   type t
end

module Make (B : B) : C_sig.S
   with type t = B.t
---
--- c.ml
module type B = sig
   type t
end

module Make (B : B) = struct
   include C_sig.S
     with type t = B.t (* 2 *)

   let not_much_code = assert false
end
---

How to do what I mark with (* 1 *) and (* 2 *) correctly? Would it help 
if I upgrade my toolchain from 3.11.2 to some more recent version?
Dawid

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] When functor yields many types - include a module with extra type equality?
  2012-04-03 11:23 [Caml-list] When functor yields many types - include a module with extra type equality? Dawid Toton
@ 2012-04-03 12:40 ` Gabriel Scherer
  2012-04-03 17:54 ` Hezekiah M. Carty
  1 sibling, 0 replies; 3+ messages in thread
From: Gabriel Scherer @ 2012-04-03 12:40 UTC (permalink / raw)
  To: Dawid Toton; +Cc: Caml

I learned a clever hack from Jacques Garrigue: recursive modules allow
you to get a module corresponding to a module type. The "module of S"
is
  module rec X : S = X

Using this trick, one can write in c_sig.ml

  module type S = sig
    ...
  end

the usual thing in .mli, and then

  module Make (B : B) = struct
    module rec S_mod : (Sig.S with type t = B.t) = S_mod
    include S_mod
    let not_much_code = assert false
  end

On Tue, Apr 3, 2012 at 1:23 PM, Dawid Toton <d0@wp.pl> wrote:
> Consider the following:
>
> --- a.mli
> module type B = sig
>  type t
> end
>
> module type S = sig
>  type t
>  type other_lenghty_definitions
>  val not_much_code : t
> end
>
> module Make (B : B) : S
>  with type t = B.t
> ---
> --- a.ml
> module type B = sig
>  type t
> end
>
> module type S = sig
>  type t
>  type other_lenghty_definitions
>  val not_much_code : t
> end
>
> module Make (B : B) = struct
>  type t = B.t
>  type other_lenghty_definitions
>  let not_much_code = assert false
> end
> ---
>
> So we have to keep 3 copies of other_lenghty_definitions. I can have it
> isolated in a separate file and insert into a.ml and a.mli in an extra
> preprocessing step. But I believe there should be some clean solution,
> something like the following:
>
> --- c_sig.ml
> module S = struct
>  type t
>  type other_lenghty_definitions
> end
>
> module type S = sig
>  include (module type of S) (* 1 *)
>  val not_much_code : t
> end
> ---
> --- c.mli
> module type B = sig
>  type t
> end
>
> module Make (B : B) : C_sig.S
>  with type t = B.t
> ---
> --- c.ml
> module type B = sig
>  type t
> end
>
> module Make (B : B) = struct
>  include C_sig.S
>    with type t = B.t (* 2 *)
>
>  let not_much_code = assert false
> end
> ---
>
> How to do what I mark with (* 1 *) and (* 2 *) correctly? Would it help if I
> upgrade my toolchain from 3.11.2 to some more recent version?
> Dawid
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] When functor yields many types - include a module with extra type equality?
  2012-04-03 11:23 [Caml-list] When functor yields many types - include a module with extra type equality? Dawid Toton
  2012-04-03 12:40 ` Gabriel Scherer
@ 2012-04-03 17:54 ` Hezekiah M. Carty
  1 sibling, 0 replies; 3+ messages in thread
From: Hezekiah M. Carty @ 2012-04-03 17:54 UTC (permalink / raw)
  To: Dawid Toton; +Cc: Caml

On Tue, Apr 3, 2012 at 7:23 AM, Dawid Toton <d0@wp.pl> wrote:
<trimmed>
> module type S = sig
>  include (module type of S) (* 1 *)
>  val not_much_code : t
> end
<trimmed>
>
> How to do what I mark with (* 1 *) and (* 2 *) correctly? Would it help if I
> upgrade my toolchain from 3.11.2 to some more recent version?
> Dawid
>

You can do (* 1 *) as-written in OCaml 3.12.x although I don't think
the ( ) are necessary.

Hez

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-04-03 17:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-03 11:23 [Caml-list] When functor yields many types - include a module with extra type equality? Dawid Toton
2012-04-03 12:40 ` Gabriel Scherer
2012-04-03 17:54 ` Hezekiah M. Carty

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).