From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 0ED14BBAF for ; Fri, 9 Oct 2009 09:09:38 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhUFAKd+zkrVujhf/2dsb2JhbACBUdh4hCoE X-IronPort-AV: E=Sophos;i="4.44,530,1249250400"; d="scan'208";a="48265336" Received: from witko.kerneis.info ([213.186.56.95]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-SHA; 09 Oct 2009 09:09:37 +0200 Received: from alf94-4-82-224-79-17.fbx.proxad.net ([82.224.79.17] helo=hokahe.kerneis.info) by witko.kerneis.info with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1Mw9bc-00052I-Gn; Fri, 09 Oct 2009 09:09:36 +0200 Received: from gabriel by hokahe.kerneis.info with local (Exim 4.69) (envelope-from ) id 1Mw9bb-0001Pl-Qs; Fri, 09 Oct 2009 09:09:35 +0200 Date: Fri, 9 Oct 2009 09:09:35 +0200 From: Gabriel Kerneis To: rouanvd@softwarerealisations.com Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Why type inference fails in this code Message-ID: <20091009070935.GA5141@kerneis.info> Mail-Followup-To: rouanvd@softwarerealisations.com, caml-list@yquem.inria.fr References: <53322.41.177.16.252.1255065941.squirrel@41.177.16.252> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <53322.41.177.16.252.1255065941.squirrel@41.177.16.252> User-Agent: Mutt/1.5.20 (2009-06-14) X-SA-Exim-Connect-IP: 82.224.79.17 X-SA-Exim-Mail-From: gabriel@kerneis.info X-SA-Exim-Scanned: No (on witko.kerneis.info); SAEximRunCond expanded to false X-Spam: no; 0.00; inference:01 0200,:01 foo:01 forall:01 ocaml:01 quantifier:01 quantified:01 workarounds:01 ocaml:01 foo:01 2009:98 wrote:01 pps:01 caml-list:01 jussieu:01 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