[m-rev.] for discussion: subtypes documentation

Mark Brown dougl at cs.mu.OZ.AU
Wed Nov 27 03:49:07 AEDT 2002


On 26-Nov-2002, Mark Brown <dougl at cs.mu.OZ.AU> wrote:
> We don't have to worry about safety, because the transformed program
> (i.e., the program that results from processing all the subtypes) will
> still be mode analysed in the usual way.  All we have to worry about is
> that any error messages generated for the transformed program are
> translated back so that they make sense for the original program.

Here's an example.

--
:- type fruit ---> apple ; orange ; lemon.
:- subtype citrus < fruit ---> orange ; lemon.

:- type z_base(T) ---> z(pred(T, int)).
:- subtype z(T) < z_base(T) ---> z(pred(T, int) :: pred(in, out) is det).

:- pred p(fruit, z(fruit), int).
:- mode p(in, in, out) is det.

p(F, z(P), N) :-
	P(F, N).
--

The above is mode correct since a fruit is supplied as the first argument
of the call to P, and that is what is expected.  Now consider several
alternative pred declarations for p/3:

:- pred p(citrus, z(fruit), int).	% ok
:- pred p(fruit, z(citrus), int).	% mode error
:- pred p(citrus, z(fruit), int).	% ok

The second of these is a mode error because the call to P is not safe.

The module below shows how something like the above can be written
without subtype declarations.  This module compiles fine as it is, but if
the commented out mode is uncommented then the compiler will reject it.

Cheers,
Mark.

--
:- module t.
:- interface.

:- type fruit ---> apple ; orange ; lemon.
:- inst fruit == ground.
:- inst citrus ---> orange ; lemon.

:- type z(T) ---> z(pred(T, int)).
:- inst z(T) ---> z(pred(in(T), out(T)) is det).

:- pred p(fruit,	z(fruit),	int).
:- mode p(in(fruit),	in(z(fruit)),	out) is det.
:- mode p(in(citrus),	in(z(fruit)),	out) is det.
% :- mode p(in(fruit),	in(z(citrus)),	out) is det.
:- mode p(in(citrus),	in(z(citrus)),	out) is det.

:- implementation.

p(F, z(P), N) :-
	P(F, N).

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list