It is easy to generalize AGs to support basic object oriented programming in a clean and efficient way. Instead of the hard-wired connect/3 relation of classic Prolog we can chain successive states of a an `object' transformed by applying to it an arbitrary method:
dcg_apply(Method):- term_append(Method,I,O,Goal), dcg_val(I), Goal, dcg_def(O).
For instance,
?-dcg_def(0),dcg_apply(+(10)),dcg_apply(*(3)),dcg_val(X).
will return X=30.
A more elaborate concept of object can be introduced by defining:
dcg_object(I>>M>>O,Init):- dcg_def(ob(I>>M>>O,Init)). dcg_next(O):- dcg_val(ob(IMO,I)),copy_term(IMO,I>>M>>O), M, dcg_def(ob(IMO,O)).
In this case executing
?-dcg_object(I>>(O is 10+I)>>O,0),dcg_next(X),dcg_next(Y).
returns X=10,Y=20.