Corollary
Since DCGs are syntactic sugar for predicates they too can be given operators for names. This works as expected when calling them as DCGs either from a DCG or using the phrase
predicates or others that are designed to work with DCGs. When calling them as predicates parentheses are required (eg A+B-->...
must be called like +(A,B,...)
) since DCG predicates take an additional two arguments for their difference lists. For operator named DCGs with more than two arguments using operator pattern matching then it is important to make sure when calling it as a predicate that the pattern matched operators are distributed correctly.
Giving operator names to DCGs that take no additional arguments can be useful if you need to call them within your program since then you can do so without using parentheses. Caution is required because it can be the case that what you save in parentheses you can lose to added spacing required to parse adjacent operators.
CaveatsExamples
/ -->a+b+X,X+d+e.
A+B+C-->[A],[B],[C].
X/[],member(c,X),phrase(f+o+o,Y),+(b+a,r,Z,[]).
Output will be
X = [a, b, c, c, d, e],
Y = [f, o, o],
Z = [b, a, r].
Try it online!
Caveats