[m-users.] Higher-order predicate issue

Sean Charles (emacstheviking) objitsu at gmail.com
Sat Oct 2 06:27:40 AEST 2021


Hi,

I am trying to pass around a predicate to execute at a later point in my translation process:

:- type renderer == (pred(instruction, lstring, rop_error, tcon, tcon)).
:- inst renderer == (pred(in, in, out, in, out) is det).

then to set the renderer:

    Renderer = target_c.render

then I have a work in progress evaluation predicate:

:- pred eval(renderer::in, location::in, instruction::in, lsnode::in,
    renderout::out, tcon::in, tcon::out) is semidet.

eval(Render, Pos, defvar, Args, Result, !T) :-
    trace[io(!Dbg), runtime(env("FELT_TRN"))]
    (sdump($pred, $line, Args, !Dbg)),

    ( if Args = [tk(Pos, VarName)] then
            %
            % this it not working well
            % ...what can be done to make it better ?
            %
348     Render(defvar, lstring(Pos, VarName), CodeM, !T),
        absorb(CodeM, Result, !T)

    else if Args = [ tk(VPos, VarName), _RHS ] then
        Render(defvar, lstring(VPos, VarName), CodeM, !T),
        absorb(CodeM, Result, !T)

    else
        add_error(Pos, defvar_form_error, !T),
357  Result = no
    ).

And I get the error:

translate.m:357: In clause for `eval(in, in, in, in, out, in, out)':
translate.m:357:   mode error in conjunction. The next 3 error messages
translate.m:357:   indicate possible causes of this error.
translate.m:357:   
translate.m:349:   In clause for `eval(in, in, in, in, out, in, out)':
translate.m:349:   mode error in conjunction. The next 2 error messages
translate.m:349:   indicate possible causes of this error.
translate.m:349:   
translate.m:348:   In clause for `eval(in, in, in, in, out, in, out)':
translate.m:348:   in argument 1 (i.e. the predicate term) of higher-order
translate.m:348:   predicate call:
translate.m:348:   mode error: variable `Render' has instantiatedness `ground',
translate.m:348:   expecting higher-order pred inst of arity 5.
translate.m:349:   
translate.m:349:   In clause for `eval(in, in, in, in, out, in, out)':
... error log truncated, see `translate.err' for the complete log.

and I fail to understand. It’s passed down from the top level code, I wrote a little test predicate to isolate the error:

:- pred testcall(renderer::in, tcon::in, tcon::out, io::di, io::uo) is det.
testcall(R,!T,!IO) :-
        (if R(include, lstring(pos(0,0,0),"testing"), Result, !T)
        then
            io.format("TESTED OUTPUT: %s", [s(string(Result))], !IO)
        else true).

This fails to compile but calling the Render within its defining scope works

    Renderer(include, lstring(pos(0,0,0), “test”), Result, !T) …

happily calls out to the relevant translation function. What’s the problem in handing it down as a parameter please ?

Thanks
Sean


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20211001/d560f38d/attachment.html>


More information about the users mailing list