caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Nicolas Ojeda Bar <nicolas.ojeda.bar@lexifi.com>
To: Guillaume Hennequin <g.hennequin@eng.cam.ac.uk>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] issue with polymorphism
Date: Mon, 16 May 2016 17:57:32 +0200	[thread overview]
Message-ID: <CADK7aFOa8KpSGTd=6jgv3hNqsC2QvNG12wqSn=k-kKEni+0xDQ@mail.gmail.com> (raw)
In-Reply-To: <1463412482-sup-8520@hennequin-xps>

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

Hi Guillaume,

You need to use either records or objects to pass a polymorphic argument
(note that the type you want for print_both is ('a. 'a -> 'a) -> unit, not
'a. ('a -> 'a) -> unit).

For example, with objects you could do:

  let print_both (o : <f: 'a. 'a -> a>) =
    print_int (o # f 1);
    print_float (o # f 1.0)

Best wishes,
Nicolas


On Mon, May 16, 2016 at 5:45 PM, Guillaume Hennequin <
g.hennequin@eng.cam.ac.uk> wrote:

> Dear caml-list,
>
> let id x = x
>
> is polymorphic, and indeed I can apply it to various types:
>
> let _ = print_float (id 1.)
> let _ = print_int (id 1)
>
> Now, say you want to write a function that takes a function of the same
> ['a->'a] type as [id] above, and applies it to two different types:
>
> let print_both f =
>  print_int (f 1);
>  print_float (f 1.0)
>
> That in fact won't compile:
>
> Error: This expression (1.0) has type float but an expression was expected
> of int
>
> Naively trying to enforce polymorphism doesn't work either:
>
> let print_both: 'a. ('a -> 'a) -> unit = fun f ->
>  print_int (f 1);
>  print_float (f 1.0)
>
> As a matter of fact, neither will this:
>
> let print1: 'a. ('a -> 'a) -> unit = fun f -> print_int (f 1)
>
> Error: This definition has type (int -> int) -> unit
> which is less general than 'a. ('a -> 'a) -> unit
>
> What am I missing? How would you go about writing such a function?
>
> Many thanks,
> Guillaume
>
> --
> 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
>

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

      parent reply	other threads:[~2016-05-16 15:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-16 15:45 Guillaume Hennequin
2016-05-16 15:57 ` Gabriel Scherer
2016-05-16 16:55   ` Guillaume Hennequin
2016-05-16 17:32     ` Gerd Stolpmann
2016-05-16 18:49       ` Yotam Barnoy
2016-05-16 18:53         ` Gregory Malecha
2016-05-16 19:04           ` Yotam Barnoy
2016-05-16 19:09             ` Gregory Malecha
2016-05-16 15:57 ` Nicolas Ojeda Bar [this message]

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='CADK7aFOa8KpSGTd=6jgv3hNqsC2QvNG12wqSn=k-kKEni+0xDQ@mail.gmail.com' \
    --to=nicolas.ojeda.bar@lexifi.com \
    --cc=caml-list@inria.fr \
    --cc=g.hennequin@eng.cam.ac.uk \
    /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).