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

CSE240 (online) Lecture 14

CSE240 (online) Lecture 14

Introduction to Programming Languages
Programming with Prolog
(201805)

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE240 – Introduction to Programming Languages (online) Lecture 14: Programming

    with Prolog Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    2 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. • It rely on the environment to find solutions that meet requirements.
  3. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    3 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 ....)
  4. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    4 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
  5. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    5 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
  6. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    6 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
  7. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    7 Install a Prolog Interpreter SWI - Prolog http://www.swi-prolog.org/download/stable
  8. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    8 Install a 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.
  9. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    10 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
  10. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    11 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
  11. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    12 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
  12. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    13 Facts and Rules % rules and facts: mortal(X) :- human(X). human(socrates). ?- mortal(socrates). true ?- mortal(P). P = socrates
  13. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    14 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).
  14. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    15 … 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
  15. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    16 … 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
  16. Javier Gonzalez-Sanchez | CSE 240 (online) | Fall 2017 |

    17 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 | CSE 240 (online) | Fall 2017 |

    18 Arithmetic Queries ?- X is 234556^100. X = 1058411813164889882981775915184... ?- 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 | CSE 240 (online) | Fall 2017 |

    19 Recursion % this is fact factorial(0, 1). % this is a rule % the factorial of F is N*F1 % if N>0 and % N1 is N-1 and % the factorial of N1 is F1 factorial(N, F) :- N>0, N1 is N - 1, factorial(N1, F1), F is N * F1. ?- factorial (3, W). W=6 1. factorial(3, W) apply rule 2 2. 3>0, N1 is 3-1, factorial (N1, F1), F is 3 *F1 solve the individual parts true, 2 is 3-1, factorial (2, F1), F is 3*F1 apply rule 2 again 3. 2>0, N2 is 2-1, factorial (N2, F2), F1 is 2 * F2 solve the individual parts true, 1 is 2-1, factorial (1, F1), F1 is 2*F2 apply rule 2 again 4. 1>0, N3 is 1-1, factorial (N3, F3), F2 is 2 * F3 solve the individual parts true, 0 is 1-1, factorial (0, F3), F2 is 1*F3 apply rule 1 again 5. factorial (0, 1) F3 is 1 6. Go back, replace F3 to get F2, then F2 to get F1, then F1 to get F.
  19. CSE240 – Introduction to Programming Languages (online) Javier Gonzalez-Sanchez [email protected]

    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.