Introduction. Prolog Programming. Summary. COMPSCI366. Foundations of
Artificial Intelligence†. †Department of Computer Science. University of Auckland.
Introduction Prolog Programming Summary
COMPSCI366 Foundations of Artificial Intelligence† † Department
of Computer Science University of Auckland
[email protected]
Computer Science 366, 2006
Note: These slides do not contain the interactive examples that were demonstrated during the tutorial.
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Outline
1
Introduction Logic Programming Prolog Syntax Running Prolog
2
Prolog Programming Musical Example Familiar Example
3
Summary
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Outline
1
Introduction Logic Programming Prolog Syntax Running Prolog
2
Prolog Programming Musical Example Familiar Example
3
Summary
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Outline
1
Introduction Logic Programming Prolog Syntax Running Prolog
2
Prolog Programming Musical Example Familiar Example
3
Summary
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
Outline
1
Introduction Logic Programming Prolog Syntax Running Prolog
2
Prolog Programming Musical Example Familiar Example
3
Summary
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
What is declarative programming? All programming languages have both declarative and imperative components. Imperative programming describes how to create something. algorithm explicit, goal implicit.
Declarative programming describes what something is. goal explicit, algorithm implicit.
Jasvir Nagra
Java public class P { public float f ( int n ){ assert n != 0; return Math . sin ( n )/ n ; } }
Math f (x) =
sin(x) x
where x 6= 0.
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
What is Prolog?
Prolog is a programming language approaches problem-solving in a declarative manner.
The idea is to define what the problem is, rather than how it should be solved.
In practice, most Prolog programs have a procedural as well as a declarative component — the procedural aspects are often necessary in order to make the programs execute efficiently.
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
Facts and Rules Everything in Prolog is defined in terms of two constructs: facts assert predicates which are unconditionally satisfied.
rules specifies the conditions under which a predicate is satisfied.
Programming in Prolog involves writing a series of facts and rules that describe your domain. These make up a database. We can then make queries against the database which Prolog tries to satisfy.
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
Facts File: directed-graph.pl edge (a , b ). edge (b , c ). edge (c , d ).
Directed Graph
node ( a ). node ( b ). node ( c ). node ( d ). path (A , C ): - edge (A , B ) , path (B , C ). path (A , B ): - edge (A , B ).
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
Rules File: undirected-graph.pl
Undirected Graph
edge (a , b ). edge (b , c ). edge (c , d ). node ( a ). node ( b ). node ( c ). node ( d ).
Question Why can we not define path as we did before?
Jasvir Nagra
edge (A , B ): - node ( A ) , node ( B ) , A == B .
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
SWI-Prolog or SICTUS
Under Windows: Start > Programs > Utilities > Prolog
Under Linux:
Commands consult halt. help.
$ swipl
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
Queries Example Queries are executed after we load our database using consult. Queries look just like facts or rules but now we are asking the database our query is satisfiable using the facts or rules in the database. If there are unbound variables in our query and they can satisfied by Prolog, the satisfying instantiations are listed by Prolog. Jasvir Nagra
? - consult ( ’ graph ’ ). Yes ? - node ( a ). Yes ? - node ( z ). No ? - node ( X ). X = a ; X = b ; X = c ; X = d ; No ?Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Logic Programming Prolog Syntax Running Prolog
Syntax summary
Prolog syntax: A predicate is an identifier followed by a n-tuple and a fullstop. Constants start with a lowercase letter. Variables start with an uppercase letter. A comma means “AND” while a semicolon means “OR”. A semicolon asks if there are any more values that satisfy a query. The relations may be used for testing the usual arithmetic relationships on numbers.
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Musical Example Familiar Example
Outline
1
Introduction Logic Programming Prolog Syntax Running Prolog
2
Prolog Programming Musical Example Familiar Example
3
Summary
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Musical Example Familiar Example
Objects & Relationships
Prolog programs deal with objects, and relationships between objects English “Jas likes music” Prolog likes(jas, music).
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Musical Example Familiar Example
Music Database Here’s an excerpt from a music database: Prolog is_song ( planet_waves ). is_song ( desire ). is_song ( slow_train ). recorded_by ( planet_waves , bob_dylan ). recorded_by ( desire , bob_dylan ). recorded_by ( slow_train , bob_dylan ). recording_year ( planet_waves , 1974). recording_year ( desire , 1975). recording_year ( slow_train , 1979).
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Musical Example Familiar Example
Music Database
The data base contains unary facts (is song) and binary facts (recorded by, recording year).
The fact is song(slow train) can be interpreted as slow train is-a-song The fact recording year(slow train, 1979) can be interpreted as the recording year of slow train was 1979.
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Musical Example Familiar Example
Family Tree Example
parent (P , C ) : - child (C , P ). husband (H , W ) : - spouse (H , W ) , mother (M , C ) : - parent (M , C ) , f male ( homer ). spouse ( homer , marge ). child ( bart , homer ). child ( maggie , homer ). child ( lisa , homer ). child ( lisa , marge ). female ( marge ).
Homework Do more of these. Chocolate fish prize for surprise question. Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
Outline
1
Introduction Logic Programming Prolog Syntax Running Prolog
2
Prolog Programming Musical Example Familiar Example
3
Summary
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
In this tutorial we have learnt... Prolog is a declarative language. A database consists of a set of facts and rules. A query consists of one or more goals: ?- likes(jas, X), smart(X).
Queries are either Satisfiable (the goal succeeds) Unsatisfiable (the goal fails)
If a query succeeds, you can get Prolog to give you all the values that satisfy a predicate using the semicolon key.
Jasvir Nagra
Foundations of Artificial Intelligence
Introduction Prolog Programming Summary
In this tutorial we have learnt...
Questions?
Jasvir Nagra
Foundations of Artificial Intelligence