[mercury-users] Another place for existentials?
Fergus Henderson
fjh at cs.mu.OZ.AU
Wed Aug 2 12:53:03 AEST 2000
On 01-Aug-2000, Ralph Becket <rbeck at microsoft.com> wrote:
> I've been coding up a framework for playing around with CSPs using
> typeclasses. What I wanted was to express the fact that a csp
> typeclass had to be related to a node typeclass, so I wrote something
> like
>
> :- typeclass csp(CSP, Node) <= node(Node) where [ ... ].
>
> where the node/1 typeclass was quite independent and defined
> elsewhere.
>
> I then ran into problems when I wrote a predicate that required
> its argument to be a csp/2, but didn't care about the type of the
> node it was related to. My first attempt was:
>
> :- func solve(CSP) = CSP <= csp(CSP, _SomeNode).
>
> The compiler didn't like this because the typeclass constraint
> contained variables that didn't appear on the LHS.
The problem here is that there can be more than one Node
type for each CSP type, e.g.
:- instance csp(my_csp, int).
:- instance csp(my_csp, string).
and the compiler has no way of knowing which Node type you want to use.
One solution is to pass an unused parameter of the Node type,
which allows the caller to specify what Node type to use:
:- func solve(CSP, Node) = CSP <= csp(CSP, Node).
:- mode solve(in, unused) = out is det.
...
main -->
{ node_type(DummyNode) },
{ Sol = solve(CSP, DummyNode) },
...
% this predicate just constrains the type of its
% argument to be `int'
:- pred node_type(int::unused) is det.
node_type(_).
--
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.
--------------------------------------------------------------------------
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