Version française
Home     About     Download     Resources     Contact us    
Browse thread
a function for polymorphic and monomorphic objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Peng Zang <peng.zang@g...>
Subject: Re: [Caml-list] a function for polymorphic and monomorphic objects
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

So my take is that since o2 is a completely different beast, ie. monomorphic, 
it really is of a different type.  And should not be confused with 
polymorphic cousins.  Functions that take them as an argument will 
fundamentally have different types.  Eg. one monomorphic, the other poly.  
You want this distinction normally as it protects you from running o2 on a 
(int -> int) and later run it on a (int -> string).

However I can see how especially when the monomorphic version is 
still "unbound", ie. still _'a, one might want to write one function to deal 
with both.  I don't know of a nice way to do this as the type sigs are 
fundamentally different.  That said, one can always resort to black magic 
when the type system gets in the way assuming you know what you're doing:


# let foo arg = (arg:<map : 'a. (int -> 'a) -> 'a list; ..>)#map 
string_of_int;;
val foo : < map : 'a. (int -> 'a) -> 'a list; .. > -> string list = <fun>
# foo o1;;
- - : string list = ["1"; "2"; "3"]
# foo (Obj.magic o2);;
- - : string list = ["1"; "2"; "3"]


Peng

On Thursday 08 November 2007 07:09:39 am Keiko Nakata wrote:
> > In your case you got it wrong when you quantified 'a in the object p.
> >
> > # let p = object method map =  fun f -> List.map f [1;2;3] end;;
> > val p : < map : (int -> 'a) -> 'a list > = <obj>
>
> Perhaps I oversimplified my example.
> Indeed I want to make p a function like
>
> let p1 l = object
>   method map : 'a.(int -> 'a) -> 'a list =  fun f -> List.map f l
> end;;
>
> let o1 = p1 [1;2;3];;
> - val o1 : < map : 'a. (int -> 'a) -> 'a list > = <obj>
>
> Then I need type annotations
>
> let p2 l = object
>   method map =  fun f -> List.map f l
> end;;
> let o2 = p2 [1;2;3];;
> - val o2 : < map : (int -> '_a) -> '_a list > = <obj>
>
>
> With best,
> keiko
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFHM7mHfIRcEFL/JewRAvCYAKCBDF0GoWWpeQTRtb/0ZTNWlCTBXgCg0HO/
VYPxEIQU88Fmctql0Cw910U=
=fcz+
-----END PGP SIGNATURE-----