[m-dev.] Ye Olde Subtyping Proposal
Fergus Henderson
fjh at cs.mu.OZ.AU
Mon Nov 18 18:04:32 AEDT 2002
On 18-Nov-2002, Ralph Becket <rafe at cs.mu.OZ.AU> wrote:
> Fergus Henderson, Monday, 18 November 2002:
> >
> > For example:
> >
> > :- module fruit.
> > :- interface.
> >
> > :- type fruit ---> apple ; orange ; lemon.
> > :- subtype citrus < fruit ---> orange ; lemon.
> > :- subtype favourite_fruit < fruit. % abstract
> >
> > :- func my_favourite = favourite_fruit.
> > :- func your_favourite = favourite_fruit.
> >
> > Suppose this is initially implemented as
> >
> > :- implementation.
> >
> > % no-one likes apples.
> > :- subtype favourite_fruit < fruit ---> orange ; lemon.
> >
> > my_favourite = lemon.
> > your_favourite = orange.
> >
> > and some other module uses this
> >
> > :- module fruit_comparison.
> > :- interface.
> > :- import_module io.
> > :- pred main(io::di, io::uo) is det.
> >
> > :- implementation.
> > :- import_module fruit.
> >
> > main --> print("My favour fruit is "),
> > print(comparison(my_favourite, your_favourite)),
> > print(" your favourite fruit."), nl.
> >
> > :- func comparison(citrus, citrus) = string.
> > comparison(orange, lemon) = "sweeter than".
> > comparison(lemon, orange) = "yellower than".
> > comparison(lemon, lemon) = "the same as".
> > comparison(orange, orange) = "the same as".
>
> This looks like it should be a type error to me: under the
> abstract-types-obey-name-subtyping rule we do not have that
> favourite_fruit < citrus.
Sure -- my point is that just putting the subtype definitions
in the `.int' files would not be sufficient to implement the
abstract-types-obey-name-subtyping rule.
> We would clearly need to add something to type checking to
> handle these cases, but I don't see why that would be a
> major effort. Of course, I haven't mucked about in the
> type checker yet...:-)
Actually subtypes would need to get checked in the mode checker, not the
type checker, because you need to know the direction of data-flow in
order to check subtypes. The mode checker already handles structural
subtyping, but it would require some non-trivial changes to support
by-naming subtyping for abstract insts.
--
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-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list