From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 70FA27FA6E for ; Mon, 16 May 2016 17:57:55 +0200 (CEST) IronPort-PHdr: 9a23:4IdpDBOjqbwFA2X5AbEl6mtUPXoX/o7sNwtQ0KIMzox0Kf77rarrMEGX3/hxlliBBdydsKIVzbCI+PG8EUU7or+/81k6OKRWUBEEjchE1ycBO+WiTXPBEfjxciYhF95DXlI2t1uyMExSBdqsLwaK+i760zceF13FOBZvIaytQ8iJ35Xxjb75qsGbSj4LrQT+SIs6FA+xowTVu5teqqpZAYF19CH0pGBVcf9d32JiKAHbtR/94sCt4MwrqHwI6LoJvvRNWqTifqk+UacQTHF/azh0t/vQqALbQACTynwZW2QQ2loUUkmWpC39C7jssy/+v/d41WG6MMvwQKkpWDLqu6gtSBLujioeKj8/2GzRhYp7hacdqRnnuh8pkKDOZ4TAEvt1eafUZps9R2dNV457XjYJVoW9ZIwLBvFHOuRVo4C7rVIUhR+zFQiiBaXkzToe1Sy+5rEzz+l0SVKO5wcnBd9b9SmNoQ== Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=nicolas.ojeda.bar@lexifi.com; spf=None smtp.mailfrom=nicolas.ojeda.bar@lexifi.com; spf=None smtp.helo=postmaster@mx20.yaziba.net Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of nicolas.ojeda.bar@lexifi.com) identity=pra; client-ip=85.233.204.164; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.com"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of nicolas.ojeda.bar@lexifi.com) identity=mailfrom; client-ip=85.233.204.164; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.com"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mx20.yaziba.net) identity=helo; client-ip=85.233.204.164; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="postmaster@mx20.yaziba.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0DeAADz7DlXkKTM6VVdDoN+fgEFqHmLd4R3AQ2BdiKFbwKBHwc4FAEBAQEBAQEBEQEBAQEJCwkJIS+CLYIWAQEDARIRBEcLBQsLBAcDFx0CAiISAQUBChIGExIQh3MDDwgECqE3gTE+MYs7jEwDhDEBAQEBBgEBAQEBIopyhz+CWQWGOwyRYIV+iCCCN4xijgMSHoEMAg8PAQGCWYEaPWwBiAUBAQE X-IPAS-Result: A0DeAADz7DlXkKTM6VVdDoN+fgEFqHmLd4R3AQ2BdiKFbwKBHwc4FAEBAQEBAQEBEQEBAQEJCwkJIS+CLYIWAQEDARIRBEcLBQsLBAcDFx0CAiISAQUBChIGExIQh3MDDwgECqE3gTE+MYs7jEwDhDEBAQEBBgEBAQEBIopyhz+CWQWGOwyRYIV+iCCCN4xijgMSHoEMAg8PAQGCWYEaPWwBiAUBAQE X-IronPort-AV: E=Sophos;i="5.24,627,1454972400"; d="scan'208,217";a="218432222" Received: from mx20.yaziba.net ([85.233.204.164]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 16 May 2016 17:57:54 +0200 Received: from mta10.int.yaziba.net (mta10.int.yaziba.net [10.4.20.30]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx20.yaziba.net (mx10.yaziba.net) with ESMTPS id 38ADE1A7469 for ; Mon, 16 May 2016 17:57:54 +0200 (CEST) Received: from mta10.int.yaziba.net (localhost [127.0.0.1]) by mta10.int.yaziba.net (Postfix) with ESMTPS id 522A3CA75B for ; Mon, 16 May 2016 17:57:54 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mta10.int.yaziba.net (Postfix) with ESMTP id 43559CA759 for ; Mon, 16 May 2016 17:57:54 +0200 (CEST) X-Virus-Scanned: amavisd-new at mta10.int.yaziba.net Received: from mta10.int.yaziba.net ([127.0.0.1]) by localhost (mta10.int.yaziba.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id INeKO6SLn_Tk for ; Mon, 16 May 2016 17:57:54 +0200 (CEST) Received: from mail-vk0-f48.google.com (mail-vk0-f48.google.com [209.85.213.48]) by mta10.int.yaziba.net (Postfix) with ESMTPSA id DFF70CA74A for ; Mon, 16 May 2016 17:57:53 +0200 (CEST) Received: by mail-vk0-f48.google.com with SMTP id z184so64604206vkg.0 for ; Mon, 16 May 2016 08:57:53 -0700 (PDT) X-Gm-Message-State: AOPr4FUclybnWfmhPaI+XI0qjPZGAFJ/62jJo8FOci4puLSHnpDSNhAjb/UKU6lejX+4fIpmJja7XCuHPPtZGQ== X-Received: by 10.176.64.4 with SMTP id h4mr13643933uad.3.1463414272713; Mon, 16 May 2016 08:57:52 -0700 (PDT) MIME-Version: 1.0 Received: by 10.159.36.17 with HTTP; Mon, 16 May 2016 08:57:32 -0700 (PDT) In-Reply-To: <1463412482-sup-8520@hennequin-xps> References: <1463412482-sup-8520@hennequin-xps> From: Nicolas Ojeda Bar Date: Mon, 16 May 2016 17:57:32 +0200 X-Gmail-Original-Message-ID: Message-ID: To: Guillaume Hennequin Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=94eb2c124596e270540532f7afc0 X-DRWEB-SCAN: ok X-VRSPAM-SCORE: -51 X-VRSPAM-STATE: legit X-VRSPAM-CAUSE: gggruggvucftvghtrhhoucdtuddrfeekledrvdeigdeliecutefuodetggdotefrucfrrhhofhhilhgvmecuggftfghnshhusghstghrihgsvgdpjgetkgfkueetnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughrpegjfhfhfffkuffvtgesrgdtreertddtjeenucfhrhhomheppfhitgholhgrshcuqfhjvggurgcuuegrrhcuoehnihgtohhlrghsrdhojhgvuggrrdgsrghrsehlvgigihhfihdrtghomheqnecuffhomhgrihhnpehinhhrihgrrdhfrhdphigrhhhoohdrtghomhenucfkphepvddtledrkeehrddvudefrdegkeenucfrrghrrghmpehmohguvgepshhmthhpohhuth X-VRSPAM-EXTCAUSE: mhhouggvpehsmhhtphhouhht Subject: Re: [Caml-list] issue with polymorphism --94eb2c124596e270540532f7afc0 Content-Type: text/plain; charset=UTF-8 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 : 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 > --94eb2c124596e270540532f7afc0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Guillaume,

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

For ex= ample, with objects you could do:

=C2=A0 let print= _both (o : <f: 'a. 'a -> a>) =3D
=C2=A0 =C2=A0 p= rint_int (o # f 1);
=C2=A0 =C2=A0 print_float (o # f 1.0)

Best wishes,
Nicolas

=

On Mon, May 16, 2= 016 at 5:45 PM, Guillaume Hennequin <g.hennequin@eng.cam.ac.uk= > wrote:
Dear caml-list,

let id x =3D x

is polymorphic, and indeed I can apply it to various types:

let _ =3D print_float (id 1.)
let _ =3D print_int (id 1)

Now, say you want to write a function that takes a function of the same [&#= 39;a->'a] type as [id] above, and applies it to two different types:=

let print_both f =3D
=C2=A0print_int (f 1);
=C2=A0print_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 =3D fun f ->
=C2=A0print_int (f 1);
=C2=A0print_float (f 1.0)

As a matter of fact, neither will this:

let print1: 'a. ('a -> 'a) -> unit =3D 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.=C2=A0 Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocam= l_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--94eb2c124596e270540532f7afc0--