Introduction to the CLIPS Programming Language


Author: Jack L. Watkin

CPS 499-03: Emerging Languages, Spring 2017


History of CLIPS

  • CLIPS = C Language Integrated Production System
  • production system: a computer system that relies on facts and rules to guide its decision making.
  • developed at NASA in 1980s as a tool to define expert systems
  • CLIPS is a nonprocedural, declarative, and rule-based programming language.
  • The following figure situates CLIPS in relation to other programming paradigms and languages [ESPP]:

    (adapted from [ESPP])


Expert Systems

  • expert system: a computer program capable of modeling (i.e., emulating) the knowledge of a human expert.
  • knowledge composed of two elements: facts and rules.
  • fact: an axiom that is asserted to be true (e.g., `It is raining.')
  • rule: a pattern-action declaration based on a fact (or other rules) expressed in the form of as an IF ... THEN ... statement (e.g., `If it is raining, then I carry an umbrella.')
  • Example in CLIPS:
    (defrule weather
       (weather raining)
       =>
       (assert (carrying umbrella)))
    


CLIPS and PROLOG

  • PROLOG = PROgramming + LOGic
  • PROLOG: Reasoning from a hypothesis (goal) by looking for facts to support that hypothesis (backward chaining) [ESPP].
  • CLIPS: Reasoning from facts to conclusions based on those facts (forward chaining) [ESPP].
  • PROLOG searches for a path to prove a hypothesis, while CLIPS applies facts to rules to make inferences.


CLIPS under the hood

  • CLIPS expert systems rely on facts, rules, and an inference engine.
  • Logical view of the CLIPS system [ESPP]:


    (adapted from [ESPP])

  • Match-Resolve-Act Cycle: a loop that the inference engine runs to process CLIPS programs.
  • efficient pattern matching: facts built into an efficient data structure to aid in speed of pattern matching (Rete Algorithm).
  • conflict resolution: order to which all pattern-matched actions should be run.
  • execution of the right-hand-side of rules: perform the THEN part of the rule.


CLIPS from the command line

  • clips command invokes the CLIPS shell (Linux/UNIX).
  • clips -f can be used to load CLIPS code from .
  • Sample CLIPS session:
    $ clips
    
    CLIPS (V6.24 06/15/06)
    
    CLIPS> (assert (initial fact))
    <Fact-0>
    CLIPS> (assert (day monday))
    <Fact-1>
    
    CLIPS> (facts)
    f-0     (initial fact)
    f-1     (day monday)
    For a total of 2 facts.
    
    CLIPS> (retract 1)
    CLIPS> (facts)
    f-0     (initial fact)
    For a total of 1 fact.
    
    CLIPS> (watch facts)
    CLIPS> (assert (day monday))
    ==> f-2     (day monday)
    <Fact-2>
    
    CLIPS> (facts)
    f-0     (initial fact)
    f-2     (day monday)
    For a total of 2 facts.
    
    CLIPS>(clear)
    CLIPS>(facts)
    CLIPS>(unwatch facts)
    CLIPS>(assert (rainy day))
    CLIPS>(defrule weather (rainy day) => (assert (carry umbrella)))
    CLIPS>(facts)
    f-0      (rainy day)
    
    CLIPS>(run)
    CLIPS>(facts)
    f-0      (rainy day)
    f-1      (carry umbrella)
    
    CLIPS>(exit)
    


Syntax

  • general rule syntax [CLUG]:
    (defrule rule_name 
    (pattern_1) ; IF Condition 1 (pattern_2) ; And Condition 2 . . (pattern_N) ; And Condition N => ; THEN (action_1) ; Perform Action 1 (action_2) ; And Action 2 . . (action_N)) ; And Action N
  • pattern: a fact that can be pattern matched.
  • action: assertion of facts or side-effect (e.g., an assignment or I/O).
  • all variables prefixed with ? (e.g., ?x)
  • variables need not be explicitly declared, but must be instantiated before used
  • all operations are expressed in prefix notation
  • when comparisons are conditions use: (test (> 5 0))
  • working example of CLIPS:
    $ cat factorial.clp
    
    (defrule factorial
       (fact_run ?x)
       =>
       (assert (fact ?x 1)))
    
    (defrule fact_helper
       (fact ?x ?y)
       (test (> ?x 0))
       =>
       (assert (fact (- ?x 1) (* ?x ?y))))
    
    (assert (fact_run 5))
    
    $ clips -f factorial.clp 
    
    CLIPS (V6.24 06/15/06) 
    CLIPS> (defrule factorial 
       (fact_run ?x) 
       => 
       (assert (fact ?x 1))) 
    CLIPS> (defrule facthelper 
       (fact ?x ?y) 
       (test (> ?x 0)) 
       => 
       (assert (fact (- ?x 1) (* ?x ?y)))) 
    
    CLIPS> (assert (fact_run 5)) 
    <Fact-0>
    
    CLIPS> (facts) 
    f-0 (fact_run 5) 
    For a total of 1 fact.
    
    CLIPS> (run) 
    CLIPS> (facts) 
    f-0     (fact_run 5)
    f-1     (fact 5 1)
    f-2     (fact 4 5)
    f-3     (fact 3 20)
    f-4     (fact 2 60)
    f-5     (fact 1 120)
    f-6     (fact 0 120)
    For a total of 7 facts.
    
    CLIPS> 
    
  • The rule facthelper matches a fact consisting of factorial followed by two values.
  • Through pattern matching, ?x and ?y are bound.
  • Once ?x and ?y are bound, they cannot be rebound within the same match (but may be rebound on subsequent matches to the rule).
  • Variables can also be defined globally using the defglobal keyword.
  • Global variables can be assigned in the action part of the rule.
  • Note: this is not exactly recursion. Rather a new fact is declared that matches the condition of the rule causing the action of the rule to be re-run.


Templates

  • Templates are used to associate related data in a single package (similar to stucts in C).
  • Templates are containers of facts where each fact is a slot in the template.
  • Templates can be pattern matched like facts to produce databases; rules can be used to query the database.
  • Example:
  • (deftemplate car
       (slot make
         (type SYMBOL)
         (allowed-symbols
          truck compact)
         (default compact))
       (multislot name
          (type SYMBOL)
          (default ?DERIVE)))
    
    (deffacts cars
       (car (make truck)
            (name Tundra))
       (car (make compact)
            (name Accord))
       (car (make compact)
            (name Passat)))
    
    (defrule compactcar
       (car (make compact) ;select only cars that are compact
             (name ?name))
       =>
       (printout t ?name crlf))
    


Conditional Facts in Rules

    &, |, and ~ can be used as logical and, or, and not, respectively.
    (defrule walk
       (light ~red&~yellow) ;if the light 
                            ;isn't yellow and 
                            ;isn't red
       (cars none|stopped)  ;no cars or stopped
       =>
       (printout t "Walk" crlf))
    


Useful Links & Resources


Exercises

The following are programming exercises that incorporate essential CLIPS concepts:
  • Build a state machine using CLIPS that accepts a language L consisting of strings in which the number of a's in the string is a multiple of three over an alphabet {a,b}. Use the following state machine:

    Examples:
    CLIPS> (run)
    String? aaabba
    Rejected
    
    CLIPS> (reset)
    CLIPS> (run)
    String? aabbba
    Accepted
    
  • Rewrite the following factorial program in CLIPS so that only the fact with the final result of the factorial rule is stored in the fact list. Hint: retract can be used to remove facts from the fact list.

    (defrule factorial
       (fact_run ?x)
       =>
       (assert (fact ?x 1)))
    
    (defrule fact_helper
       (fact ?x ?y)
       (test (> ?x 0))
       =>
       (assert (fact (- ?x 1) (* ?x ?y))))
    
    (assert (fact_run 5))
    
    Examples:
    CLIPS> (assert (fact_run 5))
    CLIPS> (run)
    CLIPS> (facts)
    f-0  (fact_run 5)
    f-1  (fact 0 120)
    


References

    [CLUG] J.C. Giarratano. 2008. CLIPS User's Guide. The MIT Press, Cambridge, MA.
    [ESPP] J. C. Giarratano and G. Riley. Expert systems principles and programming, Third Edition. PWS Publishing Company, Boston, MA, 1998.

Return Home