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:



aa aaa aaa aaa aaa aaa aaa 
		 		 [T : E1 in D1, $\ldots$, En in Dn, LocalVars,Goal] 
where the optional LocalVars specifies a list of local variables, and the optional Goal 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 is true after the local variables are renamed, 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 in 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 2013-01-25