4 Lisp (Chapt-3) Prolog
4 Lisp (Chapt-3) Prolog
CSE 453
1
Contents
LISP and Other AI programming Language
Introduction to LISP
LISP: Syntax and Numeric Functions
List Manipulation function in LISP
PROLOG
2
Introduction to LISP
LISP (LISt processing) is invented by Jhon
McCarthy during the late 1950.
The basic building blocks of LISP are the atom,
list, and the string.
An atom is a number of string of contiguous
characters, including numbers and special
characters
A List is a sequence of atoms and/or other lists
enclosed within parentheses.
String is a group of characters enclosed in double
quotation marks.
3
Example of Atom, List & String
Atom: sat, week_day
List: (mon, tue, wed, thur, fri, sat, sun)
String: “please enter your name”
Atom, List and String are the only valid objects in
LISP and they are called Symbolic expression.
Any s-expression is potentially a valid program.
4
Introduction: LISP
LISP programs run either on an interpreter or as
compiled code.
The interpreter examines source programs in a
repeated loop, called read-evaluate-print loop.
For example: sum of 3 numbers
-> (+ 5 6 9)
20
->
LISP uses prefix notation.
5
Example of LISP
Convert Fahrenheit to Centigrade
C/5=(F-32)/9
or, F=C(9/5)+32
For C=50,
LISP program
->(+(*(/ 9 5)50)32)
122
->
Numerical functions: +, -, *, /
6
Basic List Manipulation Functions
7
Basic List Manipulation Functions
(last `(a b c d)) (d) return a list containing the last element.
8
Example of (setq()) function
9
How to Define Function?
(defun name (parm1 parm2 …) body)
Example:
->(defun avgthree (n1 n2 n3) (/(+n1 n2 n3) 3))
AVGTHREE
->
Note: its return the function name. If you call this
function it will return the result.
10
The Most Common Predicate calls
11
Conditional Statement
12
Input, Output Variables
14
Iteration
-> (defun factorial(n)
(do (count n (-count 1))
(product n (* product (-count 1))
((equal 0 count) product)))
FACTORIAL
->
15
Property Lists and Arrays
One of the unique and most useful features of
LISP as an AI language is the ability to assign
properties to atoms.
Example: properties of a car are make, year,
color, style, etc.
16
Property Lists and Arrays
The function putprop assign properties to an
atom or objects
(purtop object value attribute)
->(putprop `car `toyota `make)
TOYOTA
->(putprop `car 1998 `year)
1998
->(putprop `car `red `color)
RED
->(putprop `car `four-door `style)
FOUR-DOOR
17
Property Lists and Arrays
remprop: remove properties
get: retrieve property
Example:
-> (get `car `color)
RED
-> (remprop `car `color)
RED
->(get `car `color)
Nil
->
18
Property Lists and Arrays
Arrays:
->(setf myarray (make-array `(10)))
#A(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
->
To access array:
->(aref myarray 9)
NIL
->
Store:
->(setf (aref myarray 0) 25)
25
->
19
Recommended Textbooks
[Negnevitsky, 2001] M. Negnevitsky “ Artificial
Intelligence: A guide to Intelligent Systems”,
Pearson Education Limited, England, 2002.
[Russel, 2003] S. Russell and P. Norvig Artificial
Intelligence: A Modern Approach Prentice Hall,
2003, Second Edition
[Patterson, 1990] D. W. Patterson, “Introduction
to Artificial Intelligence and Expert Systems”,
Prentice-Hall Inc., Englewood Cliffs, N.J, USA,
1990.
20
PROLOG
PROLOG (PROgramming in LOGic)
Invented by Alain Colmerauer, 1970.
Programming in PROLOG is accomplished by
creating a data base of facts and rules about
objects, their properties, and their relationships to
other objects.
21
PROLOG
sister(sue,bill)
parent(ann,sam)
parent(joe,ann)
male(joe)
female(ann)
grandfather(X,Z):-parent(X,Y), parent(Y,Z), male(X)
22
PROLOG
For all X,Y, and Z
X is the grandfather of Z
If X is the parent of Y, and
Y is the parent of Z, and
X is male
23
PROLOG
PROLOG means PROgramming in LOGic
–The programmer uses the system to draw
inferences from facts and rules
PROLOG is
–declarative - specify facts and logical
relationships
–symbolic - symbols are used to represent objects
–high level - contains a built-in problem solving
mechanism
PROLOG Programs
–solve problems by declaring objects and their
24
relationships
PROLOG Paradigm
The PROLOG Programmer
-Loads facts and rules into the database.
-Makes queries to the database to see if a fact is:
in the database or
can be implied from the facts and rules therein
Queries
Facts:
isa(dog, mammal).
isa = predicate
(dog, mammal) = argument PROLOG database
2 = binary
25 Facts Rules
PROLOG Paradigm
Predicates (clauses)
In this example, both “isa” and “animal” are examples of predicates.
Predicates are used to indicate relations between objects and hence can
represent FACTS and RULES.
Queries
Two ways to use predicates in a query:
–1. As a TRUE/FALSE test:
?- isa(dog, mammal).
yes
–2. For DATA RETRIEVAL PROLOG database
?- isa(dog, X).
X = mammal
Facts Rules
26
PROLOG syntax
Constants
-Atoms
Alphanumeric atoms - alphabetic character
sequence starting with a lower case letter
Examples: apple a1 apple_cart
Quoted atoms - sequence of characters
surrounded by single quotes
Examples: „Apple‟ „hello world‟
Symbolic atoms - sequence of symbolic
characters
Examples: & < > * - + >>
Special atoms
Examples: ! ; [ ] {}
-Numbers
Integers and Floating Point numbers
27
Examples: 0 1 9821 -10 1.3 -1.3E102
PROLOG syntax
Variable Names
A sequence of alphanumeric characters beginning
with an upper case letter or an underscore
Examples: Anything _var X
28
System Interaction
Reminder:
Problem solving in PROLOG
1. insert facts and rules into the database
2. ask questions (queries) based on the contents
of the database
Facts
Used to represent unchanging information about
objects and their relationships.
-Only facts in the PROLOG database can be used
for problem solving.
-Insert facts into the database by,
typing the facts into a file and loading
(consulting) the file into a running PROLOG
29
system
System Interaction
Queries
Retrieve information from the database by
entering QUERIES
-A query,
is a pattern that PROLOG is asked to match
against the database
has the syntax of a compound query
may contain variables
32
Simple Backtracking
PROLOG searches its database attempting
to satisfy a query (goal), stopping at the
first success or returning no for failure.
Often, there will be more than one
successful match and the programmer
would like to tell the PROLOG system to try
again and search for other successful
matches.
When PROLOG retries a goal, it is called
backtracking.
Backtracking may be forced by typing a ;
33
Simple Backtracking
38
Arithmetic Functions/ Predicates
Operators for the basic arithmetic operations
+, -, *, /, **, mod, // (integer division).
Examples:
?- 7+3=10. ? - X is 5/2
No ? - Y is 17//3.
?- 10 is 7+3. ?- Z is 23 mod 5.
Yes
Predicates for the basic arithmetic operations
<, =<, >, >=, =.=, =\=.
Examples:
?- 14-9=.=3+2. ? - 10 =\=7+5.
Yes Yes
39
PROLOG syntax … …
Lists
A sequence of terms of the form
[t1, t2, t3, t4, ..., tn]
where term ti is the ith element of the list
Examples:
[a,b,c] is a list of three elements a, b and c.
40
Recommended Textbooks
[Negnevitsky, 2001] M. Negnevitsky “ Artificial
Intelligence: A guide to Intelligent Systems”,
Pearson Education Limited, England, 2002.
[Russel, 2003] S. Russell and P. Norvig Artificial
Intelligence: A Modern Approach Prentice Hall,
2003, Second Edition
[Patterson, 1990] D. W. Patterson, “Introduction
to Artificial Intelligence and Expert Systems”,
Prentice-Hall Inc., Englewood Cliffs, N.J, USA,
1990.
41