简体   繁体   English

带参数的Prolog DCG

[英]Prolog DCG with arguments

I can't figure out how to work with DCGs using arguments. 我不知道如何使用参数使用DCG。 Suppose we want to use DCGs to represent parents and their children, we could then say: 假设我们要使用DCG来代表父母及其子女,那么我们可以说:

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], []). 然后,您可以通过以下方式查询: ?- s([Peter, is, father, of, Guido], []). Which returns true . 返回true

How can I use arguments on the DCGs by saying perhaps father(name) . 如何通过说出father(name)在DCG上使用参数。

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. 诀窍在于,知道将DCG转换为常规Prolog时,会在将每个谓词转换为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. 可以随意命名它们,我个人更喜欢S0S作为状态,但是如果它们具有更具体的含义,请更改它们。

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 ' , eg 'Peter' . 在以下代码中还要注意的是,由于名称以大写字母开头且必须为原子,因此原子不使用peter ,而是以' ,例如'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. 更好的方法是使用短语/ 2短语/ 3 ,我说使用而不回答,因为您问了有关如何查询子项父亲的问题,而不是使用它来解析数据或使用短语谓词。

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 / 0 ,则可以看到DCG转换为Prolog,它将显示通过谓词穿线的两个额外参数。

?- 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM