Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 24: Programming with Prolog Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs Announcements § Videos for C++ and LISP are available on Canvas (Week 11 to Week 15). § Quiz 5 and Homework 5 are due today (Nov 30 at 11:59pm)

Slide 3

Slide 3 text

jgs Logic Programming Paradigm PROLOG

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs Paradigms

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs Summary Logic/Declarative Paradigm Key idea: A program is a set of facts about objects, rules about objects, and questions (queries) about the relations between objects. Features: • get rid of programming altogether.

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs Predicate Logic § Predicate logic involves statements that contain variables, which may be true or false depending on variables' value or values. predicates

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs Predicate Logic PROGRAM data relationships

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 8 jgs Logic Paradigm § It focus on what is needed (requirements), instead of how to implement them. It just describe what the problem is and let the computer find a way to solve the problem, instead describing how to solve the problem.

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs Prolog § PROLOG (PROgramming LOGic) § Interpreter. § Deductive database: set of statements and a deduction system. § Facts and Rules § Queries § Upper-Case variables § Lower-Case constants and names

Slide 10

Slide 10 text

jgs Tools PROLOG

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs Install a Prolog Interpreter http://www.swi-prolog.org/download/stable

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs Online Prolog Interpreter

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs Local Prolog Interpreter To enter rules from the command line, type this : [user]. This puts you in a mode where you can enter facts and rules. Otherwise you get this kind of message: ERROR: Undefined procedure: (DWIM could not correct goal) You can then enter facts (or rules) e.g. : father(me,sarah). After having entered the knowledge, type CONTROL-D to come back to the mode where you can enter questions. Then you can ask: ?- father(me,X). X = sarah To quit: halt.

Slide 14

Slide 14 text

jgs Getting Started PROLOG

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 jgs Predicate Logic § It uses a simplified variation of predicate logic syntax, which is similar to natural language. § Predicate logic eliminate all unnecessary words from sentences, then transform the sentence by placing the relationship (predicate) first and grouping the objects (arguments) after the relationship predicate (object, object, object ....)

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 jgs Predicate Logic § 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, e.g., Bill is the father of Joe, if Joe is the son of bill. father(bill, joe) :- son(joe, bill). , means AND ; means OR

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 jgs Natural Language Predicate Logic Type of Predicate A car is fast. fast(car). fact A rose is red. red(rose). fact Bill likes the car if the car is fast. likes(bill, car) :- fast(car). rule Humidity is high if it rains high(humidity):- rains(). rule Jane is mother of Elaine mother_of(jane, elaine). fact Jane is mother of Mike mother_of(jane, mike). fact David is father of Jesse father_of(david, jesse). fact Jesse is father of Obed father_of(jesse, obed). fact Grandmother X of Z if (X is mother of Y and (Y is mother of Z or Y is father of Z) grandmother_of(X, Z) :- mother_of(X, Y), ( mother_of(Y, Z); father_of(Y, Z) ). rule

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 jgs Simple Facts § Facts and rules creates a database, so, step 1, provide data % facts: exam_easy. arizona_hot. july_4_is_independence_day. § step 2, run queries ?- exam_easy. true ?- arizona_hot. true ?- july_4_is_independence_day true

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 jgs Facts with Arguments likes(john,mary). eats(fred,oranges). age(john,32). age(ian,2). ?- eats(fred,oranges). true ?- eats(mike,apples). false ?- age(ian,two). false

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 20 jgs Facts with Arguments likes(john,mary). eats(fred,oranges). age(john,32). age(ian,2). ?- eats(fred,X). X = oranges ?- eats(mike,X). false ?- age(ian,Y). Y = 2 ?- likes (WhoX, WhoY). WhoX = john, WhoY = mary

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 23 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

Slide 24

Slide 24 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 24 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

Slide 25

Slide 25 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 25 jgs Questions

Slide 26

Slide 26 text

jgs Test Yourselves

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 29 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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 32 jgs Q7 1 2 3 4

Slide 33

Slide 33 text

jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.