My notes on Learn Prolog Now!
Introduction
To be fair to the authors by Patrick Blackburn, Johan Bos, and Kristina Striegnitz they do a good job of introducing prolog and anything that i write here should not detract anyone from reading their book as their's is the proper book and this is just my notes that i make as i read the book i may make an observation that the authors then make as well. Anyone else reading this should only be doing so in conjunction with the actual book on https://lpn.swi-prolog.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse1
First page
In one of the books in the intro pages it talks about setting up a list of fact in a Knowledgebase or KB
woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.
What it does not say is that if I want to run that on my own computer it will not work because I would have to either write it in a text file or enter consult(user). SWI Prolog the changes the prompt to |: (pipe colon) with ctrl to exit, so I then type in the five facts and press ctrl + d it will then return me to the ?- prompt so I can then ask the questions like
?- playsAirGuitar(mia).
It should return true. To be fair the simply-logical book at the first website has an interactive widget on the page and it does not need to enter the consult mode. It does not expicitly say that the termination character is the dot
The second example also had a bit of difficulty (for me) in that I didn't see the <space> between the :- and the next part.
consult(user). # you need this line if using a linux shell
happy(yolanda). listens2Music(mia). listens2Music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2Music(mia). playsAirGuitar(yolanda):- listens2Music(yolanda).
The first two are simple facts but the third is a rule that states if happy(yolander) listen2Music(yolander) is true so the second part is the condition and the first part is the true result. This concept of making listen2Music(yolander) true if Happy(yolander) simply-logical book calls modus ponens. The book goes on to say that one modus ponens can be chained together with another so if we ask playsAirGuitar(yolander). it should result in true because the first fact says happy yolander and the first rule (third line) says if yolander is happy she listens2Music and finally on the 5th line it says if she listens2music yolander playsAirGuitar. As is pointed in the book out this is not explicitly stated but it is deduced from the previous facts. This demonstrates that a result of a rule can become the fact of any following rule. The simply-logical book states that the KBs like happy(yolander). is a predicate and that the facts and rules are called clauses. the happy(yoalder) and listen2Music(mia) are Predicates defined with one clause and listens2Music(yolanda):- happy(yolanda). is a a predicate with two clauses.
The next example in simply simply-logical
consult(user). # you need this line if using a linux shell
happy(vincent). listens2Music(butch). playsAirGuitar(vincent):- listens2Music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2Music(butch).
Note from these examples that a statement is terminated by a dot but it can have extra clauses if separated by a comma the comma appears to be similar to an and. In the set above if vincent lists2Music AND is happy vincent playsAirGuitar. The book adds as an incidental that the formatting does not have to be like that it could be
playsAirGuitar(vincent):- listens2Music(vincent),happy(vincent).
or
playsAirGuitar(vincent):- listens2Music(vincent),happy(vincent).
So that is how we do an AND in prologue to do an or we would simply have two predicates as above we do with butch on the last two statements butch playsAirGuitar if happy or listens2Music. However, it would seem that we can replace the two statements for a single if using a semicolon as
playsAirGuitar(butch):- happy(butch); listens2Music(butch).
The book states that it would be more efficient as it is a single rule but harder to read, I would guess that would depend on the reader and if the writer was consistent with formatting. The next part of the book sets out some simple predicates and note there is no space between the loves predicates
consult(user). # you need this line if using a linux shell
woman(mia). woman(jody). woman(yolanda). loves(vincent,mia). loves(marsellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin).
What the book describes using theses latest predicates is the use of variables. variables seem a bit different in Prolog than in other languages (at least to me). We can do a querry that could have more than one result like
woman(X).
This will retrun the first instance of woman but unlike previous it shows it without returning us to the ?- prompt
X = mia
If we now add space semincolon it returns the next instance and it still doesn't return us to the ?-. The third and final woman is shown we press semicolon one more time but as it is the last result from the query so as soon as it is output the ?- is displayed. There a few things to note here.
- Variables in Prolog are anything starting with an upper case letter (which would explain why playAirGuitar and the others have a lower case first letter but the Air and Guitar both had upper case)
- Semincolon is the Prolog OR and used in a query it returns the next instance so presumably as queries get longer the OR and AND will become more important
- All of the time there are more items (instances) to show the prompt does not show it only allows the next part of the query to be entered.
The very next item in the book is about AND and is
loves(marsellus,X), woman(X).
In this query we ask it to return instances that have loves(marcellas), (AND) woman (so marsellus's woman) although there are 3 instances of woman there is only one instance of marsellus and that instance happens to have a woman in this case mia so mia is the one and only return value in X