[mercury-users] Can this be done better?

Michael Richter ttmrichter at gmail.com
Tue Jan 31 02:09:12 AEDT 2012


On 30 January 2012 22:03, Julien Fischer <juliensf at csse.unimelb.edu.au>wrote:

> The compiler's support for inst subtyping like this is a bit flakey.
> Nevertheless, it's possible to write a version of apply/3 that only
> accepts input lists of even length -- depending on the circumstances
> it may not be possible to call it however.
>

That sounds ominous…  :)


>    :- inst evenlist == bound([] ; [ground | oddlist]).
>    :- inst oddlist  == bound([ground | evenlist])
>

That's the kind of stuff I was toying with, but …


> Unfortunately, the above won't work since to use the new definition the
> _compiler_ needs to be able to prove that the output of the call to
> list.zip/2 also has the inst evenlist.  (Which is not possible given
> the definition of zip/2.)
>

…this was the bar my head kept hitting.


>    main(!IO) :-
>        Forward = [math.sin, math.cos, (func(X) = math.ln(X))],
>        Reverse = [math.asin, math.acos, (func(X) = math.exp(X))],
>        Results = list.map_corresponding(
>           (func(F, R) = compose(R, F, 0.5)), Forward, Reverse),
>
>        io.write_list(Results, ", ", io.write_float, !IO),
>        io.write_string("\n", !IO).
>

*THIS* is awesome, however!  There's a lot of stuff in the standard
library, but the documentation is … a wee bit terse.  I completely
overlooked that one.  I'm going to have to spend a few years on the library
docs it seems.  :)


>    main(!IO) :-
>        Forward = [math.sin, math.cos, (func(X) = math.ln(X))],
>        Reverse = [math.asin, math.acos, (func(X) = math.exp(X))],
>        apply(assoc_list.from_**corresponding_lists(Forward, Reverse),
>
>            [], Results),
>        io.write_list(Results, ", ", io.write_float, !IO),
>        io.write_string("\n", !IO).
>
>     :- pred apply(assoc_list((func(float) = float)), list(float),
>
>       list(float)).
>     :- mode apply(in, in, out) is det.
>
>    apply([], A, Results) :- Results = A.
>    apply([F - R | Functions], A, Results) :-
>
>        apply(Functions, [compose(R, F, 0.5) | A], Results).
>

I'm not sure I understand this one at all, however.  I mean I understand my
apply/3 predicate still, even with the funky F - R match there, but would
you mind walking me through the rest of this a bit?

-- 
"Perhaps people don't believe this, but throughout all of the discussions
of entering China our focus has really been what's best for the Chinese
people. It's not been about our revenue or profit or whatnot."
--Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20120130/cdbcd176/attachment.html>


More information about the users mailing list