Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Prolog Fundamentals

Artificial Intelligence Programming


in Prolog

LAB 01-Introduction to Prolog

Prepared by: Ebtehal Alsaggaf


Introduction to PROLOG
• Programming in Logic
• Used for symbolic and non-numerical computation
• Has a built in intelligent search mechanism
• Can handle complex problems in compact
programs
• Writing a program in Prolog means writing facts and
rules which together comprise knowledge base
• Facts and rules use predicates which represent
relationships among data objects

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 2


INTRODUCTION
• Prolog programs are made up of facts and rules.
• A fact asserts some property of an object, or relation
between two or more objects.
e.g. parent(jane,alan).
Can be read as “Jane is the parent of Alan.”
• Rules allow us to infer that a property or relationship
holds based on preconditions.
e.g. parent(X,Y) :- mother(X,Y).
= “Person X is the parent of person Y if X is Y’s
mother.”

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 3


Prolog
• We will use SWISH, one of the many
implementations used currently.
• Prolog has an interactive interpreter
• After starting SWISH, the interpreter
can start reading your Prolog files and
accept your queries.
– https://swish.swi-prolog.org/
– You can save your file in Notepad and
Prolog program files usually have the
extension .pl or .pro

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 4


swish.swi-prolog.org

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 5


Data Objects

Data Objects

structures simple objects

variables constants

numbers atoms

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 6


Atoms
• Atoms : non-numeric literal constants. Strings
containing combinations of
– lower case letters
– upper case letters
– digits
– special characters like +, -, _, *, >, < etc.
• Three types of atom : alphanumerics, special
character strings and quoted character strings.
• Alphanumerics :
– Similar to C++ or Java identifiers.
– Strings of letters, digits and _. Must start with a lower case
letter. Relation names must always be alphanumeric
atoms.
Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 7
Variables

• String of letters, digits and underscore characters


that starts
– upper-case letter or with an underscore:
– Y, Child, _a23, Student_List

• PROLOG variables are logical variables, not store


variables.
– Given values by instantiation during matching not by
assignment .

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 8


Variables
• Sometimes we don’t want to give a variable a name
because we don’t care about it’s value.
– Anonymous variables

• Prolog Anonymous variable _ (underscore) is a


"don't-care" variable, which will match anything
(atom, number, structure, …).
– For example, the rule:
bad(Dog) :- bites(Dog, _).
says that something ( Dog ) is bad if Dog bites anything.

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 9


Structures
• Objects that have many components
• Components can themselves be structures
• Functor is used to combine components into
single structure
– date(1, jan, 2007), date(Date, Month, 2007)
– segment(point(1,1),point(3,3))
• Functors are recognized by:
– Name
– Number of arguments (Arity)

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 10


Predicate Definitions
• A predicate consists of a head and a number of
arguments
• Both facts and rules are predicate definitions.
• ‘Predicate’ is the name given to the word occurring
before the bracket in a fact or rule:

parent(jane,alan).
Predicate name Arguments

• By defining a predicate you are specifying which


information needs to be known for the property
denoted by the predicate to be true.
Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 11
Clauses
• An individual definition (whether it be a fact or
rule).

e.g. mother(jane,alan). = Fact


parent(P1,P2):- mother(P1,P2). = Rule

head body

• A clause consists of a head


• and sometimes a body.
– Facts don’t have a body because they are always
true.

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 12


Clauses
• Facts
– Declare things that are unconditionally true
– Have a head and empty body
Examples:
parent( tom, bob). %tom is parent of bob
likes(joe,dogs). % joe likes dogs
• Rules
– Declare things (predicates) that are true depending on a
given condition
– Have a non-empty body
Example: mother(X,Y):-parent(X,Y), female(X).
– X is mother of Y if X is parent of Y and X is female

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 13


Recursive rules
Example
• Rule 1:
– predecessor(X,Z):-parent (X,Z).

• Rule 2:
– predecessor(X,Z):- parent(X,Y),predecessor(Y,Z)

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 14


Naming tips
• Use real English when naming predicates, constants,
and variables.
e.g. “John wants to help Somebody.”
Could be: wants(john,to_help,Somebody).
Not: x87g(j,_789).

• Use a Verb Subject Object structure:


teachs(john, Physics).

• BUT do not assume Prolog Understands the meaning


of your chosen names!
– You create meaning by specifying the body (i.e.
preconditions) of a clause.

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 15


Queries

• Means of asking the program what things are true


• Have empty head

– Example:
? parent(tom, bob).
Is tom parent of bob ?

– Example:
? parent( tom, X).
Find X such that tom is parent of X.

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 16


Matching
• Two terms (eg. predicate instance) will
match if:
– They are identical, or
– The variables in the terms should be instantiated such that
after substitution of variables by these objects the terms
become identical

• Example:
– ?date(D,M,2007)=date(10,jan,Y)
• D=10
• M=jan
• Y=2007

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 17


Summary
• A Prolog program consists of predicate definitions.
• A predicate denotes a property or relationship between objects.
• Definitions consist of clauses.
• A clause has a head and a body (Rule) or just a head (Fact).
• A head consists of a predicate name and arguments.
• A clause body consists of a conjunction of terms.
• Terms can be atoms, variables, or compound terms.
• We can set our program goals by typing a command that unifies
with a clause head.
• A goal unifies with clause heads in order (top down).
• Unification leads to the instantiation of variables to values.
• If any variables in the initial goal become instantiated this is
reported back to the user.

Ebtehal Alsaggaf CCAI 221 –UJ -CCSE -2020 18

You might also like