+ All Categories
Home > Documents > 1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003...

1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003...

Date post: 01-Jan-2016
Category:
Upload: gladys-gaines
View: 217 times
Download: 1 times
Share this document with a friend
Popular Tags:
29
1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel
Transcript
Page 1: 1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.

1

CS2136:Paradigms of Computation

Class 03:Logic Programming

Prolog

Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel

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

2

Logic Programming

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

3

Logic Programming: Why?

Another way to use computers.Useful for mathematical proofs.Useful in AI.This could be done in almost any

language, but Prolog is designed for it.

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

4

Things to Remember

Logic deals with domains of discourse. Mathematics Geography Company organization

Logic can only deal with a model of the real world.

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

5

What You Have &What You Can Do

You have statements which you assert are true.

You have rules which govern how to derive conclusions from statements.

You try to prove other statements either true or false.

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

6

Proving Something True“Proving something true” in logic really

just means you can derive that statement from what you already claim you know. It does not guarantee it is true in the real

world. Some of the statements you based it on

may be false. Some of the rules may be incorrect. Some things are true but cannot be proven.

Russell & Whitehead Principia MathematicaGodel’s Incompleteness Theorem

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

7

Proving Something False

“Proving something false” in logic may really mean that you cannot derive that statement from what you already claim you know. It does not guarantee it is false in the

real world.Must really distinguish between:

Really proving something false. Not being able to prove that something

is true.

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

8

“Closed World Assumption”

If you cannot prove something is true, it is considered false.

“Negation as Failure”

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

9

First-Order Predicate Calculus

Deals with logical statements i.e. statements which can be either true

or false.Deals with objects and their

relationships.Deals with rules for these

relationships.

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

10

FOPC Example (in English)

Socrates is a human.All humans are mortal.Therefore, Socrates is mortal.

Note: This was proven true in the real world.

More on FOPC later.

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

11

Prolog

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

12

Prolog

“Programming in Logic”Concerned with objects and their

relationships. Not “objects” in the sense of “object-

oriented”. Examples (in English, not Prolog):

John owns the book.John likes Mary.

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

13

Programming Prolog

Consists of: Declaring facts about objects and their

relationships. Defining rules about objects and their

relationships. Asking questions about objects and

their relationships.

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

14

Representing Facts

Meaning: John likes Mary.

In Prolog likes (john, mary).

Looks like Lisp!In this example:

The first argument is the liker. The second argument is the likee (??).

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

15

Representing Facts IIlikes (john, mary).Note:

Relationship name (a.k.a. “predicate”) is lower-case and comes first.

Object names are lower-case. Period (a.k.a. “full stop”) at the end. #arguments > 2 is OK. Order is arbitrary, but…

You must decide.You must be consistent.

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

16

Representing Facts III

Remember: Prolog knows absolutely nothing about

the real world. The meaning is entirely arbitrary. These would work just as well:

likes (john, mary).likes (dirt, anger).afgh (xyyxx, yhhww).

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

17

More Examples of Facts

female(jane).play(john, mary, football).owns(john, gold).Is “gold”

A particular object? A chemical elements? An attribute of objects?

All the facts in a Prolog program form a “database”, stored in the order entered.

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

18

Even More Facts About Facts

Objects can be more than single words with no upper-case letters; use single quotation marks. loves(john, ‘WPI’). loves(john, ‘Worcester Polytechnic

Institute’).If you enter an identical fact twice,

Prolog saves both copies in the database.

What do you do with these facts?

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

19

Running Prolog

The CCC Unix machines have SWI-Prolog loaded.

You can download it for: Windows

window and command-line versions

Unix Linux

Many other Prolog implementations are available.

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

20

Running Prolog IIClick on a menu, click an icon, or

type: pl

Get the prompt: ?-

Load a program using: consult(filename).

Don’t forget the period!Suffix assumed to be “.pl”.

• Can conflict with Perl.

Example: consult(‘likes.pl’).

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

21

Running Prolog IIIThe Clocksin & Mellish book assumes

you can just enter everything from the keyboard.

SWI-Prolog is a compiler, so: Programs must ordinarily be read from a

file. You can ask questions from the keyboard.

Prolog has no built-in graphics, but can be integrated with graphics packages.

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

22

Listing the Database

?- consult(‘likes.pl’). [Me]% likes.pl compiled 0.11 sec, 40 bytesYes?- listing(likes). [Me]likes(john,mary).likes(john,gold).likes(mary,john).likes(john,football).likes(john,mary).

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

23

Some Commands

listing. Lists everything.

listing(predicate). Lists just facts and rules with that

predicate.halt.

Exits Prolog.

Arity = number of arguments.

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

24

Asking Questions

No need to type in the “?-” that shows in the book.

Can ask specific or more general questions.

Specific: likes(john,mary).

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

25

Questions & Answers

Prolog answers yes or no.“Yes” just means it can prove what

you asked is true. i.e. it found a match in the database.

“No” means “can’t prove it by me!” i.e. no match. This does not necessarily mean “false”.

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

26

ExampleFile ‘likes.pl’:

likes(john,mary).likes(john,gold).likes(mary,john).likes( john, football).likes(john,mary).

Questions & Answers:?- likes(john,mary).Yes?- likes(john,jane).No

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

27

Variables

They can stand for objects.A variable is

“Instantiated” if it currently stands for an object.

“Not instantiated” if it doesn’t.Start with an upper-case letter.

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

28

Questions with Variables

Prolog looks for a match.likes(john,X).

Logically means: “Is there anything John likes?”

Really means: “Is there an X such that this is true?”

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

29

Next Time

More Prolog: Variables Goals Backtracking


Recommended