Some Picat-like constructs for list comprehension in Prolog.
:- use_module(library(clpfd)).
goal_expansion(R=[Vars:-G|Gs], comprehend_list([Vars:-G|Gs],R)).
comprehend_membership(G) :-
G=in(A,[H|T]) -> member(A,[H|T])
; G.
comprehend_list(Goal, Bindings) :- findall(X, comprehend(Goal,X), Bindings).
comprehend([Vars :- G|Gs], VarsCp) :-
copy_term(Vars-[G|Gs], VarsCp-GsCp),
maplist(comprehend_membership, GsCp),
term_variables(VarsCp, Lbls),
catch(label(Lbls),_,true).
% plunit can be used to test solution
:- begin_tests(example).
:- include(example).
test(example_test) :-
R = [(A,I) :- A in [a,b], I in 1..2],
format("R = ~w.", [R]).
% ?- R = [(A,I) :- A in [a,b], I in 1..2].
% R = [(a,1),(a,2),(b,1),(b,2)].
:- end_tests(example).
The user defined custom arithmetic function should be visible from plunit.
But then the directive in the .pl Code should be recognized also!
Why won't it allow it?
:- arithmetic_function(double/1).
double(N,M) :- M is N*2.
% plunit can be used to test solution
:- begin_tests(example).
:- include(example).
test(example_test) :-
N is 9,
M is double(N+1),
assertion(20 is M).
:- end_tests(example).