[mercury-users] overloading
Fergus Henderson
fjh at cs.mu.OZ.AU
Wed Jul 26 14:57:12 AEST 2000
On 26-Jul-2000, Michael Day <mcda at students.cs.mu.oz.au> wrote:
>
> This has come up before (and what hasn't?) but I inadvertedly ran into it
> again and can't see an obvious way out.
>
> :- func vector * float = vector.
> :- func float * vector = vector.
>
> */3 now has two different definitions and the compiler gets very upset.
> Hacking around it by putting them in separate submodules won't help if
> they're type class methods.
>
> Any ideas?
How about using non-overloaded names for the class methods,
and then defining overloaded versions in nested modules
that just call the non-overloaded versions?
For an example, see the attached code.
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3 | -- the last words of T. S. Garp.
-------------- next part --------------
:- module vector.
:- interface.
:- typeclass vector(Vector, Scalar) where [
func vector_times_scalar(Vector, Scalar) = Vector,
func scalar_times_vector(Scalar, Vector) = Vector
].
:- module vector_scalar.
:- interface.
:- func Vector * Scalar = Vector <= vector(Vector, Scalar).
:- end_module vector_scalar.
:- module scalar_vector.
:- interface.
:- func Scalar * Vector = Vector <= vector(Vector, Scalar).
:- end_module scalar_vector.
:- implementation.
:- module vector_scalar.
:- implementation.
Vector * Scalar = vector_times_scalar(Vector, Scalar).
:- end_module vector_scalar.
:- module scalar_vector.
:- implementation.
Scalar * Vector = scalar_times_vector(Scalar, Vector).
:- end_module scalar_vector.
:- end_module vector.
-------------- next part --------------
:- module myvector.
:- interface.
:- import_module vector, array.
:- type array_of_float ---> myvector(array(float)).
:- instance vector(array_of_float, float).
:- implementation.
:- import_module require.
:- instance vector(array_of_float, float) where [
func(vector_times_scalar/2) is array_times_float,
func(scalar_times_vector/2) is float_times_array
].
:- func array_times_float(array_of_float, float) = array_of_float.
:- func float_times_array(float, array_of_float) = array_of_float.
array_times_float(_A, _F) = _AF :- error("NYI: F*A").
float_times_array(_F, _A) = _FA :- error("NYI: A*F").
:- end_module myvector.
-------------- next part --------------
:- module myvector_test.
:- interface.
:- import_module myvector.
:- func foo(array_of_float, float) = array_of_float.
:- func bar(array_of_float, float) = array_of_float.
:- implementation.
:- import_module vector.
:- import_module vector__vector_scalar.
:- import_module vector__scalar_vector.
foo(A, F) = A * F.
bar(F, A) = F * A.
:- end_module myvector_test.
More information about the users
mailing list