Prolog DCG with arguments

纵然是瞬间 提交于 2019-12-02 18:17:17

问题


I can't figure out how to work with DCGs using arguments. Suppose we want to use DCGs to represent parents and their children, we could then say:

father --> [Peter].
mother --> [Isabel].

child --> [Guido].
child --> [Claudia].

verb --> [is].
relation --> [father, of].
relation --> [mothter, of].

s --> father, verb, relation, child.
s --> mother, verb, relation, child.

You can then query by: ?- s([Peter, is, father, of, Guido], []). Which returns true.

How can I use arguments on the DCGs by saying perhaps father(name).


回答1:


Adding the arguments is easy and I would not be surprised if you did it as done below, but could not get the query to work. The trick to this is knowing that DCG is translated to regular Prolog by threading two extra arguments to each predicate when they are translated to Prolog. They can be named what ever you like, I personally prefer S0 and S for state, but change them if they have a more specific meaning.

Also of note in the following code, since the names start with capital letters and they need to be atoms, instead of using peter the atoms are bookended with ', e.g. 'Peter'.

father('Peter') --> ['Peter']. 
mother('Isabel') --> ['Isabel'].

child('Guido') --> ['Guido']. 
child('Claudia') --> ['Claudia'].

verb(is) --> [is]. 
relation('father of') --> [father, of]. 
relation('mother of') --> [mother, of].

s --> father(Father), verb(Verb), relation(Relation), child(Child). 
s --> mother(Father), verb(Verb), relation(Relation), child(Child).

Now to check with your first query:

?- s([Peter, is, father, of, Guido], []).
true ;
true ;
true ;
true.

For others reading this, that is the same answer without the arguments added. Check it if you have doubts.

Now for the father query with the added hidden arguments being used.

?- father(Father,S0,S).
Father = 'Peter',
S0 = [_5662|S].

You could also do

?- father(Father,_,_).
Father = 'Peter'.

Side note:

A better way to do this would be to use phrase/2 or phrase/3, I say use and not answer because you asked the question on how to query the clause father, not use it to parse the data or work with phrase predicate.

test :-
    DCG = father(Father),
    phrase(DCG,Input,Rest),
    format('Father: ~w~n',[Father]).

?- test.
Father: Peter
true.

or

?- phrase(father(Name),_).
Name = 'Peter'.

These also work

?- s(S0,S).
S0 = ['Peter', is, father, of, 'Guido'|S] ;
S0 = ['Peter', is, father, of, 'Claudia'|S] ;
S0 = ['Peter', is, mother, of, 'Guido'|S] ;
S0 = ['Peter', is, mother, of, 'Claudia'|S] ;
S0 = ['Isabel', is, father, of, 'Guido'|S] ;
S0 = ['Isabel', is, father, of, 'Claudia'|S] ;
S0 = ['Isabel', is, mother, of, 'Guido'|S] ;
S0 = ['Isabel', is, mother, of, 'Claudia'|S].

?- s(S0,[]).
S0 = ['Peter', is, father, of, 'Guido'] ;
S0 = ['Peter', is, father, of, 'Claudia'] ;
S0 = ['Peter', is, mother, of, 'Guido'] ;
S0 = ['Peter', is, mother, of, 'Claudia'] ;
S0 = ['Isabel', is, father, of, 'Guido'] ;
S0 = ['Isabel', is, father, of, 'Claudia'] ;
S0 = ['Isabel', is, mother, of, 'Guido'] ;
S0 = ['Isabel', is, mother, of, 'Claudia'].

?- phrase(s,S,[]).
S = ['Peter', is, father, of, 'Guido'] ;
S = ['Peter', is, father, of, 'Claudia'] ;
S = ['Peter', is, mother, of, 'Guido'] ;
S = ['Peter', is, mother, of, 'Claudia'] ;
S = ['Isabel', is, father, of, 'Guido'] ;
S = ['Isabel', is, father, of, 'Claudia'] ;
S = ['Isabel', is, mother, of, 'Guido'] ;
S = ['Isabel', is, mother, of, 'Claudia'].

If you use listing/0 you can see the DCG converted to Prolog which will reveal the two extra arguments threaded through the predicates.

?- listing.  

child('Guido', ['Guido'|A], A).
child('Claudia', ['Claudia'|A], A).

verb(is, [is|A], A).

relation('father of', [father, of|A], A).
relation('mother of', [mother, of|A], A).

father('Peter', ['Peter'|A], A).
mother('Isabel', ['Isabel'|A], A).

s(A, B) :-
    father(Father, A, C),
    verb(Verb, C, D),
    relation(Relation, D, E),
    child(Child, E, B).
s(A, B) :-
    mother(Father, A, C),
    verb(Verb, C, D),
    relation(Relation, D, E),
    child(Child, E, B).

test :-
    DCG=father(Father),
    phrase(DCG, Input, Rest),
    format('Father: ~w~n', [Father]).

true.


来源:https://stackoverflow.com/questions/56135477/prolog-dcg-with-arguments

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