I wrote this as a way to help limit solutions even though there are infinite solutions. But I realized there would be a way to rearrange the rules to get shorter results first.
Because ad --> a, ad, d.
is evaluated before ad --> bc.
it tries to satisfy ad --> a, ad, a.
before ad --> bc.
. I would put ad --> bc.
before ad --> a, ad, a.
. The same goes for the bc --> b, b, bc, c, c.
and bc --> [].
rules
As arian pointed out have the terminating rules applied first, would ensure the shorter solutions are found first.
I also want to point out that there are two solutions for []
s and s -> ad -> bc -> [] I would eliminate s --> [].
as it's redundant.
All in all I would try this solution:
s --> ad.
a --> [a].
b --> [b].
c --> [c].
d --> [d].
ad --> bc.
bc --> [].
ad --> a, ad, d.
bc --> b, b, bc, c, c.
ORIGINAL POST:
I had to look up how to do counting (it's been a while since I did prolog) But there are an infinite number and since prolog tries to find all solutions it never stops looking, although I'm sure you'll hit a stack over flow or some error before then :).
One way to limit the number of results is to limit the size of the solution
phrase(s, X), length(X, 4).
Gets all solutions with exactly 4 values, which would be
X = [a, a, d, d]
X = [b, b, c, c]
increasing to 6 would yield:
X = [a, a, a, d, d, d]
X = [a, b, b, c, c, d]
Or use ranges:
phrase(s, X), length(X, Y), Y >= 4 , Y < 10, Y != 6.