[m-users.] Problem with higher-order pred passed as argument

Sean Charles (emacstheviking) objitsu at gmail.com
Tue Jan 25 01:24:28 AEDT 2022


Hi,

I have created a virtual CPU in my transpiler called a TPU, text processing unit, it does the repetitive grunt work I need, and it has worked well so far but I now have a compiler error that whilst I understand what it means, I have failed to solve my problem. I might as well start with the declarations of things…

    % Processor program inlined-code
    %
:- type tpu_inject == (pred(tpu_state, tpu_state)).
:- mode tpu_inject == (pred(in, out) is det).

    % Processor Instruction Set.
    %
:- type tpu_inst
    --->    loads(ps)
    ;       vartype
    ;       identifier
    ;       callterm(felt_options, int, call_term)
    ;       t_indent(tpu_inject)
    .
    % Processor State.
:- type tpu_state
    --->    tpu_state(
                reg_ps1   :: ps,        % REGISTER PS1
                reg_str1  :: string,    % REGISTER STR1
                reg_str2  :: string,    % REGISTER STR2
                reg_type1 :: string,    % REGISTER TYPE1
                reg_fail  :: bool       % FAIL FLAG
            ).

A typical use of my processor is like this, the problem I have is that the final instruction, that renders indented text:

    tpu_runner(
        [ loads(ps(Pv, VName))
        , vartype
        , identifier
        , t_indent(
            (pred(!.State::in, !:State::out) is det :-
                trace[io(!Dbg), runtime(env("FELT_TRN"))]
                (
                    ttrace(0, "INLINED!!!! %s", [s(string(!.State))], !Dbg)
                )
            )
        )
        ],
        TPU, !Tmap, !Errors
    )

The compiler error I get concerns the actual implementation of the program loop code, I have a disjunction on the instruction type, the code to execute an inlined closure is this,

		:
		:
        ;
            Op = t_indent(Closure),  <= line 169
            Closure(!S)
        )

The line in bold is the source of the errors, those being that it says…

tpu.m:169:   In clause for `tpu_run(in, sub_identifier, sub_callterm, in, out,
tpu.m:169:   out, in, out)':
tpu.m:169:   in argument 1 (i.e. the predicate term) of higher-order predicate
tpu.m:169:   call:
tpu.m:169:   mode error: variable `Closure' has instantiatedness `ground',
tpu.m:169:   expecting higher-order pred inst of arity 2.

I pass higher-order functions around with no issues as I have the pred and mode declarations correctyl defined but in this case, WHY is the compiler not seeing it as a higher order function despite the datatype t_indent(tpu_inject) and how do I tell it so?

Thanks
Sean

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20220124/68275adb/attachment-0001.html>


More information about the users mailing list