2010年6月6日日曜日

Prologの技芸 2.1節の練習問題

サザエさんデータベース
male(波平).
male(マスオ).
male(カツオ).
male(タラオ).
male(ノリスケ).
male(イクラ).
male(海平).
male(藻屑).
male(サケオ).
male(ノリオ).
male(鯛造).
male(トシオ).
male(カオル).
male(マコト).

female(サザエ).
female(ワカメ).
female(フネ).
female(タイ子).
female(なぎえ).
female(おこぜ).
female(ナナコ).

% Wife, Husband
married_couple(サザエ,マスオ).
married_couple(フネ,波平).
married_couple(タイ子,ノリスケ).
married_couple(おこぜ,鯛造).

father(藻屑,波平).
father(藻屑,海平).
father(藻屑,なぎえ).
father(波平,サザエ).
father(波平,カツオ).
father(波平,ワカメ).
father(海平,カオル).
father(海平,マコト).
father(ノリスケ,イクラ).
father(サケオ,ノリオ).
father(トシオ,ナナコ).

mother(フネ,サザエ).
mother(フネ,カツオ).
mother(フネ,ワカメ).
mother(サザエ,タラオ).
mother(なぎえ,ノリスケ).
mother(タイ子,イクラ).

(1)
% XはYの姉妹
sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X), X \= Y.
% XはYの姪
niece(X,Y) :- daughter(X,Z), sibling(Z,Y).
% 完全な兄弟姉妹(父親,母親が同じ)
sibling2(X,Y) :- father(Z,X), father(Z,Y), mother(W,X), mother(W,Y), X \= Y.

(2)
% XはYの義母
mother_in_law(X,Y) :- mother(X,Z), married_couple(Y,Z).
mother_in_law(X,Y) :- mother(X,Z), married_couple(Z,Y).
% XはYの義理の兄弟
brother_in_law(X,Y) :- brother(X,Z), married_couple(Y,Z).
brother_in_law(X,Y) :- brother(X,Z), married_couple(Z,Y).
brother_in_law(X,Y) :- married_couple(Z,X), sibling2(Y,Z).
% XはYの義理の息子
son_in_law(X,Y) :- married_couple(Z,X), parent(Y,Z).

(3)
% OutputはInput1とInput2の論理ORである.
or_gate(Input1,Input2,Output) :-
Input1 \= Input2,
transistor(Input1,ground,Output),
transistor(Input2,ground,Output),
resistor(power,Output).

% OutputはInput1とInput2の論理NORである.
nor_gate(Input1,Input2,Output) :-
or_gate(Input1,Input2,X),
inverter(X,Output).

大体合っていると思いますが,保証は無いのでご了承を.

0 件のコメント: