Prolog predicate calling
Asked Answered
C

2

2

In the following tutorial: http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/7_3.html

There is the part:

test_parser :- repeat,
               write('?? '), 
               read_line(X),
               ( c(F,X,[])   | q(F,X,[])  ),
               nl, write(X), nl, write(F), nl, fail.

Now I'm extremely confused about the c(F,X,[]) and q(F,X,[]) part because it doesn't seem to match any thing that I have seen, c only takes one parameter from what I can tell and these parameters don't seem to make sense for q. Please help me understand what is going on here.

Confidence answered 31/1, 2012 at 19:17 Comment(0)
R
2

c is defined with -->, which actually adds two hidden arguments to it. The first of these is a list to be parsed by the grammar rule; the second is "what's left" after the parse. c(F,X,[]) calls c on the list X to obtain a result F, expecting [] to be left, i.e. the parser should consume the entire list X.

Riggs answered 31/1, 2012 at 20:8 Comment(0)
I
6

c//1 and q//1 are entry points (aka top level production) of the Definite Clauses Grammar defined below, where you find

c(F) --> ....
q(F) --> ....

This style of 'call' on a DCG entry point is discouraged, usually is better to invoke the phrase(Grammar, TextToAnalyze, TextAfterAnalysis), in this case phrase((c(F) ; q(F)), "some text", "")...

The --> operator is usually rewritten adding 2 arguments, that are cause of your concern.

EDIT

I.e. c(L) --> lead_in,arrange(L),end.

is rewritten to

c(L,X,Y) :- lead_in(X,X1),arrange(L,X1,X2),end(X2,Y).

Incombustible answered 31/1, 2012 at 19:26 Comment(1)
That didn't exactly help me understand the point of having q(F,X,[]). Is there any other way you can try to explain what you are trying to say?Confidence
R
2

c is defined with -->, which actually adds two hidden arguments to it. The first of these is a list to be parsed by the grammar rule; the second is "what's left" after the parse. c(F,X,[]) calls c on the list X to obtain a result F, expecting [] to be left, i.e. the parser should consume the entire list X.

Riggs answered 31/1, 2012 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.