Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
typeclasses in OCaml
[ 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] typeclasses in OCaml
Hash: SHA1

On Friday 22 August 2008 10:57:53 pm Jacques Garrigue wrote:
> There is no way to specify "a 'z version of 'self", and this is the
> reason you cannot do this in ocaml.
> Even if you don't require structural polymorphism for your object type
> (which you need here, since you want to write #mappable), there are
> other difficulties related to recursive types having to be regular.
> A classical workaround is to define map as a function using #fold and
> cons:
> let map cons f (o : 'a #foldable) =
>   o#fold (fun x o' -> cons (f x) o')
> Note that for this to work you need #fold to be the correct fold
> (i.e. fold_right)
> Hope this helps,
> Jacques Garrigue

Yes this has been very helpful.  It's good to know I'm not just missing 
something.  I will use your workaround instead.  It makes perfect sense.  
There is nothing inherently special about mappable.  It is just a container 
that we can access the elements of (that's #foldable) and that has a 
constructor.  Thanks,

Version: GnuPG v2.0.7 (GNU/Linux)