CSE240 – Introduction to
Programming Languages
Lecture 24:
Programming with Prolog III
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
A program is Facts, Rules …
% facts
instructor(john,cs365).
instructor(mary,cs311).
instructor(paul,cs446).
enrolled(joseph,cs311).
enrolled(joseph,cs365).
enrolled(joseph,cs446).
enrolled(danielle,cs365).
enrolled(danielle,cs446).
% rules
teaches(P,S) :- instructor(P,C), enrolled(S,C).
classmates(S1, S2) :- enrolled(S1, C), enrolled(S2, C).
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
… and Queries (part 1 / 2)
?- enrolled (joseph, cs365).
true
?- enrolled (X, cs365).
X=joseph
X=danielle
?- teaches (X, joseph).
X=john
X=mary
X=paul
?- classmates (joseph, danielle).
true
?- classmates (joseph, jessica).
false
?- classmates (jessica, danielle).
false
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
… and Queries (part 2 / 2)
% An additional rule
classmates (S1, S2, C) :- enrolled (S1, C), enrolled (S2, C)
?- classmates (joseph, danielle, C).
cs365
cs446
?- classmates (joseph, jessica, C).
false
?- classmates (jessica, danielle, C).
false
Test Yourselves
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Q1
What is the predicate logic expression equivalent to
"Charlie owns a computer"
owns(charlie, computer).
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Q2
instructor(john,cs365).
instructor(mary,cs311).
enrolled(danielle,cs365).
enrolled(danielle,cs446).
• Which query provides a list of all the students enrolled in cs365?
enrolled(Who, cs365).
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Q3
If weather is cold, everyone likes it.
Write this statement as a Prolog clause.
likes(X, weather) :- cold(weather).
%notice “weather” in lowercase
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Q5
• Given the following facts
• cat(tom).
• black_spots(tom).
• dog(pluto).
• white_spots(pluto)
•
Which rule defines in Prolog the following sentence:
• "mary owns a Pet if it is a cat and it has black spots"
owns(mary, Pet):- cat(Pet), black_spots(Pet).
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Q6
Which rule defines in Prolog the following sentence:
"If someone owns something, he loves it"
loves(Who, What):-owns(Who, What).
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Q7
1
2
3
4
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

201801 CSE240 Lecture 24

  • 1.
    CSE240 – Introductionto Programming Languages Lecture 24: Programming with Prolog III Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 2 A program is Facts, Rules … % facts instructor(john,cs365). instructor(mary,cs311). instructor(paul,cs446). enrolled(joseph,cs311). enrolled(joseph,cs365). enrolled(joseph,cs446). enrolled(danielle,cs365). enrolled(danielle,cs446). % rules teaches(P,S) :- instructor(P,C), enrolled(S,C). classmates(S1, S2) :- enrolled(S1, C), enrolled(S2, C).
  • 3.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 3 … and Queries (part 1 / 2) ?- enrolled (joseph, cs365). true ?- enrolled (X, cs365). X=joseph X=danielle ?- teaches (X, joseph). X=john X=mary X=paul ?- classmates (joseph, danielle). true ?- classmates (joseph, jessica). false ?- classmates (jessica, danielle). false
  • 4.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 4 … and Queries (part 2 / 2) % An additional rule classmates (S1, S2, C) :- enrolled (S1, C), enrolled (S2, C) ?- classmates (joseph, danielle, C). cs365 cs446 ?- classmates (joseph, jessica, C). false ?- classmates (jessica, danielle, C). false
  • 5.
  • 6.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 6 Q1 What is the predicate logic expression equivalent to "Charlie owns a computer" owns(charlie, computer).
  • 7.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 7 Q2 instructor(john,cs365). instructor(mary,cs311). enrolled(danielle,cs365). enrolled(danielle,cs446). • Which query provides a list of all the students enrolled in cs365? enrolled(Who, cs365).
  • 8.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 8 Q3 If weather is cold, everyone likes it. Write this statement as a Prolog clause. likes(X, weather) :- cold(weather). %notice “weather” in lowercase
  • 9.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 9 Q5 • Given the following facts • cat(tom). • black_spots(tom). • dog(pluto). • white_spots(pluto) • Which rule defines in Prolog the following sentence: • "mary owns a Pet if it is a cat and it has black spots" owns(mary, Pet):- cat(Pet), black_spots(Pet).
  • 10.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 10 Q6 Which rule defines in Prolog the following sentence: "If someone owns something, he loves it" loves(Who, What):-owns(Who, What).
  • 11.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 11 Q7 1 2 3 4
  • 12.
    CSE240 – Introductionto Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.