caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Yaron Minsky <yminsky@janestreet.com>
To: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
Cc: Misha Aizatulin <avatar@hot.ee>,
	"caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] typechecking
Date: Sun, 23 Mar 2014 09:12:10 +1100	[thread overview]
Message-ID: <CACLX4jQofUU-cDgc29f+ibFVW3Tg-fxZ4R1rE4B-kUrSfPipUw@mail.gmail.com> (raw)
In-Reply-To: <2E564C20A45F40FFA6BDB6946F088F03@erratique.ch>

Marshal is of course not type-safe, but it's still worth explaining
why this happens.  My type-theory is weak, but I'll do my best.

A naive mental model of type-checking is that the application of [f]
should require [t] to be of type [t2], and the constraint in the
return value should constrain [t] to be of type [t1], and thus there
should be a type-error.  Here are some examples that emphasize the
point.

utop[2]> let z () : t1 = let t = Obj.magic () in f t; t;;
val z : unit -> t1 = <fun>

If [t] comes in as an argument, we get the expected error:

utop[10]> let z t : t1 = f t; t;;
Error: This expression has type t2 but an expression was expected of type t1

The issue here, I think, is that the result of Obj.magic (or
input_value) is truly polymorphic, so it's simultaneously compatible
with all types.  And the fact that it's in some contexts where it's
used doesn't mean its definition is constrained.  A value passed in as
an argument, however, is monomorphic within the body of the function.

Here's a similar example, without using Obj.magic, just using an empty
list, whose type is truly polymorphic.

utop[11]> let z () : t1 list = let t = [] in ignore (List.map ~f t); t;;
val z : unit -> t1 list = <fun>

On the other hand, if we constraint t at the definition time, we get a
type error, because that makes the type not polymorphic.

utop[13]> let z () : t1 list = let t = ([] : t2 list) in t;;
Error: This expression has type t2 list but an expression was expected of type
         t1 list
       Type t2 is not compatible with type t1

In the end, Daniel's advice of annotating the value at its creation
point is sound, but it's worth understanding why.

y

On Sun, Mar 23, 2014 at 7:40 AM, Daniel Bünzli
<daniel.buenzli@erratique.ch> wrote:
> Le samedi, 22 mars 2014 à 21:02, Misha Aizatulin a écrit :
>> type t1 = T1
>> type t2 = T2
>>
>> let f T2 = ()
>>
>> let input (c : in_channel) : t1 =
>> let t = input_value c in
>> f t;
>> t
>
> Unmarshaling is not type-safe, you always need to add a type annotation when you input_value. See the warning in the documentation here [1]. Your program should read:
>
> type t1 = T1
> type t2 = T2
>
> let f T2 = ()
>
> let input (c : in_channel) : t1 =
> let t = (input_value c : t1) in
> f t;
> t
>
>
>
> Which the compiler rejects.
>
> Best,
>
> Daniel
>
> [1] http://caml.inria.fr/pub/docs/manual-ocaml-4.01/libref/Marshal.html
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2014-03-22 22:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-22 20:02 Misha Aizatulin
2014-03-22 20:40 ` Daniel Bünzli
2014-03-22 22:12   ` Yaron Minsky [this message]
2014-03-22 22:24     ` Jonathan Protzenko
2014-03-23  4:41 ` oleg
2014-03-23 12:59   ` Jacques Garrigue
2014-03-24  8:46     ` Alain Frisch

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=CACLX4jQofUU-cDgc29f+ibFVW3Tg-fxZ4R1rE4B-kUrSfPipUw@mail.gmail.com \
    --to=yminsky@janestreet.com \
    --cc=avatar@hot.ee \
    --cc=caml-list@inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    /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).