Updated: 03 September 2023
Introduction to Prolog
Notes from Derek Banas’ Youtube
Prolog consists of a collection of facts and rules that can be queried
These rules are stored in a Database or Knowledgebase file
Installation
You can install Prolog using Chocolatey with
1choco install swi-prologYou can then open Prolog terminal which will be in your Start Menu, or you can look for the installation directory and add that to your path (preferred)
In my case the installation is here:
1C:\Program Files\swipl\binYou should then be able to run the swipl command from your terminal to start that up
You can close it with the halt. command
Hello Worlding
You can hello-world with the following in the swipl terminal
1write('Hello World').Or multiple lines with:
1write('Hello World'), nl,2write('Bye World').Note that statements end with a .
Creating a Knowledgebase
Facts and rules are stored in a .pl file, in our case we use db.pl
You can define a fact which consists of a predicate and atoms/arguments, these start with lowercase letters
A fact can be defined with something like:
1loves(romeo, juliet).A rule can be defined, for example juliet loves romeo if romeo loves juliet
1loves(juliet, romeo) :- loves(romeo, juliet).A database file can be loaded with the name of the file in [] as follows:
1[db].A variable is something that allows us to answer questions, these start with capital letters
1loves(romeo, X).Will return:
1X = julietWe can define some additional facts with the following code:
1male(john).2male(jeff).3male(bob).4
5female(sally).6female(jenny).7female(amy).You can view a listing of facts with the following command
1listing.Or all facts of a specific kind such as
1listing(male).You can view all the combinations of facts with
1male(X), female(Y).You can type ; to move to the next element
Rules are used to state that a fact depends on another fact, the :- is like saying if
Example, albert runs if happy
1runs(albert) :-2 happy(albert).We can also have multiple conditions separated by a , (and)
1dances(alice) :-2 happy(alice),3 with_albert(alice).We can define or with making use of diffeent rules, such as:
1dances(alice) :-2 happy(alice).3
4dances(alice) :-5 with_albert(alice).