Propositional logic example in Newlisp documentation

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Propositional logic example in Newlisp documentation

Post by jopython »

I was looking at the newlisp manual today, and this caught my eye. I want to take this example a step further.
Will it be possible to insert a function in the 'rules' database, so that i can make a query as follows?

Code: Select all

(query '(einstein minor))

See code below.

Code: Select all

(set 'facts '(
    (socrates philosopher)
    (socrates greek)
    (socrates human)
    (einstein german)
    (einstein (studied physics))
    (einstein human)
    (einstein (age 50))
))

(set 'rules '(
    ((X mortal) <- (X human))
    ((X (knows physics)) <- (X physicist))
    ((X physicist) <- (X (studied physics)))
    ((X minor)  <-   ; what should go here if i need check age is less than 18?
))


(define (query trm)
    (or  (if (find trm facts) true) (catch (prove-rule trm))))

(define (prove-rule trm)
    (dolist (r rules)
        (if (list? (set 'e (unify trm (first r))))
            (if (query (expand (last r) e))
                (throw true))))
    nil
)

#> (query '(socrates human))


cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Propositional logic example in Newlisp documentation

Post by cormullion »

I know little about this, but I suspect that

Code: Select all

(age 50)
isn't evaluated, just "matched". You might have to change the code so that there's another stage of evaluation. But perhaps Lutz can give you definitive answer....

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Propositional logic example in Newlisp documentation

Post by rickyboy »

jopython wrote:Will it be possible to insert a function in the 'rules' database ...
Lutz talks about that in the manual.
Lutz (at http://www.newlisp.org/downloads/newlisp_manual.html#unify) wrote:Larger PROLOG implementations also allow the evaluation of terms in rules. This makes it possible to implement functions for doing other work while processing rule terms. prove-rule could accomplish this testing for the symbol eval in each rule term.
(λx. x x) (λx. x x)

Locked