caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Yaron Minsky <yminsky@janestreet.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 23:10:47 +0100	[thread overview]
Message-ID: <CAAxsn=EPYaji50_g7o_3s0KJWELD4COUYDpzKRu1E-BXsYEivA@mail.gmail.com> (raw)
In-Reply-To: <CACLX4jTDY+Y40vwHAxh-tspRn8FaR+ZaGAE+totOz283c7b+1w@mail.gmail.com>

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-19 22:10 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 [this message]
2012-10-20  1:34   ` Yaron Minsky
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='CAAxsn=EPYaji50_g7o_3s0KJWELD4COUYDpzKRu1E-BXsYEivA@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=dpowers@janestreet.com \
    --cc=nlinger@janestreet.com \
    --cc=sweeks@janestreet.com \
    --cc=yminsky@janestreet.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).