Hi,

  I have a query regarding Type classes.

I want to know how to use type classes. I have tried writing the
code by looking at the example in the manuals, but still it 
gives
me errors.

I want to write a code to take a number, square it and display 
it
on the screen.

Plz look at the code and tell where should I correct it.
:- module squar.
:- interface.
:- import_module io.
:- pred main(io__state, io__state).
:- mode main(di, uo) is det.

:- typeclass squar(T) where [
         pred num(T, int, int),
         mode num(in, out, out) is det,
         func translate(T, int, int) = T ].

:-implementation.
:- import_module int.
num(T,N) :-
         ( if T = 0 then
                 N = 0
         else
                 N = T * T).
main --> io__write_string(squar(10,20)).

I execute the above code with the following commands and get the
results (errors).
$ mmake squar.depend
mmc --generate-dependencies --grade asm_fast.gc         squar

$ mmake squar
rm -f squar.c
mmc --compile-to-c --grade asm_fast.gc         squar.m > 
squar.err
2>&1
make: *** [squar.c] Error 1

I get errors here. They are:-
squar.m:014: Error: clause for predicate `squar:num/2'
squar.m:014:   without preceding `pred' declaration.
squar.m:019: In clause for predicate `squar:main/2':
squar.m:019:   in argument 1 of call to predicate
`io:write_string/3':
squar.m:019:   error: undefined symbol `squar/2'.
squar.m:014: Inferred :- pred num(int, int).
For more information, try recompiling with `-E'.

In the Mercury Language Reference Manual, the code is as
follows:-
:- typeclass point(T) where[
     pred coords(T, float, float),
     mode coords(in, out, out) is det,

     func translate(T, float, float) =T
].

Please do explain how it is used.

Thanks,

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

