Higher-Order Predicates¶
Higher-order predicates take a goal (predicate or lambda) as an argument and apply it to list elements. Combined with lambdas, they give Clausal Prolog a functional programming feel.
Clausal Prolog and the seam
Examples on this page are Clausal Prolog
(.clausal), ISO Prolog syntax. An inline lambda is written with
library(lambda): \X^Goal takes one argument, \X^Y^Goal two. The
seam, Clausal's Python-syntax surface, writes the same
lambdas as anonymous clauses, (X <- Goal) and ((X, Y) <- Goal)
(see Lambdas).
Quick Example¶
:- use_module(library(clpz)).
:- use_module(library(lambda)).
double(X, Y) :-
Y #= X * 2.
test("double all") :-
maplist(double, [1, 2, 3], [2, 4, 6]).
test("keep evens") :-
include(\X^(X mod 2 =:= 0), [1, 2, 3, 4, 5, 6], [2, 4, 6]).
Calling Goals¶
call/1..8 and call_goal/1..8¶
call(Goal) invokes a goal. call(Goal, A1, ..., AN) appends extra arguments.
call_goal is an alias.
:- use_module(library(clpz)).
:- use_module(library(lambda)).
test("call/1") :-
call(\X^(X #= 42), 42).
test("call/2") :-
call(\X^Y^(Y #= X * 2), 5, 10).
The goal can be a lambda, a predicate name, or a partial goal term (a cell such as add(1), which call extends with the extra arguments).
Mapping¶
maplist/2¶
maplist(Goal, List) — test Goal(Elem) for every element. Succeeds if the
goal succeeds for all elements, and backtracks into every call, as the ISO
prologue's definition by call/N does: with p(1), p(2), p(3),
maplist(p, [X, Y]) has nine answers. On an OPEN list (unbound, or a
partial list [a|T]) it enumerates as that definition does, depth first
and without end: maplist(p, L) answers L = [], L = [1], L = [1, 1],
...
maplist/3¶
maplist(Goal, Xs, Ys) — transform each element via Goal(X, Y); like
maplist/2 it backtracks into every call.
:- use_module(library(clpz)).
square(X, Y) :-
Y #= X ^ 2.
test("squares") :-
maplist(square, [1, 2, 3, 4], [1, 4, 9, 16]).
With an inline lambda:
:- use_module(library(clpz)).
:- use_module(library(lambda)).
test("squares inline") :-
maplist(\X^Y^(Y #= X ^ 2), [1, 2, 3, 4], [1, 4, 9, 16]).
Filtering¶
include/3¶
include(Goal, List, Included) — keep elements where Goal(Elem) succeeds.
exclude/3¶
exclude(Goal, List, Kept) — keep elements where Goal(Elem) fails.
The inverse of include.
filter_map/3¶
filter_map(Goal, List, Result) — map and filter in one pass. Keep the output
value when Goal(Elem, Out) succeeds; skip elements where it fails.
safe_sqrt(X, Y) :-
X >= 0,
Y is X ** 0.5.
test("filtermap") :-
filter_map(safe_sqrt, [4, -1, 9, -2, 16], [2.0, 3.0, 4.0]).
Folding¶
foldl/4, foldl/5, foldl/6¶
foldl(Goal, List, V0, V) — left fold. Applies Goal(Elem, Acc, NewAcc)
across the list, threading an accumulator from V0 to V.
foldl(Goal, Xs, Ys, V0, V) calls Goal(X, Y, Acc, NewAcc) over two lists
of one length, and foldl/6 over three, as Scryer's library(lists).
Like maplist, foldl backtracks into every call (each solution of the goal
gives another fold), and an open list enumerates: foldl(G, L, 0, S) with
L unbound answers L = [] first, then longer lists.
:- use_module(library(clpz)).
add_step(X, ACC, OUT) :-
OUT #= ACC + X.
test("sum") :-
foldl(add_step, [1, 2, 3, 4], 0, 10).
With an inline lambda:
:- use_module(library(clpz)).
:- use_module(library(lambda)).
test("sum inline") :-
foldl(\X^ACC^OUT^(OUT #= ACC + X), [1, 2, 3, 4], 0, 10).
Prefix & Suffix¶
take_while/3¶
take_while(Goal, List, Prefix) — longest prefix where Goal(Elem) succeeds.
:- use_module(library(lambda)).
test("takewhile") :-
take_while(\X^(X < 5), [1, 3, 7, 2, 4], [1, 3]).
drop_while/3¶
drop_while(Goal, List, Suffix) — drop the prefix where Goal(Elem) succeeds.
:- use_module(library(lambda)).
test("dropwhile") :-
drop_while(\X^(X < 5), [1, 3, 7, 2, 4], [7, 2, 4]).
span/4¶
span(Goal, List, Yes, No) — take_while + drop_while in one pass.
:- use_module(library(lambda)).
test("span") :-
span(\X^(X < 5), [1, 3, 7, 2, 4], [1, 3], [7, 2, 4]).
Grouping & Sorting¶
group_by/3¶
group_by(Goal, List, Groups) — group consecutive elements by key.
Goal(Elem, Key) extracts the grouping key.
first_char(S, C) :-
sub_atom(S, 0, 1, _, C).
test("group by first char") :-
group_by(first_char, [apple, avocado, banana, blueberry, cherry], [[apple, avocado], [banana, blueberry], [cherry]]).
Note: groups are consecutive runs, not global grouping. sort first if you need global groups.
sort_by/3¶
sort_by(Goal, List, Sorted) — sort by projected key.
abs_key(X, K) :-
abs_(X, K).
test("sort by abs") :-
sort_by(abs_key, [3, -1, -4, 2], [-1, 2, 3, -4]).
max_by/3, min_by/3¶
max_by(Goal, List, max_) / min_by(Goal, List, min_) — element with the
largest/smallest projected key.
(The key goals use the ISO builtins sub_atom/5 and atom_length/2; the
seam versions of these examples reach into Python instead, C is ++S[0]
and K is ++len(S) — see Python interop.)
Patterns & Recipes¶
Map then filter (pipeline)¶
:- use_module(library(clpz)).
:- use_module(library(lambda)).
sq(X, Y) :-
Y #= X * X.
test("pipeline") :-
maplist(sq, [1, 2, 3, 4, 5], SQUARES),
include(\X^(X > 10), SQUARES, BIG),
BIG == [16, 25].
flatten via foldl¶
:- use_module(library(lambda)).
test("flat") :-
foldl(\CHUNK^ACC^OUT^append(ACC, CHUNK, OUT),
[[1, 2], [3], [4, 5]],
[],
[1, 2, 3, 4, 5]).
Count occurrences¶
:- use_module(library(lambda)).
count(PRED, LIST, N) :-
include(PRED, LIST, MATCHED),
length(MATCHED, N).
test("count evens") :-
count(\X^(X mod 2 =:= 0), [1, 2, 3, 4, 5, 6], 3).
Gotchas¶
- Goal argument order matters —
maplist/3callsGoal(X, Y)where X is input, Y is output.foldl/4callsGoal(Elem, AccIn, AccOut). ^is not a goal —Y^Goalmeans something only as bagof/setof's goal argument, or as a lambda parameter after\;call(Y^p(Y))isexistence_error(procedure, (^)/2), as in ISO and Scryer.group_bygroups consecutive runs — not global grouping. sort first if needed.include/excludeare committed-choice — they test each element once (first solution only) and do not backtrack into the goal.maplistdoes.- Lambda syntax — load
library(lambda); single-arg:\X^Body, multi-arg:\X^Y^Body. In the seam:(X <- (body))and((X, Y) <- (body)). See Lambdas for details. call/Nappends arguments —call(foo, 1, 2)callsfoo(1, 2).
See also: Lambdas — goal closure syntax, Meta-Predicates — findall, bagof, setof, Lists — list operations.