Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Paradigms
CSI2120
Jochen Lang
EECS, University of Ottawa
Canada
Logic Programming in Prolog
• History
• Logic Programming
• Prolog
– facts and rules
– atoms and variables
• Queries
– Search
– Variable instantiation
– Unification
• First Examples
Alain Colmerauer and Philippe Roussel. The birth of Prolog. In History of programming languages---II,
ACM, New York, NY, USA 331-367, 1996.
∀x Subset x, x ,
∀ ∀ ∀ , ∧ , → , ,
∀a ∀ ℎ , ℎ → ℎ , ℎ ,
∀x ∀ ∀r ∀ ′ ∀y′
, , ∧ , ! ∧ , ′ → , ′, ′ .
Alain Colmerauer and Philippe Roussel. The birth of Prolog. In History of programming languages---II,
ACM, New York, NY, USA 331-367, 1996.
• Specify Facts
– which are true in a problem domain. Will remain true forever.
• Define rules
– which when applied establish new facts.
• Start queries
– and the prolog interpreter answers
• Prolog uses first order logic to prove answers
– It answers Yes following a successfully proven answer
– It answers No otherwise
• A no answer means it could not prove a positive answer
• Consists of
– predicate symbols
– equality
– negation
– logic binary connections
– quantifiers ‘for all …’ and ‘there exists … such that’
• More on this later …
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.
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, in this case “liker” is first,
“liked” is second, i.e., like(cats,dogs). is a different
fact.
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
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)
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.
Note
• , represents “and”
• can have any number of questions separated by , (comma)
and ending with . (dot)
• The head of the rule describes what fact the rule is intended
to define.
• The body can be a conjunction of goals.
– “Horses like X if X like hay and mice."
like(horses,X) :- like(X,hay), like(X,mice).
• There are 3 occurrences of X. Whenever X becomes
instantiated, all X's are instantiated to the same thing.
• Logic Programming
• Prolog
– facts and rules
– atoms and variables
• Queries
– Search
– Variable instantiation
– Unification
• First Examples