caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Record fields not reexported from functors.
@ 2015-05-10 21:58 Jordan W
  2015-05-10 23:19 ` Jacques Garrigue
  2015-05-11 17:24 ` Leo White
  0 siblings, 2 replies; 3+ messages in thread
From: Jordan W @ 2015-05-10 21:58 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1451 bytes --]

I believe there is an issue with record fields and functors.
It appears that while types are correctly exported from functors, the
corresponding record field scopes are not. Here is an example.

   module type HasType = sig
      type t
    end

    module EchoModule (Input:HasType): (HasType with type t = Input.t) =
(struct
      type t = Input.t
    end)

    module ClonedModuleWithRecordType = EchoModule (struct
      type t = {innerRecordField: int}
    end)

    (* This doesn't type check, but I can't see why it shouldn't. Record
fields'
       scopes should be reexported just like the types themselves.

      let myRecord: ClonedModuleWithRecordType.t = {
        ClonedModuleWithRecordType.innerRecordField=10;
      }

      In this case, the original "scope" of the record fields is "lost"
      because the record type was declared in an anonymous module
expression.  I
      believe that it *should* be "lost" as it is, but then reexported as an
      identical type in the module returned from the functor (identically
because
      of the "with constraint").

    *)

    module ClonedModuleWithNonRecordType = EchoModule (struct
      type t = string list
    end)

    (* This typechecks perfectly fine! Evidence that types are reexported,
but not
       the "scope" of record labels *)
    let myRecord: ClonedModuleWithNonRecordType.t = ["asdf"; "fff"]


Is anyone aware of whether or not this issue is already being tracked?

[-- Attachment #2: Type: text/html, Size: 2058 bytes --]

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

* Re: [Caml-list] Record fields not reexported from functors.
  2015-05-10 21:58 [Caml-list] Record fields not reexported from functors Jordan W
@ 2015-05-10 23:19 ` Jacques Garrigue
  2015-05-11 17:24 ` Leo White
  1 sibling, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2015-05-10 23:19 UTC (permalink / raw)
  To: Jordan W; +Cc: OCaML List Mailing

On 2015/05/11 06:58, Jordan W wrote:
> 
> I believe there is an issue with record fields and functors.
> It appears that while types are correctly exported from functors, the corresponding record field scopes are not. Here is an example.
> 
>    module type HasType = sig
>       type t
>     end
> 
>     module EchoModule (Input:HasType): (HasType with type t = Input.t) = (struct
>       type t = Input.t
>     end)
> 
>     module ClonedModuleWithRecordType = EchoModule (struct
>       type t = {innerRecordField: int}
>     end)
> 
>     (* This doesn't type check, but I can't see why it shouldn't. Record fields'
>        scopes should be reexported just like the types themselves.
> 
>       let myRecord: ClonedModuleWithRecordType.t = {
>         ClonedModuleWithRecordType.innerRecordField=10;
>       }

There is a misunderstanding here: the declaration “type t = Input.t” does not
re-export Input.t, it just defines a type abbreviation pointing to Input.t.
A type abbreviation does not contain field information.
You should qualify the field name with the original module name; unfortunately
there is no such module here since you defined the module inline.
Here is the type you get for ClonedModuleWithRecordType:
   module ClonedModuleWithRecordType : sig type t end
Since there is no module to refer to, all information about the type is lost.

The solution is to properly define the module argument:

   module MyRecord = struct type t = {innerRecordField: int} end
   module ClonedModuleWithRecordType = EchoModule (MyRecord)
   … {MyRecord.innerRecordField = 10 } …

which gives you:

   module ClonedModuleWithRecordType : sig type t = MyRecord.t end

Note that since 4.02 you can also omit the module name when the type is known.
But again, this will only work if there is a module name.

Jacques Garrigue

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

* Re: [Caml-list] Record fields not reexported from functors.
  2015-05-10 21:58 [Caml-list] Record fields not reexported from functors Jordan W
  2015-05-10 23:19 ` Jacques Garrigue
@ 2015-05-11 17:24 ` Leo White
  1 sibling, 0 replies; 3+ messages in thread
From: Leo White @ 2015-05-11 17:24 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2316 bytes --]

To expand slightly on Jacques' answer. Record fields (and variant
constructors) must be explicitly exported, using the:

type t = Foo.t = { bar : int }

syntax.

For example, the following version of your example works:

module type HasType = sig type t = { innerRecordField: int } end

module EchoModule (Input : HasType) : HasType with type t = Input.t =
struct type t = Input.t = {innerRecordField: int} end

module ClonedModuleWithRecordType = EchoModule(struct type t =
{innerRecordField: int} end)

let myRecord: ClonedModuleWithRecordType.t = {
ClonedModuleWithRecordType.innerRecordField=10; }

Of course, the `EchoModule` functor can now only operate on modules
where the type t is defined as:

type t = { innerRecordField : int }

Jacques' solution of giving the functor argument a name, and using that
name to access the field, is a much more flexible approach.

Regards,

Leo

On Sun, 10 May 2015, at 05:58 PM, Jordan W wrote:
> I believe there is an issue with record fields and functors. It
> appears that while types are correctly exported from functors, the
> corresponding record field scopes are not. Here is an example.
>
> module type HasType = sig type t end
>
> module EchoModule (Input:HasType): (HasType with type t = Input.t) =
> (struct type t = Input.t end)
>
> module ClonedModuleWithRecordType = EchoModule (struct type t =
> {innerRecordField: int} end)
>
> (* This doesn't type check, but I can't see why it shouldn't. Record
> fields' scopes should be reexported just like the types themselves.
>
> let myRecord: ClonedModuleWithRecordType.t = {
> ClonedModuleWithRecordType.innerRecordField=10; }
>
> In this case, the original "scope" of the record fields is "lost"
> because the record type was declared in an anonymous module
> expression. I believe that it *should* be "lost" as it is, but then
> reexported as an identical type in the module returned from the
> functor (identically because of the "with constraint").
>
> *)
>
> module ClonedModuleWithNonRecordType = EchoModule (struct type t =
> string list end)
>
> (* This typechecks perfectly fine! Evidence that types are reexported,
> but not the "scope" of record labels *) let myRecord:
> ClonedModuleWithNonRecordType.t = ["asdf"; "fff"]
>
>
> Is anyone aware of whether or not this issue is already being tracked?
>


[-- Attachment #2: Type: text/html, Size: 4263 bytes --]

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

end of thread, other threads:[~2015-05-11 17:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-10 21:58 [Caml-list] Record fields not reexported from functors Jordan W
2015-05-10 23:19 ` Jacques Garrigue
2015-05-11 17:24 ` Leo White

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