caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: boris@yakobowski.org
Cc: caml-list caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] obj.magic for polymorphic record fields
Date: Thu, 24 Dec 2009 13:10:07 +0100	[thread overview]
Message-ID: <87hbrg7eu8.fsf@frosties.localdomain> (raw)
In-Reply-To: <76c7f53f0912221349n409ebb0h9639c576562a87cd@mail.gmail.com> (Boris Yakobowski's message of "Tue, 22 Dec 2009 22:49:35 +0100")

Boris Yakobowski <boris@yakobowski.org> writes:

> On Tue, Dec 22, 2009 at 2:35 PM, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>> But the type inference should deduce that in
>>
>> (Obj.magic fn) x
>>
>> the 'a is actually 'b -> 'c as I am applying an argument to it.
>
> Sure, and it does. But it remains that the principal type of
> (Obj.magic fn) is 'a, without any other constraint on 'a. The fact
> that you use it with type 'b -> 'c for some 'b and 'c is irrelevant,
> and the type-checker can safely draw conclusions from the principal
> type. (This is similar to considering List.map (fun x -> x). It has
> type 'a list -> 'a list, even though it is used with type int list ->
> int list if you apply it to [1].)
>
> Hope this helps,
>
> -- 
> Boris

Nope.

# Obj.magic;;
- : 'a -> 'b = <fun>

# let id x = x;;
val id : 'a -> 'a = <fun>

# let f x y = (id x) y;;
val f : ('a -> 'b) -> 'a -> 'b = <fun>

The type checker inferes correctly that the 'a in id must actualy be
('a -> 'b) and gives that type to x.

# let g x y = (Obj.magic x) y;;
Warning X: this argument will not be used by the function.
val g : 'a -> 'b -> 'c = <fun>

No such luck here.

I think the difference is that in Obj.magic there is no connection
between the input ('a) and the output ('b). Something I think only
Obj.magic has. So the type inference can not infere that the 'a in g
must be of type ('d -> 'e) where 'd is compatible with 'b and 'e
compatible with 'c (as in it doesn't segfault).

Maybe there should be a special Obj.less_magic that preserves the
number of arguments between input and output type. Obj.less_magic
could only have types

'a               -> 'b
('a -> 'b)       -> ('c -> 'd)
('a -> 'b -> 'c) -> ('d -> 'e -> 'f)
...

That would be something that needs to be hardcoded into the type
inference or needs a new type syntax. But it would give verry little
extra security. Obj.magic just is evil. :)

MfG
        Goswin


  reply	other threads:[~2009-12-24 12:12 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
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 [this message]
  -- 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=87hbrg7eu8.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=boris@yakobowski.org \
    --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).