[m-users.] Returning a predicate from a function
Mark Clements <[email protected]>
| Newsgroups | gmane.comp.lang.mercury.general |
|---|---|
| Message-ID | <GV3P280MB0001D8E5E033F55B62913DEAF0E89@GV3P280MB0001.SWEP280.PROD.OUTLOOK.COM> |
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>.
_______________________________________________
users mailing list
[email protected]
https://lists.mercurylang.org/listinfo/users