From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 15250BC57 for ; Sun, 28 Mar 2010 19:32:40 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwEAG8vr0teFxRU/2dsb2JhbACDGZkArQqPYIErgmxqBA X-IronPort-AV: E=Sophos;i="4.51,323,1267398000"; d="scan'208";a="47615053" Received: from rps.up7.fr (HELO glyptal.glondu.net) ([94.23.20.84]) by mail3-smtp-sop.national.inria.fr with ESMTP; 28 Mar 2010 19:32:39 +0200 Received: from [10.198.42.2] (unknown [10.198.42.2]) by glyptal.glondu.net (Postfix) with ESMTP id 1D97659D87; Sun, 28 Mar 2010 19:32:37 +0200 (CEST) Message-ID: <4BAF92B5.1060502@glondu.net> Date: Sun, 28 Mar 2010 19:32:37 +0200 From: =?UTF-8?B?U3TDqXBoYW5lIEdsb25kdQ==?= User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20091109) MIME-Version: 1.0 To: Dario Teixeira Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Structural subtyping problem References: <61102.44294.qm@web111503.mail.gq1.yahoo.com> In-Reply-To: <61102.44294.qm@web111503.mail.gq1.yahoo.com> X-Enigmail-Version: 0.95.0 OpenPGP: id=49881AD3 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; subtyping:01 recursion:01 bool:01 bool:01 ocaml:01 cheers:01 phane:98 steph:98 phane:98 polymorphic:01 rec:01 caml-list:01 int:01 int:01 parameter:02 Dario Teixeira a =C3=A9crit : > A simple nonsensical example that illustrates the problem is listed bel= ow; the > type-checking error occurs in function "step1", where the optional para= meter "story" > is used as an object of type "< title:string; .. >". In function "step= 3", this > parameter "story" is actually instantiated with objects of type "< titl= e:string >" > and "< title:string; count:int >". As said elsewhere, this is because you are trying to do polymorphic recursion. Standard tricks work here: ------------------------------------------------------------------------ type steps =3D { step1 : 'a. ?story:( as 'a) -> unit -> bool; step2 : string -> bool; step3 : 'a. story:(<title : string; count : int; ..> as 'a) -> bool } let rec steps =3D { step1 =3D begin fun ?story () -> match story with | Some s -> steps.step2 s#title | None -> steps.step2 "title1" end; step2 =3D begin fun title -> let story =3D object method title =3D title method count =3D 0 end in steps.step3 story end; step3 =3D begin fun ~story -> match story#count with | 0 -> steps.step1 ~story () | 1 -> let story =3D object method title =3D "title2" end in steps.step1 ~story () | _ -> true end } let step1 =3D steps.step1 let step2 =3D steps.step2 let step3 =3D steps.step3 ------------------------------------------------------------------------ With OCaml 3.12, I guess this should be feasible without using the intermediate "steps" record. Cheers, --=20 St=C3=A9phane