Re: [m-users.] Returning a predicate from a function
Mark Clements <[email protected]>
| Newsgroups | gmane.comp.lang.mercury.general |
|---|---|
| Message-ID | <GV3P280MB0001B8A43E75A4091F6C97D3F0E89@GV3P280MB0001.SWEP280.PROD.OUTLOOK.COM> |
Thank you everyone -- that has been very helpful. I admit that I was using an older version of Mercury (20.06.1). On that version, I *was* able to use either `=` or `is` for the mathematical equations. I have now installed the latest release. I admit that I was using an older version of Mercury (20.06.1). On that version, I *was* able to use either `=` or `is` for the mathematical equations. I have now installed the latest release. Julien: that was the answer that I was hoping for:). Tomas: your code is elegant. I understand that lists or arrays would be quite suitable for the given example -- but my motivating case is for doing bag aggregates on (nondet) relationships (see https://rosettacode.org/wiki/Merge_and_aggregate_datasets#Mercury). [https://static.miraheze.org/rosettacodewiki/d/d3/RosettaCodeTitle.png]<https://rosettacode.org/wiki/Merge_and_aggregate_datasets#Mercury> Merge and aggregate datasets - Rosetta Code<https://rosettacode.org/wiki/Merge_and_aggregate_datasets#Mercury> Merge and aggregate datasets Task. Merge and aggregate two datasets as provided in .csv files into a new resulting dataset. Use the appropriate methods and data structures depending on the programming language. rosettacode.org With the recent interest in Verse (https://news.ycombinator.com/item?id=33946933), which combines nondeterminism and functional aspects, I hope there will be increased interest in Mercury. Sincerely, Mark. ________________________________ From: Tomas By <[email protected]> Sent: 22 December 2022 16:13 To: Mark Clements <[email protected]> Cc: [email protected] <[email protected]> Subject: Re: [m-users.] Returning a predicate from a function [You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ] On Thu, 22 Dec 2022 12:52:52 +0100, Mark Clements wrote: > 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)). I'm not 100% sure what you mean for this to do. In particular I am not sure what "aggregate" does (in Python I assume). Some general points: maths is not nondet; the basic data structure is a list. not some abstract generator as in some other languages that I could mention; you do not always need all these declarations. This seems to work: |:- module test. | |:- interface. |:- import_module io. |:- pred main(io::di, io::uo) is det. | |:- implementation. |:- import_module int, float, list. |:- use_module math. | |main(!IO) :- | test1(!IO), | test2(!IO). | |:- pred test1(io::di, io::uo) is det. | |test1(!IO) :- | Data = map(float,1..5), | LnData = map(math.ln,Data), | write_list(LnData,"",print_line,!IO). | |:- pred test2(io::di, io::uo) is det. | |test2(!IO) :- | Data = map(float,1..5), | LnData = map(f_ln,Data), | write_list(LnData,"",print_line,!IO). | |:- func f_ln(float::in) = (float::out) is det. | |f_ln(F0) = F :- F = math.ln(F0). | |:- end_module test. Is this what you meant? /Tomas 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