caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Kerneis <kerneis@pps.jussieu.fr>
To: rouanvd@softwarerealisations.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Why type inference fails in this code
Date: Fri, 9 Oct 2009 09:09:35 +0200	[thread overview]
Message-ID: <20091009070935.GA5141@kerneis.info> (raw)
In-Reply-To: <53322.41.177.16.252.1255065941.squirrel@41.177.16.252>

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.

> I read that there are lots of workarounds for the above code snippet,
> but I would like to Know why this code fails and what is the best
> workaround for this type of code.

You have many ways to emulate universal types in Ocaml (objects,
records) and browsing the archives of this list should provide many good
examples.

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
;;

(depending on what your printerf function does, but I can't find any
counter-example)

Regards,
-- 
Gabriel Kerneis


  parent reply	other threads:[~2009-10-09  7:09 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 [this message]
2009-10-09  8:22   ` Virgile Prevosto
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=20091009070935.GA5141@kerneis.info \
    --to=kerneis@pps.jussieu.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=rouanvd@softwarerealisations.com \
    /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).