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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id AAD0BBBAF for ; Mon, 21 Dec 2009 17:05:43 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuEAAAkrL0tIDtyekGdsb2JhbACQGoF1iQA/AQEBAQkJDAcTA6wngTKEJ4gpAQIDBYQpBIFl X-IronPort-AV: E=Sophos;i="4.47,431,1257116400"; d="scan'208";a="42536106" Received: from fg-out-1718.google.com ([72.14.220.158]) by mail1-smtp-roc.national.inria.fr with ESMTP; 21 Dec 2009 17:05:43 +0100 Received: by fg-out-1718.google.com with SMTP id e21so2312214fga.3 for ; Mon, 21 Dec 2009 08:05:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=PFAsgwE7QsR0gNZpd5+4DhAOcC1D3r5sGtUFElN/je0=; b=RpeoCmuAk16/ZVSyPimyJYCsGrnXQNSxaFORezVXVGVE4m6L2MnZE90F/D9mznRsDN v9SfBkHpbRtuhDqrOtH7PoBcGbUwKGM2zNZ3pPyhzBSkjznFlUxEHWmQSUnrR9E1tdGX amI7d0kaZxsdt8zHckoAlybmI6q0u5RE2WLcM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=OjG4qkn9Re1JO1TuokyVs06yDvgpFm5T4azvZTUSK6Jc3gijtTbHQyoo5M86uDLIaq H2xNS4a3yOMAIfc1xg+SBl7+ADnUeUyxHgptYGzP6ccFZo+AaV3FR/zG60/51RA/hoek 9hPxEPKGiL8H7m8ZDfnDKJa/CgtleyQQGZ/WY= MIME-Version: 1.0 Received: by 10.216.90.146 with SMTP id e18mr3135539wef.153.1261411542709; Mon, 21 Dec 2009 08:05:42 -0800 (PST) In-Reply-To: <76c7f53f0912210600y91c9b76ib07d130cf7cf251f@mail.gmail.com> References: <531142069376641352@orange.fr> <87zl5cmof9.fsf@frosties.localdomain> <76c7f53f0912210600y91c9b76ib07d130cf7cf251f@mail.gmail.com> Date: Mon, 21 Dec 2009 11:05:42 -0500 Message-ID: Subject: Re: [Caml-list] obj.magic for polymorphic record fields From: Jacques Le Normand To: boris@yakobowski.org Cc: Goswin von Brederlow , caml-list caml-list , Damien Guichard Content-Type: multipart/alternative; boundary=0016e6db2980eb3827047b3f4221 X-Spam: no; 0.00; camlp:01 sig:01 val:01 occurence:01 struct:01 yakobowski:01 yakobowski:01 inference:01 ocaml:01 compiler:01 compiler:01 baz:01 baz:01 beginner's:01 ocaml:01 --0016e6db2980eb3827047b3f4221 Content-Type: text/plain; charset=ISO-8859-1 right, the use case is totally wrong I'm automatically generating code from type declarations using camlp4 and it would break down in presence of polymorphic record fields. Here's the fix I chose (Thanks to Mr. Pouillard): module type MagicSignature = sig val x : 'a end and I would replace every occurence of {bar = x} with let module X : MagicSignature = (struct let y = x end) in {bar = X.y} -Jacques L. On Mon, Dec 21, 2009 at 9:00 AM, Boris Yakobowski wrote: > On Mon, Dec 21, 2009 at 2:44 PM, Goswin von Brederlow > wrote: > >> However it issues a warning so i acknowledge it's less elegant. > > > > Which I don't quite understand. > > The warning is based on the results of the type inference algorithm. > You're not supposed to find values of type 'a. 'a "in the wild", yet > (Obj.magic a) has this type. Thus the Ocaml compiler deduces that > you're doing something wrong: your function never returns, it does not > use its arguments, etc. Here, using Obj.magic breaks the crystal ball > of the compiler, and the warning is incorrect. > > There is an easy way to silence the warning though: > > let baz = {bar = fun x -> (Obj.magic a : _ -> _) x} > > (but not let baz = {bar = fun x -> (Obj.magic a : 'a -> 'a) x} for > ugly reasons...) > > You are however quite correct. Using Obj.magic in this particular case > seems quite wrong. > > -- > Boris > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > --0016e6db2980eb3827047b3f4221 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable right, the use case is totally wrong
I'm automatically generating c= ode from type declarations using camlp4 and it would break down in presence= of polymorphic record fields.

Here's the fix = I chose (Thanks to Mr. Pouillard):

module type MagicSignature =3D=A0
sig
=A0=A0val x : 'a
end

and I w= ould replace every occurence of=A0

{bar =3D x}

with

let module X : MagicSignature =3D = (struct let y =3D x end) in {bar =3D X.y}

-Jacques= L.

On Mon, Dec 21, 2009 at 9:00 AM, Bo= ris Yakobowski <boris@yakobowski.org> wrote:
On Mon, Dec 21, 2009 at 2= :44 PM, Goswin von Brederlow <goswi= n-v-b@web.de> wrote:
>> However it issues a warning so i acknowledge it's less elegant= .
>
> Which I don't quite understand.

The warning is based on the results of the type inference algorithm.<= br> You're not supposed to find values of type 'a. 'a "in the = wild", yet
(Obj.magic a) has this type. Thus the Ocaml compiler deduces that
you're doing something wrong: your function never returns, it does not<= br> use its arguments, etc. Here, using Obj.magic breaks the crystal ball
of the compiler, and the warning is incorrect.

There is an easy way to silence the warning though:

let baz =3D {bar =3D fun x -> (Obj.magic a : _ -> _) x}

(but not let baz =3D {bar =3D fun x -> (Obj.magic a : 'a ->= 'a) x} for
ugly reasons...)

You are however quite correct. Using Obj.magic in this particular case
seems quite wrong.

--
Boris

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.in= ria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--0016e6db2980eb3827047b3f4221--