这样的事情应该做你想做的事:
% movie_people/2 ----------------------------------
%
% finds all the people involved in a given movie:
% - look up the movie
% - look up the director
% - find all the actors involved in the film
%-------------------------------------------------
movie_people( Title , [directed_by(Director)|Actors] ) :-
movie(Id,Title,DirectorId,_) ,
director(DirectorId,Director) ,
findall( actor(Name) , movie_cast(Id,Name) , Actors )
.
% movie_cast ------------------------------------------------
%
% Enumerate the actors involved in a movie via backtracking.
%
%------------------------------------------------------------
movie_cast( MovieId , Name ) :-
cast(MovieID , ActorId ) ,
actor(ActorId,Name)
.
但是,您的模型虽然使用了 [可能是数字的] ID,但似乎不是很 Prolog-ish。它带有程序性思维的味道。
Prolog 中更典型的模型可能如下所示:
%----------------------------------------------------------------
% Title Actor Role
%----------------------------------------------------------------
cast( the_thin_man , william_powell , nick_charles ) .
cast( the_thin_man , myrna_loye , nora_charles ) .
cast( the_thin_man , maureen_o_sullivan , dorothy ) .
cast( the_thin_man , nat_pendleton , guild ) .
cast( the_thin_man , minna_gombell , mimi ) .
cast( the_thin_man , porter_hall , maccaulley ) .
cast( the_thin_man , henry_wadsworth , tommy ) .
cast( the_thin_man , william_henry , gilbertt ) .
cast( the_thin_man , harold_huber , nunheim ) .
cast( the_thin_man , cesar_romero , chris ) .
cast( the_thin_man , natalie_moorhead , julia_wolf ) .
cast( the_thin_man , edward_brophy , morelli ) .
cast( the_thin_man , edward_ellis , wynant ) .
cast( the_thin_man , cyril_thornton , tanner ) .
cast( wife_vs_secretary , clark_gable , van ) .
cast( wife_vs_secretary , jean_harlow , whitey ) .
cast( wife_vs_secretary , myrna_loy , linda ) .
cast( wife_vs_secretary , may_robson , mimi ) .
cast( wife_vs_secretary , george_barbier , underwood ) .
cast( wife_vs_secretary , james_stewart , dave ) .
cast( wife_vs_secretary , hobart_cavanaugh , joe ) .
cast( wife_vs_secretary , tom_dugan , finney ) .
cast( wife_vs_secretary , gilbert_emery , simpson ) .
cast( wife_vs_secretary , marjorie_gateson , eve_merritt ) .
cast( wife_vs_secretary , gloria_holden , joan_carstairs ) .
film( the_thin_man ) .
film( wife_vs_secretary ) .
category( the_thin_man , comedy ) .
category( the_thin_man , screwball_comedy ) .
category( the_thin_man , film_noir ) .
category( the_thin_man , mystery ) .
category( wife_vs_secretary , comedy ) .
category( wife_vs_secretary , drama ) .
category( wife_vs_secretary , romance ) .
directed_by( the_thin_man , w_s_van_dyke ) .
director_by( wife_vs_secretary , clarence_brown ) .
然后事情变得容易
演员是在电影中出演的人
actor(X) :- cast(_,X,_) , ! .
导演是导演过电影的人
director(X) :- directed_by(_,X) , ! .
查找给定演员演过的所有电影:
films_of(X,Titles) :-
findall(Title,cast(Title,X,_),Titles) .
列出参与电影的人员
movie_people( Title , Director , Cast ) :-
directed_by( Title , Director ) ,
findall(Actor,cast(Title,Actor,_),Actors)
.
等等