[mercury-users] Typeclass question

Julien Fischer juliensf at csse.unimelb.edu.au
Mon Mar 7 18:07:59 AEDT 2011


Hi,

On Sat, 5 Mar 2011, Chris King wrote:

> Say I have a typeclass foo(A).  Making a container type bar(A) for
> items of this typeclass is easy, as I can place a universal typeclass
> constraint foo(A) on all predicates over bar(A) which produce values
> of type A.  (Although it would be nice to be able to place the
> constraint on the declaration of bar(A) itself as recently discussed
> this is not supported.)
>
> Now my question is: how do I abstract bar(A) as a typeclass itself?
> Intuitively I should be able to write:
>
> :- typeclass bar(Bar, A) <= (Bar -> A) where [...].
> :- type bar_impl(A).
> :- instance bar(bar_impl(A), A) <= foo(A).
>
> but of course the compiler balks at the unadorned A in the instance
> declaration.  Instead one could try with existential types:
>
> :- all [A] some [Bar] (pred create_bar(Bar::out) is det => bar(Bar,
> A)) <= foo(A).
>
> but now the compiler complains that A is "existentially constrained,
> but is universally quantified."  Which is true, but nonetheless
> frustrating.
>
> For reference, what I wish to achieve is easily representable with
> functors in ML:
>
> module type Foo: sig type a end
> module Bar(F: Foo): sig type a = F.a ... end
>
> What is the proper way to accomplish what I have in mind?

It would require Mercury to support constructor classes (which it
doesn't currently), plus (possibly) lifting some of the current
restrictions on type class instances.

> If there is none, is this a current limitation of the Mercury compiler
> or something deeper and irreconcilable?

Just a feature that hasn't been implemented -- unfortunately supporting
constructor classes entails quite a bit of reengineering of both the
compiler and the RTTI system.  I guess you can read that as a "current
limitation" if you wish.

Cheers,
Julien.


More information about the users mailing list