List Info

Thread: "ocaml_beginners"::[] Keeping polymorphism?




"ocaml_beginners"::[] Keeping polymorphism?
country flaguser name
United States
2007-05-24 11:39:02

Hello,

I have studied a example from OCaml system manual,
and cannot get throuth idea of "keeping polymorphism"
as following:
#type idref = { mutable id: 'a. 'a -> 'a } ;;

I don't understand why there is a dot
between 'a. 'a -> 'a
In my understanding, it's the same thing with
#type 'a idref = { mutable id: 'a -> 'a } ;;

Does the dot notation serve for any purpose else?

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Keeping polymorphism?
country flaguser name
United States
2007-05-24 12:12:02

On Thu, 24 May 2007, LORENZO wrote:

> Hello,
>
> I have studied a example from OCaml system manual,
> and cannot get throuth idea of "keeping polymorphism"
> as following:
> #type idref = { mutable id: 'a. 'a -> 'a } ;;
>
> I don't understand why there is a dot
> between 'a. 'a -> 'a
> In my understanding, it's the same thing with
>; #type 'a idref = { mutable id: 'a -> 'a } ;;
>
> Does the dot notation serve for any purpose else?

Yes. It keeps the 'a from becoming confined to a single type when you
create a value of type idref. For example:

# type idref = { mutable id: 'a. 'a -> 'a } ;;
type idref = { mutable id : 'a. 'a -> 'a; }
# type 'a idref' = { mutable id': 'a -> 'a } ;;
type 'a idref' = { mutable id' : 'a -> 'a; }
# let id = {id = fun x -> x};;
val id : idref = {id = <fun>;}
# let id' = {id' = fun x -> x};; (* note the monomorphic type signature *)
val id' : '_a idref' = {id' = <fun>;}
# id.id 1;;
- : int = 1
# id.id "a&quot;;;
- : string = "a&quot;
# id'.id' 1;;
- : int = 1
# id';; (* note that id' is now restricted to type int idref' *)
- : int idref' = {id' = <fun>;}
# id'.id' "a&quot;;;
Characters 8-11:
id'.id' "a&quot;;;
^^^
This expression has type string but is here used with type int

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy.&quot;

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )