[mercury-users] define predicate with currying?

Julien Fischer juliensf at csse.unimelb.edu.au
Fri Jan 6 16:24:24 AEDT 2012


On Fri, 6 Jan 2012, Julien Fischer wrote:

> On Thu, 5 Jan 2012, Michael Hendricks wrote:
>
>> I have the following simple program:
>> 
>>    main(!IO) :-
>>        hello([s("World")], !IO),
>>        hello([s("Mercury")], !IO).
>> 
>>    :- pred hello(list(string.poly_type)::in, io::di, io::uo).
>>    hello(Xs,!IO) :- format("Hello, %s!\n",Xs,!IO).
>> 
>> Is it possible to define 'hello' by currying 'format'?  In Haskell,
>> I'd do something like
>> 
>>    hello :: String -> IO ()
>>    hello = printf "Hello, %s!\n"
>> 
>> The best I could come up with in Mercury was
>> 
>>    main(!IO) :-
>>        Hello = format("Hello, %s!\n"),
>>        Hello([s("World")], !IO),
>>        Hello([s("Mercury")], !IO).
>> 
>> Of course, that only defines 'Hello' locally.  I was hoping for a
>> global definition.
>
> In general, function composition of that style is bit less elegant in
> Mercury than it is in Haskell.  One reason for this is, of course, that
> not everything in Mercury is a function, in particular io.format/4 is a
> predicate and the Mercury compiler needs to be able to distinguish
> between the two.
>
> The closest I think we can get to the above Haskell in Mercury is
> a function that returns a curried predicate, something like the
> following:
>
>    :- func hello =
>        pred(list(string.poly_type), io, io)::out(pred(in, di, uo) is det)).
>
>    hello = format("Hello, %s\n").
>
>    main(!IO) :-
>      (hello)([s("World")], !IO),
>      (hello)([s("Mercury")], !IO).
>
> (Note that you need to parenthize a call to a zero-arity function in
> Mercury.)

Hmmm, I must have been asleep when I wrote that last bit.  You do not
need to parenthsize calls to zero-arity function in general, just in
this particular context.

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