[m-dev.] A hole in the type system (typeclasses and user defined equality)

Ralph Becket rafe at cs.mu.OZ.AU
Wed May 24 13:14:52 AEST 2006

I am trying to write a generic interface to our linear constraint
solvers (currently we have interfaces to CPLEX and GLPK and more
are anticipated).

Each linear solver back-end implements it's own constrained linear
variable type and makes it an instance of the linear_solver_var type

:- typeclass linear_solver_var(V) where [


	% If ListOfCoefficients = [A1, A2, ..., AN] and
	% ListOfVars = [V1, V2, ..., VN] then
	% post_eq_constraint(ListOfCoefficients, ListOfVars, Const)
	% posts the constraint A1*V1 + A2*V2 + ... + AN*VN = Const.
	impure pred post_eq_constraint(list(float), list(V), Const),



My generic interface module defines a type lp_expr(V) which defines
expressions over linear solver var type V and implements the usual
arithmetic operations and a higher-level interface to the underlying
solver for type V.

I want to provide my own equality predicate for lp_expr(V) which
calls the post_eq_constraint method for type V.  If I write

:- type lp_expr(V) ---> ... where equality is eq.

:- pred eq(lp_expr(V), lp_expr(V)) <= linear_solver_var(V).

eq(A, B) :- ...

then I rightly get a compilation error saying that the type class
constraint on eq/2 is not met in the context of eq/2 as equality for

Unfortunately the language does not allow me to place type class
constraints on type definitions...


-- Ralph

[I recall that Richard O'Keefe made a good case some years ago for
allowing such things, but various technical reasons were stated
suggesting it was a bad idea (alas I haven't been able to dig up
that thread from the archives).]

mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au

More information about the developers mailing list