Logic Programming - PowerPoint PPT Presentation

1 / 79
About This Presentation
Title:

Logic Programming

Description:

... representation language, and a or model-generator is used as the problem-solver. ... Example: Crossword. abalone. abandon. enhance. anagram. connect ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 80
Provided by: joha166
Category:

less

Transcript and Presenter's Notes

Title: Logic Programming


1
Logic Programming
  • Raul Cruz-Cano

2
Announcements
3
Logic Programming
  • the use of mathematical logic for computer
    programming.
  • In this view of logic programming, which can be
    traced at least as far back as John McCarthy's
    1958 advice-taker proposal
  • It was probably the first proposal to use logic
    to represent information in a computer and the
    first paper to propose common sense reasoning
    ability as the key to artificial intelligence.
  • In his paper, McCarthy advocated
  • "programs to manipulate in a suitable formal
    language common instrumental statements. The
    basic program will draw immediate conclusions
    from a list of premises. These conclusions will
    be either declarative or imperative sentences.
    When an imperative sentence is deduced the
    program takes a corresponding action."
  • Logic is used as a purely declarative
    representation language, and a or
    model-generator is used as the problem-solver.

4
Logic Programming
  • The problem-solving task is split between
  • the programmer, who is responsible only for
    ensuring the truth of programs expressed in
    logical form, and
  • the theorem-prover or model-generator, which is
    responsible for solving problems efficiently.

5
Prolog
  • The programming language Prolog was developed in
    1972 by Alain Colmerauer (Marseille) and Robert
    Kowalski (Edinburgh)
  • Colmerauer was working on natural language
    understanding, using logic to represent semantics
    and using resolution for question-answering.
  • During the summer of 1971, they discovered that
    the clausal form of logic could be used to
    represent formal grammars and that resolution
    theorem provers could be used for parsing.
  • It was in the following summer of 1972, that
    Kowalski, again working with Colmerauer,
    developed the procedural interpretation of
    implications which later became formalised in the
    Prolog notation

6
Learn Prolog Now!
7
GNU Prolog
  • Freely available Prolog interpreter
  • Works with Windows,
  • There are many more Prolog interpreters
  • Not all are ISO compliant

8
Lecture
  • Theory
  • Introduction to Prolog
  • Facts, Rules and Queries
  • Prolog Syntax
  • Exercises
  • Exercises of LPN chapter 1
  • Practical work

9
Aim of this lecture
  • Give some simple examples of Prolog programs
  • Discuss the three basic constructs in Prolog
  • Facts
  • Rules
  • Queries
  • 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

10
Prolog
  • "Programming with Logic"
  • Declarative
  • Very different from other (procedural)
    programming languages
  • Good for knowledge-rich tasks

11
History of Prolog
first Prolog interpreter by Colmerauer and Roussel
1972
1980s/1990s
1977
1980
2005
12
History of Prolog
implementation of DEC10 compiler by Warren
1972
1980s/1990s
1977
1980
2005
13
History of Prolog
Definite Clause Grammars implementation by
Pereira and Warren
1972
1980s/1990s
1977
1980
2005
14
History of Prolog
Prolog grows in popularity especially in Europe
and Japan
1972
1980s/1990s
1977
1980
2005
15
History of Prolog
Prolog used to program natural language interface
in International Space Station by NASA
1972
1980s/1990s
1977
1980
2005
16
Basic idea of Prolog
  • Describe the situation of interest
  • Ask a question
  • Prolog logically deduces new facts about the
    situation we described
  • Prolog gives us its deductions back as answers

17
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

18
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

19
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?-
20
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- woman(mia).
21
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- woman(mia). yes ?-
22
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- woman(mia). yes ?- playsAirGuitar(jody).
23
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- woman(mia). yes ?- playsAirGuitar(jody). yes ?-
24
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- woman(mia). yes ?- playsAirGuitar(jody). yes ?-
playsAirGuitar(mia). no
25
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- tattoed(jody).
26
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- tattoed(jody). no ?-
27
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- tattoed(jody). ERROR predicate tattoed/1 not
defined. ?-
28
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- party.
29
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- party. yes ?-
30
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- rockConcert.
31
Knowledge Base 1
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • playsAirGuitar(jody).
  • party.

?- rockConcert. no ?-
32
Knowledge Base 2
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

33
Knowledge Base 2
fact
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

34
Knowledge Base 2
fact
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

fact
35
Knowledge Base 2
fact
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

fact
rule
36
Knowledge Base 2
fact
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

fact
rule
rule
37
Knowledge Base 2
fact
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

fact
rule
rule
rule
38
Knowledge Base 2
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

head
body
39
Knowledge Base 2
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

