[mercury-users] Human beats compiler?

Julien Fischer juliensf at csse.unimelb.edu.au
Sun Jan 4 23:34:21 AEDT 2009

On Sun, 4 Jan 2009, Michael Day wrote:

> This hand-rolled C predicate appears to work, and is almost as short as the 
> original Mercury implementation. Are there any obvious problems?

I'd be interested in how far the current mode analyser allows you take
this approach.  For list(int) it's probably okay, for lists of
non-atomic types it might get "interesting".

> :- pred filter_neg(list(int), list(int)).
> :- mode filter_neg(di, uo) is det.
> :- pragma foreign_proc(c,
>        filter_neg(Xs::di, Ys::uo),
>        [will_not_call_mercury, thread_safe, promise_pure], "
>    Ys = Xs;
>    while (Xs != 0)
>    {
>        MR_Integer X = (MR_Integer) MR_hl_field(MR_mktag(1), Xs, 0);
>        MR_Word Xs0 = (MR_Word) MR_hl_field(MR_mktag(1), Xs, 1);
>        if (X < 0)
>        {
>            MR_hl_field(MR_mktag(1), Xs, 0) = 0;
>        }
>        Xs = Xs0;
>    }
> ").
> It surprised me that this code was grade independent; at least it seems to 
> work fine in asm_fast.gc as well as hlc.gc. Are there any issues that could 
> pop up with this kind of code in certain grades?

Well, it certainly won't work in the java, il and erlang grades ;-)

It works in both the asm_fast and hlc grades because they both use
the same data representation.  It won't work in grades that use 
--high-level-data, e.g. hl.gc.  (Although, if you use MR_list_cons
and friends from the runtime instead of MR_hl_field and MR_mktag
directly, you could probably write a version that does.)

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