caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: Yaron Minsky <yminsky@janestreet.com>,
	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: Sat, 20 Oct 2012 11:16:04 +0200	[thread overview]
Message-ID: <CAPFanBFAhWwbkWpiwwm8S4fffvQ2hqzkW8YLU5dWnjXZit5HRA@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=EPYaji50_g7o_3s0KJWELD4COUYDpzKRu1E-BXsYEivA@mail.gmail.com>

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

You can capture the whole type "as" the polymorphic variable.

  type s = { z : 'a . [< `Foo ] as 'a }

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

  let f s =
    let module M : S = struct
      let z = s.z
    end in ()

This is essentially the same as your solution, using a polymorphic
field in a record instead of a polymorphic structure item in a
first-class module. I'm not exactly sure if/why this would solve or
not Yaron's problem, but in any regard I would consider the two
solutions equivalent.

On Sat, Oct 20, 2012 at 12:10 AM, 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 ()
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  parent reply	other threads:[~2012-10-20  9:16 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
2012-10-20  9:16   ` Gabriel Scherer [this message]
  -- 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=CAPFanBFAhWwbkWpiwwm8S4fffvQ2hqzkW8YLU5dWnjXZit5HRA@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=dpowers@janestreet.com \
    --cc=nlinger@janestreet.com \
    --cc=sweeks@janestreet.com \
    --cc=yallop@gmail.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).