Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.

Similar presentations


Presentation on theme: "1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel."— Presentation transcript:

1 1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel

2 2 Logic Programming

3 3 Logic Programming: Why? zAnother way to use computers. zUseful for mathematical proofs. zUseful in AI. zThis could be done in almost any language, but Prolog is designed for it.

4 4 Things to Remember zLogic deals with domains of discourse. yMathematics yGeography yCompany organization zLogic can only deal with a model of the real world.

5 5 What You Have & What You Can Do zYou have statements which you assert are true. zYou have rules which govern how to derive conclusions from statements. zYou try to prove other statements either true or false.

6 6 Proving Something True z“Proving something true” in logic really just means you can derive that statement from what you already claim you know. yIt does not guarantee it is true in the real world. ySome of the statements you based it on may be false. ySome of the rules may be incorrect. ySome things are true but cannot be proven. xRussell & Whitehead Principia Mathematica xGodel’s Incompleteness Theorem

7 7 Proving Something False z“Proving something false” in logic may really mean that you cannot derive that statement from what you already claim you know. yIt does not guarantee it is false in the real world. zMust really distinguish between: yReally proving something false. yNot being able to prove that something is true.

8 8 “Closed World Assumption” zIf you cannot prove something is true, it is considered false. z“Negation as Failure”

9 9 First-Order Predicate Calculus zDeals with logical statements yi.e. statements which can be either true or false. zDeals with objects and their relationships. zDeals with rules for these relationships.

10 10 FOPC Example (in English) zSocrates is a human. zAll humans are mortal. zTherefore, Socrates is mortal. zNote: This was proven true in the real world. zMore on FOPC later.

11 11 Prolog

12 12 Prolog z“Programming in Logic” zConcerned with objects and their relationships. yNot “objects” in the sense of “object- oriented”. yExamples (in English, not Prolog): xJohn owns the book. xJohn likes Mary.

13 13 Programming Prolog zConsists of: yDeclaring facts about objects and their relationships. yDefining rules about objects and their relationships. yAsking questions about objects and their relationships.

14 14 Representing Facts zMeaning: yJohn likes Mary. zIn Prolog ylikes (john, mary). zLooks like Lisp! zIn this example: yThe first argument is the liker. yThe second argument is the likee (??).

15 15 Representing Facts II zlikes (john, mary). zNote: yRelationship name (a.k.a. “predicate”) is lower-case and comes first. yObject names are lower-case. yPeriod (a.k.a. “full stop”) at the end. y#arguments > 2 is OK. yOrder is arbitrary, but… xYou must decide. xYou must be consistent.

16 16 Representing Facts III zRemember: yProlog knows absolutely nothing about the real world. yThe meaning is entirely arbitrary. yThese would work just as well: xlikes (john, mary). xlikes (dirt, anger). xafgh (xyyxx, yhhww).

17 17 More Examples of Facts zfemale(jane). zplay(john, mary, football). zowns(john, gold). zIs “gold” yA particular object? yA chemical elements? yAn attribute of objects? zAll the facts in a Prolog program form a “database”, stored in the order entered.

18 18 Even More Facts About Facts zObjects can be more than single words with no upper-case letters; use single quotation marks. yloves(john, ‘WPI’). yloves(john, ‘Worcester Polytechnic Institute’). zIf you enter an identical fact twice, Prolog saves both copies in the database. zWhat do you do with these facts?

19 19 Running Prolog zThe CCC Unix machines have SWI-Prolog loaded. zYou can download it for: yWindows xwindow and command-line versions yUnix yLinux zMany other Prolog implementations are available.

20 20 Running Prolog II zClick on a menu, click an icon, or type: ypl zGet the prompt: y?- zLoad a program using: yconsult(filename). xDon’t forget the period! xSuffix assumed to be “.pl”. Can conflict with Perl. xExample: consult(‘likes.pl’).

21 21 Running Prolog III zThe Clocksin & Mellish book assumes you can just enter everything from the keyboard. zSWI-Prolog is a compiler, so: yPrograms must ordinarily be read from a file. yYou can ask questions from the keyboard. zProlog has no built-in graphics, but can be integrated with graphics packages.

22 22 Listing the Database ?- consult(‘likes.pl’). [Me] % likes.pl compiled 0.11 sec, 40 bytes Yes ?- listing(likes). [Me] likes(john,mary). likes(john,gold). likes(mary,john). likes(john,football). likes(john,mary).

23 23 Some Commands zlisting. yLists everything. zlisting(predicate). yLists just facts and rules with that predicate. zhalt. yExits Prolog. Arity = number of arguments.

24 24 Asking Questions zNo need to type in the “?-” that shows in the book. zCan ask specific or more general questions. zSpecific: ylikes(john,mary).

25 25 Questions & Answers zProlog answers yes or no. z“Yes” just means it can prove what you asked is true. yi.e. it found a match in the database. z“No” means “can’t prove it by me!” yi.e. no match. yThis does not necessarily mean “false”.

26 26 Example zFile ‘likes.pl’: likes(john,mary). likes(john,gold). likes(mary,john). likes( john, football). likes(john,mary). zQuestions & Answers: ?- likes(john,mary). Yes ?- likes(john,jane). No

27 27 Variables zThey can stand for objects. zA variable is y“Instantiated” if it currently stands for an object. y“Not instantiated” if it doesn’t. zStart with an upper-case letter.

28 28 Questions with Variables zProlog looks for a match. zlikes(john,X). yLogically means: “Is there anything John likes?” yReally means: “Is there an X such that this is true?”

29 29 Next Time zMore Prolog: yVariables yGoals yBacktracking


Download ppt "1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel."

Similar presentations


Ads by Google