10

What does the --> operator do in Prolog and what is the difference between it and :-?

I'm using SWI Prolog.

repeat
  • 18,496
  • 4
  • 54
  • 166
Joel Lindholm
  • 101
  • 1
  • 4

2 Answers2

6

It is used to define a DCG (Definite Clause Grammar) rule as opposed to a normal predicate. See this tutorial for a very nice explanation of DCG rules.

There are many examples here on Stack Overflow. See the DCG tag.

Here is one very simple solution, showing both the DCG and the normal predicate.

Note that you have to use phrase/2 or phrase/3 to evaluate a DCG. Make sure to read the section of the SWI-Prolog manual on DCGs. The two phrase predicates are documented there.

Community
  • 1
  • 1
2

The --> operator reads in Definite Clause Grammar rules. It's syntactic sugar to transform rules of the form:

parenthesized_expression(Inner) -->
  [ open ],
  expression(Inner),
  [ close ],
  { nl }.

Into something more like this:

parenthesized_expression(Inner, [open | T1], T2) :-
  expression(Inner, T1, [close | T2]),
  nl.

This makes writing grammars very convenient. There are helper predicates available to consume them, though you're allowed to do so by hand if you prefer.

JB.
  • 40,344
  • 12
  • 79
  • 106
  • 1
    Anonymous downvoter, you're more than welcome to explain what you think is wrong with my answer so we can all learn from your expertise :-) – JB. Sep 15 '15 at 13:49
  • 2
    Some issues I see: (1) There are several mistakes in your expansion. Please compare your code with the actual expansion used in Prolog systems, paying particular attention to steadfastness issues. (2) The expansion may, but need not, occur. Therefore, you cannot rely on this transformation. To keep the code portable, it is better to treat DCGs as a formalism on its own, without relying on any particular expansion method. (3) "reads in" is misleading, and "consume" is too one-sided and imperatively worded. To fix these issues, please see Boris's contribution for a careful and thorough answer. – mat Sep 15 '15 at 21:59
  • @mat Thank you very much for actually taking the time to answer. (1) The expansion was absolutely not intended as literal (hence "more like"). I actually dumbed it down from an actual expansion so it didn't end up too long. (2) It didn't read that way to me from the docs (I'm mostly SWI-biased). When can it not happen? (3) Unfortunate as it may be, the imperative wording was kind of deliberate. I was aiming for explaining, not defining. (4) The reason I posted despite Boris's preexisting answer is that it read like a bunch of links with no explanation to me. – JB. Sep 15 '15 at 22:13
  • 1
    Also, to see how one can go about the expansion, check out the great [DCG reference implementation](http://www.complang.tuwien.ac.at/ulrich/iso-prolog/dcgs/dcgs_exp.pl). I hope this helps you to get the expansion right. – mat Sep 15 '15 at 22:14
  • 2
    In SWI, the expansion does occur. However, it is still important not to rely on it: First, for portability to other Prolog systems, and second, to enable optimisations in the (far) future with new SWI versions. For example, for efficiency, it will make sense to actually flip the arguments in the expansion, and using the `phrase/2` interface to DCGs ensures that your code will work without changes if the DCG implementation changes behind the scenes in the future. – mat Sep 15 '15 at 22:20
  • 1
    I get your point. I'm thankful for the time you took to make it. But I don't think a fully accurate rule expansion and a normative consumption guide constitute an appropriate answer to a "difference between `:-` and `-->`" question. (link answers frowned upon no matter what). I'll sleep over it in any case. And likely add some disclaimer to clear up the "explanation" vs "this is not 100% accurate" situation in the morning. If you're up to it, I *would* like to know *how* not using `phrase/2` could actually blow up. I don't use DCGs every day, but FBOW that's how they were taught to me. – JB. Sep 15 '15 at 22:51
  • 1
    Just to explain the "link-only answer": the question is definitely **not** a good question for a forum like Stack Overflow. With such a question, I see two options: 1) close it, which I dislike and avoid doing or 2) show OP where they can get the information they need, since this is the nice thing to do. Any attempt at actually _answering_ such a broad question will end up being a crappy summary of the linked material (not saying that _your answer_ is crappy, but it is definitely not going to save OP from actually reading). Just my opinion. –  Sep 16 '15 at 07:34