Fergus' hack (was Re: [mercury-users] polymorphism)

Bart Demoen bmd at cs.kuleuven.ac.be
Wed Oct 1 17:34:50 AEST 1997


Lee wrote

> It allows you to have more code reuse

I didn't understand this at first, especially not when one writes the
predicate like

	p(forward,...) :- ...
	p(backward,...) :- ...

I see no code reuse: it is the same code reuse as if you have defined
a predicate for each mode.

But there is code reuse if you write it in the form

	p(H,...) :-
		(H = forward ->
			.....
		;       .....).

because now you can add common code, like in

	p(H,...) :-
		something_in_common1,
		(H = forward ->
			.....
		;       .....),
		something_in_common2.


I wanted to explore thus but instead I went through a series of
programs (of which I give two) whose compilation unexpectedly (for me)
failed, all with the declarations:

:- type ab ---> (a ; b).

:- inst a = bound(a).
:- inst b = bound(b).

:- pred f(ab,int,int).
:- mode f(in(a),in,out).
:- mode f(in(b),out,in).

prog1
=====

f(Z,X,Y) :-
	T = 9,
        (Z = a ->
            Y is T + 1
        ;   X is T + 7
        ).


prog2
=====

f(Z,X,Y) :-
        (Z = a ->
            Y is  1
        ;   X is  7
        ).

---------------

the message was in every case something like

fergushack.m:032: In clause for `f(in(fergushack:a), in, out)':
fergushack.m:032:   mode mismatch in if-then-else.
fergushack.m:032:   `Y' :: unique(1), free.


???

Bart



More information about the users mailing list