[m-users.] Returning a predicate from a function

Mark Clements mark.clements at ki.se
Thu Dec 22 22:52:52 AEDT 2022


Dear Mercury users,

Is there any way to write a function that takes a predicate and returns a transformed predicate without (a) naming a new predicate or (b) using a lambda? In practice, I would like to move the lambda to a function. However, the following code gives an error: "Cannot unify two terms of type `(pred float)'". Can I assume that this is outside of Mercury's mode system?

Sincerely, Mark.

-----

:- module tests.

:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module float, ranges, solutions.
:- use_module math.

main(!IO) :-
    test1(!IO), % OK
    test2(!IO). % Does not compile

:- pred test1(io::di, io::uo) is det.
test1(!IO) :-
    Data = (pred(Y::out) is nondet :- nondet_member(I,range(1,5)), Y = float(I)), % some data
    LnData = (pred(Y::out) is nondet :- Data(X), Y = math.ln(X)), % a transformation
    aggregate(LnData, print_line, !IO).

:- pred test2(io::di, io::uo) is det.
test2(!IO) :-
    Data = (pred(Y::out) is nondet :- nondet_member(I,range(1,5)), Y = float(I)), % some data
    LnData = f_ln(Data), %% This transformation does not compile:(
    aggregate(LnData, print_line, !IO).

:- func f_ln(pred(float)::(pred(out) is nondet)) = (pred(float)::(pred(out) is nondet)).
f_ln(Pred) = (pred(Y::out) is nondet :- Pred(X), Y is math.ln(X)).




När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter. Här finns information om hur KI behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.


Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI's processing of personal data here<https://ki.se/en/staff/data-protection-policy>.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20221222/7f867369/attachment.html>


More information about the users mailing list