CSE240
Chapter 5
Dr. Yinong Chen
www.asu.edu/myasu
CSE240
Introduction to Programming Languages
Logic Programming language
‹#›
Ch 5
CSE240
11/19/2002
Chapter 5
Logic Language Prolog
Lecture 24
Prolog Basics
Reading: Textbook Sections 5.1 – 5.3 and Appendix B.5
Dr. Yinong Chen
CSE240
Introduction to Programming Languages
‹#›
Ch 5
CSE240
11/19/2002
Introduction
Logic programming paradigm
Prolog programs: facts, rules, and goals
Factbase
Goals (Questions)
Rulebase
Compound questions
Arithmetic operations
Recursive rules and recursive programs
Structures of facts and rules
Pairs, lists and their operations
Flow Control
Chapter Outline
‹#›
Ch 5
CSE240
11/19/2002
Dictionary Definition:
hungry: need for food
Buy pizza base, tomato sauce, cheese
Buy flour, tomato, meat, cheese, etc.
Imperative Programming
Object-Oriented Programming
‹#›
Ch 5
CSE240
11/19/2002
Dictionary Definition:
hungry: need for food
You sit in a restaurant, you tell the waiter / waitress:
I have $10 to spend
I like cheese and tomato
I need a drink
Phone call: I want a pizza, with
thin base,
double cheese, olives
ham, and bacon
Functional Programming
Logic Programming
+
or
‹#›
Ch 5
CSE240
11/19/2002
Different Programming Paradigms
Imperative: complex with full detail & full steps, create the code in the way you want.
Object-oriented: Reuse the components. No need to know how the components are made. Still need the process of using the components.
Functional: Focus on the functions of each component. The process is not the focus at all.
Logic: Focus on what is needed (requirements), instead of how. Rely on the environment to find solutions that meet requirements.
‹#›
Ch 5
CSE240
11/19/2002
Logic Programming Paradigm
Declarative/Logic: take a step further towards getting rid of programming altogether. Just describe what the problem is and let the computer find a way to solve the problem, instead describing how to solve the problem.
Prolog uses a simplified variation of predicate logic syntax, which is easy to understand and similar to natural language.
Predicate logic was developed for easily conveying logic-based ideas of true and false values into a written form.
In predicate logic, you first eliminate all unnecessary words from your sentences, then transform the sentence by placing the relationship (predicate) first and grouping the objects (arguments) after the relationship – in prefix notation!
‹#›
Ch 5
CSE240
11/19/2002
Predicate Logic
Natural Language: Predicate Logic:
A car is fast. fast(car).
A rose is red. red(rose).
Bill likes the car likes(bill, car)
if the car is fast. :- fast(car).
Humidity is high if it rains high(humidity):-rains().
Facts: What is known
e.g., Bill likes car and bike, and he travels with one of them
likes(bill, car), likes(bill, bike)
travels(bill, car); travels(bill, bike)
Rules: What you can infer from the given facts.
Rules enable you to infer facts from other facts.
Bill is the father of Joe, if Joe is the son of bill.
father(bill, joe) :- son(joe, bill).
‹#›
Ch 5
CSE240
11/19/2002
Prolog Terminology
A Prolog program is a set of facts and rules about objects, and relationship among these objects, that is, a program is a collection of information describing a particular situation.
A prolog program has three types of statements (clauses):
facts (axioms) about objects and their relationship
mother_of(jane, elaine). mother_of(jane, mike).
father_of(david, jesse). father_of(jesse, obed).
rules that extend facts: about objects and their relationship
grandmother_of(X, Z) :- mother_of(X, Y),
(mother_of(Y, Z); father_of(Y, Z)).
questions about objects and their relationship
?- grandmother_of(jane, conrad). ?- father_of(X, andrew).
AND
OR
‹#›
Ch 5
CSE240
11/19/2002
Facts
Syntax for facts:
relationship(object, …, object).
predicate
(functor)
arguments
# arguments = arity
weather(tempe, winter, warm).
weather(tempe, summer, hot).
weather/3
grandmother_of(jane, conrad).
grandmother_of/2
Notation: we use predicate/arity to refer to a set of facts or rules.
‹#›
Ch 5
CSE240
11/19/2002
Factbase (Database)
Facts are the simplest kind of Prolog statement:
male(luke). % male/1
male(mike).
female(sarah). % female/1
weather(tempe, summer, hot). % weather/3
weather(tempe, fall, hot).
weather(tempe, winter, warm).
class(cse240, programming, tue, thu). % class/4
The set of facts and rules forms a database.
Put the facts (rules) with the same predicates together.
‹#›
Ch 5
CSE240
11/19/2002
Goals: Asking Questions
Prolog program retrieves information from database by asking questions — goals. A goal succeeds, if there are facts (rules) that match or unify the goal. A goal clause (statement) and a fact unify, if
They have the same predicate.
They have the same arity (# arguments).
their corresponding arguments match.
If there is no match, a goal fails.
?- male(luke). –> yes
?- male(john). –> no
?- weather(phoenix, summer, hot). –> no
?- weather(tempe, hot). –> no
?- weather(tempe, fall, hot). –> yes
male(luke).
male(mike).
female(sarah).
weather(tempe, summer, hot).
weather(tempe, fall, hot).
weather(tempe, winter, warm).
class(cse240, programming, tue, thu).
‹#›
Ch 5
CSE240
11/19/2002
Prolog Variables
You can ask special and complex questions by placing variables in questions. A variable matches with anything.
?- male(X). –> X = luke; X = mike.
?- male(X, Y). –> no
?- class(cse240, Title, Day, thu).
–> Title = programming, Day = tue
?- weather(City, summer, hot).
–> City = tempe
A Prolog variable is a place-holder, not a memory location.
A value is returned to a variable.
A variable begins with an upper-case letter or an underscore.
A constant (value) doesn’t start with an upper-case letter.
A non-numerical constant is also called an atom, because
it is an entity that cannot be split into smaller components.
male(luke).
male(mike).
female(sarah).
weather(tempe, summer, hot).
weather(tempe, fall, hot).
weather(tempe, winter, warm).
class(cse240, programming, tue, thu).
‹#›
Ch 5
CSE240
11/19/2002