DCG: foo(A1, A2, A3, ... , An) --> bar.
Prolog: foo(A1, A2, A3, ... , An, X, Y) :- bar(X,Y)
So, s
should be changed to:
s(X) --> first(X), operator, second.
first(X) --> [X].
operator --> ['+'].
second --> [X].
Of course, it might be better to return the actual result; to do this you should encapsulate prolog code in the DCG clause which is done with {}
:
s(Z) --> first(X), operator, second(Y), {Z is X+Y}.
first(X) --> [X].
operator --> ['+'].
second(X) --> [X].
(naturally, if you have more operators, the prolog code won't be that simple).
Regarding the do/4
predicate, it should be something like this:
do(X,Y,[H|T],Sum) -->
{H == 1, %check if H is 1
X = H,
Y = T,
Additional is H+5,
Sum is Additional+Additional}.
but I don't see why you would want that.
One last tip: it's recommended to use phrase/3
instead of adding the last two arguments in a DCG predicate.