Lists of ground terms can also be denoted by a list comprehension expression. Examples are:
[X**2::X in L & not X in [0,1]] % Squares of numbers other than 0,1 in nums list L [C :: exists A (child_of(C,tom) & age_of(C,A) & A<18)] % Non-adult children of tom in order found
The general form of a list comprehension is:
[Expression :: exists VarSequence Condition]
where the exists VarSequence
is optional.
There are constraints on the variables that can be used in such a comprehension. Each variable in Expression
must either appear in Condition
and be such that it will be given a ground value by some call in the conjunction, or it must appear before the comprehension expression in a query or rule and will have been given a ground value. Every variable in Condition
must either be underscore, appear in Expression
or in VarSequence
, or must appear before the comprehension expression in a query or rule and will have been given a ground value. This ensures that the value of a list comprehension is always a list of ground terms.
VarSequence
is a single variable or a comma separated sequence of variables such as X,Y,Z
The syntax for Condition
is given below.