Learn Prolog Now! SWI Prolog Freely available Prolog interpreter - - PowerPoint PPT Presentation

learn prolog now swi prolog
SMART_READER_LITE
LIVE PREVIEW

Learn Prolog Now! SWI Prolog Freely available Prolog interpreter - - PowerPoint PPT Presentation

Learn Prolog Now! SWI Prolog Freely available Prolog interpreter Works with Linux, Windows, or Mac OS There are many more Prolog interpreters Not all are ISO compliant/free Lecture 1 Theory Introduction to


slide-1
SLIDE 1

Learn Prolog Now!

slide-2
SLIDE 2

SWI Prolog

  • Freely available Prolog interpreter
  • Works with

– Linux, – Windows, or – Mac OS

  • There are many more Prolog

interpreters

  • Not all are ISO compliant/free
slide-3
SLIDE 3

Lecture 1

  • Theory

– Introduction to Prolog – Facts, Rules and Queries – Prolog Syntax

  • Exercises

– Exercises of LPN chapter 1 – Practical work

slide-4
SLIDE 4

Aim of this lecture (1/2)

  • Give some simple examples of

Prolog programs

  • Discuss the three basic constructs

in Prolog:

– Facts – Rules – Queries

slide-5
SLIDE 5

Aim of this lecture (2/2)

  • Introduce other concepts, such as

– the role of logic – unification with the help of variables

  • Begin the systematic study of Prolog

by defining

– terms – atoms, and – variables

slide-6
SLIDE 6

Prolog

  • "Programming with Logic"
  • Very different from other programming

languages

– Declarative (not procedural) – Recursion (no “for” or “while” loops) – Relations (no functions) – Unification

slide-7
SLIDE 7

History of Prolog

1972 1977 1980 1980s/1990s 2005

first Prolog interpreter by Alain Colmerauer and Philippe Roussel

slide-8
SLIDE 8

History of Prolog

1972 1977 1980 1980s/1990s 2005

implementation of DEC10 compiler by David H.D. Warren

slide-9
SLIDE 9

History of Prolog

1972 1977 1980 1980s/1990s 2005 Definite Clause Grammars implementation by Pereira and Warren

slide-10
SLIDE 10

History of Prolog

1972 1977 1980 1980s/1990s 2005 Prolog grows in popularity especially in Japan and Europe

slide-11
SLIDE 11

History of Prolog

1972 1977 1980 1980s/1990s 2005 Prolog used to program natural language interface in International Space Station by NASA

slide-12
SLIDE 12

History of Prolog

1972 1977 1980 1980s/1990s 2011 Parts of IBM’s Watson QA supercomputer were coded in Prolog

slide-13
SLIDE 13

Prolog and Web Applications

  • prolog programs are often smaller
  • smallness encourages well written code
  • hence, easier to maintain

Source: Source: http://www.pathwayslms.com/swipltuts/

slide-14
SLIDE 14

Basic idea of Prolog

  • Describe the situation of interest
  • Ask a question
  • Prolog:

– logically deduces new facts about the situation we described – gives us its deductions back as answers

slide-15
SLIDE 15

Consequences

  • Think declaratively, not procedurally

– Challenging – Requires a different mindset

  • High-level language

– Not as efficient as, say, C – Good for rapid prototyping – Useful in many AI applications (knowledge representation, inference)

slide-16
SLIDE 16

Knowledge Base 1

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

slide-17
SLIDE 17

Knowledge Base 1

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

slide-18
SLIDE 18

Knowledge Base 1

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

slide-19
SLIDE 19

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). yes ?-

slide-20
SLIDE 20

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody).

slide-21
SLIDE 21

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody). yes ?-

slide-22
SLIDE 22

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody). yes ?- playsAirGuitar(mia).

slide-23
SLIDE 23

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). yes ?- playsAirGuitar(jody). yes ?- playsAirGuitar(mia). no

slide-24
SLIDE 24

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody).

slide-25
SLIDE 25

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody). no ?-

slide-26
SLIDE 26

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?-

slide-27
SLIDE 27

Knowledge Base 1

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

slide-28
SLIDE 28

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- party. yes ?-

slide-29
SLIDE 29

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- rockConcert.

slide-30
SLIDE 30

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- rockConcert. no ?-

slide-31
SLIDE 31

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

slide-32
SLIDE 32

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

fact

slide-33
SLIDE 33

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

fact fact

slide-34
SLIDE 34

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

fact fact rule

slide-35
SLIDE 35

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

fact fact rule rule

slide-36
SLIDE 36

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

fact fact rule rule rule

slide-37
SLIDE 37

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

head body

slide-38
SLIDE 38

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?-

slide-39
SLIDE 39

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?-

slide-40
SLIDE 40

Knowledge Base 2

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?- playsAirGuitar(yolanda). yes

slide-41
SLIDE 41

Clauses

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

