[mercury-users] Modes in "int" module.

Bartlomiej Szymczak rhywek at gmail.com
Tue Jul 10 21:59:12 AEST 2007


On 7/10/07, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> Bartlomiej Szymczak, Monday,  9 July 2007:
> > Hello.
> >
> > I don't understand one thing in the "int" library module:
> >
> >     :- pred int.max(int::in, int::in, int::out) is det.
> >
> >     :- func int + int = int.
> >     :- mode in  + in  = uo  is det.
> >
> > Both operations (max and +) have the same type from mathematical point
> > of view - they take two numbers and deterministically produce another
> > number.
> >
> > What I don't understand is why + produces result in mode uo. Shouldn't
> > it produce result in mode out, like max does?
>
> The uo mode is mainly there for hysterical raisins.
> Usually the output of an addition is a new integer, whereas the output
> of max is one of its inputs, hence the justification for uo for +, but
> not max.  In practice we don't properly support uniqueness yet (for which
> we're slightly embarrassed!), but we're hoping to get a PhD student on
> board to redo the mode system, at which point hopefully uniqueness will
> work properly.
>

Thanks for your answer, Ralph. :-)

I don't quite see how that fact can decide about uo or out mode. So
whenever a function outputs something which is not one of it's inputs,
it should make it uo? I thought uo is suited for outputs that should
be only used once (like arrays, hash tables), because they have state.

Should the result of addition be referenced just once? I don't quite see why.

> > Additionally I don't like that there are only functions to do some
> > things. It would be really nice for me if predicate versions were
> > provided.
>
> Our convention is that relations with mode (in, in, ..., in, out) is det
> should be functions and all others should be predicates.  Of course, we
> don't strictly stick to this for (a) historical reasons and (b) field
> access functions.

It's just that I prefer notation resembling predicate logic. And if I
use only predicates, without functions, I can use Prolog's top-level
to test my predicates. As far as I can remember, that's how first
version of Mercury compiler was bootstrapped. Functions just make it
impossible.

>
> > One more problem is that in the documentation the modes for int.plus
> > function are not shown:
> > :- func int.plus(int, int) = int.
>
> A func with no mode takes the default mode (in, in, ..., in) = out is det.
> It's a useful shorthand.

Yes, the shorthand is useful, but now I'm confused even more. It turns
out that "+" has "uo" output, but "plus" has "out" output, according
to the convention. Am I right?

It's really confusing, eg.:

         % addition
         %
     :- func int + int = int.
     :- mode in  + in  = uo  is det.
     :- mode uo  + in  = in  is det.
     :- mode in  + uo  = in  is det.

     :- func int.plus(int, int) = int.

I thought that plus has the same modes as +, just prefix syntax
instead of infix.

Do all other prefixed int operations produce "out" output? That would
be more convenient for me than "uo".

>
> -- Ralph
> --------------------------------------------------------------------------
> 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
> --------------------------------------------------------------------------
>

-- 
Bartlomiej Antoni Szymczak

Gmail: rhywek at gmail.com
Work mail: bas at imm.dtu.dk
DTU office: 321/129
DTU tel.: +4545253892
Mobile: +4520789323
--------------------------------------------------------------------------
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