?-
40
Knowledge Base 2
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

?- playsAirGuitar(mia). yes ?-
41
Knowledge Base 2
  • happy(yolanda).
  • listens2music(mia).
  • listens2music(yolanda)- happy(yolanda).
  • playsAirGuitar(mia)- listens2music(mia).
  • playsAirGuitar(yolanda)- listens2music(yolanda).

?- playsAirGuitar(mia). yes ?- playsAirGuitar(yola
nda). yes
42
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.
43
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
44
Knowledge Base 3
  • happy(vincent).
  • listens2music(butch).
  • playsAirGuitar(vincent)- listens2music(vincent),
    happy(vincent).
  • playsAirGuitar(butch)- happy(butch).
  • playsAirGuitar(butch)- listens2music(butch).

45
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
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 ?-
47
Knowledge Base 3
  • happy(vincent).
  • listens2music(butch).
  • playsAirGuitar(vincent)- listens2music(vincent),
    happy(vincent).
  • playsAirGuitar(butch)- happy(butch).
  • playsAirGuitar(butch)- listens2music(butch).

?- playsAirGuitar(butch). yes ?-
48
Expressing Disjunction
  • happy(vincent).
  • listens2music(butch).
  • playsAirGuitar(vincent)- listens2music(vincent),
    happy(vincent).
  • playsAirGuitar(butch)- happy(butch).
  • playsAirGuitar(butch)- listens2music(butch).

happy(vincent). listens2music(butch). playsAirGuit
ar(vincent)- listens2music(vincent),
happy(vincent). playsAirGuitar(butch)-
happy(butch) listens2music(butch).
49
Prolog and Logic
  • Clearly Prolog has something to do with logic
  • Operators
  • Implication -
  • Conjunction ,
  • Disjunction
  • Use of modus ponens
  • Negation

50
Knowledge Base 4
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • loves(vincent, mia).
  • loves(marsellus, mia).
  • loves(pumpkin, honey_bunny).
  • loves(honey_bunny, pumpkin).

51
Prolog Variables
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • loves(vincent, mia).
  • loves(marsellus, mia).
  • loves(pumpkin, honey_bunny).
  • loves(honey_bunny, pumpkin).

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

?- woman(X). Xmia
53
Asking Alternatives
  • woman(mia).
  • woman(jody).
  • woman(yolanda).
  • loves(vincent, mia).
  • loves(marsellus, mia).
  • loves(pumpkin, honey_bunny).
  • loves(honey_bunny, pumpkin).

?- woman(X). Xmia
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). Xmia Xjody
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). Xmia Xjody Xyolanda
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). Xmia Xjody Xyolanda no
57
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).
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). Xmia yes ?-
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(pumpkin,X), woman(X).
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). no ?-
61
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).

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).

?- jealous(marsellus,W).
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). Wvincent ?-
64
Prolog Syntax
  • What exactly are facts, rules and queries built
    out of?

Terms
Terms
Simple Terms
Complex Terms
Simple Terms
Complex Terms
Constants
Variables
Constants
Variables
Atoms
Numbers
Atoms
Numbers
65
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',
    '_at_'
  • A sequence of special characters
  • Examples , . -

66
Numbers
  • Integers 12, -34, 22342
  • Floats 34573.3234

67
Variables
  • A sequence of characters of upper-case letters,
    lower-case letters, digits, or underscore,
    starting with either an uppercase letter or an
    underscore
  • Examples X, Y, Variable, Vincent, _tag

68
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 of arguments
  • Arguments are put in round brackets, separated by
    commas
  • The functor must be an atom

69
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))))

70
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

71
Arity is important
  • In Prolog 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

72
Example of Arity
happy(yolanda). listens2music(mia). listens2music(
yolanda)- happy(yolanda). playsAirGuitar(mia)-
listens2music(mia). playsAirGuitar(yolanda)-
listens2music(yolanda).
  • This knowledge base defines
  • happy/1
  • listens2music/1
  • playsAirGuitar/1

73
Summary of this lecture
  • Simple examples of Prolog programs
  • Introduced three basic constructs in Prolog
  • Facts
  • Rules
  • Queries
  • Discussed other concepts, such as
  • the role of logic
  • unification with the help of variables
  • Definition of Prolog constructs terms, atoms,
    and variables

74
Next Lecture?
  • Discuss unification in Prolog
  • Prolog's search strategy

75
Exercise Witches
76
Example Family Tree
77
Example Sentence
78
Example Crossword
  • abalone
  • abandon
  • enhance
  • anagram
  • connect
  • elegant

79
Example Sudoku
Write a Comment
User Comments (0)
About PowerShow.com