caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Yaron Minsky <yminsky@janestreet.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: caml-list@inria.fr, Stephen Weeks <sweeks@janestreet.com>,
	 David Powers <dpowers@janestreet.com>,
	Nathan Linger <nlinger@janestreet.com>
Subject: Re: [Caml-list] A confusing example with modules and polymorphic variants
Date: Fri, 19 Oct 2012 21:34:44 -0400	[thread overview]
Message-ID: <CACLX4jTAvbj0j_-=5zF5_CgEej0zKt+6uiftC4vVz7wBJOb12Q@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=EPYaji50_g7o_3s0KJWELD4COUYDpzKRu1E-BXsYEivA@mail.gmail.com>

Thanks!  That all makes perfect sense, now that you say it.  Not sure
why it was so hard for me to figure it out on my own.

One interesting bit is that a very similar example can be made to work
without resorting to these kinds of tricks.  In particular, if we
change the [< `Foo] to [> `Foo], then, we can get the polymorphism we
need back by using a cast:

    module type S = sig
      val z : [> `Foo ]
    end

    let f z =
      let module M : S = struct
        let z = (z : [`Foo] :> [> `Foo])
      end in
      ()

But this doesn't really help in the case I'm after.

y


On Fri, Oct 19, 2012 at 6:10 PM, Jeremy Yallop <yallop@gmail.com> wrote:
> On 19 October 2012 22:18, Yaron Minsky <yminsky@janestreet.com> wrote:
>> We've been running into some troubles with polymorphic variants,
>> modules, and the value restriction, that we're not quite able to
>> unravel.  Here's a stripped down version of the problem.
>>
>>     module type S = sig
>>       val z : [< `Foo ]
>>     end
>>
>>     let f z =
>>       let module M : S = struct let z = z end in ()
>
> I believe the problem here is the monomorphic parameter restriction: the type
> of S requires the field z to be polymorphic, but type inference only assigns
> monomorhpic types to function parameters.  That is, your example doesn't type
> check for essentially the same reason that the following code doesn't type
> check:
>
>     type s = {
>       z : 'a. 'a -> 'a
>     }
>
>     let f z =
>       let s = {
>         z = z
>       } in
>       ()
>
> One way to work around the monomorphic parameter assumption is to wrap the
> parameter in a record with a polymorphic field.
>
>     type z_type = { field : 'a. 'a -> 'a}
>
>     let f z =
>       let s = {
>         z = z.field
>       } in
>       ()
>
> The parameter is still assigned a monomorphic type (z_type), but the projected
> field is polymorphic, as required.
>
> I don't see how to make that work in your case, though, because of the nature
> of the polymorphism -- i.e. it's based on a row variable rather than a
> standard type variable, and there isn't any syntax for quantifying it.
>
> However, you *can* modify your example so that it passes type checking by
> wrapping the argument in a first-class module, since module fields can be
> row-polymorphic.  The easiest way in the cut-down example is to reuse the
> signature you already have:
>
>     module type S = sig
>       val z : [< `Foo ]
>     end
>
>     let f (module Z : S) =
>       let module M : S =
>           struct
>             let z = Z.z
>           end
>       in ()

  reply	other threads:[~2012-10-20  1:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-19 21:18 Yaron Minsky
2012-10-19 22:10 ` Jeremy Yallop
2012-10-20  1:34   ` Yaron Minsky [this message]
2012-10-20  9:16   ` Gabriel Scherer
  -- strict thread matches above, loose matches on Subject: below --
2012-10-19 21:13 Yaron Minsky

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='CACLX4jTAvbj0j_-=5zF5_CgEej0zKt+6uiftC4vVz7wBJOb12Q@mail.gmail.com' \
    --to=yminsky@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=dpowers@janestreet.com \
    --cc=nlinger@janestreet.com \
    --cc=sweeks@janestreet.com \
    --cc=yallop@gmail.com \
    /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).