List comprehensions

A list comprehension is a construct for building lists in a declarative way. List comprehensions are very common in functional languages such as Haskell, Ocaml, and F#. We propose to introduce this construct into Prolog.

A list comprehension takes the form:


		 		 [$T$ : $E_1$ in $D_1$, $\ldots$, $E_n$ in $D_n$, $LocalVars$,$Goal$] 
where $LocalVars$ (optional) specifies a list of local variables, $Goal$ (optional) must be a callable term. The construct means that for each combination of values $E_1 \in D_1$, $\ldots$, $E_n \in D_n$, if the instance of $Goal$, after the local variables being renamed, is true, then $T$ is added into the list.

Note that, syntactically, the first element of a list comprehension, called a list constructor, takes the special form of T:(E in D). A list of this form is interpreted as a list comprehension in calls to '@='/2 and constraints in B-Prolog.

A list comprehension is treated as a foreach call with an accumulator. For example, the query L@=[(A,I) : A in [a,b], I in 1..2] is the same as

    foreach(A in [a,b], I in 1..2, ac1(L,[]),L^0=[(A,I)|L^1]).



Subsections
Neng-Fa Zhou 2012-01-03