Skip to content

DCG variable substitution #1949

Answered by UWN
mvolkmann asked this question in Q&A
Aug 3, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

In demo2//0 there is place for only a single character/term. If you want to include an arbitrary sequence, use seq//1. But beware, you are now having a definition that may not terminate! In fact:

demo3 -->
  "start ",
  seq(Middle),
  " end",
  { Middle = "middle" }.

?- phrase(demo3,Xs).
   Xs = "start middle end"
;  loops.

Note that you only see the loop, if you ask for the next answer.

Whereas

demo4 -->
  "start ",
  { Middle = "middle" },
  seq(Middle),
  " end".

?- phrase(demo4,Xs).
   Xs = "start middle end".

works as expected. To understand all of this, you need to master the notion of a failure slice. In this case, it would be

demo3 -->
  "start ",
  seq(Middle), {false},
  " end"

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mvolkmann
Comment options

Answer selected by mvolkmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants