In my mind a chat can take place between more than one person, so I changed the result to a list which can hold zero or more items to represent the people in the chat. Also the code makes use of findall/3 from Finding all Solutions to a Goal since the data is stored in facts.
Hopefully this gives you a better idea of what to do.
session(chat1,person1).
session(chat1,person2).
session(chat2,person2).
session(chat3,person1).
session(chat3,person3).
session(chat3,person4).
session_users(Session,Users) :-
findall(Person,session(Session,Person),Users).
Here are some test cases.
:- begin_tests(session).
session_case(1,chat1,[person1, person2]).
session_case(2,chat3,[person1, person3, person4]).
session_case(3,chat4,[]).
session_case(4,chat2,[person2]).
test(session_users,[forall(session_case(_,Session,Users))]) :-
session_users(Session,Users).
:- end_tests(session).
A demonstration of building the code which also runs the test cases.
?- make.
% c:/users/groot/documents/projects/prolog/swi-discourse_040 compiled 0.05 sec, 8 clauses
% PL-Unit: session .... done
% All 4 tests passed
true.
Enjoy.
I am sure you will have some questions so do ask, but don’t expect that I will be the only responding.
EDIT
Expanded with a second predicate to show the sessions a user is in and the added test cases.
session(chat1,person1).
session(chat1,person2).
session(chat2,person2).
session(chat3,person1).
session(chat3,person3).
session(chat3,person4).
session_users(Session,Users) :-
findall(Person,session(Session,Person),Users).
user_sessions(User,Sessions) :-
findall(Session,session(Session,User),Sessions).
Test cases.
:- begin_tests(session).
session_user_case(1,chat1,[person1, person2]).
session_user_case(2,chat3,[person1, person3, person4]).
session_user_case(3,chat4,[]).
session_user_case(4,chat2,[person2]).
test(session_users,[forall(session_user_case(_,Session,Users))]) :-
session_users(Session,Users).
user_sessions_case(1,person1,[chat1, chat3]).
user_sessions_case(2,person2,[chat1, chat2]).
user_sessions_case(3,person3,[chat3]).
user_sessions_case(4,person4,[chat3]).
user_sessions_case(5,person5,[]).
test(user_sessions,[forall(user_sessions_case(_,User,Sessions))]) :-
user_sessions(User,Sessions).
:- end_tests(session).
Example run.
?- make.
% c:/users/groot/documents/projects/prolog/swi-discourse_040 compiled 0.00 sec, 0 clauses
% PL-Unit: session ......... done
% All 9 tests passed
true.