Prolog Program for a recordings database - list

I have three types of facts:
album(code, artist, title, date).
songs(code, songlist).
musicians(code, list).
Example:
album(123, 'Rolling Stones', 'Beggars Banquet', 1968).
songs(123, ['Sympathy for the Devil', 'Street Fighting Man']).
musicians(123, [[vocals, 'Mick Jagger'], [guitar, 'Keith Richards', 'Brian Jones']].
I need to create these 4 rules:
together(X,Y) This succeeds if X and Y have played on the same album.
artistchain(X,Y) This succeeds if a chain of albums exists from X to Y;
two musicians are linked in the chain by 'together'.
role(X,Y) This succeeds if X had role Y (e.g. guitar) ever.
song(X,Y) This succeeds if artist X recorded song Y.
Any help?
I haven't been able to come up with much but for role(X,Y) I came up with:
role(X,Y) :- prole(X,Y,musicians(_,W)).
prole(X,Y,[[Y|[X|T]]|Z]).
prole(X,Y,[[Y|[H|T]]|Z]) :- prole(X,Y,[[Y|T]|Z]).
prole(X,Y,[A|Z]) :- prole(X,Y,Z).
But that doesn't work. It does work if I manually put in a list instead of musicians(_,W) like [[1,2,3],[4,5,6]].
Is there another way for me to insert the list as a variable?
As for the other rules I'm at a complete loss. Any help would really be appreciated.

You have a misconception about Prolog: Answering a goal in Prolog is not the same as calling a function!
E.g.: You expect that when "role(X,Y) :- prole(X,Y,musicians(_,W))." is executed, "musicians(_,W)" will be evaluated, because it is an argument to "prole". This is not how Prolog works. At each step, it attempts to unify the goal with a stored predicate, and all arguments are treaded either as variables or grounded terms.
The correct way to do it is:
role(X,Y) :- musicians(_, L), prole(X,Y,L).
The first goal unifies L with a list of musicians, and the second goal finds the role (assuming that the rest of your code is correct).

Little Bobby Tables is right, you need to understand the declarative style of Prolog. Your aim is to provide a set of rules that will match against the set of facts in the database.
Very simply, imagine that I have the following database
guitarist(keith).
guitarist(jim).
in_band('Rolling Stones', keith).
in_band('Rolling Stones', mick).
Supposed I want to find out who is both a guitarist and in the Rolling Stones. I could use a rule like this
stones_guitarist(X):-
guitarist(X),
in_band('Rolling Stones', X).
When a variable name is given within a rule (in this case X) it holds its value during the rule, so what we're saying is that the X which is a guitarist must also be the same X that is in a band called 'Rolling Stones'.
There are lots of possible ways for you to arrange the database. For example it might be easier if the names of the musicians were themselves a list (e.g. [guitar,[keith,brian]]).
I hope the following example for song(X,Y) is of some help. I'm using Sicstus Prolog so import the lists library to get 'member', but if you don't have that it's fairly easy to make it yourself.
:- use_module(library(lists)).
song(ARTIST,SONG):-
album(CODE,ARTIST,_,_),
songs(CODE,TRACKS),
member(SONG,TRACKS).

Related

Prolog add certain items to list

I'm unable to figure out why my code isn't working despite looking through answers to similar questions. I'm too new at Prolog to properly name things, but I hope you can see what I'm trying to get at.
I am defining a timetable roughly based on this program and am struggling with getting a list of the Classes that Mike teaches for a given result (Next step will be to declare that only results where both Mike and Phil teach 2 should be returned, but I want to work through it so that I can see and understand what's going on).
I imagine this should be simple but any combinations of the addToList(List,C) predicate never work. I know there is the append predicate but I hear it's inefficient, and would like to learn the 'raw' way. I don't know how many variations I've attempted and can't get my head around the way Prolog works in this regard and don't know on what level I'm going wrong - it's all a bit of a black box mystery working with it.
var program =
:- use_module(library(lists)).
prefers(may,a).
prefers(may,b).
prefers(may,c).
prefers(may,d).
prefers(bob,a).
prefers(bob,b).
prefers(bob,c).
prefers(pete,a).
prefers(pete,b).
prefers(pete,c).
prefers(pete,d).
prefers(tom,a).
prefers(tom,b).
prefers(tom,c).
prefers(tom,d).
teacher_pref(mike,a).
teacher_pref(mike,b).
teacher_pref(mike,c).
teacher_pref(phil,b).
teacher_pref(phil,c).
teacher_pref(phil,d).
addToList([C|List],C):- addToList(List,C).
timetable([a,[C1,S1,T1],b,[C2,S2,T2],c,[C3,S3,T3],d,[C4,S4,T4]],List1):-
teacher_pref(T1,C1),
teacher_pref(T2,C2),
teacher_pref(T3,C3),
teacher_pref(T4,C4),
prefers(S1,C1),
prefers(S2,C2),
S1\\=S2,
prefers(S3,C3),
S1\\=S3,
S2\\=S3,
prefers(S4,C4),
S1\\=S4,
S2\\=S4,
S3\\=S4,
addToList(List1,C):-
teacher_pref(mike,C).
session.consult( program );
session.query('timetable([C1,[a,S1,T1],C2,[b,S2,T2],C3,[c,S3,T3,L3],C4,[d,S4,T4]],List1).')
If I understand correctly you have:
teacher_pref(mike,a).
teacher_pref(mike,b).
teacher_pref(mike,c).
And you want to get a list of these classes, which would be:
[a, b, c]
In Prolog we have some higher-order predicates that are for occasions like this:
% (What to find, the goal to call, all the results)
?- findall(Class, teacher_pref(mike, Class), Classes).
Classes = [a, b, c].
In the Tau-Prolog docs they're under All Solutions, in SWI-Prolog there's a couple more.
To make this into a more generic predicate:
teacher_prefs(Teacher, Prefs) :-
findall(Pref, teacher_pref(Teacher, Pref), Prefs).

How to extract data to list in Prolog?

I have an array L of some type, I'm trying to extract the data to an array, for example:
L=[day(sunday),day(monday)]
to
Target=[sunday,monday]
Tried using forall and searched for related questions on Prolog lists.
extract_data_to_list(L,Target) :-
member(day(Day),L),
length(L, L1),
length(Target, L1),
member(Day,Target).
Current output:
?- extract_data_to_list([day(sunday),day(monday)],Target).
Target = [sunday, _5448] ;
Target = [_5442, sunday] ;
Target = [monday, _5448] ;
Target = [_5442, monday].
Desired output:
?- extract_data_to_list([day(sunday),day(monday)],Target).
Target=[sunday,monday]
This is an ideal problem for maplist:
day_name(day(DayName), DayName).
dates_daylist(Dates, DayList) :-
maplist(day_name, Dates, DayList).
Maplist applies day_name to each corresponding pair of elements in Dates and DayList.
This is an ideal problem for library(lambda) for SICStus|SWI:
maplist(\day(N)^N^true, Dates, Daylist).
I have a couple other ways you can do this, just in case you're wondering.
?- findall(D, member(day(D), [day(monday), day(tuesday)]), Days).
Days = [monday, tuesday].
The trick here is that you can use findall/3 to drive a simple loop, if the Goal (argument 2) uses member/2. In this case, we're unifying day(D) with each item in the list; no further work really needs to happen besides the unification, so we're able to "tear off the wrapping" just with member/2 but you could drive a more complex loop here by parenthesizing the arguments. Suppose you wanted to change day to day-of-week, for instance:
?- findall(DoW, (member(day(D),
[day(monday), day(tuesday)]), DoW=day_of_week(D)),
Days).
Days = [day_of_week(monday), day_of_week(tuesday)].
Making the goal more complex works, in other words, as long as you parenthesize it.
The second trick is specific to SWI-Prolog (or Logtalk, if you can use that), which is the new library(yall):
?- maplist([Wrapped,Day]>>(Wrapped=day(Day)),
[day(monday),day(tuesday)], X).
X = [monday, tuesday].
library(yall) enables you to write anonymous predicates. [Wrapped,Day]>>(Wrapped=day(Day)) is sort of like an inline predicate, doing here exactly what #lurker's day_name/2 predicate is doing, except right inside the maplist/3 call itself without needing to be a separate predicate. The general syntax looks something like [Variables...]>>Goal. This sort of thing was previously available as library(lambda) and has been a feature of Logtalk for many years.

Prolog - Appending to an existing file term list

I have a term contained in a file:
fruit(apple, []).
I'm trying to write input to it, so that each set of information will be appending to the empty list as a list to compose a list of lists, in a manner like:
fruit(apple, [[30, 'fresh'], [10, 'old'], ... ]).
So far I only know how to write a separate term:
add_fruit(File, Type, Price, State) :-
open(File, append, Stream),
writeq(Stream, fruit(Type, [Price, State])),
write(Stream, '.'),
nl(Stream), nl(Stream),
close(Stream).
This will produce a term such as:
fruit(apple, [20, 'some state']).
However I don't want to have to create a new term every time I use the add_fruit predicate, rather I want to append a list containing Price and State to the appropriate fruit type term.
Would it be possible to extend the predicate so that it will write to the existing term, rather than create a new one?
You are mixing up concepts here a bit. First off, if you have a Prolog file, say "food.pl" with the contents:
fruit(apple, []).
then fruit/2 is not exactly a term, but rather the fact fruit/2 (predicate without a body, only with a head) which maps the atom apple (first argument) to the empty list (the second argument).
What you can do now is consult the file, which will put the fact in the database.
Another thing: are you sure you need to keep appending to the list in the second argument? Why not normalize your database, and have, for example:
fruit(apple, price_state(30, fresh)).
fruit(apple, price_state(10, old)).
% etc
Here, you now have a table, fruit, with two columns. The first is the name of the fruit, the second is a term price_state/2 with the price as the first argument and the state as the second. You could have just as well said:
fruit_price_state(apple, 30, fresh).
fruit_price_state(apple, 10, old).
% etc
if you indeed want your table to map the fruit to a state and a price. You can now add and remove rows from the table using assertz and retract. In other words, design your database as you would design a relational database, using facts as tables. This approach translates directly to Prolog. You can then query the database, for example, to give you the combination of price and state for apples:
?- bagof(price_state(P, S), fruit(apple, P, S), PSs).
This will put a list of price_state/2 in PSs, or fail, if you don't have apples in your database (you could use findall/3 if you prefer to get an empty list instead, but then you would have to deal with that empty list in any predicate that takes PSs).
One way to deal with persistence, that is, load facts to the database, add/remove rows, and then save the updated database back to the same file is using Edinburgh-style I/O with see, tell, told etc.
If you are using SWI-Prolog, you also have the option of using library(persistency). See the link for a working minimal example.

prolog - tail recursion issue

I am new to prolog and I am trying to create a predicate and am having some trouble.
I have a list of cities that are connected via train. They are connected via my links/2 clause.
links(toronto, ajax).
links(toronto, markham).
links(toronto, brampton).
links(brampton, markham).
links(markham, ajax).
links(brampton, mississauga).
links(mississauga, toronto).
links(mississuaga, oakville).
links(oakville, st.catharines).
links(oakville, hamilton).
links(hamilton, st.catharines).
I am writing a predicate called addnewcities which will take a list of cities and then return a new list containing the original list, plus all the cities that are directly connected to each of the cities in the original list.
Here is a (rough looking) visual representation of the links.
If my input list was [toronto] I want my output to be (order doesnt matter) [ajax,markham,brampton,mississauga,toronto].
If input was [oakville,hamilton] I want the output to be [mississauga,st.catharines,oakville,hamilton].
Here is my predicate so far.
addnewcities([],_).
addnewcities([CitiesH|Tail],Ans):- directer(CitiesH,Ans2), merger(Ans2,[CitiesH],Ans), addnewcities(Tail,Ans).
directer/2 takes a city and saves a list containing all the directly connected cities in the second arg.
merger/3 just merges two lists making sure there are no duplicates in the final list.
When my input is a list with one element ie [toronto] it works!
But when I have a list with multiple elements [toronto,ajax] it says "false" every time.
I'm pretty sure my issue is that when it recurses for the second time, merge is what says its false. I just don't know how to get around this so that my list can keep being updated instead of being checked if true or false.
Any help is appreciated!
this query uses library support to solve the problem:
addcities(Cs, L) :-
setof(D, C^(member(C,Cs), (C=D;link(C,D);link(D,C))), L).
This should work for what you want:
addcities(A,B):-
addcitiesaux(A,[],B).
addcitiesaux([],X,X).
addcitiesaux([X|Xs],L,R):-
link(X,A),
\+ member(A,L),
!,
addcitiesaux([X|Xs],[A|L],R).
addcitiesaux([X|Xs],L,R):-
link(A,X),
\+ member(A,L),
!,
addcitiesaux([X|Xs],[A|L],R).
addcitiesaux([X|Xs],L,R):-
addcitiesaux(Xs,[X|L],R).

Using Prolog Lists in Recursion

So I am trying to use a recursive method to find a path between two people. Here is the quick background:
I define some facts in(X,Y). That show who is related, ie. in(person1,project1), in(person2,project1), etc etc. Now any two people are related if they were in the same project as each other, or there is a linking path of people between them. For example p1 worked on A p2 worked on A and B and p3 worked on B therefore there is a path from p1 to p3 through p2. These paths can be any length.
I am trying to solve this recursively (don't see any other way), but there is an annoying problem:
related(A,B) :-
in(A,X),
in(B,X),
not(A=B).
chain(A,B) :-
related(A,B).
chain(A,B) :-
related(A,Y),
chain(Y,B).
The issue is that the path can repeat itself. It can go from p1 to p2 back to p1 endless times. A person should not be in the path more than 1 time.
I tried to fix this with a list that I add to. If a person is already in the list, they can't be added again:
related(A,B,L) :-
in(A,X),
in(B,X),not(A=B).
chain(A,B,L) :-
related(A,B,L).
chain(A,B,L) :-
related(A,Y,L),
not(member(Y,L)),
append(L,[Y],Q),
chain(Y,B,Q).
And it sort of worked, but caused a ton of random errors, repeating some people multiple times, some only once, and then failing. Does this approach look right? Am I totally using lists wrong?
Thank You.
First improvement. Are you looking for all the chains of relations or do you want to check if there is one chain of relation? In the first case add a cut.
chain(A,B) :-
related(A,B), !.
chain(A,B) :-
related(A,Y),
chain(Y,B).
In the second case, Prolog does exactly what it's asked to do, that is finding all the possible chains.
Please post a query that causes problems so that we can reason together on it and improve the solution.
Here is an alternative way, maybe less efficient but rather general, based on fixpoint computation.
connected(Found, Connected) :-
collect(Found, [], Ext),
( Ext == Found
-> Connected = Found
; connected(Ext, Connected)
).
collect([], Set, Set).
collect([E|Es], Set, Fix) :-
extend(E, Set, Ext),
collect(Es, Ext, Fix).
extend(E, Set, Ext) :-
directly(E, DirectConn),
ord_union(DirectConn, Set, Ext).
directly(A, DirectConn) :-
setof(B, P^(in(A, P), in(B, P)), DirectConn).
We must call connected(Found, Connected) with a sorted list, and it loops until the set cannot be extended. For instance, with this test data
in(anna, project1).
in(bob, project1).
in(bob, project2).
in(chris, project2).
in(dan, project3).
?- connected([bob],L).
L = [anna, bob, chris].
?- connected([dan],L).
L = [dan].
I allow on purpose directly/2 get identity, i.e.
?- directly(X,Y).
X = anna,
Y = [anna, bob] ;
...
X = dan,
Y = [dan].
I think I was never perfectly clear enough, but I ended up solving this myself. I put the code below.
What really mattered was an effective notchain predicate and then making sure I did the appends correctly. I also created a notsame predicate to replace the not(A=B). The code is below. Most of the answer was making sure that there were [] around what was being appended to the list. Not having the correct [] around what was being appended caused errors.
notchain(X,L) :-
member(X,L),!,fail.
notchain(X,L).
And then:
chain(A,B,L) :-
related(A,B),
append(L,[A],Q),
append(Q,[B],Z),
write(final),writeln(Z).
chain(A,B,L) :-
notchain(A,L),
append(L,[A],Q),
related(A,Y),
chain(Y,B,Q).
This was used in related:
notsame(A,B) :-
(A=B),!,fail.
notsame(A,B).