Entry level Question: DCGs over structured alphabets

Hi,
I am a novice in Prolog and I am currently looking into DCGs. Most of the (few) examples I have seen define grammars for unstructured alphabets. For instance the language a^nb^n or a^n or ab^n. However I would like to define grammars for structured alphabets. In such a setting every element in the produced sentence must assign a value to a set of symbols, e.g. {a, b, c}. An example sentence for example could be: (a = 1, b = 0, c = 0) ; (a = 0, b = 1, c = 1). One can implement this I imagine in various ways. I would just like to check whether there is some “prefered” or “standard” way of doing this. A minimal example:

as --> [].
as --> as, [a].

vs

aandbs --> [].
aandbs --> aandbs, [holds(a), holds(b)].

Which introduces the extra predicate holds/1. Just wondering whether there is a cleaner way of doing this.

It’s not clear to me what you’re asking. Do you want this?:

as([]) --> [].
as([1|Is]) -> [a], as.
as([2|Is]) -> [b], as.
as([3|Is]) -> [c], as.

which can probably be refactored to something like:

as([]) --> [].
as([I|Is]) --> [C], {one_as(C,I)}, as(Is).

one_as(a, 1).
one_as(b, 2).
one_as(c, 3).

Note that I changed your code from left-recursive (which will infinite loop unless you use tabling) to right-recursive.

(Also, you could use sequence//2 from library(dcg/high_order).)

My guess on what @nmag had in mind is structured/3 below
which is used like this.

?- structured(a^3*b^3, [a,a,a,b,b,b], []).

However I am not sure on my guess, because @nmag intends
to use rather like this, with variable N, and I could not write that version of ‘structured.’

?- structured(a^N*b^N, [a,a,a,b,b,b], []).

BTW, if I remember correct the language {a^n*b^n*c^n} is not context-free, proved by showing that ‘pumping lemma’
does not hold with it.

structured(_^0)-->[].
structured(X^J)-->{J>0}, [X], {K is J-1}, structured(X^K).
structured(X*Y)-->structured(X), structured(Y).
structured(X)-->[X].