[m-rev.] for review: subtypes

Mark Brown mark at mercurylang.org
Tue Dec 31 13:58:57 AEDT 2013

On Tue, Dec 31, 2013 at 11:44 AM, Michael Day <mikeday at yeslogic.com> wrote:
> Hi Mark,
>> I have had a shot at implementing something along the lines of the old
>> subtypes proposal. It is for review by anyone. The log message is
>> attached, and the diff can be found at
> Do you have any examples of subtype usage?

Hi Michael,

Here's an example of a det function that takes the first element of
the first element of a non-empty list of non-empty lists. First, the
version not using subtypes:

  :- inst non_empty_list(I) ---> [I | list(I)].   % list.m only
defines non_empty_list/0

  :- func head_first_old(list(list(T))::in(non_empty_list(non_empty_list)))
      = (T::out) is det.

  head_first_old([[T | _] | _]) = T.

Since the first argument does not have the default mode, the full mode
and determinism needs to be provided. The inst is complex because
there is subtype information deeply nested.

Here's the version with subtypes:

  :- subtype non_empty_list(T) < list(T) ---> [ground | ground].

  :- func head_first_new(non_empty_list(non_empty_list(T))) = T.

  head_first_new([[T | _] | _]) = T.

This time we can use the default func mode, which is much more
concise. The deeply nested subtype information in the first argument
is propagated into the default 'in' mode, and the compiler can thereby
prove that the function is det.

For a larger example, see the attached version of parsing_utils.m
using subtypes.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: parsing_utils.m
Type: application/octet-stream
Size: 34270 bytes
Desc: not available
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20131231/c144193c/attachment.obj>

More information about the reviews mailing list