From: Virgile Prevosto <virgile.prevosto@m4x.org>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Why type inference fails in this code
Date: Fri, 9 Oct 2009 10:22:18 +0200 [thread overview]
Message-ID: <20091009102218.639677c8@is010235> (raw)
In-Reply-To: <20091009070935.GA5141@kerneis.info>
Hello,
Le ven. 09 oct. 2009 09:09:35 CEST,
Gabriel Kerneis <kerneis@pps.jussieu.fr> a écrit :
> On Fri, Oct 09, 2009 at 07:25:41AM +0200, rouanvd@softwarerealisations.com wrote:
> > ======================================================
> > type t = MyInt of int | MyFloat of float | MyString of string ;;
> >
> > let foo printerf = function
> > | MyInt i -> printerf string_of_int i
> > | MyFloat x -> printerf string_of_float x
> > | MyString s -> printerf (fun x -> x) s
> > ;;
> > ======================================================
>
> Type-inference has nothing to do with it, it's the type system itself
> which fails.
>
> What is the type you would expect? I guess:
> (forall 'a. ('a -> string) -> 'a -> 'b) -> t -> 'b
>
> This is impossible with Ocaml: you cannot have a universal quantifier,
> type variables are existantially quantified.
>
Well, I'm afraid this the other way round: a polymorphic type in Ocaml
is implicitly universally quantified: (fun x->x : 'a -> 'a) means the
identity function can be applied to an argument of any type, not that
there exists a type 'a on which you can apply it.
In addition, the desired type for printerf is indeed an existential
one (printerf: exists 'a. 'a -> string -> 'a -> 'b): you don't want the
first argument to convert any type to a string, just the type of the
second argument.
A possible but very heavy solution would be to use functors and local
modules everywhere (I guess that Haskell's typeclasses would help here).
type t = MyInt of int | MyFloat of float | MyString of string ;;
module type Printerf=functor(A:sig type t val to_string: t -> string
end) -> sig val print: A.t -> unit
end
module Foo(Printerf:Printerf) = struct
let foo = function
| MyInt i ->
let module M = Printerf(struct type t = int
let to_string = string_of_int end)
in M.print i
| MyFloat x ->
let module M = Printerf(struct type t = float
let to_string = string_of_float end)
in M.print x
| MyString s ->
let module M = Printerf(struct type t = string
let to_string = fun x -> x end)
in M.print s
end
;;
but personally I'd do the same thing as you:
> In you very specific case, though, I guess refactoring it is possible:
> let foo printerf = function
> | MyInt i -> printerf (string_of_int i)
> | MyFloat x -> printerf (string_of_float x)
> | MyString s -> printerf s
> ;;
--
E tutto per oggi, a la prossima volta.
Virgile
next prev parent reply other threads:[~2009-10-09 8:22 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-09 5:25 rouanvd
2009-10-09 6:54 ` [Caml-list] " Vincent Aravantinos
2009-10-09 7:09 ` Gabriel Kerneis
2009-10-09 8:22 ` Virgile Prevosto [this message]
2009-10-09 8:41 ` Gabriel Kerneis
2009-10-09 8:59 ` Gabriel Kerneis
2009-10-09 10:22 ` Jeremy Yallop
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=20091009102218.639677c8@is010235 \
--to=virgile.prevosto@m4x.org \
--cc=caml-list@yquem.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