caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Philippe Wang <lists@philippewang.info>
To: micha <micha-1@fantasymail.de>, ocaml ml <caml-list@inria.fr>
Subject: Re: [Caml-list] typing question
Date: Wed, 25 Apr 2007 00:22:31 +0200	[thread overview]
Message-ID: <462E8327.3020101@philippewang.info> (raw)
In-Reply-To: <20070424201435.38f2f5f9@hmmm.de>

micha wrote:
> Hi,
>
> why is the type of register1  'a -> string but the type of register2
> _'a -> string?
>
>
> cheers
>  Michael
>
>
>
> let symbol_id = ref 0;;
> let register1 fkt =
>     let name = "symbol-" ^ (string_of_int !symbol_id) in
>     incr symbol_id;
>     Callback.register name fkt;
>     name
> ;;
>
> let register2 =
>     let symbol_id = ref 1 in
>     fun fkt -> let name = "symbol-" ^ (string_of_int !symbol_id) in
>     incr symbol_id;
>     Callback.register name fkt;
>     name
> ;;
>   

It is because register2 is detected as an "expansive" expression.

"Expansive expressions" are those that we can't generalize without 
taking the risk to allow type errors at execution, so its type variable 
remains unknown and
can't be "forall 'a. 'a".

Take this simple example :

let x = ref []
So x : '_a ref, because if x were 'a ref, then you could write
x := [343] ; x := ["hello"]
and there would be a type error at runtime.

Thus, even if an expression such as id2 in :
let id x = x ;;
let id2 = id id ;;
does exactly the same computing as id, it can't be 'a -> 'a because id2 
is an "application" and ocaml doesn't generalize applications...

Good luck,

--
 Philippe Wang



      reply	other threads:[~2007-04-24 22:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-24 18:14 micha
2007-04-24 22:22 ` Philippe Wang [this message]

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=462E8327.3020101@philippewang.info \
    --to=lists@philippewang.info \
    --cc=caml-list@inria.fr \
    --cc=micha-1@fantasymail.de \
    /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).