带参数的 Prolog DCG
Prolog DCG with arguments
我不知道如何使用参数来处理 DCG。假设我们想用 DCGs 来表示 parents 和它们的 children,那么我们可以说:
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.
然后您可以通过以下方式查询:?- s([Peter, is, father, of, Guido], []).
Which returns true
.
我如何通过说 father(name)
.
在 DCG 上使用参数
添加参数很容易,如果您按照下面的方式进行,但无法使查询正常工作,我也不会感到惊讶。这样做的诀窍是知道 DCG 被翻译成常规 Prolog,方法是在将两个额外的参数转换为 Prolog 时将两个额外的参数线程化到每个谓词。它们可以随心所欲地命名,我个人更喜欢 S0
和 S
作为状态,但如果它们具有更具体的含义,请更改它们。
在下面的代码中也需要注意,因为名称以大写字母开头并且它们必须是原子,而不是使用 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).
现在检查您的第一个查询:
?- s([Peter, is, father, of, Guido], []).
true ;
true ;
true ;
true.
对于阅读这篇文章的其他人来说,这是相同的答案,但没有添加参数。有疑问就去看看吧
现在使用添加的隐藏参数的父亲查询。
?- father(Father,S0,S).
Father = 'Peter',
S0 = [_5662|S].
你也可以
?- father(Father,_,_).
Father = 'Peter'.
旁注:
更好的方法是使用 phrase/2 or phrase/3,我说使用而不是回答,因为你问的是如何查询子句父亲的问题,而不是用它来解析数据或使用短语谓词。
test :-
DCG = father(Father),
phrase(DCG,Input,Rest),
format('Father: ~w~n',[Father]).
?- test.
Father: Peter
true.
或
?- phrase(father(Name),_).
Name = 'Peter'.
这些也有效
?- 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'].
如果您使用 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.
我不知道如何使用参数来处理 DCG。假设我们想用 DCGs 来表示 parents 和它们的 children,那么我们可以说:
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.
然后您可以通过以下方式查询:?- s([Peter, is, father, of, Guido], []).
Which returns true
.
我如何通过说 father(name)
.
添加参数很容易,如果您按照下面的方式进行,但无法使查询正常工作,我也不会感到惊讶。这样做的诀窍是知道 DCG 被翻译成常规 Prolog,方法是在将两个额外的参数转换为 Prolog 时将两个额外的参数线程化到每个谓词。它们可以随心所欲地命名,我个人更喜欢 S0
和 S
作为状态,但如果它们具有更具体的含义,请更改它们。
在下面的代码中也需要注意,因为名称以大写字母开头并且它们必须是原子,而不是使用 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).
现在检查您的第一个查询:
?- s([Peter, is, father, of, Guido], []).
true ;
true ;
true ;
true.
对于阅读这篇文章的其他人来说,这是相同的答案,但没有添加参数。有疑问就去看看吧
现在使用添加的隐藏参数的父亲查询。
?- father(Father,S0,S).
Father = 'Peter',
S0 = [_5662|S].
你也可以
?- father(Father,_,_).
Father = 'Peter'.
旁注:
更好的方法是使用 phrase/2 or phrase/3,我说使用而不是回答,因为你问的是如何查询子句父亲的问题,而不是用它来解析数据或使用短语谓词。
test :-
DCG = father(Father),
phrase(DCG,Input,Rest),
format('Father: ~w~n',[Father]).
?- test.
Father: Peter
true.
或
?- phrase(father(Name),_).
Name = 'Peter'.
这些也有效
?- 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'].
如果您使用 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.