From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id pA314omn003465 for ; Thu, 3 Nov 2011 02:04:50 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AsQBAHDnsU7RVaE2kGdsb2JhbABEmgqHeQGHbAgiAQEBAQkJDQcUBCGBcgEBAQMBEgIsARsSCwEDAQsGBQsNDSEiAREBBQEKEgYTEgkHh2AIl3EKi1SCYIVePYhwAgUKgzmFTQSCWIUvjA2NOT2EDg X-IronPort-AV: E=Sophos;i="4.69,446,1315173600"; d="scan'208";a="116435048" Received: from mail-fx0-f54.google.com ([209.85.161.54]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 03 Nov 2011 02:04:44 +0100 Received: by faar19 with SMTP id r19so1961720faa.27 for ; Wed, 02 Nov 2011 18:04:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=G/AjEyePlIs3uSlRGCpD/k+xHAHQ60H3EwC7kRlczg8=; b=qxQTofPz1aymb7MRr1TbQa3mqY7c4bdb2lW676SzJ/VvTK4NCyloxyhN3z0hxzxe64 xo8Ju09s3XoN2A/ldakYVBU6QHuvNl9VIIIr6u66NHqXbDbuc+7OB/Wrc96CuWEStF0X 1aBAne6OR2hyxaiZ6AU2ZCvhdqm0bqH85iZl4= MIME-Version: 1.0 Received: by 10.223.76.217 with SMTP id d25mr12238437fak.31.1320282284603; Wed, 02 Nov 2011 18:04:44 -0700 (PDT) Received: by 10.223.6.210 with HTTP; Wed, 2 Nov 2011 18:04:44 -0700 (PDT) In-Reply-To: <4EB1E325.4020207@ens-lyon.org> References: <4EB1E325.4020207@ens-lyon.org> Date: Wed, 2 Nov 2011 19:04:44 -0600 Message-ID: From: Anthony Tavener To: Martin Jambon Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=001517447fae93b95004b0ca2c2d Subject: Re: [Caml-list] Nested module exposing type from parent? --001517447fae93b95004b0ca2c2d Content-Type: text/plain; charset=ISO-8859-1 Aha! That's more like what I was going for, Martin. I didn't realize you could equate types in a chain like that, permitting the definition of structure but also equality to another existing type. That's good stuff. This list, and OCaml, often amaze me... solutions keep getting better. :) On Wed, Nov 2, 2011 at 6:41 PM, Martin Jambon wrote: > On 11/02/2011 12:41 PM, Anthony Tavener wrote: > > I've been struggling with this occasionally... > > > > I'm using nested modules to "open" access to select features of a > > module. My problem is I can't find a way to *expose* types in the parent > > module through such nested modules. > > > > A simplified example of what I'm looking at: > > > > module Vec = struct > > > > type t = { x: int; y: int } > > let make x y = {x;y} > > let add a b = {x=a.x+b.x; y=a.y+b.y} > > > > module Type = > > (* something which has type t = Vec.t, > > * with exposed structure when "open"ed. > > * Also note that Vec is not really an > > * explicit module like this; instead it > > * is implemented in vec.ml *) > > end > > > > Example usage... > > > > let n = Vec.make 2 5 > > open Vec.Type > > let m = {x=1;y=2} > > Vec.add m n > > I hope I understand the problem correctly. > > In order for that code to work, you can do this: > > module Vec = struct > > type t = { x: int; y: int } > let make x y = {x;y} > let add a b = {x=a.x+b.x; y=a.y+b.y} > > module Type = struct > type t' = t = { x: int; y: int } > type t = t' = { x: int; y: int } > (* something which has type t = Vec.t, > * with exposed structure when "open"ed. > * Also note that Vec is not really an > * explicit module like this; instead it > * is implemented in vec.ml *) > end > end > > > Or more simply: > > > module Vec = struct > > module Type = struct > type t = { x: int; y: int } > (* something which has type t = Vec.t, > * with exposed structure when "open"ed. > * Also note that Vec is not really an > * explicit module like this; instead it > * is implemented in vec.ml *) > end > > type t = Type.t = { x: int; y: int } > let make x y = {x;y} > let add a b = {x=a.x+b.x; y=a.y+b.y} > > end > > > Now you can open either Vec or Vec.Type and have direct access to the > record fields. > > > Martin > > -- > 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 > > --001517447fae93b95004b0ca2c2d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Aha! That's more like what I was going for, Martin.

= I didn't realize you could equate types in a chain like that, permittin= g the definition of structure but also equality to another existing type. T= hat's good stuff.

This list, and OCaml, often amaze me... solutions keep = getting better. :)


= On Wed, Nov 2, 2011 at 6:41 PM, Martin Jambon <martin.jambon@ens-lyon.org> wrote:
On 11/02/2011 12:41 PM, A= nthony Tavener wrote:
> I've been struggling with this occasionally...
>
> I'm using nested modules to "open" access to select feat= ures of a
> module. My problem is I can't find a way to *expose* types in the = parent
> module through such nested modules.
>
> A simplified example of what I'm looking at:
>
> =A0 module Vec =3D struct
>
> =A0 =A0 type t =3D { x: int; y: int }
> =A0 =A0 let make x y =3D {x;y}
> =A0 =A0 let add a b =3D {x=3Da.x+b.x; y=3Da.y+b.y}
>
> =A0 =A0 module Type =3D
> =A0 =A0 =A0 (* something which has type t =3D Vec.t,
> =A0 =A0 =A0 =A0* with exposed structure when "open"ed.
> =A0 =A0 =A0 =A0* Also note that Vec is not really an
> =A0 =A0 =A0 =A0* explicit module like this; instead it
> =A0 =A0 =A0 =A0* is implemented in vec.ml <h= ttp://vec.ml> *)
> =A0 end
>
> Example usage...
>
> =A0 let n =3D Vec.make 2 5
> =A0 open Vec.Type
> =A0 let m =3D {x=3D1;y=3D2}
> =A0 Vec.add m n

I hope I understand the problem correctly.

In order for that code to work, you can do this:

module Vec =3D struct

=A0type t =3D { x: int; y: int }
=A0let make x y =3D {x;y}
=A0let add a b =3D {x=3Da.x+b.x; y=3Da.y+b.y}

=A0module Type =3D struct
=A0 =A0type t' =3D t =3D { x: int; y: int }
=A0 =A0type t =3D t' =3D { x: int; y: int }
=A0 =A0(* something which has type t =3D Vec.t,
=A0 =A0 * with exposed structure when "open"ed.
=A0 =A0 * Also note that Vec is not really an
=A0 =A0 * explicit module like this; instead it
=A0 =A0 * is implemented in vec.ml <http://vec.m= l> *)
=A0end
end


Or more simply:


module Vec =3D struct

=A0module Type =3D struct
=A0 =A0type t =3D { x: int; y: int }
=A0 =A0(* something which has type t =3D Vec.t,
=A0 =A0 * with exposed structure when "open"ed.
=A0 =A0 * Also note that Vec is not really an
=A0 =A0 * explicit module like this; instead it
=A0 =A0 * is implemented in vec.ml <http://vec.m= l> *)
=A0end

=A0type t =3D Type.t =3D { x: int; y: int }
=A0let make x y =3D {x;y}
=A0let add a b =3D {x=3Da.x+b.x; y=3Da.y+b.y}

end


Now you can open either Vec or Vec.Type and have direct access to the
record fields.


Martin

--
Caml-list mailing list. =A0Subscription 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


--001517447fae93b95004b0ca2c2d--