带参数的 Prolog DCG [英] Prolog DCG with arguments

查看:18
本文介绍了带参数的 Prolog DCG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用参数处理 DCG.假设我们想用 DCG 来代表父母和他们的孩子,我们可以说:

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.

然后您可以通过以下方式查询:?- s([Peter, is, Father, of, Guido], []). 返回true.

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

我如何通过说father(name) 来使用 DCG 上的参数.

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

推荐答案

添加参数很容易,如果您按照下面的方法进行操作,但无法使查询工作,我不会感到惊讶.解决这个问题的诀窍是知道 DCG 通过在将两个额外的参数转换为 Prolog 时将两个额外的参数线程化到每个谓词来转换为常规 Prolog.它们可以随意命名,我个人更喜欢 S0S 表示状态,但如果它们有更具体的含义,请更改它们.

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.

在下面的代码中还要注意,由于名称以大写字母开头并且它们必须是原子,而不是使用 peter 原子以 ' 结尾,例如'彼得'.

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

你也可以

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

<小时>

附注:

更好的方法是使用 phrase/2phrase/3,我说使用并没有回答,因为你问的是如何查询子句父亲的问题,而不是用它来解析数据或使用短语谓词.

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.

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

<小时>

如果你使用 listing/0 你可以看到DCG 转换为 Prolog,这将显示穿过谓词的两个额外参数.


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.

这篇关于带参数的 Prolog DCG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