Prolog

From Sea of Fate
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Introduction

The main use for Prolog will be to use it as the AI engine for the Image Processing Application. There may be other applications but that is one that will get things moving. It is assumed that it will take some time to learn how to use it with enough skill so starting now is a good idea. We will be using SWI Prolog.

Installation

To install SWI-Prolog on Ubuntu, the most common and recommended method is to use its Personal Package Archive (PPA). This ensures you get the latest stable version. You might be prompted to press Enter to confirm adding the repository.

sudo add-apt-repository ppa:swi-prolog/stable

After adding the PPA, you need to update your system's package information so it knows about the new packages available from the PPA:

sudo apt update

Now you can install the swi-prolog package:

sudo apt install swi-prolog

It is possible that there will be other sources but this is probably the most up to date. To prove that it is installed and working type

swipl

There should be some welcome message as the interactive interpreter try

write('Hello, Prolog!').

The interpreter should return the message Hello, Prolog! followed by true

Some learning resources

A quick search with google returned https://book.simply-logical.space/src/simply-logical.html and another site https://lpn.swi-prolog.org/lpnpage.php?pageid=top Both of these appear to be quite old, probably late 90s. hopefully they will give a good enough background to get me started.

General Concept

The concept of Prolog is that it is rules based rather than the more common procedural based languages like C. The focus is more on defining the problem as opposed to defining a solution by creating rule and facts to form a knowledge base

My notes on Learn Prolog Now!

My notes on the book that is free on the https://lpn.swi-prolog.org website.

Introduction to Prolog by dissecting Learn Prolog Now! book

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

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+D 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 CTRL+D is the end of line in a Linux shell, if swipl is running on Windows it would be a CTRL+Z because that is the Windows EOL

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 The next bit adds the line about jealousy and using rules with variables, telling exactly what this next line does require a bit of thought.

jealous(X,Y):- loves(X,Z), loves(Y,Z).

I read this as saying X is jealous of Y if Xs loves Z and Y loves Z.