Prolog Lecture1 (Lab)
Prolog Lecture1 (Lab)
Programming in Prolog
An Introduction
References
• No course text book
• Useful references:
– Bratko I. Prolog Programming for Artificial
Intelligence (3rd edition), 2001.
Lecture 1: An Introduction 2
What is Artificial Intelligence
Programming (AIP)
• A comprehensive introduction to
Prolog.
• Specific focus on Artificial Intelligence
programming techniques:
– Knowledge representation and manipulation,
– Database construction and management,
– State-space Search,
– Planning,
– Meta-programming,
– Text parsing and Definite Clause Grammars.
Lecture 1: An Introduction 3
What is Prolog?
• PROgrammation et Logique.
• Edinburgh syntax is the basis of ISO
standard.
• High-level interactive language.
• Logic programming language.
Lecture 1: An Introduction 4
What is Prolog? (2)
• Programming languages are of two
kinds:
– Procedural (BASIC, ForTran, C++, Pascal, Java);
– Declarative (LISP, Prolog, ML).
• In procedural programming, we tell the
computer how to solve a problem.
• In declarative programming, we tell the
computer what problem we want solved.
• (However, in Prolog, we are often forced to
give clues as to the solution method).
Lecture 1: An Introduction 5
What is Prolog used for?
• Good at
– Grammars and Language processing,
– Knowledge representation and reasoning,
– Unification,
– Pattern matching,
– Planning and Search.
• i.e. Prolog is good at Symbolic AI.
• Poor at:
– Repetitive number crunching,
– Representing complex data structures,
– Input/Output (interfaces).
Lecture 1: An Introduction 6
Prolog Program
• 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 constants, 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.
Lecture 1: An Introduction 7
Basic Elements of Prolog
• Our program is a database of facts and
rules.
Example questions:
Who is Jim's father?
Is Jane the mother of Fred?
Is Jane the mother of Jim?
Does Jack have a grandchild?
Lecture 1: An Introduction 9
Prolog in Prolog
Example Database: Example Database:
John is the father of Jim. father( john, jim ).
Jane is the mother of Jim. mother( jane, jim ).
Jack is the father of John. father( jack, john ).
parent( Person1, Person2 ) :-
Person 1 is a parent of Person 2 if father( Person1, Person2 ).
Person 1 is the father of Person 2 or parent( Person1, Person2 ) :-
Person 1 is the mother of Person 2. mother( Person1, Person2 ).
Lecture 1: An Introduction 10
Complete Prolog Program-I
predicates
nondeterm parent(symbol,symbol). male(bob).
nondeterm mother(symbol,symbol). male(jim).
nondeterm father(symbol,symbol). female(liz).
nondeterm brother(symbol,symbol). female(pat).
nondeterm sister(symbol,symbol). female(ann).
nondeterm female(pam).
grandfather(symbol,symbol). mother(X,Y):-parent(X,Y),female(X).
nondeterm father(X,Y):-parent(X,Y),male(Y).
predecessor(symbol,symbol). brother(X,Y):-
nondeterm parent(Z,X),parent(Z,Y),male(X).
maternal_aunt(symbol,symbol). sister(X,Y):-
male(symbol). parent(Z,X),parent(Z,Y),female(X).
female(symbol). grandfather(X,Y):-parent(X,Y).
clauses predecessor(W,X):-
parent(Y,X),predecessor(W,Y).
parent (pam,bob). maternal_aunt(X,Y):-
parent(Z,Y),female(X),female(Z),pare
parent (tom,bob). nt(W,Z),parent(W,Z).
parent (tom,liz). goal
parent (bob,ann).
parent (bob,pat). grandfather(X,jim).
parent (pat,jim).
male(tom).
Lecture 1: An Introduction 11
Complete Prolog Program-II
PREDICATES
nondeterm likes(symbol,symbol)
CLAUSES
likes(ellen,tennis).
likes(john,football).
likes(tom,baseball).
likes(eric,swimming).
likes(mark,tennis).
likes(bill,Activity):-
likes(tom, Activity).
GOAL
likes(bill, baseball).
Lecture 1: An Introduction 12