Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 26

CSE240 Lecture 26

Introduction to Programming Languages
Expressions in Prolog
(202011)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

January 26, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 26: Expressions

    in Prolog Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. jgs Previously …

  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs

    Facts, Rules, and Queries Facts § mother_child(trude, sally). § father_child(tom, sally). § father_child(tom, erica). § father_child(mike, tom). Rules § sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). § parent_child(X, Y) :- father_child(X, Y). § parent_child(X, Y) :- mother_child(X, Y). Query § ?- sibling(sally, erica). true
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs

    Facts, Rules, and Queries % rules and facts: mortal(X) :- human(X). human(socrates). ?- mortal(socrates). true ?- mortal(P). P = socrates
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs

    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).
  6. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs

    … 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
  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs

    … 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
  8. jgs Test Yourselves

  9. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs

    Q1 What is the predicate logic expression equivalent to "Charlie owns a computer" owns(charlie, computer).
  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs

    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).
  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs

    Q3 If weather is cold, everyone likes it. Write this statement as a Prolog clause. likes(X, weather) :- cold(weather). %notice “weather” in lowercase
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs

    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).
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs

    Q6 Which rule defines in Prolog the following sentence: "If someone owns something, he loves it" loves(Who, What):-owns(Who, What).
  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 jgs

    Q7 1 2 3 4
  15. jgs Expressions

  16. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 jgs

    Arithmetic Queries ?- X is 3 + 5. X=8 ?- X is 3 mod 2. X=1 ?- X is 3 mod 2 - 1 * 5 / 9. X=0.4444444444444444 ?- X is 5+4, Y is 5-4. X=9 Y=1 ?- X is 5*4, Y is 2^3. X=20 Y=8
  17. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 jgs

    Arithmetic Queries ?- X is 234556^100. X = 105841181316488988298177591518 4... ?- X is 5/3. X=1.66666667 ?- X is sqrt(3),Y is 3^0.5. X=Y Y=1.7320508075688772 ?- X is 8 mod 3. X=2 ?- Y is 10^3 * (1+1) + 3. Y=2003
  18. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 jgs

    Arithmetic Queries ?- X is 10+5. X = 15 ?- 6 is 10+5. false ?- 15 is 10+5. true ?- 10+5 is X. Error: Arguments are not sufficiently instantiated ?- 10+5 is 10+5. false
  19. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 20 jgs

    Arithmetic Queries § X is 5 /3, X = 1. false § X is 5 /3, Y = 1. X=1.666666667, Y=1 § X is 5 /3; X = 1. X = 1.6666666667 X = 1
  20. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    Spring 2018 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.