Intro. to AI CLIPS Tutorial

CLIPS stands for C Language Integrated Production System. It is an expert system shell developed by NASA from 1985 to 1998 and is now public domain software. This page provides an introduction to the CLIPS system and language.

Where to find CLIPS

If you have a CSEE account you can use CLIPS on grad and on the Sunray computers in the C4 lab. To obtain a copy for yourself go to: http://www.ghg.net/clips/CLIPS.html.

Running and exiting CLIPS

For UNIX or Linux users: To run CLIPS, from the command line enter "clips"
# grad: clips
        FuzzyCLIPS V6.10c (10/24/2002)
CLIPS> 

This is will leave you in the CLIPS expert system shell. To leave this shell type "(exit)".

 
CLIPS> (exit)
# grad: 

Data Types

There are eight primitive data types in CLIPS. These are float, integer, symbol, string, and four address types. Numbers (floats and integers) are represented using the usual syntax, with e for exponentiation.

Valid Numbers in CLIPS: 123, +12, 5.41, -4.32, 2.45e-10, 237e3

Strings are defined using double quotes, e.g.: "Mary" and "a\"quote"

All other sequences of characters that do not contain delimiters (<new line>, <tab> <,> <"> <(> <)> <∧> <|> <<> <~>), and do not start with ? or $? are symbols.

Valid Symbols in CLIPS: foo, B76-HI, 127A, 456-93-039, @+=-%, 2each

Numbers, strings, and symbols entered in CLIPS are treated as constants and always evaluate to themselves.

CLIPS> Mary
Mary
CLIPS> "Mary"
"Mary"
CLIPS> 3.14159
3.14159

Relations and Functions

Relations and functions can have any number of arguments and are written with the following syntax: (<function name> <arg1> <arg2>...)

To see how this works for functions, try entering the following arithmetic expressions in CLIPS:

(+ 3 4 5)
(* 5 6.0 2)
(+ 3 (* 8 9) 4)
(* 8 (+ 3 (* 2 3 4) 9) (* 3 4))

This is what arithmetic looks like in prefix notation, and it works because CLIPS comes with predefined functions called +, * which implement addition and multiplication for all numeric parameters. (Side note: What you are used to seeing (e.g. 3+4) is called infix notation. Try figuring out what the above expressions are in infix notation.)

Relations can be defined in a similar fashion:

(friends Claudia Frank)

Notice that if you enter this relation into CLIPS it mistakes it for an undefined function "friends".

CLIPS> (friends Claudia Frank)
 
[EXPRNPSR3] Missing function declaration for friends.

Instead you want to use predefined functions to handle relations as facts.

Facts

Facts are a set of relations that are known to be TRUE. In CLIPS they are stored in a fact-list.

The predefined function assert adds facts to CLIPS's fact-list, i.e. the list of facts that are known to be true.

CLIPS> (assert (friends Claudia Frank))
<Fact-0>

assert evaluates to, or returns, a reference to the fact called its address. This can be used to modify or remove the fact later.

To see the fact list use the function facts.

CLIPS> (facts)
f-0     (friends Claudia Frank) CF 1.00 
For a total of 1 fact.

To remove a fact you can use the retract function which requires either a fact address (this is used in rules) or the index of the fact in the fact-list.

CLIPS> (retract 0)
CLIPS> (facts)

Another way of defining a set of facts is to use the deffacts construction. The syntax is as follows:

(deffacts  "comment"
	fact0
	fact1
	...
	factn
)

<deffacts-name> is the name you give to this set of facts, and "comment" is some comment you want to attach to that set. For example try entering the following:

(deffacts exampleFacts "AI Class Example Facts"
        (lawyer Claudia)
        (works-for-advocacy-firm Claudia)
        (lawyer Frank)
        (friends Claudia Frank)
)

You can define any number of these fact sets. To see the sets that you have defined so far use the following predefined function: (ppdeffacts <deffacts-name>)

You can also list the fact set names using (list-deffacts), or delete a fact set using (undeffacts <deffacts-name>).

Defining a set of facts is not the same as asserting that they are TRUE. In order to do this you use the reset function. Try the following with the exampleFacts set defined.

CLIPS> (facts)
CLIPS> (reset)
CLIPS> (facts)
f-0     (initial-fact) CF 1.00 
f-1     (lawyer Claudia) CF 1.00 
f-2     (works-for-advocacy-firm Claudia) CF 1.00 
f-3     (lawyer Frank) CF 1.00 
f-4     (friends Claudia Frank) CF 1.00 
For a total of 5 facts.

Rules

Rules are implications that are defined using the defrule function. The syntax is as follows:
(defrule  "comment"
	[(salience )]
        fact0
        fact1
        ...
        factn
=>
	action0
	action1
	...
	actionn
)

The facts before the '=>' symbol are the conditions that must be TRUE before the actions following the '=>' symbol can be taken. For example the following states that if Claudia is a lawyer then she is rich:

(defrule rich-lawyer
        (lawyer Claudia)
=>
        (assert (rich Claudia))
)

Rules become more useful when they use variables, like the following which instead states that all lawyers are rich.

(defrule rich-lawyers 
        (lawyer ?x)
=> 
        (assert (rich ?x))
)

Notice that the variable x is preceded by a ? (this indicates that it is a variable) and that the conditions assign the value of that variable for the actions.

Once the set of facts and rules are established there are typically two things that are done with them:

They are applied. This is done using the run command.

CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact) CF 1.00 
f-1     (lawyer Claudia) CF 1.00 
f-2     (works-for-advocacy-firm Claudia) CF 1.00 
f-3     (lawyer Frank) CF 1.00 
f-4     (friends Claudia Frank) CF 1.00 
f-5     (rich Frank) CF 1.00 
f-6     (rich Claudia) CF 1.00 
For a total of 7 facts.

They are stored for future use. This is done using the save function which returns TRUE if there were no errors in the process of saving.

CLIPS> (save "example.clp")
TRUE

Now download the complete example from here.

You can load this file into CLIPS using the load function.

CLIPS> (load "clipsExample.clp")
TRUE

Use the functions you already know to assert the set of facts, apply the rules defined in the file, and check the resulting set of facts.


This page written by Jennifer Carlson, Thu Apr 7 11:19:22 EDT 2005