From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p76Cx70f022411 for ; Sat, 6 Aug 2011 14:59:07 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AocGAEQ5PU5KfVK2kGdsb2JhbABChEeUHINZiyYIFAEBAQEJCQ0HFAQhgUABAQEBAxICDwQZATgBAwwBBQUYCSECAg8CIwEFARwTAQUCAQEeh02hdwqLcYMVhDKJKAIDBoUwgRAEjgKFAIUIgSaGIjyBP4If X-IronPort-AV: E=Sophos;i="4.67,328,1309730400"; d="vcf'?scan'208";a="104840283" Received: from mail-wy0-f182.google.com ([74.125.82.182]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 06 Aug 2011 14:59:05 +0200 Received: by wyg24 with SMTP id 24so4686306wyg.27 for ; Sat, 06 Aug 2011 05:59:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:x-enigmail-version:content-type; bh=f6vDLh1knHQjHkIdfeog8kXKa9mcWzTK4OCyzzP5Rz4=; b=s6XvPgxvhxovqDoGxvdYmDkt2OIdvJ8xmDdOmtirD4550bSpqxBx7WuVJGMsGRzU1m nvucV52pCPZ0Pi3/h4ns0gs8BPdffaMVlaeh69+ofhXzCjT6PD3JSrCpnQta195EY5O7 MES/W3wI3oSA7+/sHz1GoOCBdkapzTlncBOfE= Received: by 10.216.221.33 with SMTP id q33mr1426846wep.90.1312635545175; Sat, 06 Aug 2011 05:59:05 -0700 (PDT) Received: from [192.168.0.3] (lns-bzn-48f-81-56-241-169.adsl.proxad.net [81.56.241.169]) by mx.google.com with ESMTPS id s12sm2399713wec.8.2011.08.06.05.59.02 (version=SSLv3 cipher=OTHER); Sat, 06 Aug 2011 05:59:04 -0700 (PDT) Sender: Fabrice Le Fessant Message-ID: <4E3D3A89.90007@inria.fr> Date: Sat, 06 Aug 2011 14:58:49 +0200 From: Fabrice Le Fessant User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 To: caml-list@inria.fr CC: rixed@happyleptic.org References: <20110806125021.GB10154@ombreroze.happyleptic.org> In-Reply-To: <20110806125021.GB10154@ombreroze.happyleptic.org> X-Enigmail-Version: 1.1.2 Content-Type: multipart/mixed; boundary="------------060708030603040600050301" X-Validation-by: fabrice.le_fessant@inria.fr Subject: Re: [Caml-list] typer strangeness (3.12.0) This is a multi-part message in MIME format. --------------060708030603040600050301 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit The type constraint that you specified does not constraint the polymorphism of the type. To declare a polymorphic constraint, you must use (with OCaml >= 3.12.0) : let pipe : 'a 'b 'c. ('a, 'b) parzer -> ('c, 'a) parzer -> ('c, 'b) parzer = fun p1 p2 -> let p1_rem = ref [] in fun bs -> match p1 (!p1_rem @ bs) with | Fail -> Fail | Wait -> Wait | Res (res, rem) -> p1_rem := rem ; (match p2 [res] with | Res (res', rem') -> if rem' <> [] then Printf.printf "WRN: second end of a pipe did not consume eveything !\n" ; Res (res', !p1_rem) | x -> x) In which case you get the following error : Error: This definition has type 'a 'b. ('b, 'b) parzer -> ('a, 'b) parzer -> ('a, 'b) parzer which is less general than 'c 'd 'e. ('e, 'd) parzer -> ('c, 'e) parzer -> ('c, 'd) parzer Fabrice On 08/06/2011 02:50 PM, rixed@happyleptic.org wrote: > Given these types: > > (* A parser is given a list of items and returns either a Failure indication, a > Wait for more inputs indication, or a result (composed of a new value and the list > of tokens that were unused) *) > type ('a, 'b) parzer_result = Wait | Res of ('a * 'b list) | Fail > type ('a, 'b) parzer = 'b list -> ('a, 'b) parzer_result > > And this function used to pipe two parsers together: > > (* Use the results of the first parser as the input elements of the second. > Stop as soon as p2 returns a result or fails. > Notice that if p1 is a ('a, 'b) parzer and p2 a ('c, 'a) parzer > then pipe p1 p2 is a ('c, 'b) parzer, which comes handy but p2 is then > forced to consume everything ! *) > let (pipe : ('a, 'b) parzer -> ('c, 'a) parzer -> ('c, 'b) parzer) p1 p2 = > let p1_rem = ref [] in > fun bs -> match p1 (!p1_rem @ bs) with > | Fail -> Fail > | Wait -> Wait > | Res (res, rem) -> > p1_rem := rem ; > (match p2 [res] with > | Res (res', rem') -> > if rem' <> [] then Printf.printf "WRN: second end of a pipe did not consume eveything !\n" ; > Res (res', !p1_rem) > | Fail -> Fail | Wait -> Wait) > > This pipe function has the expected type : > > # pipe;; > - : ('a, 'b) parzer -> ('c, 'a) parzer -> ('c, 'b) parzer = > > Now, if I change it's last line for : "| x -> x)", ie not repeating Wait and Fail, the result is > very different : > > # pipe;; > - : ('a, 'a) parzer -> ('b, 'a) parzer -> ('b, 'a) parzer = > > How come? > And why didn't the compiler complain since I explicitely typed the function definition? > > --------------060708030603040600050301 Content-Type: text/x-vcard; charset=utf-8; name="fabrice_le_fessant.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fabrice_le_fessant.vcf" begin:vcard fn:Fabrice LE FESSANT n:LE FESSANT;Fabrice org:INRIA Saclay -- Ile-de-France;P2P & OCaml adr;quoted-printable:;;Parc Orsay Universit=C3=A9 ;Orsay CEDEX;;91893;France email;internet:fabrice.le_fessant@inria.fr title;quoted-printable:Charg=C3=A9 de Recherche tel;work:+33 1 74 85 42 14 tel;fax:+33 1 74 85 42 49 url:http://fabrice.lefessant.net/ version:2.1 end:vcard --------------060708030603040600050301--