I’m busy with part 2 of my Prolog databases tutorial, redoing the Social-Network problems from the Stanford SQL MooC in Prolog.
It’s all gone well until the final problem which involves adding entries to a relation I needed to declare as a table to avoid cycle hangs.
My problem goes as follows:
For all cases where A is friends with B, and B is friends with C, add a new friendship for the pair A and C. Do not add duplicate friendships, friendships that already exist, or friendships with oneself.
My solution looks like so:
findall(friend(A, C),
( friend(A, B), friend(B, C),
\+friend(A, C), A \== C
), L),
forall(member(friend(ID1, ID2), L),
assertz(friend(ID1, ID2))).
Nothing gets changed in the friend/2 table by running the above. I’m stuck on whether this is due to with faulty logic in my code, or if this is due to the friend/2 relation being tabled.
I added this to the top of the data declarations, but it doesn’t seem to have a difference.
:- table friend/2 as dynamic.