The --> symbol --> used in many Prolog implementations to create Declarative Clause (DCG) grammar rules that take the form:
head --> body.
similar to the usual Prolog rules:
head :- body.
In fact, every DCG rule can be translated into a normal Prolog rule (and is internally), but the DCG syntax serves as a convenient and very powerful shortcut for creating rules that link lists to different Prolog structures. Often, DCG rules are used for a rather limited purpose of list analysis.
The question asked here is to give a simple usage example --> , in other words, showing how DCG rules work in a simple case. The head of the DCG rule is effectively a predicate of the underlying Prolog rule with two additional arguments that are a list of differences, namely one list presented as a longer list, minus some end part of this longer list.
Here's a DCG example taken from the DCI SWI-Prolog tutorial adapted by Anne Oborn from the Marcus Trisk textbook cited in a comment by Boris :
as --> [ ]. % empty list is okay as --> [a], as. % list [a|T] is okay iff T is okay
To distinguish this from the usual Prolog predicate, we denote this as//0 , but it is equivalent to the normal Prolog predicate with two additional arguments. We can query the basic Prolog predicate directly by providing two additional arguments:
?- as([ ],[ ]). true
This succeeds because the difference between the two lists (which is again an empty list) is in the order as//0 . Also:
?- as([a],[ ]). true
succeeds because the difference between the two lists is [a] , which is quite consistent with recursion with as//0 .
Another way to use as//0 is the built-in predicate Prolog phrase/2 , which takes a DCG header as a first argument and a list as a second argument. In this case, phrase/2 will generate lists matching as//0 :
?- phrase(as, Ls). Ls = '[]' ; Ls = [a] ; Ls = [a, a] ; Ls = [a, a, a] ;
etc. until you complete a successful request by clicking on return.
This example also works with Amzi! Prologue with slight differences in output.