CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

24
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries

Transcript of CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Page 1: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

CSC 270 – Survey of Programming Languages

Prolog Lecture 1 – Facts, Rules, and Queries

Page 2: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Basic Constructs of Prolog

• Prolog has only three basic constructs:– Facts – statements that are unconditionally true– Rules – statements that are conditionally true– Queries – questions pertaining to the information

stored in a knowledge base

• A knowledge base is a collection of facts and rules

• Writing a program in Prolog is all about creating the knowledge base.

Page 3: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Knowledge Base #1

woman(mia).woman(jody).woman(yolanda).playsAirGuitar(yolanda).party.

Page 4: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Our Prolog Environment

• Panther – swipl– Create files using notepad. – Consult the file using [filename without.pl].• For kb1.pl use [kb1].

– Ctrl d to exit OR exit command

• SWI-Prolog on windows or mac– http://www.swi-prolog.org/Download.html

Page 5: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Querying Knowledge Base #1

1 ?- % c:/documents and settings/adelphi/my documents/prolog/kb1 compiled 0.00 sec, 6 clauses1 ?- woman(mia).true.2 ?- playsAirGuitar(jody).false.3 ?- playsAirGuitar(mia).false.4 ?- playsAirGuitar(vincent).false.

Page 6: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

5 ?- tatooed(jody).ERROR: toplevel: Undefined procedure: tatooed/1 (DWIM could not correct goal)6 ?- party.true.7 ?- rockConcert.ERROR: toplevel: Undefined procedure: rockConcert/0 (DWIM could not correct goal)8 ?-

Page 7: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Infer (or Deduce)

• Prolog determines that Vincent doesn’t play Air Guitar because there is no fact from which it can infer (or deduce).

• The error messages for tatooed and rockConcert are due to the fact that SWI Prolog did not find these terms when compiling the knowledge base.

Page 8: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Knowledge Base #2Add 3 rules:

happy(yolanda).listens2Music(mia).listens2Music(yolanda) :- happy(yolanda).playsAirGuitar(mia) :- listens2Music(mia).playsAirGuitar(yolanda) :- listens2Music(yolanda).listens2Music(amy) :- happy(amy).

If Yolanda is happy then she listens to musicLeft is head, right is bodyProlog can infer the head from the body being true

3 rules, 2 facts, 5 clausesPredicates or procedures: happy, listens2Music and playsAirGuitar

Page 9: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Querying Knowledge Base #2

9 ?- playsAirGuitar(mia).true.

10 ?- playsAirGuitar(yolanda).true.

11 ?- listens2Music(amy).False.

Page 10: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Knowledge Base #3

• This knowledge base consists of 2 facts and 3 rules. happy(vincent).listens2Music(butch).playsAirGuitar(vincent):-

listens2Music(vincent),happy(vincent).playsAirGuitar(butch):-happy(butch).playsAirGuitar(butch):-listens2Music(butch).

Translation: • If vincent is happy and vincent listens2music,

then vincent plays air guitar.

Page 11: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Running Knowledge Base #3

11 ?- "playsAirGuitar(vincent)"?false.

12 ?-

• This is because while KB3 contains happy(vincent) , it does not explicitly contain the information listens2Music(vincent) , and this fact cannot be deduced either.

Page 12: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Running Knowledge Base #3 (continued)

12 ?- playsAirGuitar(butch).true.13 ?-

• The knowledge base gives us two ways of deducing this through two different rules:

playsAirGuitar(butch):-happy(butch).playsAirGuitar(butch):-listens2Music(butch).

• This is effectively equivalent to an OR.

Page 13: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Knowledge Base #4

7 factswoman(mia).woman(jody).woman(yolanda).

loves(vincent,mia).loves(marsellus,mia).loves(pumpkin,honey_bunny).loves(honey_bunny,pumpkin).-- read as vincent loves mia

Page 14: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Prolog Variables

• Capital letters start a variable name – Actually a placeholder to lookup a match– Ex : woman(X).• Tell me which individual you know to be a woman

– Returns the first occurrence– Press ; for each additional match• X = mia ;• X = jody ;• X = yolanda ;• X = honey-bumpkin ;

Page 15: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Querying Knowledge Base #4

17 ?- loves(marsellus, X).-- who loves marsellus?X = mia.18 ?- loves(marsellus, X), woman(X).-- who loves marsellus and is a woman?; requires unification of two facts with the matching XX = mia.

Page 16: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Knowledge Base #5

A rule with a variable:

loves(vincent,mia).loves(marsellus,mia).loves(pumpkin,honey_bunny).loves(honey_bunny,pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).

Page 17: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Running Knowledge Base #5

20 ?- jealous(marsellus, W).W = vincent -- any other jealous people?-- trace.-- stop with notrace.

Page 18: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Prolog Syntax

• Facts, rules and queries are built from terms.• There are four types of terms in Prolog:– atoms– numbers– variables– complex terms (or structures)

• Atoms and numbers are constants and constants and variables make up the simple terms of Prolog.

Page 19: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Atoms

• An atom is either:1. A string of uppercase letters, lowercase letters,

underscores and digits that begin with a lowercase letter. (E.g., butch, big_kahuna, etc.)

2. An arbitrary sequence of characters inside single quotes (E.g., ‘Vincent’, ‘The Gimp’, ‘((*&^*&^*&^’)

3. A string of special characters (E.g., @= , ===>, ;, and :-)

Page 20: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Numbers

• The typical Prolog application does not make heavy use of numbers.

• Most Prolog implmentations support floating point numbers and integers.

Page 21: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Variables

• A variable is a string of uppercase letters, lowercase letters, digits and underscores that starts with either an uppercase letter or an underscore. (E.g., X, Y, Variable, _tag, X_526, List, List24, etc.)

• _ is a special variable and it is called the anonymous variable.

Page 22: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Complex Terms

• We can combine atoms, numbers and variables to form complex terms (also called structures).

• Complex terms are built out of a functor followed by a sequence of arguments.

• Examples– playsAirGuitar(jody).– loves(vincent, mia).– hide(X, father(father(father(butch)))).

Page 23: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Recursive Structure

• Nesting complex terms inside complex terms indefinitely leads to recursive structures.

Page 24: CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Arity

• Arity refers to the number of arguments that a functor has.

• It becomes important when the same functor can have more than one different count of arguments (or different arities).