# LOGIC PROGRAMMING. - lec 1 by nyut545e2

VIEWS: 9 PAGES: 8

• pg 1
```									                                                                                                   Lectures 1-2
LOGIC PROGRAMMING
EMAT 108                                                    •course introduction
1st Stage
Engineering Mathematics
Computer Systems Engineering                                         •introduction to computing
2002 - 03

Lecturer                                                 •artificial intelligence
Dr. Trevor Martin
Trevor.Martin@bristol.ac.uk                                        •logic programming and Fril
Laboratories
Dr. Jonathan Rossiter                                           •representing facts
Jonathan.Rossiter@bristol.ac.uk
Demonstrators
Sachin Karale
Tom Griffin

OBJECTIVE
A BRIEF Introduction To Computers
To give a practical and theoretical introduction to the fundamental
To give a practical and theoretical introduction to the fundamental
ideas of logic programming using the Fril language,
ideas of logic programming using the Fril language,
particularly the difference between declarative and procedural
particularly the difference between declarative and procedural                    collections
collections          letters, lecture notes, class lists
interpretations, and the use of recursion.                                         of data
of data              telephone numbers, input for
interpretations, and the use of recursion.                                                              mathematical calculations,
...

STRUCTURE                                                          programs
programs             instructions telling the computer
how to process data - e.g. a
system to find phone numbers
from a data file, a system to
2 lectures per week from wk 1 - wk 7 - not all will be used                                        ...

3 hours programming lab from wk 1 - wk 7                                  Both are stored as files

Assessment - by project and report, to be completed by end of week 9.    Programs interact with the user, and with data files

Programming and Logic                                                        Artificial Intelligence
I   programming                                                              I    Artificial Intelligence
–   telling the computer what to do                                            –   programming computers to act as though they are intelligent
I   logic
–   the science of reasoning
–   provides a precise language for expressing ideas,                    I    “to predict the future, we need logic but we also need
assumptions, relationships, arguments                                     faith and imagination which can sometimes defy logic
I   logic programming                                                             itself”
attempt to use the techniques of logic in computer                         –   (Arthur C. Clarke)
programming
describe a problem to the computer                                   I    HAL was built on Jan 12 1997
define assumptions and knowledge

1
What’s in the box ?                                                                            What’s in the box ?
I    computers that act like human beings
–   how can we tell ?
I   computers that act like human beings
–   do you need to be intelligent to play noughts and crosses ?
–   how can we tell ?                                                                     Possible rules (in order of preference):
if two X’s and one space in a line, put X in the space
–   do you need to be intelligent to play noughts                                         OR
and crosses ?                                                                         if two O’s and one space in a line, put X in the space
OR
if one X and two spaces in a line, put X in a space
(order of preference -     any square giving two X-X-space lines
centre square
corner square
middle of edge
OR
if all spaces, put X in any corner square

Answer : NO - we can write a simple rule-based program to play it

What is intelligence ?                                                                                                 Turing Test

I   Is it                                                                                                                                     Judge

–   playing noughts and crosses ?
–   playing chess ?
–   answering A-level Maths questions ?                                                                               Human or computer???
–   diagnosing diseases ?
–   driving a car? (or even parking it?)
I   Can we tell whether a computer is intelligent?
–   Turing test?

Intelligent Computers
Human or Computer ?                                                                       I   Alan Turing (1950)
A           Let's talk about something vaguely interesting. We are participating in a
Turing test here. It's kind of cool. You get to decide whether I'm a human             –    “By the end of the century one will be able to speak of machines
being or a computer program, and I get to abuse total strangers! Since I don't              thinking without being contradicted”
know what you're on about, I'll ask you a question. How old are you?
B           Old enough to have used punched cards to run my first program. How about          I   Think about progress in hardware, software, interfaces, …
you?                                                                                   –    we take for granted spelling, grammar checkers, typesetting, …
A           Hmmm, I suppose your age doesn't really matter. Tell me something I don't
know! Uh-huh...Would you like to hear a joke?
1950’s             1960’s          1970’s           1980’s            1990’s....    2001
B           A joke? Nothing off color, OK? These transcripts are being archived.                    room-sized        filing cabinet
A           Well, you wouldn't have found my joke funny anyway. Hmmm, I really don't                                                      desktop PC notebook PC PDA             ubiquit
computers         sized computers
know many jokes, but I do enjoy them. I particularly like knock-knock jokes, if
you have any to tell.
Nothing? Are you *sure* about that? Interesting...You don't seem to me to be a                                        keyboard graphical
punched tape, punched                                                voice
typical American, mate. I was wondering which country you're from?                                                       +        user
hardwiring    cards                                               recognition
B           America.                                                                                                               monitor interface
A           Well, I suppose that would have been my first guess anyway, since we're in New
York City!
B           Look I gotta go now. Better luck next time.
A           Whatever. Fine. Do you have a partner?                                                                                                                games
Noughts and
Draughts Backgammon Chess            machines
crosses

2
Prediction vs Reality
Fuzzy logic washing machine
Samsung's new range of washing machines offer a host of innovative features. Each model holds
I2001(book)        - “Hal” was created Jan 12, 1997                                        a large 5Kg / 11 pound wash loadand offers spin speed selections of 400 to 100 rpm. The
SWF-P12 offers 1200rpm spinning and the SWF-P14 offers up to 1400rpm spin speed.
Prediction                                Reality                                       General Features
Fuzzy Logic Control
magnetic tape memory                   solid state memory                            Fuzzy Logic Controls     The term 'Fuzzy Logic' describes a machine
mainframes, teletype                   PC’s , notebooks, mice                        Jog Dial                      capable of making a decision. Using
Informative Digital Display computerised sensors the Samsung
computers could lip-read               recognition - single words and still images   Delay Start
Washing Machine senses the weight of the
Fault Check Indicator
computers win at chess                 go to Dixons                                  'A' Grade Washing Performance load and the water temperature and
electronic newspads, updated hourly    WWW                                                Simple               determines the most appropriate amount
of water to use, in addition to in technological
The new washing machine from Samsung comes packed with the latestthe length
Economical                    and stages of the wash cycle.
your computer wants to kill you        you want to kill your computer                The Fuzzy Logic Microcomputer constantly monitors the washing process resulting in the highest
possible wash performance, whilst saving time, energy and water.
Fuzzy Logic Control
The term 'Fuzzy Logic' describes a machine capable of making a decision. Using computerised
Iprogress    in hardware has been phenomenal                                                 sensors the Samsung Washing Machine senses the weight of the load and the water
temperature and determines the most appropriate amount of water to use, in addition to the
Iprogress    in software is also remarkable - not always as predicted                        length and stages of the wash cycle.
Practical
The Jog Dial can be operated simply in either direction and carries all of the most frequently used
programs, as well as many more. The slanted control panel allows easy access to the
features from a standing position

Is Programming Logical?                                                                Logical deduction
Logical axioms                      inference
Theorems
I   When does X equal Y ?                                                             (knowledge base)

for i = 1 to 10
All postmen drink tea                                          Pat drinks tea
Y=Y+1                 program
X=X-3                                                                 Pat is a postman
end
express as X drinks tea if X is a postman
question : who drinks tea
y + 10 = x - 30                mathematics
X drinks tea if X is a postman
X drinks coffee if X is a fireman
k is a blodge                              k⊕X=X                                                  Pat is a postman
s(X) is a blodge if X is a blodge          s(X) ⊕ Y = s(X ⊕ Y )                                   Sam is a fireman
Is s(s(s(k))) a blodge ?                   Does s(k) ⊕ s(k) = s(s(k)) ?
do you need to know the meaning of the symbols to recognise valid deduction?

Fril                                                                              A Simple Knowledge Base
I    this represents information on flights between
I   Fril is a logic programming language,                                              various cities
which we will use on this course.                                                   –   “there is a flight from London to Dublin with flight
number ba172”
I   It interacts with you and with logic                                          I    we wish to extract information from the computer
programs which you have written and                                                 –   query ?((flight paris amsterdam af271))
stored in files.                                                                    –   computer answers yes        ((flight london dublin ba172))

((flight london paris ba345))
I   To run a session with Fril:                                                                                                      ((flight paris amsterdam af271))
–    start Fril                                                                                                               ((flight edinburgh london ba215))

–    ask Fril questions                                                                                                       ((flight amsterdam frankfurt ba119))

3
A Simple Knowledge Base                                                             A Simple Knowledge Base
I   this represents information on flights between                                  I   this represents information on flights between
various cities                                                                      various cities
–   “there is a flight from London to Dublin with flight                            –   “there is a flight from London to Dublin with flight
number ba172”                                                                       number ba172”
I   we wish to extract information from the computer                                I   we wish to extract information from the computer
–   query ?((flight paris amsterdam af271))                                         –   query ?((flight paris amsterdam af271))
–   computer answers yes        ((flight london dublin ba172))                      –   computer answers yes        ((flight london dublin ba172))

((flight london paris ba345))                                                       ((flight london paris ba345))

((flight paris amsterdam af271))                                                    ((flight paris amsterdam af271))

Predicate                        ((flight edinburgh london ba215))                  Predicate                        ((flight edinburgh london ba215))

((flight london frankfurt lf419))                                                   ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))
Arguments                      ((flight amsterdam frankfurt ba119))

Queries                                                                             Queries
I   Knowledge base + query ⇒ execution                                              I   Knowledge base + query ⇒ execution
?((flight edinburgh london X))                                                      ?((flight edinburgh london X))

((flight london dublin ba172))                                                      ((flight london dublin ba172))

((flight london paris ba345))                                                       ((flight london paris ba345))
((flight paris amsterdam af271))                                                    ((flight paris amsterdam af271))

((flight edinburgh london ba215))                                                   ((flight edinburgh london ba215))
((flight london frankfurt lf419))                                                   ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                                                ((flight amsterdam frankfurt ba119))

Queries                                                                             Queries
I   Knowledge base + query ⇒ execution                                              I   Knowledge base + query ⇒ execution
?((flight edinburgh london X))                                                      ?((flight edinburgh london X))

((flight london dublin ba172))                                                      ((flight london dublin ba172))

((flight london paris ba345))                                                       ((flight london paris ba345))
((flight paris amsterdam af271))                                                    ((flight paris amsterdam af271))

((flight edinburgh london ba215))                                                   ((flight edinburgh london ba215))

((flight london frankfurt lf419))                                                   ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                                                ((flight amsterdam frankfurt ba119))

4
Queries                                                             Multiple Queries
I   Knowledge base + query ⇒ execution                                    I   Knowledge base + query ⇒ execution
?((flight edinburgh london X))                                    ?((flight london X F1) (flight X amsterdam F2) (p X))

X = ba215                                                   X=dublin, F1=ba172

((flight london dublin ba172))                                            ((flight london dublin ba172))

((flight london paris ba345))                                             ((flight london paris ba345))

((flight paris amsterdam af271))                                          ((flight paris amsterdam af271))
((flight edinburgh london ba215))                                         ((flight edinburgh london ba215))

((flight london frankfurt lf419))                                         ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                                      ((flight amsterdam frankfurt ba119))

Multiple Queries                                                          Multiple Queries
I   Knowledge base + query ⇒ execution                                    I   Knowledge base + query ⇒ execution
?((flight london X F1) (flight dublin amsterdam F2) (p dublin))           ?((flight london X F1) (flight dublin amsterdam F2) (p dublin))

((flight london dublin ba172))                                            ((flight london dublin ba172))

((flight london paris ba345))                                             ((flight london paris ba345))
((flight paris amsterdam af271))                                          ((flight paris amsterdam af271))

((flight edinburgh london ba215))                                         ((flight edinburgh london ba215))
((flight london frankfurt lf419))                                         ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                                      ((flight amsterdam frankfurt ba119))

Multiple Queries                                                          Multiple Queries
I   Knowledge base + query ⇒ execution                                    I   Knowledge base + query ⇒ execution
?((flight london X F1) (flight dublin amsterdam F2) (p dublin))           ?((flight london X F1) (flight dublin amsterdam F2) (p dublin))

Try to find
another solution
((flight london dublin ba172))
for X          ((flight london dublin ba172))

((flight london paris ba345))                                             ((flight london paris ba345))
((flight paris amsterdam af271))                                          ((flight paris amsterdam af271))
(… etc)
((flight edinburgh london ba215))                                         ((flight edinburgh london ba215))

((flight london frankfurt lf419))                                         ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                                      ((flight amsterdam frankfurt ba119))

5
Multiple Queries                                                             Multiple Queries
I   Knowledge base + query ⇒ execution                                      I   Knowledge base + query ⇒ execution
?((flight london X F1) (flight X amsterdam F2) (p X))                      ?((flight london X F1) (flight paris amsterdam F2) (p paris))

X=paris, F1=ba345

((flight london dublin ba172))                                               ((flight london dublin ba172))

((flight london paris ba345))                                                ((flight london paris ba345))

((flight paris amsterdam af271))                                             ((flight paris amsterdam af271))
((flight edinburgh london ba215))                                            ((flight edinburgh london ba215))

((flight london frankfurt lf419))                                            ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                                         ((flight amsterdam frankfurt ba119))

Multiple Queries                                                             Multiple Queries
I   Knowledge base + query ⇒ execution                                      I   Knowledge base + query ⇒ execution
?((flight london X F1) (flight paris amsterdam F2) (p paris))               ?((flight london X F1) (flight paris amsterdam F2) (p paris))

F2=af271

((flight london dublin ba172))                                               ((flight london dublin ba172))

((flight london paris ba345))                                                ((flight london paris ba345))
((flight paris amsterdam af271))                                             ((flight paris amsterdam af271))

((flight edinburgh london ba215))                                            ((flight edinburgh london ba215))
((flight london frankfurt lf419))                                            ((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                                         ((flight amsterdam frankfurt ba119))

Multiple Queries
Multiple Queries
I   Knowledge base + query ⇒ execution
?((flight london X F1) (flight paris amsterdam F2) (p paris))           I   Backtracking search

?((flight london X F1) (flight X amsterdam F2) (p X))
((flight london dublin ba172))

((flight london paris ba345))
((flight paris amsterdam af271))
(flight london X F1)                                                    (p X)
((flight edinburgh london ba215))                                                                 (flight X amsterdam F2)
((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                  1   2       3      4     5       6

1       2   3   4      5     6

6
Multiple Solutions                                                                                                      Negation
((flight london dublin ba172))
((flight london dublin ba172))
((flight london paris ba345))
((flight london paris ba345))
((flight paris amsterdam af271))
((flight paris amsterdam af271))
((flight edinburgh london ba215))
((flight edinburgh london ba215))
?((flight london edinburgh X))
((flight london frankfurt lf419))
((flight london frankfurt lf419))
((flight amsterdam frankfurt ba119))                        no
((flight amsterdam frankfurt ba119))
?(( flight london X F) (p X F))
matches first clause in knowledge base but could also match                                    Two possibilities to deal with negation:
second and fifth clause - need to force backtracking
Closed world - all relevant information is in the knowledge base, if
query doesn't match anything then it is false
?((flight london X F) (p X F) (pp) (fail))
dublin ba172
paris ba345                                                                                 Open world - information in the knowledge base is true but not
frankfurt lf419                                                                               necessarily comprehensive. If query doesn't match any clause
then its truth is unknown - may be true or false
no

Queries
Multiple Queries
I   Knowledge base + query ⇒ execution
?((flight edinburgh london X))
I       Backtracking search

?((flight london X F1) (flight X amsterdam F2) (p X))
((flight london dublin ba172))

((flight london paris ba345))
((flight paris amsterdam af271))
(flight london X F1)                                                                            (p X)
((flight edinburgh london ba215))
(flight X amsterdam F2)
((flight london frankfurt lf419))

((flight amsterdam frankfurt ba119))                               1       2           3         4       5        6

1        2       3       4        5       6

Rules                                                                       Execution of Rules
dublin
(( two_stage X Y) (flight X Z F1)(flight Z Y F2))
edinburgh                    london                        paris             amsterdam
?((two_stage london A) (p A))

frankfurt
(two_stage london A)                                                        (p A)
flight defines a relation between cities linked by a single
flight. Suppose we want to define a relation two_stage                                                              X=london
between cities linked by a journey involving two flights                                                            Y=A

(flight X Z F1) (flight Z Y F2)
((   two_stage    edinburgh dublin))
((   two_stage    edinburgh paris))
(i) As facts        ((   two_stage    edinburgh frankfurt ))
((   two_stage    london amsterdam))
((   two_stage    paris frankfurt ))                                              (flight X Z F1)                                  (flight Z Y F2)

(ii) As a rule    (( two_stage X Y) (flight X Z F1) (flight Z Y F2))

conclusion      if condition1 and condition2                          1           2         3       4       5        6            1       2       3       4        5       6

7
Logic and Programming                                                                                     Arithmetic
(( two_stage X Y) (flight X Z F1) (flight Z Y F2))                                     ((sum 0 0 0))         ((sum 1 0 1)) …
((sum 0 1 1))         ((sum 1 1 2)) …
((sum 0 2 2))         …
For all X and Y, (two_stage X Y) can be derived if there exists Z,                  ((sum 0 3 3))
F1, and F2 such that (flight X Z F1) and (flight Z Y F2) hold
• defines the relation sum on non-negative integers
• could extend to negatives, reals, other relations…
In order to execute (two_stage X Y), first execute (flight X Z F1) and              • not an efficient way to implement the relation
then (flight Z Y F2)
Alternative : sum is a built-in predicate which is almost
• interpret program                                                                    equivalent to the definition above
either as logical statements                                                        ?((sum 1 2 3))
or as a sequence of instructions                                                    ?((sum 1 2 4))
?((sum 1 2 X) ( pp X))                  3
• interpret query                                                                   ?((sum 1 X 3) (pp X))                 2
?((sum X Y 3) (pp X Y))
either as theorem to be proved
or as starting point for execution
Similarly times square sin etc
• program can be used in many different ways                                     help arith in Fril gives further information
according to query

Recursion                                                                      Recursive Rule
• related to mathematical proof by induction                                      Attempt (ii)
• define (travel X Y) to be true if it is possible to travel from city
X to city Y by any number of flights                                          ((travel X Y) (flight X Y F))
((travel X Y) (flight X Z F) (travel Z Y))
Rule 1
Attempt (i)
((travel X Y) (flight X Y F))
((travel X Y)(two_stage X Y))
((travel X Y)(two_stage X Z)(flight Z Y F))                                                    X
Y
((travel X Y)(two_stage X Z)(two_stage Z Y))
dublin                           Rule 2 (i)
Y
edinburgh                london                 paris                amsterdam

X          Z
frankfurt

(ii)                                                           Y
Z'
What happens if we add more flights ?
How can be sure we have covered all cases ?
X'

8

```
To top