5 Prolog Intro
5 Prolog Intro
Programming Paradigms
1
• Course overview
• Introduction to programming
paradigms
• Review: The object-oriented
Topics paradigm in Java
• Imperative and concurrent
programming paradigm: Go.
• Logic paradigm: Prolog.
• Functional paradigm: Scheme.
• Quiz 2 is available ends on
Thursday Feb 4th
• Quiz 1 ends on Thursday Jan 28th
Announcement • Assignment 1 – late submission
ends Jan 27th
• Comprehensive assignment –
Part 2 – GO will be uploaded soon
The slides posted through the term are based of the
slides offered by:
Prof. Robert Laganiere
Prof. Jochen Lang
Acknowledgment Demo code:
https://www.site.uottawa.ca/~jl
ang/CSI2120/demoCode.html
Logic Programming in Prolog
• History
• Logic Programming
• Prolog
– facts and rules
– atoms and variables
• Queries
– Search
– Variable instantiation
– Unification
• First Examples
Prolog History
• Consists of
– predicate symbols
– equality
– negation
– logic binary connections
– quantifiers ‘for all …’ and ‘there exists … such that’
• More on this later …
Computation in Prolog
Specified by
• partly by the logical declarative semantics of Prolog
(more on this later),
• partly by what new facts Prolog can infer from the given
ones, and
• partly by explicit control information supplied by the
programmer.
– In other words Prolog has/requires some imperative,
or prescriptive features.
Facts
In Prolog: like(dogs,cats).
• lower case for both individuals and relationships
• relationship (or predicate) is written first
• individuals (or arguments) are written in parenthesis,
separated by commas
• ends with a dot “.”
• order of arguments is important but it is up to us to
define, in this case “liker” is first, “liked” is second, i.e.,
like(cats,dogs). is a different fact.
More facts
Other examples:
domestic(cows). % cows are domestic animals.
faster(horses,cows). % horses run faster than cows
take(cats,milk,cows). % cats take milk from cows
isYellow(hay). % hay is yellow.
eat(cows,hay). % Cows eat hay.
• Constants or Atoms
– Example: cows, horses, hay, cats, milk
– Symbolic: small caps letter followed by letters and
numbers
– Numbers : integer and float
Interpretation of Facts
Is “cats” an individual?
Yes, but there is more than one way to interpret it.
• a particular type of cat, e.g., house cats
• a family of animals encompassing tigers, leopards, etc.
Arity of Predicates
Predicates can have an arbitrary number of arguments
domestic/1 isYellow/1 % 1 argument
faster/2 like/2 eat/2 % 2 arguments
takes/3 % 3 arguments
Database
• a collection of facts (part of a program)
Queries or Questions
Example: ?- eat(cats,mice).
• Means "Do cats eat mice?" or "Is it a fact that cats eat
mice?"
• Note as before, cats are interpreted as a specific species
(house cats) and mice are all type of mice.
• Note that the syntax is the same as for facts, except for
the special symbol ?- (printed by the interpreter) to
distinguish from a fact.
A Database
Note
• , represents “and”
• can have any number of questions separated by ,
(comma) and ending with . (dot)
Example with Variables
“Is there anything that horses and cows both like?”
2 steps:
1. Find out if there is some X that cows like.
2. Then find out if horses like whatever X is.
?- like(cows,X), like(horses,X).
Note:
• After finding the first answer for X (hay), Prolog marks the
place in the database.
• Prolog attempts to satisfy the second goal (with X
instantiated).
• If it succeeds, Prolog marks (separately) that goal's place
in the database.
• Each goal keeps its own place-marker.
Rules
• Logic Programming
• Prolog
– facts and rules
– atoms and variables
• Queries
– Search
– Variable instantiation
– Unification
• First Examples