Prolog:许多家庭都写有清单。仅针对最后写的家庭的问题答案

Prolog: Many families written with lists. Questions answers only for last family writen

我已经写了 4 个带有列表和一些规则的家庭,如下所示:

     family(
person( tom,  fox,  date(7,may,1960),  works( bbc,  15200)),
person( ann,  fox,  date(9,may,1961),  unemployed),
[ person( pat,  fox,  date(5,may,1983),  unemployed),
   person( jim,  fox,  date(5,may,1983),  unemployed) ] ).


     family(
person( chris,  adler,  date(1,jan,1965),  works( rekit,  23000)),
person( mary,  adler,  date(7,apr,1968),  works( dia, 9500),
[ person( john,  adler,  date(19,apr,1995),  unemployed),
   person( randal,  adler,  date(22,nov,1992),  unemployed) ] ).


     family(
person( james,  gordon,  date(23,aug,1953),  works( GCPD,  17000)),
person( kristen,  gordon,  date(22,may,1955), works( Hospital, 15000),
[ person( penny,  gordon,  date(2,dec,1995),  unemployed),
   person( george,  gordon,  date(2,dec,1995),  unemployed),
   person( nick,  gordon,  date(15,sep,1993),  unemployed) ] ).



     family(
person( bilbo,  baggins,  date(7,may,1980),  works( farmer,  19200)),
person( beladona,  baggins,  date(9,sep,1971),  works( farmer,  18200)),
[ person( frodo,  baggins,  date(15,jun,1995),  unemployed),
   person( sam,  baggins,  date(25,may,1993),  unemployed) ],
   person( mery,  baggins,  date(9,mar,1992),  unemployed),
   person( pippin,  baggins,  date(17,dec,1998),  unemployed), ).



     family(
person( ragnar,  lothbrok,  date(7,may,1970),  works( WarIN,  19500)),
person( lagertha,  lothbrok,  date(9,may,1971),  unemployed),
[ person( rolo,  lothbrok,  date(5,may,1995),  unemployed),
   person( bjorn,  lothbrok,  date(5,may,1983),  unemployed) ] ).




     husband( X) :- family( X,  _,  _).
     wife( X) :- family( _,  X,  _).
     child( X) :- family( _,  _,  Children),  member( X,  Children).
     exists( Persons) :- husband( Persons); wife( Persons);
                                     child( Persons).
     dateofbirth( person(_,  _,  Date,  _),  Date).

问题是当我问一个问题时:

?- child( X),  dateofbirth( X,  date( _,  _,  1995)).

我得到的结果是最后一个家庭的 child,而不是全部 4 个。

X = person(rolo, lothbrok, date(5, may, 1995), unemployed) ;
false.

有什么帮助吗?

您需要做的就是修复知识库中的一些语法错误。当第二个 parent 有工作时,你错过了结束 parent 的论点。此外,您应该对 Hospital、GCPG 和 WarnIn 等原子使用小写符号。

family(
person( tom,  fox,  date(7,may,1960),  works( bbc,  15200)),
person( ann,  fox,  date(9,may,1961),  unemployed),
[ person( pat,  fox,  date(5,may,1983),  unemployed),
   person( jim,  fox,  date(5,may,1983),  unemployed) ] ).

family(
person( chris,  adler,  date(1,jan,1965),  works( rekit,  23000)),
person( mary,  adler,  date(7,apr,1968),  works( dia, 9500)),
[ person( john,  adler,  date(19,apr,1995),  unemployed),
   person( randal,  adler,  date(22,nov,1992),  unemployed) ] ).

family(
person( james,  gordon,  date(23,aug,1953),  works( gcpd,  17000)),
person( kristen,  gordon,  date(22,may,1955), works( hospital, 15000)),
[ person( penny,  gordon,  date(2,dec,1995),  unemployed),
   person( george,  gordon,  date(2,dec,1995),  unemployed),
   person( nick,  gordon,  date(15,sep,1993),  unemployed) ] ).

family(
person( bilbo,  baggins,  date(7,may,1980),  works( farmer,  19200)),
person( beladona,  baggins,  date(9,sep,1971),  works( farmer,  18200)),
[ person( frodo,  baggins,  date(15,jun,1995),  unemployed),
   person( sam,  baggins,  date(25,may,1993),  unemployed) ,
   person( mery,  baggins,  date(9,mar,1992),  unemployed),
   person( pippin,  baggins,  date(17,dec,1998),  unemployed)] ).

family(
person( ragnar,  lothbrok,  date(7,may,1970),  works( warin,  19500)),
person( lagertha,  lothbrok,  date(9,may,1971),  unemployed),
[ person( rolo,  lothbrok,  date(5,may,1995),  unemployed),
   person( bjorn,  lothbrok,  date(5,may,1983),  unemployed) ] ).

husband( X) :- family( X,  _,  _).
wife( X) :- family( _,  X,  _).
child( X) :- family( _,  _,  Children),  member( X,  Children).
exists( Persons) :- husband( Persons); wife( Persons);
                                     child( Persons).
dateofbirth( person(_,  _,  Date,  _),  Date).

测试:

child( X),  dateofbirth( X,  date( _,  _,  1995)).

结果:

X = person(john, adler, date(19, apr, 1995), unemployed)
X = person(penny, gordon, date(2, dec, 1995), unemployed)
X = person(george, gordon, date(2, dec, 1995), unemployed)
X = person(frodo, baggins, date(15, jun, 1995), unemployed)
X = person(rolo, lothbrok, date(5, may, 1995), unemployed)
false