[mercury-users] Existential types and if-then-else

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Oct 7 14:19:29 AEST 1999


On 06-Oct-1999, Peter Ross <petdr at cs.mu.OZ.AU> wrote:
> Here is an existential type problem that I encountered today.
> 
> Basically if you have some semidet predicate which returns an
> existential type, T, you want to store that type in a maybe-like type 
> that has T in it if the predicate is succesful or indicates
> that the predicate has failed.
> 
> exist1.m uses maybe(T) with the existential quantification on the
> predicates.
...
> :- some [T1] pred p(maybe(T1)) => tc(T1).
> :- mode p(out) is det.
> p(X) :-
> 	(
> 		q(1, Y)
> 	->
> 		X = yes(Y)
> 	;
> 		X = no
> 	).
> 
> :- some [T2] pred q(int::in, T2::out) is semidet => tc(T2).

Here's what the compiuler reports:
	exist.m:027: In clause for `p(out)':
	exist.m:027:   in call to predicate `exist:q/2':
	exist.m:027:   scope error: attempt to bind a non-local variable
	exist.m:027:   inside the condition of an if-then-else.
	exist.m:027:   Variable `TypeClassInfo_for_tc' has instantiatedness `free',
	exist.m:027:   expected instantiatedness was `ground'.

The compiler is right to reject this predicate, because if q/2 were to fail,
then there would be no binding for the type variable T1.

> exist2.m works by getting rid of the if_then_else
...
> :- some [T1] pred p(T1) => tc(T1).
> :- mode p(out) is semidet.
> p(X) :-
> 	q(1, X).

Yes, in that case, if q/2 fails, then p/1 will fail; there is no possibility
of p/1 succeeding with the type variable T1 not bound.  So the compiler
is right to allow that.

> exist3.m uses maybe which has an existentially typed data constructor.
...
> :- type maybe
> 	--->	no
> 	;	some [T] (yes(T) => tc(T)).

That type definition should be allowed, but currently the compiler
reports some spurious errors in the automatically generated
unification/compare predicates for that type.  This is a compiler bug.
I'll have a look at it.

-- 
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