There are five clauses in this knowledge base: two facts, and three rules. The end of a clause is marked with a full stop.

slide-42
SLIDE 42

Predicates

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

There are three predicates in this knowledge base: happy, listens2music, and playsAirGuitar

slide-43
SLIDE 43

Knowledge Base 3

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

slide-44
SLIDE 44

Expressing Conjunction

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

The comma “," expresses conjunction in Prolog

slide-45
SLIDE 45

Knowledge Base 3

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(vincent).

slide-46
SLIDE 46

Knowledge Base 3

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(vincent). no ?-

slide-47
SLIDE 47

Knowledge Base 3

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(butch).

slide-48
SLIDE 48

Knowledge Base 3

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(butch). yes ?-

slide-49
SLIDE 49

Expressing Disjunction

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch); listens2music(butch).

slide-50
SLIDE 50

Prolog and Logic

  • Clearly, Prolog has something to do

with logic...

  • Use of inference (modus ponens)
  • Negation (?)

Prolog Logic Implication A :- B B  A Conjunction A,B A ∧ B Disjunction A;B A ∨ B

slide-51
SLIDE 51

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

slide-52
SLIDE 52

Prolog Variables

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X).

slide-53
SLIDE 53

Variable Instantiation

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia

slide-54
SLIDE 54

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia;

slide-55
SLIDE 55

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody

slide-56
SLIDE 56

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody; X=yolanda

slide-57
SLIDE 57

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody; X=yolanda; no

slide-58
SLIDE 58

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(marsellus,X), woman(X).

slide-59
SLIDE 59

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(marsellus,X), woman(X). X=mia yes ?-

slide-60
SLIDE 60

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(pumpkin,X), woman(X).

slide-61
SLIDE 61

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(pumpkin,X), woman(X). no ?-

slide-62
SLIDE 62

Knowledge Base 5

loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).

slide-63
SLIDE 63

Knowledge Base 5

loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- jealous(marsellus,W).

slide-64
SLIDE 64

Knowledge Base 5

loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- jealous(marsellus,W). W=vincent ?-

slide-65
SLIDE 65

Syntax of Prolog

  • Q: What exactly are facts, rules and

queries built out of?

  • A: Prolog terms
slide-66
SLIDE 66

Prolog terms

Terms Simple Terms Complex Terms Constants Variables Atoms Numbers Terms Simple Terms Complex Terms Constants Variables Atoms Numbers

slide-67
SLIDE 67

Atoms

  • A sequence of characters of upper-case letters,

lower-case letters, digits, or underscore, starting with a lowercase letter

– Examples: butch, big_kahuna_burger, playGuitar

slide-68
SLIDE 68

Atoms

  • A sequence of characters of upper-case letters,

lower-case letters, digits, or underscore, starting with a lowercase letter

– Examples: butch, big_kahuna_burger, playGuitar

  • An arbitrary sequence of characters enclosed in

single quotes

– Examples: 'Vincent', 'Five dollar shake', '@$%'

slide-69
SLIDE 69

Atoms

  • A sequence of characters of upper-case letters,

lower-case letters, digits, or underscore, starting with a lowercase letter

– Examples: butch, big_kahuna_burger, playGuitar

  • An arbitrary sequence of characters enclosed in

single quotes

– Examples: 'Vincent', 'Five dollar shake', '@$%'

  • A sequence of special characters

– Examples: : , ; . :-

slide-70
SLIDE 70

Numbers

  • Integers:

12, -34, 22342

  • Floats:

34573.3234, 0.3435

slide-71
SLIDE 71

Variables

  • A sequence of characters of upper-

case letters, lower-case letters, digits,

  • r underscore, starting with either an

uppercase letter or an underscore

  • Examples:

X, Y, Variable, Vincent, _tag

slide-72
SLIDE 72

Complex Terms

  • Atoms, numbers and variables are

building blocks for complex terms

  • Complex terms are built out of a

functor directly followed by a sequence

  • f arguments

– Arguments are put in round brackets, separated by commas – The functor must be an atom

slide-73
SLIDE 73

Examples of complex terms

  • Examples we have seen before:

– playsAirGuitar(jody) – loves(vincent, mia) – jealous(marsellus, W)

  • Complex terms inside complex terms:

– hide(X,father(father(father(butch))))

slide-74
SLIDE 74

Arity

  • The number of arguments a complex

term has is called its arity

  • Examples:

woman(mia) is a term with arity 1 loves(vincent,mia) has arity 2 father(father(butch)) arity 1

slide-75
SLIDE 75

Arity is important

  • You can define two predicates with the

same functor but with different arity

  • Prolog would treat this as two different

predicates!

  • In Prolog documentation, arity of a

predicate is usually indicated with the suffix "/" followed by a number to indicate the arity

slide-76
SLIDE 76

Example of Arity

  • This knowledge base defines

– happy/1 – listens2music/1 – playsAirGuitar/1

happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).