在 Prolog 中创建侄子规则 [英] Creating a Niece Rule in Prolog

查看:47
本文介绍了在 Prolog 中创建侄子规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用家庭数据库,我需要在 swi-prolog 中创建一个侄女规则 (niece(X,Y)),其定义为如果 X 是 Y 的兄弟或姐妹的女儿,则 X 是 Y 的侄女".这是具有我已经设计好的规则的给定数据库:

Using a family database, I need to create a niece rule (niece(X,Y))in swi-prolog which is defined as "X is a niece of Y if X is a daughter of Y's brother or sister." This is the given database with my already designed rules:

% family DB

grandfather(don,who).

father(don,ted).
father(don,barb).
father(don,paula).
father(greg,erin).
father(greg,austin).
father(wes,alyssa).
father(ted,jessica).
father(ted,david).
%mother(ted, john).

mother(audrey,ted).
mother(audrey,barb).
mother(audrey,paula).
mother(paula,erin).
mother(paula,austin).
mother(barb,alyssa).

married(don,audrey).
married(wes,barb).
married(greg,paula).

male(don).
male(ted).
male(wes).
male(greg).
male(austin).
male(david).

female(audrey).
female(barb).
female(paula).
female(alyssa).
female(jessica).
female(erin).

parent(X,Y) :-
    father(X,Y)
  ; mother(X,Y).

grandfather(X,Y) :-
   father(X,Z),
   (  father(Z,Y)
   ;  mother(Z,Y)
   ).

samefather(X,Y) :- 
   father(F,X),
   father(F,Y).

samemother(X,Y) :- 
   mother(M,X),
   mother(M,Y).

sameparent(X,Y) :-
   samefather(X,Y).
sameparent(X,Y) :-
   samemother(X,Y),
   not(samefather(X,Y)).

couple(X,Y) :- 
   married(X,Y),
   married(X,Y).

这是我对侄女规则的初步尝试:

Here is my initial try at the niece rule:

niece(X,Y) :-
   parent(F,X),
   sameparent(Y,F).

我的想法是使用 sameparent 规则来检查 Y 和 F 是否是兄弟姐妹,然后检查 F 是否是 X 的父级.此规则目前不起作用.我仍在努力理解组合多个规则的语法.如果有人可以使用相同的逻辑来帮助我,将不胜感激.

My idea is to use the sameparent rule to check if Y and F are siblings and then check if F is the parent of X. This rule currently doesn't work. I'm still struggling to understand the syntax of combining multiple rules. If anyone could help me by using this same logic, it would be greatly appreciated.

推荐答案

删除不必要的规则:

parent(don,ted).
parent(don,barb).
parent(don,paula).
parent(greg,erin).
parent(greg,austin).
parent(wes,alyssa).
parent(ted,jessica).
parent(ted,david).

parent(audrey,ted).
parent(audrey,barb).
parent(audrey,paula).
parent(paula,erin).
parent(paula,austin).
parent(barb,alyssa).

male(don).
male(ted).
male(wes).
male(greg).
male(austin).
male(david).

female(audrey).
female(barb).
female(paula).
female(alyssa).
female(jessica).
female(erin).

father(X,Y) :-
    male(X),
    parent(X,Y).

mother(X,Y) :-
    female(X),
    parent(X,Y).

sameparent(X,Y) :-
   parent(A,X),
   parent(A,Y).

给你:

niece(X,Y) :-
    female(X),
    parent(Z,X),
    sameparent(Z,Y),
    Z \= Y.

一行一行的意思:

  • X 是 Y 的侄女,如果
  • X 是女性并且
  • X 的一个父母
  • 与 Y 有一个共同的父母
  • 谁不是他/她自己.

这给了你:

| ?- niece(X,Y).

X = alyssa
Y = ted;

X = alyssa
Y = paula;

X = jessica
Y = barb;

X = jessica
Y = paula;

X = erin
Y = ted;

X = erin
Y = barb

两次,因为在您的数据库中,每个兄弟/姐妹共享相同的两个父母.

Twice because in the case of your database every brothers/sisters share the same two parents.

例如,如果A是B的女儿,B是C的同父异母兄弟,那么A仍然是C的侄女.

For example, if A is the daughter of B and B is the half-brother of C, A is still the niece of C.

如果您希望该规则为假,并且仅当 B 和 C 是兄弟/姐妹(而不是一半)时 A 才是 C 的侄女,您可以将侄女规则更改为以下内容:

If you want that rule to be false and A to be the niece of C only if B and C are brothers/sisters (and not only half), you can change the niece rule to the following :

sameFather(X,Y) :-
    father(A,X),
    father(A,Y).

sameMother(X,Y) :-
    mother(A,X),
    mother(A,Y).

niece(X,Y) :-
    female(X),
    parent(Z,X),
    sameFather(Z,Y),
    sameMother(Z,Y),
    Z \= Y.

这样你就不会得到与 niece(X,Y) 重复的结果.

Then you won't get duplicate results whith niece(X,Y).

这篇关于在 Prolog 中创建侄子规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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