[mercury-users] Typeclass constraint on type declaration

Ian MacLarty maclarty at csse.unimelb.edu.au
Fri Feb 11 09:23:46 AEDT 2011


On Fri, Feb 11, 2011 at 12:29 AM, Vladimir Gubarkov <xonixx at gmail.com> wrote:
> ThanksĀ JulienĀ for your reply.
> I played a little with this and found that we can create typeclass
> constraints on a type in case that we use existential types, such that we
> can get working code roughly equivalent of my first code:
> :- module tc1.
> :- interface.
> :- import_module io.
> :- pred main(io::di, io::uo) is det.
> :- implementation.
> :- import_module int.
> :- typeclass a(T) where [
> func aaa(T) = T
> ].
> :- instance a(int) where [
> aaa(E) = E+1
> ].
> :- type q ---> some [T] q(T) => a(T).
> z(q(T)) = aaa(T).
> main --> print(z('new q'(123))).
> ===
> $ tc1
> 124
> Now my question is: are the two approaches REALLY equivalent? Or should the
> (not yet implemented) approuch give us some fundamentally new means in
> programming with mercury?
>

They're not equivalent.  With existential types the inferred signature of z is:

:- some [T] (func z(q) = T => (a(T)))

You don't know anything at compile time about the return value of z
except that it is an instance of a.
There is no guarantee that the input value in q is the same type as
the return value of z.

If you instead did:

:- type q(T) ---> q(T).
:- func z(q(T)) = T <= a(T).

then you have a static guarantee that q's argument is the same type as
z's return value (T).  You might want this if, for example, you wanted
to put the input and output values in the same list:

B = z(q(A)),
C = [A, B],

With existential types this wouldn't work:

B = z('new q'(A)),
C = [A, B],

You'd get a compilation error, because the signature of z doesn't
guarantee that A and B are the same type.

Ian.

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list