[mercury-users] intermodule optimization and type classes

Michael Day mikeday at bigpond.net.au
Thu Jan 3 10:00:00 AEDT 2002


> Can you post the code?  That would help diagnosis.

Here is a trimmed example that demonstrates the problem. vector defines a
type class that has superclass constraints from group and scalable. rgb
defines a type that implements the group, scalable and vector type
classes. buffer imports rgb and *nothing else* - when buffer is compiled 
with intermodule optimization enabled it gives an error about the vector 
interface. When buffer is modified to also import vector, the error goes 
away. So at least I can make it stop now, although I'm curious as to why 
it's happening at all.

Michael
-------------- next part --------------
:- module group.

:- interface.

:- typeclass group(T) where
    [
        func add(T, T) = T,
        func sub(T, T) = T,
	func zero = T
    ].
-------------- next part --------------
:- module scalable.

:- interface.

:- import_module float.

:- typeclass scalable(T) where
    [
	func scale(float, T) = T
    ].
-------------- next part --------------
:- module vector.

:- interface.

:- import_module float, group, scalable.

:- typeclass vector(T) <= (group(T), scalable(T)) where
    [
	func dot(T, T) = float
    ].
-------------- next part --------------
:- module rgb.

:- interface.

:- import_module float, group, scalable, vector.

:- type rgb == rgb(float).
:- type rgb(T)
    --->    rgb(r :: T, g :: T, b :: T).

:- instance group(rgb(T)) <= group(T).
:- instance scalable(rgb(T)) <= scalable(T).
:- instance vector(rgb(T)) <= vector(T).

:- implementation.

:- instance group(rgb(T)) <= group(T) where
    [
	add(rgb(R1,G1,B1), rgb(R2,G2,B2)) =
				rgb(add(R1,R2), add(G1,G2), add(B1,B2)),
	sub(rgb(R1,G1,B1), rgb(R2,G2,B2)) =
				rgb(sub(R1,R2), sub(G1,G2), sub(B1,B2)),
	zero = rgb(zero `with_type` T, zero `with_type` T, zero `with_type` T)
    ].

:- instance scalable(rgb(T)) <= scalable(T) where
    [
	scale(S, rgb(R, G, B)) = rgb(scale(S, R), scale(S, G), scale(S, B))
    ].

:- instance vector(rgb(T)) <= vector(T) where
    [
	dot(rgb(R1,G1,B1), rgb(R2,G2,B2)) = dot(R1,R2)+dot(G1,G2)+dot(B1,B2)
    ].

-------------- next part --------------
:- module buffer.

:- interface.

:- import_module rgb.


More information about the users mailing list