caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* typing question
@ 2007-04-24 18:14 micha
  2007-04-24 22:22 ` [Caml-list] " Philippe Wang
  0 siblings, 1 reply; 2+ messages in thread
From: micha @ 2007-04-24 18:14 UTC (permalink / raw)
  To: caml-list

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
;;


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] typing question
  2007-04-24 18:14 typing question micha
@ 2007-04-24 22:22 ` Philippe Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Philippe Wang @ 2007-04-24 22:22 UTC (permalink / raw)
  To: micha, ocaml ml

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-04-24 22:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-24 18:14 typing question micha
2007-04-24 22:22 ` [Caml-list] " Philippe Wang

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).