[m-dev.] Ye Olde Subtyping Proposal
Ralph Becket
rafe at cs.mu.OZ.AU
Mon Nov 18 17:43:45 AEDT 2002
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. It's just (un)happy coincidence
that this module compiles at all.
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...:-)
- Ralph
--------------------------------------------------------------------------
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