Prolog vs CLIPS: A Detailed Comparison with Examples

Classified in Computers

Written on in English with a size of 197.63 KB

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:

  1. State space with a state variable
  2. Perceived environment variable
  3. Allowable action for each agent
  4. Individual agent strategy
  5. Utility variable
  6. Goal to be achieved



Image

Related entries: