Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] typeclasses in OCaml

102 views
Skip to first unread message

Peng Zang

unread,
Aug 22, 2008, 8:54:24 AM8/22/08
to caml...@yquem.inria.fr
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I have been using the Object system in OCaml for the equivalent functionality
of haskell typeclasses for some time. It allows nice things like:

class type ['a] foldable = object
method fold : 'z. ('z -> 'a -> 'z) -> 'z -> 'z
end

let forall (f:'a -> bool) (obj:'a #foldable) =
obj#fold (fun acc x -> acc && f x) true


Recently however, I tried to make a "mappable" class. Something like:

class type ['a] mappable = object('self)
method map : 'z. ('a -> 'z) -> 'z 'self
end

Which of course is invalid syntax. However, I think the point is clear. I
want to specify the type of all container objects that can map their
contents. How do you specify this in OCaml?

I realize there are some inherent issues, eg. polymorphic classes are
invarient. So I'm willing to accept some limited use of magic as long as it
can be hidden away and offer a type-safe interface. Nevertheless, after half
a day of trying, I cannot seem to figure this out. I don't even know how to
specify:

" a 'z version of 'self "

In a syntactically correct way. Is this possible? Thank in advance,

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

iD8DBQFIrrb0fIRcEFL/JewRAuDZAJ4zGo7+B6dF0YbnUFwQPGW+2MenZACfbAJ6
OKVIlsPpwkVwOEefT8KiRMU=
=+8yF
-----END PGP SIGNATURE-----

_______________________________________________
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

Jacques Garrigue

unread,
Aug 22, 2008, 10:58:41 PM8/22/08
to peng...@gmail.com, caml...@yquem.inria.fr
From: Peng Zang <peng...@gmail.com>

> Recently however, I tried to make a "mappable" class. Something like:
>
> class type ['a] mappable = object('self)
> method map : 'z. ('a -> 'z) -> 'z 'self
> end
>
> Which of course is invalid syntax. However, I think the point is clear. I
> want to specify the type of all container objects that can map their
> contents. How do you specify this in OCaml?
>
> I realize there are some inherent issues, eg. polymorphic classes are
> invarient. So I'm willing to accept some limited use of magic as long as it
> can be hidden away and offer a type-safe interface. Nevertheless, after half
> a day of trying, I cannot seem to figure this out. I don't even know how to
> specify:
>
> " a 'z version of 'self "
>
> In a syntactically correct way. Is this possible? Thank in advance,
>
> Peng

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

Peng Zang

unread,
Aug 23, 2008, 8:50:41 AM8/23/08
to caml...@yquem.inria.fr, Jacques Garrigue
-----BEGIN PGP SIGNED MESSAGE-----
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,

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

iD8DBQFIsAeTfIRcEFL/JewRApXnAKDTDuq97Dfe7/WcIVtFLWfqUeb56QCeKcla
a61KJh5dWEnQmHwalusb0Mo=
=lP8V
-----END PGP SIGNATURE-----

0 new messages