[mercury-users] State variables as function arguments.

Ralph Becket rafe at csse.unimelb.edu.au
Mon Feb 12 09:24:53 AEDT 2007


Julian Fondren, Friday,  9 February 2007:
> The Language Reference Manual says:
> 
>  `!X' may not appear as an argument in a function application
>  since this would not make sense given the usual interpretation
>  of state variables and functions.
> 
> What is the 'usual interpretation' of these?

State variables are shorthand for sequances of argument pairs for which
one can give an imperative reading.

Functions, in the (...) = out direction are usually interpreted in the
same way as for other functional languages (e.g., they are
deterministic, they are referentially transparent, etc.)  We don't
currently enforce these constraints outside our coding standards, but I
think we ought to.

> I've regarded
> state variables as only a convenience -- so that I can instead
> of:
> 
>  :- func test(string, io, io) = int.
>  :- mode test(in, di, uo) = out is det.
>  test(S, IO1, IO) = test(S, 1, IO1, IO).

If the result mode of a function is `out', then it's almost certainly a
Bad Idea to treat the function as a relation in the sense of also having
some of its `arguments' as outputs.  It makes reasoning about functions
much harder.

The code you've written above is very confusing because of the
functional notation.  Use preds to avoid this sort of problem.

> (which Mercury doesn't mind)

But it should!  We really ought to make the compiler issue an error for
things like this.

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



More information about the users mailing list