[mercury-users] Higher-order semideterminism in discriminated unions

Fergus Henderson fjh at cs.mu.OZ.AU
Sun Aug 31 20:20:01 AEST 2003


On 26-Aug-2003, Andr? Platzer <api at gmx.de> wrote:
> :- func cast_det(T::in) = (S::out) is det.
> cast_det(X) = Y :-
>    univ_to_type(univ(X), Y)
>    ;
>    error("casting failed").

This coding style should not be used, because you are assuming
that the compiler does not reorder disjunctions.  You should use
an if-then-else rather than a disjunction.
(Also, it's generally much easier to read programs if all disjunctions
and if-then-elses are enclosed in paretheses.)

So it would be better written as

	cast_det(X) = Y :-
	  ( univ_to_type(univ(X), Y1) ->
	  	Y = Y1
	  ;
	  	error("casting failed")
	  ).

or

	cast_det(X) = 
	  (if univ_to_type(univ(X), Y) then Y
	  else func_error("casting failed")).

> Erm, yes thanks. Of course you are right, in the simplified example I 
> have provided this really solves the problem. But in my real application 
> scenario, unfortunately this cannot be applied - as far as I know. 

One possibility in your real application scenario would be to have your
data structure store det functions that return a maybe(T) type rather
than being semidet.  The functions in most of your application could be
semidet, you'd just need to wrap them up in a lambda expression before
storing them in the data structure.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- 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