caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: "Damien Guichard" <alphablock@orange.fr>
Cc: "caml-list caml-list" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] obj.magic for polymorphic record fields
Date: Mon, 21 Dec 2009 14:44:58 +0100	[thread overview]
Message-ID: <87zl5cmof9.fsf@frosties.localdomain> (raw)
In-Reply-To: <531142069376641352@orange.fr> (Damien Guichard's message of "Sun, 20 Dec 2009 19:45:53 +0100")

"Damien Guichard" <alphablock@orange.fr> writes:

>> I once faced this situation and the solution is to use modules.
> That is one good practical solution.
>
> The simpler solution that immediatly came to my mind is eta-expansion.
>
>   type foo = {bar : 'a. 'a -> 'a}
>   let a : int -> int = fun x -> x
>   let baz = {bar = fun x -> (Obj.magic a) x}

This would create a new closure though. Wastes time and space. Bad if
you create a million foo's all with the same function.

> However it issues a warning so i acknowledge it's less elegant.

Which I don't quite understand.

> - damien

Why not use magic on the overall type of the record instead of the
individual function?

# type foo = { foo : 'a. 'a -> 'a; }
  type 'a bar = {bar : 'a -> 'a}
  let a : int -> int = fun x -> x
  let baz = (Obj.magic {bar = a} : foo);;

type foo = { foo : 'a. 'a -> 'a; }
type 'a bar = { bar : 'a -> 'a; }
val a : int -> int = <fun>
val baz : foo = {foo = <fun>}

It means duplicating the record type but you can magic it without
warnings.


Overall I have to say though that you are doing something wrong here.
(Should I assume your actual use case is larger than this simple
example?)

# let b x = x + 1
  let buzz = (Obj.magic {bar = b} : foo)
  buzz.foo [1;2;3];;
val b : int -> int = <fun>
val buzz : foo = {foo = <fun>}
- : int list = [-2330612807164231680]

Doesn't always segfault but lets get nastier:

# (buzz.foo buzz).foo 1;;
zsh: segmentation fault  ocaml

The obj.magic only works safely if the function you use is of type 'a
-> 'a (even if the interface restricts it to a less general type). The
right solution would be to loosen the interface to use the more
general type.

MfG
        Goswin


  reply	other threads:[~2009-12-21 13:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-20 18:45 Re[2]: " Damien Guichard
2009-12-21 13:44 ` Goswin von Brederlow [this message]
2009-12-21 14:00   ` Boris Yakobowski
2009-12-21 16:05     ` Jacques Le Normand
2009-12-22 13:35     ` Goswin von Brederlow
2009-12-22 21:49       ` Boris Yakobowski
2009-12-24 12:10         ` Goswin von Brederlow
  -- strict thread matches above, loose matches on Subject: below --
2009-12-20 17:44 Jacques Le Normand
2009-12-20 18:17 ` [Caml-list] " Nicolas Pouillard

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=87zl5cmof9.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=alphablock@orange.fr \
    --cc=caml-list@yquem.inria.fr \
    /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).