[mercury-users] mode/inst etc

Mark Brown mark at csse.unimelb.edu.au
Thu Jan 3 08:57:58 AEDT 2008


Hi,

On 02-Jan-2008, Tomas By <tomas at basun.net> wrote:
> Hello,
> 
> Suppose I have a type:
> 
>   :- type tree(K,V) ---> empty ; tree(K,V,tree(K,V),tree(K,V)).
> 
> and a procedure that is only ever called for a non-empty tree:
> 
>   :- pred dosomething(tree(K,V)::in,K::out,V::out) is det.
> 
> I could write
> 
>   dosomething(tree(K,V,_,_),K,V).
>   dosomething(empty,_,_) :- error("Tree is empty").
> 
> but it would be nicer if I could tell the compiler to not allow the tree
> to be empty. Perhaps something like
> 
>   :- mode intree == in(tree(bound,bound)).
> 
>   :- pred dosomething(tree(K,V)::intree,K::out,V::out) is det.
> 
> Is this possible?

Yes.

> What should the code like?

You use an inst declaration, but do not specify all of the constructors.
For example, if `ground' will suffice for the keys aand values, then you
can use:

    :- inst non_empty_tree ---> tree(ground, ground, ground, ground).

Then intree would be:

    :- mode intree == in(non_empty_tree).

Cheers,
Mark.

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list