[mercury-users] typeclass issue #1

Terrence Brannon princepawn at earthlink.net
Thu May 3 13:28:32 AEST 2001


from the manual we have:

:- type coordinate
        ---> coordinate(
                float,           % X coordinate
                float            % Y coordinate
        ).

:- instance point(coordinate) where [
        pred(coords/3) is coordinate_coords,
        func(translate/3) is coordinate_translate
].


in my eyes, there is a lack of ability to make the word 'coordinate'
implicit throughout this code body. It seems tedious, error-prone and
against the idea of packaging related concepts, to have to prefix every
method relating to type coordinate in typeclass instance point with
the word coordinate.

my suggested rewrite, adding a second type to make it interesting

:- type coordinate
        ---> coordinate(
                float,           % X coordinate
                float            % Y coordinate
        ).

:- type color ---> color( float, float, float ).

:- instance point(coordinate~C, color~RGB) where [
        pred(coords/3) is {~C}_coords,
        func(translate/3) is {~C}_translate,
        pred(set_color/6) is {~RGB}_set_color
].

package coordinate {
  
  :- pred coords    .... 
  :- func translate ...

}  % end 


But in the final analysis, I think the main issue is that modules are
one form of typing, creating one sort of namespace, addressable via
module__class_method and type classes are trying to create another
one, without any syntax to extend the above. 

Perhaps a bit of Perl will serve as inspiration for how to get this
right:

my $self = bless { a => 12 }, 'coordinate';

$self->coords(2,3,5); 
# translates to 
# coordinate::coords( do bless({ a => 12 }, 'coordinate'), 2,3,5);
      
But actually, Mercury seems more flexible because it can handle two
types within the same typeclass, whereas one has to strap on
additional machinery in Perl to get what I believe is called multiple
dispatch. 

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list