[mercury-users] Failing at typeclasses.

Julian Fondren ayrnieu at gmail.com
Sat Feb 17 21:24:20 AEDT 2007


On 2/16/07, Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
> Have a look at library/stream.  It defines several multi-parameter
> typeclasses.

It doesn't in the latest release, but... looking at the ROTD
changes since 0.13.1, I think I should be using that anyway.

>       :- typeclass prng(T, S, R) <= (state(T), seed(S), return(R)) where [
> #10       pred seed(S::in, T::uo) is det,
> #11       pred random(R::out, T::di, T::uo) is det
>       ].
> There are no functional dependencies in the above typeclass declaration
> and method seed does not refer to type variable R in it's signature;
> likewise method random does not refer to type variable S in it's signature.

And since the type of the seed and the type of the result are entirely
unrelated, I can't fix this by moving forward, like I suspected :-(

As a horrible experiment, I changed unconstrained.m so that PRNG has
functional dependencies ((T -> S), (T -> R)), which required duplicate
fox_char_state fox_int_state, and then much corresponding duplication in
the implementation.

I think my conception of a PRNG here is simply wrong -- but with
ROTD improvements, I might restructure things so that a PRNG takes
a seed of random bits and then simply produces random bits, with other
-- RNG-agnostic -- functions that turn random bits into Mercury types.

>       :- typeclass fox_prng(T) <= prng(fox_state, fox_seed, T) where [ ].
>
> #22   :- instance fox_prng(int).
> #23   :- instance fox_prng(char).
>
> The typeclass declaration says that if T is an instance of fox_prng/1 then
> prng(fox_state, fox_seed, T) must be an instance of prng/3.  There are no
> such instances in your program., e.g. the compiler is complaining that the
> following are not defined:
>
>       :- instance prng(fox_state, fox_seed, int).
>       :- instance prng(fox_state, fox_seed, char).

Ow, is there any use for an abbreviated typeclass like fox_prng/1, then?

> #30   :- instance fox_prng(int) where [
>           pred(seed/2) is fox_seed,
>           (random(R, !RNG) :- fox_rand(R0, !RNG), char.to_int(R0, R))
>       ].
>
> The problem here is that the typeclass fox_prng/1 has no methods named
> seed/2 or random/3.  They are methods of the superclass prng/3.
> Note that in Mercury you cannot define the methods of the superclass
> in an instance declaration for a sub-class and expect it to act as
> an instance definition for the superclass as well.  You need a separate
> instance definition for the superclass.

... oh.  This is more logical -- that the '<= (typeclass(T))' of
:- typeclass works the same as it does in independent :- pred -- and this
also neatly prohibits seperate typeclasses from defining incompatible
implementations for a common interface, the way I think I did at points
in random.m  I think I interpreted the 'superclas' of the docs too OOly.

Thanks :-)
Julian
--------------------------------------------------------------------------
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