Did you know ... Search Documentation:
Pack dcgutils -- prolog/dcg_pair.pl
PublicShow source

This module contains predicates for using DCGs as a tool for stateful programming, where the state can be pair of arbitrary types. This enables DCGs to be 'composed'.

The type pair(A,B) will be used to denote the type of terms with functor (-)/2 and arguments of types A and B respectively:

pair(A,B) ---> A-B.

This type is used to support a set of general purpose predicates for combining commands in two distinct DCGs into a single DCG over a product space of states.

 \<(P:phrase(A), ?S1:pair(A,B), ?S2:pair(A,B)) is nondet
Apply phrase P to left part of a paired state.
 \>(P:phrase(B), ?S1:pair(A,B), ?S2:pair(A,B)) is nondet
Apply phrase P which must be of type pred(B,B) to right part of a paired state.
 <\>(PA:phrase(A), PB:phrase(B), ?S1:pair(A,B), ?S2:pair(A,B)) is nondet
Apply phrases PA and PB to paired states by applying PA to left state and PB to right state.
 run_left(P:phrase(pair(A,B)), ?A1:A, ?A2:A, ?B1:B, ?B2:B) is multi
Applies DCG phrase P to state formed by pairing A1 and A2 with current DCG states B1 and B2. Phrase can use (\<) to access the A state and (\>) to access the underlying B state.
 run_right(P:phrase(pair(A,B)), ?B1:B, ?B2:B, ?A1:A, ?A2:A) is multi
Applies DCG phrase P to state formed by pairing A1 and A2 with current DCG states B1 and B2. Phrase can use (\<) to access the A state and (\>) to access the underlying B state.
 \#(N:natural, P:phrase(A), ?S1, ?S2) is nondet
Apply phrase P to the Nth argument of state which must be a compound term (with arbitrary functor), with the Nth argument of type A.
 transduce(+T:phrase(pair(list(A),list(B))), ?L1:list(A), ?L2:list(B)) is nondet
Run a transducer between two lists of elements of type A and B respectively. Transducer is a DCG goal working on a state which is a pair of lists. It can use the facilities in this module to match sequences of elements in either list.