1

Consider a clause with cut

f(X) :- g(X), !, h(X).

I think it is a common sense that we can refactor cuts as follows:

h1(X) :- !, h(X).
f(X) :- g(X), h1(X).

However, for DCGs, this kind of macro-like refactoring for cuts doesn't seem to work. For instance, I tried to refactor a DCG clause like this

f(X) --> g(X), !, h(X).

into something like below

h1(X) --> !, h(X).
f(X) --> g(X), h1(X).

but it just didn't work. That is, failure in h(X) in the latter while it didn't backtrack in the former before refactoring the cut.

Is there a general principles when refactoring DCGs with cuts? Or, some additional dark corners to consider when using cuts inside DCGs?

Wheat Wizard
  • 3,982
  • 14
  • 34
kyagrd
  • 21
  • 4
  • 1
    Your first observation is flawed already. Take `g(1). g(2). h(2).` and query `?- f(X).` This originally fails, but succeeds in your refactored `f/1`. – false Nov 14 '16 at 16:19
  • 1
    Your first example is not equivalent if there where more clauses of `f/1`, e.g. take `f(X):-g(X),!,h(X). f(hello).` is not the same as `f(X):-g(X),h1(X). f(hello). h1(X):-!, h(X).` – gusbro Nov 14 '16 at 16:21
  • Oh, it doesn't even work with normal clauses. Thanks for both comments. – kyagrd Nov 14 '16 at 16:32

0 Answers0