Prolog vs CLIPS: Comparison of Syntax, Rules and Search Strategies
Summary
This document compares Prolog and CLIPS, two languages used in artificial intelligence and expert systems. It contrasts their facts and rules through family-tree examples, shows a rule-based recommendation system, and demonstrates breadth-first and depth-first search in CLIPS. It closes with the theoretical components of an intelligent agent.
Key points
- Prolog and CLIPS are both used in artificial intelligence and expert systems development.
- In Prolog, facts are written as predicates such as father(tom, john) and rules use the :- operator.
- In CLIPS, facts are defined with deffacts and rules with defrule, using => to separate conditions from actions.
- CLIPS can assert new facts such as parent and grandparent through forward-chaining rules.
- CLIPS strategies such as breadth and depth control the order in which rules fire during a search.
- The document lists the theoretical components of an agent: state space, perceived environment, actions, strategy, utility and goal.
Prolog vs. CLIPS: A Detailed Comparison
This document provides a comparison between Prolog and CLIPS, two popular languages used in artificial intelligence and expert systems development. We will explore their syntax, features, and applications through examples.
Facts and Rules
Prolog:
Facts:
father(tom, john). % tom is father of john
mother(susan, john). % susan is mother of john
father(george, tom). % george is father of tom
Rules:
parent(X, Y) :- father(X, Y), mother(X, Y).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
grandfather(X, Z) :- father(X, Y), parent(Y, Z).
grandmother(X, Z) :- mother(X, Y), parent(Y, Z).
CLIPS:
Facts:
(deffacts families
(father tom john) ; tom is father of john
(mother susan john) ; susan is mother of john
(father george tom)) ; george is father of tom
Rules:
(defrule parent-rule
(or (father ?x ?y) (mother ?x ?y))
=>
(assert (parent ?x ?y)))
(defrule grandparent-rule
(and (parent ?x ?y) (parent ?y ?z))
=>
(assert (grandparent ?x ?z)))
(defrule grandfather-rule
(and (father ?x ?y) (parent ?y ?z))
=>
(assert (grandfather ?x ?z)))
Result Example
CLIPS:
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (father tom john)
f-2 (mother susan john)
f-3 (father george tom)
f-4 (parent george tom)
f-5 (parent susan john)
f-6 (parent tom john)
f-7 (grandparent george john)
f-8 (grandfather george john)
For a total of 9 facts.
Table Example
(defrule Sencor-1
(weed B)
(crop C | S)
(organic-matter 1)
=>
(printout t crlf "Do not use Sencor!" crlf))
(defrule Sencor-2
(weed B)
(crop C | S)
(organic-matter 2 | 3)
=>
(printout t crlf "Use 3/4 pt/ac of Sencor" crlf))
(defrule Lasso-1
(weed B | G)
(crop C | S)
(organic-matter 1)
=>
(printout t crlf "Use 2 pt/ac of Lasso" crlf))
etc.
(defrule input
(initial-fact)
=>
(printout t crlf "What is the crop? (C: corn, S: soybean) ")
(assert (crop =(read))) ;this line reads what the user types
(printout t crlf "What is the weed problem? (B: broadleaf, G: grass) ")
(assert (weed =(read)))
(printout t crlf "What is the percentage of organic matter content? ")
(printout t crlf "(1: 4%) ")
(assert (organic-matter =(read)))
(printout t crlf "RECOMMENDATIONS:" crlf))
Result
CLIPS> (reset)
CLIPS> (run)
What is the crop? (C: corn, S: soybean) C
RECOMMENDATIONS:
Use 3/4 pt/ac of Sencor
CLIPS> (dribble-off)
Primitive Templates and Rules
(deftemplate primitive
(slot contents))
;; productions
(defrule rule1
(primitive (contents a))
(primitive (contents b))
=>
(assert (primitive (contents c))))
(defrule rule2
(primitive (contents b))
(primitive (contents g))
=>
(assert (primitive (contents d))))
(defrule rule3
(primitive (contents a))
(primitive (contents c))
=>
(assert (primitive (contents e))))
(defrule rule4
(primitive (contents e))
(primitive (contents d))
=>
(assert (primitive (contents f))))
(defrule rule5
(primitive (contents d))
(primitive (contents g))
=>
(assert (primitive (contents f))))
;; check if goal reached (f in wm)
(defrule check-f
(primitive (contents f))
=>
(halt))
;; initial wm data
(deffacts startup
(primitive (contents a))
(primitive (contents b))
(primitive (contents g)))
Breadth-First Search
CLIPS> (set-strategy breadth)
depth
CLIPS> (get-strategy)
breadth
CLIPS> (load "clips-3.3.7-search.clips")
Defining deftemplate: primitive
Defining defrule: rule1 +j+j
Defining defrule: rule2 +j+j
Defining defrule: rule3 =j+j
Defining defrule: rule4 +j+j
Defining defrule: rule5 +j+j
==> f-5 (primitive (contents d))
FIRE 3 rule3: f-1,f-4
==> f-6 (primitive (contents e))
FIRE 4 rule5: f-5,f-3
==> f-7 (primitive (contents f))
FIRE 5 rule4: f-6,f-5
FIRE 6 check-f: f-7
[PRCCODE4] Execution halted during the actions of defrule check-f.
Depth-First Search
CLIPS> (get-strategy)
depth
CLIPS> (load "clips-3.3.7-search.clips")
Defining deftemplate: primitive
Defining defrule: rule1 +j+j
Defining defrule: rule2 +j+j
Defining defrule: rule3 =j+j
Defining defrule: rule4 +j+j
Defining defrule: rule5 +j+j
Defining defrule: check-f +j
Defining deffacts: startup
TRUE
CLIPS> (reset)
==> f-0 (initial-fact)
==> f-1 (primitive (contents a))
==> f-2 (primitive (contents b))
==> f-3 (primitive (contents g))
CLIPS> (run)
FIRE 1 rule2: f-2,f-3
==> f-4 (primitive (contents d))
FIRE 2 rule5: f-4,f-3
==> f-5 (primitive (contents f))
FIRE 3 check-f: f-5
[PRCCODE4] Execution halted during the actions of defrule check-f.
Theoretical Components
Theoretical components:
- State space with a state variable
- Perceived environment variable
- Allowable action for each agent
- Individual agent strategy
- Utility variable
- Goal to be achieved
Download
PDF Word HTMLFAQ
What is the difference between facts in Prolog and CLIPS?
In Prolog, facts are written as predicates ending in a period, such as father(tom, john). In CLIPS, they are grouped in a deffacts block and written as lists, such as (father tom john).
How does CLIPS run rules?
After (reset) loads the initial facts, (run) fires the rules whose conditions match the current facts, asserting new facts until no more rules apply.
What is the difference between breadth-first and depth-first search in CLIPS?
The breadth strategy fires rules in the order they were activated, exploring shallow states first. The depth strategy fires the most recently activated rules first, exploring deeper paths before backtracking.
Test yourself
Flashcards
Click a card to see the answer.
Quiz
Questions generated by AI from the document: check the answers against the text.