caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Maxence Guesdon <Maxence.Guesdon@inria.fr>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Function not polymorphic enough
Date: Fri, 19 Dec 2014 16:33:12 +0100	[thread overview]
Message-ID: <CAPFanBHtHtDC5kZP+xfdOTxAUED3vXKMhXR_2yWTimCpf73k=A@mail.gmail.com> (raw)
In-Reply-To: <20141219143039.425107b3@alcazar2>

My understanding of the issue is that the type of "extend" cannot be
specified without first-class polymorphism (talking about argument
types that are polymorphic, instead of prenex polymorphism at the very
beginning of the function type). extend should take a function of type
('a . ('a -> unit) -> 'a list) -> unit). To do that in OCaml, you
should explicitly take a first-class polymorphic argument (this cannot
be inferred), namely a record or object that has a polymorphic
field/method. Using the polymorphic record approach, this means you
need a definition

  type t = {poly : 'a . ('a -> unit) -> 'a list -> unit}

that is *not* local to your definition, so that users of "extend" can
talk about it. (You could use an object type if you want a structural
rather than nominal type here.)

Using this idea, there is actually no need to define a type locally, a
simple reference on my t will do, which gives the following:

# let (fn, extend) =
  let v = ref { poly = fun _f _li -> assert false  } in
  let fn f li = !v.poly f li in
  let extend g = v := g !v in
  (fn, extend);;
val fn : ('_a -> unit) -> '_a list -> unit = <fun>
val extend : (t -> t) -> unit = <fun>

If you want to non-polymorphic variables to go away, you can reveal
that "fn" is really a t:

# let (fn, extend) =
  let v = ref { poly = fun _f _li -> assert false  } in
  let fn = { poly = fun f li -> !v.poly f li } in
  let extend g = v := g !v in
  (fn, extend);;
val fn : t = {poly = <fun>}
val extend : (t -> t) -> unit = <fun>


On Fri, Dec 19, 2014 at 2:30 PM, Maxence Guesdon
<Maxence.Guesdon@inria.fr> wrote:
> Hello,
>
> With the following code:
>
> let (fn, extend) =
>   let module M = struct
>     type t = { mutable f : 'a. ('a -> unit) -> 'a list -> unit }
>     let v =
>        { f = fun (type a) -> fun (poly_a : a -> unit) -> fun _ ->
>          assert false
>        }
>   end in
>   let fn = fun f x -> M.v.M.f x in
>   let extend g = M.v.M.f <- g M.v.M.f in
>   (fn, extend)
>
> I get the following message about " g M.v.M.f":
> Error: This field value has type ('b -> unit) -> 'b list -> unit
>        which is less general than 'a. ('a -> unit) -> 'a list -> unit
>
> Does anyone know how to make "g M.v.M.f" general enough ?
>
> Regards,
>
> Maxence
>
> --
> 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

  reply	other threads:[~2014-12-19 15:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-19 13:30 Maxence Guesdon
2014-12-19 15:33 ` Gabriel Scherer [this message]
2014-12-19 18:58   ` Maxence Guesdon

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='CAPFanBHtHtDC5kZP+xfdOTxAUED3vXKMhXR_2yWTimCpf73k=A@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=Maxence.Guesdon@inria.fr \
    --cc=caml-list@inria.fr \
    /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).