Comments to Dr. Riggs: drkriggs at comcast.net
These are the examples we did in class plus a few more. Please try and advise me of any errors or other questions.
(The formal methods people will find out how to discover errors / prove correctness!)
; "hello world" clips
(defrule helloWorld
=>
(printout t "Hello world!" crlf))
; re-write x's to y's
(deffacts originalList
(list x y a m n x)
(alphaBeta x r)
)
(defrule x2y
(list $?pfx ?alpha $?sfx)
(alphaBeta ?alpha ?beta)
=>
(assert (list $?pfx ?beta $?sfx)))
; re-write x's to y's
(deffacts originalList
(list x y a m n x)
(alphaBeta x r)
)
(defrule x2y
(list $?pfx ?alpha $?sfx)
(alphaBeta ?alpha ?beta)
=>
(assert (list $?pfx ?beta $?sfx)))
;; do a transpositon code
(deffacts clearText+code
(clearText t h i s i s a t e s t)
; partial transposition (+1) code
(code t u)
(code h i)
(code i j)
(code s t)
(code a b)
(code e d)
(codeText)
)
(defrule encode
?f<-(clearText ?ltr $?rest)
(code ?ltr ?code)
?g<-(codeText $?coded)
=>
(retract ?f ?g)
(assert (clearText $?rest)
(codeText $?coded ?code)))
; solve search where
; inputs as (target <t>)
; (list <l1> <l2> ... <lm>)
(defrule found
(target ?t)
(list $?a ?t $?)
=>
(printout t "found " ?t " at " (+ (length$ $?a) 1) crlf))
(defrule notFound
(target ?t)
(not (list $? ?t $?))
=>
(printout t ?t " is not there!" crlf))
(deffacts eg1
(list 5 7 9 2 3 1)
(target 8))
;; sort indexed set of values (val 1 <x>} (val 2 <y>) .... (val <n> <z>)
;; with (target ?t)
(defrule found
(target ?t)
(val ?i ?t)
=>
(printout t ?t " found at " ?i crlf))
(deffacts example
(val 1 4)
(val 2 6)
(val 3 2)
(val 4 4)
(target 4)
)
;; the mother (most general in terms of possible 'traces') of exchange sorts
(deffacts example
(list 6 5 4 3 2 1)
)
(defrule sort
?f<-(list $?a ?x $?b ?y $?c)
(test (< ?y ?x))
=>
(retract ?f) ; else we get a lot of useless duplication!
(printout t " changed from " (create$ $?a ?x $?b ?y $?c) crlf)
(assert (list $?a ?y $?b ?x $?c)))
(defrule reportFinal
(declare (salience -20))
(list $?l)
=>
(printout t "sorted list: " $?l crlf))
;; prefix expression parser
; grammar: PEXPR := NUMBER | BINOP PEXPR PEXPR
; NUMBER := INTEGER
(deffacts examle
(PEXPR - * 3 4 + 5 6)
)
(defrule isInteger
?g<-(PEXPR $?a ?x $?b)
(test (integerp ?x))
=>
(retract ?g)
(assert (PEXPR $?a INTEGER $?b)))
(defrule binop
?f<-(PEXPR $?a + | - | * | / $?b)
=>
(retract ?f)
(assert (PEXPR $?a BINOP $?b)))
(defrule isNumber
?f<-(PEXPR $?A INTEGER $?B)
=>
(retract ?f)
(assert (PEXPR $?A NUMBER $?B)))
(defrule isNumber
?f<-(PEXPR $?A NUMBER $?B)
=>
(retract ?f)
(assert (PEXPR $?A PEXPR $?B)))
(defrule isPEXPR
?f<-(PEXPR $?A BINOP PEXPR PEXPR $?B)
=>
(retract ?f)
(assert (PEXPR $?A PEXPR $?B)))
;; prefix expression evaluator (incomplete - see if you can finish it!)
(deffacts examle
(PEXPR - * 3 4 + 5 6)
)
; is this needed now? (assume parse was valid)
(defrule literalIsInteger
?g<-(PEXPR $?a ?x $?b)
(test (integerp ?x))
=>
(retract ?g)
(assert (PEXPR $?a INTEGER $?b)))
(defrule binop+
?f<-(PEXPR $?a + ?x ?y $?b)
(test (and (integerp ?x)
(integerp ?y)))
=>
(retract ?f)
(assert (PEXPR $?a (+ ?x ?y) $?b)))
(defrule binop-
?f<-(PEXPR $?a + | - | * | / $?b)
=>
(retract ?f)
(assert (PEXPR $?a BINOP $?b)))
(defrule binop*
?f<-(PEXPR $?a + | - | * | / $?b)
=>
(retract ?f)
(assert (PEXPR $?a BINOP $?b)))
(defrule binop/
?f<-(PEXPR $?a + | - | * | / $?b)
=>
(retract ?f)
(assert (PEXPR $?a BINOP $?b)))
(defrule isNumber
?f<-(PEXPR $?A INTEGER $?B)
=>
(retract ?f)
(assert (PEXPR $?A NUMBER $?B)))
(defrule isNumber
?f<-(PEXPR $?A NUMBER $?B)
=>
(retract ?f)
(assert (PEXPR $?A PEXPR $?B)))
(defrule gotValue
(PEXPR ?x)
=>
(printout t "value is: " ?x crlf))