* [Caml-list] issue with polymorphism @ 2016-05-16 15:45 Guillaume Hennequin 2016-05-16 15:57 ` Gabriel Scherer 2016-05-16 15:57 ` Nicolas Ojeda Bar 0 siblings, 2 replies; 9+ messages in thread From: Guillaume Hennequin @ 2016-05-16 15:45 UTC (permalink / raw) To: caml-list 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 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 15:45 [Caml-list] issue with polymorphism Guillaume Hennequin @ 2016-05-16 15:57 ` Gabriel Scherer 2016-05-16 16:55 ` Guillaume Hennequin 2016-05-16 15:57 ` Nicolas Ojeda Bar 1 sibling, 1 reply; 9+ messages in thread From: Gabriel Scherer @ 2016-05-16 15:57 UTC (permalink / raw) To: Guillaume Hennequin; +Cc: caml users This is possibly, but you have to write it a bit differently. See the FAQ entry, "How to write a function with polymorphic arguments?" https://ocaml.org/learn/faq.html#Howtowriteafunctionwithpolymorphicarguments On Mon, May 16, 2016 at 11:45 AM, 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 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 15:57 ` Gabriel Scherer @ 2016-05-16 16:55 ` Guillaume Hennequin 2016-05-16 17:32 ` Gerd Stolpmann 0 siblings, 1 reply; 9+ messages in thread From: Guillaume Hennequin @ 2016-05-16 16:55 UTC (permalink / raw) To: caml users Thanks all for your prompt answers; > See the FAQ entry, "How to write a function with polymorphic arguments?" > https://ocaml.org/learn/faq.html#Howtowriteafunctionwithpolymorphicarguments this page explains how to do it with records or objects, but ends with a mysterious "FIXME: a direct way now exists". Does anyone know what this might refer to? best wishes, Guillaume ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 16:55 ` Guillaume Hennequin @ 2016-05-16 17:32 ` Gerd Stolpmann 2016-05-16 18:49 ` Yotam Barnoy 0 siblings, 1 reply; 9+ messages in thread From: Gerd Stolpmann @ 2016-05-16 17:32 UTC (permalink / raw) To: Guillaume Hennequin; +Cc: caml users [-- Attachment #1: Type: text/plain, Size: 1042 bytes --] Am Montag, den 16.05.2016, 17:55 +0100 schrieb Guillaume Hennequin: > Thanks all for your prompt answers; > > > See the FAQ entry, "How to write a function with polymorphic arguments?" > > https://ocaml.org/learn/faq.html#Howtowriteafunctionwithpolymorphicarguments > > this page explains how to do it with records or objects, but ends with a > mysterious "FIXME: a direct way now exists". Does anyone know what this might > refer to? What you would need is let print_both : ('a . 'a -> 'a) -> unit = ... i.e. the scope of the quantifier is restricted to the first argument. This doesn't exist to my knowledge. Maybe it was part of some dev version? Gerd -- ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de My OCaml site: http://www.camlcity.org Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de ------------------------------------------------------------ [-- Attachment #2: This is a digitally signed message part --] [-- Type: application/pgp-signature, Size: 473 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 17:32 ` Gerd Stolpmann @ 2016-05-16 18:49 ` Yotam Barnoy 2016-05-16 18:53 ` Gregory Malecha 0 siblings, 1 reply; 9+ messages in thread From: Yotam Barnoy @ 2016-05-16 18:49 UTC (permalink / raw) To: Gerd Stolpmann; +Cc: Guillaume Hennequin, caml users [-- Attachment #1: Type: text/plain, Size: 1245 bytes --] Paging the type experts -- is there a concrete reason why we disallow writing this type directly? On Mon, May 16, 2016 at 1:32 PM, Gerd Stolpmann <info@gerd-stolpmann.de> wrote: > Am Montag, den 16.05.2016, 17:55 +0100 schrieb Guillaume Hennequin: > > Thanks all for your prompt answers; > > > > > See the FAQ entry, "How to write a function with polymorphic > arguments?" > > > > https://ocaml.org/learn/faq.html#Howtowriteafunctionwithpolymorphicarguments > > > > this page explains how to do it with records or objects, but ends with a > > mysterious "FIXME: a direct way now exists". Does anyone know what this > might > > refer to? > > What you would need is > > let print_both : ('a . 'a -> 'a) -> unit = ... > > i.e. the scope of the quantifier is restricted to the first argument. > This doesn't exist to my knowledge. Maybe it was part of some dev > version? > > Gerd > -- > ------------------------------------------------------------ > Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de > My OCaml site: http://www.camlcity.org > Contact details: http://www.camlcity.org/contact.html > Company homepage: http://www.gerd-stolpmann.de > ------------------------------------------------------------ > > [-- Attachment #2: Type: text/html, Size: 2167 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 18:49 ` Yotam Barnoy @ 2016-05-16 18:53 ` Gregory Malecha 2016-05-16 19:04 ` Yotam Barnoy 0 siblings, 1 reply; 9+ messages in thread From: Gregory Malecha @ 2016-05-16 18:53 UTC (permalink / raw) To: Yotam Barnoy, Gerd Stolpmann; +Cc: Guillaume Hennequin, caml users [-- Attachment #1: Type: text/plain, Size: 1679 bytes --] There may other reasons as well, e.g. performance and compilability, but the main reason that I know is that inferring these types is difficult (undecideable in general). For a point of comparison, GHC supports types like this, but only when they are explicitly written. On Mon, May 16, 2016, 11:50 AM Yotam Barnoy <yotambarnoy@gmail.com> wrote: > Paging the type experts -- is there a concrete reason why we disallow > writing this type directly? > > On Mon, May 16, 2016 at 1:32 PM, Gerd Stolpmann <info@gerd-stolpmann.de> > wrote: > >> Am Montag, den 16.05.2016, 17:55 +0100 schrieb Guillaume Hennequin: >> > Thanks all for your prompt answers; >> > >> > > See the FAQ entry, "How to write a function with polymorphic >> arguments?" >> > > >> https://ocaml.org/learn/faq.html#Howtowriteafunctionwithpolymorphicarguments >> > >> > this page explains how to do it with records or objects, but ends with a >> > mysterious "FIXME: a direct way now exists". Does anyone know what this >> might >> > refer to? >> >> What you would need is >> >> let print_both : ('a . 'a -> 'a) -> unit = ... >> >> i.e. the scope of the quantifier is restricted to the first argument. >> This doesn't exist to my knowledge. Maybe it was part of some dev >> version? >> >> Gerd >> -- >> ------------------------------------------------------------ >> Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de >> My OCaml site: http://www.camlcity.org >> Contact details: http://www.camlcity.org/contact.html >> Company homepage: http://www.gerd-stolpmann.de >> ------------------------------------------------------------ >> >> > -- - gregory malecha gmalecha.github.io [-- Attachment #2: Type: text/html, Size: 2886 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 18:53 ` Gregory Malecha @ 2016-05-16 19:04 ` Yotam Barnoy 2016-05-16 19:09 ` Gregory Malecha 0 siblings, 1 reply; 9+ messages in thread From: Yotam Barnoy @ 2016-05-16 19:04 UTC (permalink / raw) To: Gregory Malecha; +Cc: Gerd Stolpmann, Guillaume Hennequin, caml users [-- Attachment #1: Type: text/plain, Size: 1944 bytes --] Right, but they can't be inferred on objects and records either, and yet they're allowed there... so what was the rationale? On Mon, May 16, 2016 at 2:53 PM, Gregory Malecha <gmalecha@gmail.com> wrote: > There may other reasons as well, e.g. performance and compilability, but > the main reason that I know is that inferring these types is difficult > (undecideable in general). For a point of comparison, GHC supports types > like this, but only when they are explicitly written. > > On Mon, May 16, 2016, 11:50 AM Yotam Barnoy <yotambarnoy@gmail.com> wrote: > >> Paging the type experts -- is there a concrete reason why we disallow >> writing this type directly? >> >> On Mon, May 16, 2016 at 1:32 PM, Gerd Stolpmann <info@gerd-stolpmann.de> >> wrote: >> >>> Am Montag, den 16.05.2016, 17:55 +0100 schrieb Guillaume Hennequin: >>> > Thanks all for your prompt answers; >>> > >>> > > See the FAQ entry, "How to write a function with polymorphic >>> arguments?" >>> > > >>> https://ocaml.org/learn/faq.html#Howtowriteafunctionwithpolymorphicarguments >>> > >>> > this page explains how to do it with records or objects, but ends with >>> a >>> > mysterious "FIXME: a direct way now exists". Does anyone know what >>> this might >>> > refer to? >>> >>> What you would need is >>> >>> let print_both : ('a . 'a -> 'a) -> unit = ... >>> >>> i.e. the scope of the quantifier is restricted to the first argument. >>> This doesn't exist to my knowledge. Maybe it was part of some dev >>> version? >>> >>> Gerd >>> -- >>> ------------------------------------------------------------ >>> Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de >>> My OCaml site: http://www.camlcity.org >>> Contact details: http://www.camlcity.org/contact.html >>> Company homepage: http://www.gerd-stolpmann.de >>> ------------------------------------------------------------ >>> >>> >> -- > > - gregory malecha > gmalecha.github.io > [-- Attachment #2: Type: text/html, Size: 3528 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 19:04 ` Yotam Barnoy @ 2016-05-16 19:09 ` Gregory Malecha 0 siblings, 0 replies; 9+ messages in thread From: Gregory Malecha @ 2016-05-16 19:09 UTC (permalink / raw) To: Yotam Barnoy; +Cc: Gerd Stolpmann, Guillaume Hennequin, caml users [-- Attachment #1: Type: text/plain, Size: 2592 bytes --] I'm not 100% familiar with the object system, but you can write them on records. type 'a stream = { fold : 'b . ('a -> 'b -> 'b) -> 'b -> 'b } The reason that writing them here does not break inference is that the inference mechanism knows that it needs to insert the universally quantified type at the position of the record constructor, i.e. when it sees { foo = X } it knows that it should introduce the quantifier there. This makes it syntax-directed. On Mon, May 16, 2016 at 12:04 PM, Yotam Barnoy <yotambarnoy@gmail.com> wrote: > Right, but they can't be inferred on objects and records either, and yet > they're allowed there... so what was the rationale? > > On Mon, May 16, 2016 at 2:53 PM, Gregory Malecha <gmalecha@gmail.com> > wrote: > >> There may other reasons as well, e.g. performance and compilability, but >> the main reason that I know is that inferring these types is difficult >> (undecideable in general). For a point of comparison, GHC supports types >> like this, but only when they are explicitly written. >> >> On Mon, May 16, 2016, 11:50 AM Yotam Barnoy <yotambarnoy@gmail.com> >> wrote: >> >>> Paging the type experts -- is there a concrete reason why we disallow >>> writing this type directly? >>> >>> On Mon, May 16, 2016 at 1:32 PM, Gerd Stolpmann <info@gerd-stolpmann.de> >>> wrote: >>> >>>> Am Montag, den 16.05.2016, 17:55 +0100 schrieb Guillaume Hennequin: >>>> > Thanks all for your prompt answers; >>>> > >>>> > > See the FAQ entry, "How to write a function with polymorphic >>>> arguments?" >>>> > > >>>> https://ocaml.org/learn/faq.html#Howtowriteafunctionwithpolymorphicarguments >>>> > >>>> > this page explains how to do it with records or objects, but ends >>>> with a >>>> > mysterious "FIXME: a direct way now exists". Does anyone know what >>>> this might >>>> > refer to? >>>> >>>> What you would need is >>>> >>>> let print_both : ('a . 'a -> 'a) -> unit = ... >>>> >>>> i.e. the scope of the quantifier is restricted to the first argument. >>>> This doesn't exist to my knowledge. Maybe it was part of some dev >>>> version? >>>> >>>> Gerd >>>> -- >>>> ------------------------------------------------------------ >>>> Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de >>>> My OCaml site: http://www.camlcity.org >>>> Contact details: http://www.camlcity.org/contact.html >>>> Company homepage: http://www.gerd-stolpmann.de >>>> ------------------------------------------------------------ >>>> >>>> >>> -- >> >> - gregory malecha >> gmalecha.github.io >> > > -- gregory malecha gmalecha.github.io [-- Attachment #2: Type: text/html, Size: 4722 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] issue with polymorphism 2016-05-16 15:45 [Caml-list] issue with polymorphism Guillaume Hennequin 2016-05-16 15:57 ` Gabriel Scherer @ 2016-05-16 15:57 ` Nicolas Ojeda Bar 1 sibling, 0 replies; 9+ messages in thread From: Nicolas Ojeda Bar @ 2016-05-16 15:57 UTC (permalink / raw) To: Guillaume Hennequin; +Cc: caml-list [-- 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 --] ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2016-05-16 19:10 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2016-05-16 15:45 [Caml-list] issue with polymorphism 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox