Artificial Inteligence
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
Function call Value Remarks
returned
(car `(a b c)) a Car takes one argument, a list, and returns
the first element.
(cdr `(a b c)) (b c) Cdr takes one argument, a list, and returns
a list with the first element removed.
(cons `a`(b c)) (a b c) Cons takes two arguments, an element and
a list and returns a list with the element ins
erted at the beginning.
(list `a`(b c)) (a (b c)) List takes any number arguments and retur
ns a list with the arguments as elements.
7
Basic List Manipulation Functions
Function call Value Remarks
returned
(append `(a) `(b c)) (a b c) Merge two or more lists into a single list
(last `(a b c d)) (d) return a list containing the last element.
(member `b `( a b d)) (b d) Returns remainder of second argument list
starting with element matching first argum
ent.
(reverse `(a (b c) d)) (d (b c) a) returns a list with top elements in reverse o
rder.
8
Example of (setq()) function
->(setq x `(a b c))
->x
ABC
->`x
X
->(setq x (+ 3 5))
8
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
Function call Value Remarks
returned
(atom `aabb) t aabb is a valid atom
(equal `a (car `(a b)) t
(evenp 3) nil
(oddp 3) t
(numberp 10ab) nil
(greaterp 2 4 27) t Arguments are successively larger
(lessp 5 3 12) nil Arguments are not successively smal
ler
(listp `(a)) t Valid list
(zerop .0001) nil
(null nil) t Nil is an empty set
11
Conditional Statement
The syntax for condition is, Example:
cond (<test1> <action1>) ->(defun maxthree(a b c)
(cond ((>a b) (cond ((>a c) a)
(<test2> <action2>)
(t c)))
. . ((> b c) b)
. . (t c)))
. . MAXTHREE
(<testk> <actionk>)) ->
->(maxthree 20 30 10)
30
->
12
Input, Output Variables
prinl: print without new line
princ: remove double quotation marks
read: ->(+ 5 (read)) terpri: it does not take arguments
6 It introduces a new line whenever it appears
and then returns nil.
11 ->(defun circle-area()
-> (terpri)
print: ->(print`(a b c)) (princ “Please enter the radius:”)
(A B C) (setq radius (read))
(princ “ The area of the circle is:”)
(A B C)
(princ (* 3.1416 radius radius))
->(print “hello”) (terpri))
“hello” CIRCLE-AREA
“hello”
13
Input, Output Variables
->(defun circle-area() ->(circle-area)
(terpri)
(princ “Please enter the radius:”) Please enter the radius: 4
The area of the circle is: 50.2656
(setq radius (read))
->
(princ “ The area of the circle is:”)
(princ (* 3.1416 radius radius))
(terpri))
CIRCLE-AREA
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
Compound Terms (structures)
an atom followed by an argument list containing
terms. The arguments are enclosed within
brackets and separated by commas
Example: isa(dog, mammal)
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
A query will cause PROLOG to
look at the database
try to find a match for the query pattern
execute the body of the matching head
30
return an answer
System Interaction
Example:
Assume that the database contains:
–likes(hasan, rita).
–likes(belal, rita).
–likes(mohsin, beli).
The actual system interaction looks like
?- likes(hasan, rita).
yes
?- likes(mohsin, rita).
no
?- likes(mohsin, X).
31
X = beli;
Variables in Prolog
Reminder: a variable starts with a capital
letter or underscore.
When a variable is used, PROLOG tries to find
a match (instantiation) for it.
?-likes(Who, rita).
Who = hasan;
Who = belal;
no
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
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 ;
34
Examples: Backtracking
?- likes(Who, rita). ?- likes(Who, beli).
Who = hasan ; Who = mohsin
Who = belal ; ?- likes(hasan, Whom).
no
Whom = rita ;
no
?- likes(Who, Whoelse). ?- likes(belal, Whom).
Who = hasan Whom = rita ;
Whoelse = rita ; no
Who = belal
Whoelse = rita ;
Who = mohsin
Whoelse = beli ;
no
35
Backtracking with Anonymous Variable
Suppose that you want to know if Hasan likes
anyone and do not care who in particular.
Then use the anonymous variable which is the
character “_”
It represents a different variable each time it occurs
?- likes(hasan, _).
yes
?- likes(_, _).
yes
Suppresses output of variable binding
36
Rules
A PROLOG rule consists of one conclusion
followed by one or more conditions
–a fact is a rule with no conditions
–conclusion :-
condition1,
.....
condition N.
–read as:
The conclusion is true if condition1 and
condition2... and condition N are true.
Conditions are separated by commas
Rules , facts and queries are all examples of
37
Horn clauses -
Rules
A PROLOG rule consists of a head and a
body
–head :-
body.
–read as:
If a match is made on the head, then
carry out the body.
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.
[[a,list,of,lists], and, numbers,[1,2,3]]
is a four element list.
[ ] is the ‘empty list’. It is an atom not a list data type
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