A portable solution for threading state through a DCG can be implemented by wrapping the state in a list and use the DCG semicontext facility. Subsequently, the following predicates may be used to access and modify the state:84This solution was proposed by Markus Triska.
state(S), [S] --> [S]. state(S0, S), [S] --> [S0].