Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use phrase in Prolog?

Tags:

prolog

dcg

I'm trying to learn more about DCGs in Prolog and while reading through different sources, I noticed that in some cases phrase wasn't being used ?

From the Learn Prolog Now website, it presents this example:

s   -->  np,vp. 
np  -->  det,n. 
vp  -->  v,np. 
vp  -->  v. 
det -->  [the]. 
det -->  [a]. 
n   -->  [woman]. 
n   -->  [man]. 
v   -->  [shoots].

They then use the query s(X,[]) to generate all the sentences in the grammar. I tried phrase(s,L) and it also generates all the sentences described by the grammar.

What's the difference in using s(X,[]) and phrase(s,L)? When should phrase be used?

like image 931
user5636966 Avatar asked Dec 03 '15 21:12

user5636966


1 Answers

Actually, SWI-Prolog performs type checking (arguments must proper lists), then exhibits a performance penalty when using phrase/3.

Also, to allow for using a DCG and perform 'state threading', that is handing off to hidden parameters the state propagation, a call_dcg/3 has been introduced.

But for normal usage - lexical parsing and generation - phrase is the way to go.

like image 123
CapelliC Avatar answered Oct 02 '22 23:10

CapelliC