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

查看:97
本文介绍了带参数的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而是以'开头. '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'.


旁注:


Side note:

一种更好的方法是使用短语/2 短语/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天全站免登陆