Getting hold of a variable in complex compound term in Prolog

这一生的挚爱 提交于 2019-12-23 03:07:22

问题


I have a Prolog sentence parser that returns a sentence (passed into it as a list) split into two parts - a Noun_Phrase and a Verb_Phrase. See example below:

sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))) :- 
        np(Sentence, Noun_Phrase, Remainder),  
        vp(Remainder, Verb_Phrase). 

Now I want to take the Noun_Phrase and Verb_Phrase and pass them into another Prolog predicate, but first I want to extract the first term from the Verb_Phrase (which should always be a verb) into a variable and the rest of the Verb_Phrase into another one and pass them separately into the next predicate.

I thought about using unification for this and I have tried:

sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))),
     [Head|Tail] = Verb_Phrase, 
     next_predicate(_, Noun_Phrase, Head, Tail, _).

But I am getting ERROR: Out of local stack exception every time. I think this has something to do with the Verb_Phrase not really being a list. This is a possible isntance of Verb_Phrase:

VP = vp(vp(verb(making), adj(quick), np2(noun(improvements))))

How could I get the verb(X) as variable Verb and the rest of the term as varaible Rest out of such compound term in Prolog?


回答1:


You could use =../2 like:

 Verb_Phrase=..[Verb|Rest_Term_list].

Example:

?- noun(improvements)=..[Verb|Rest_Term_list].
Verb = noun,
Rest_Term_list = [improvements].


来源:https://stackoverflow.com/questions/43836735/getting-hold-of-a-variable-in-complex-compound-term-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!