Propositional logic example in Newlisp documentation

Started by jopython, March 29, 2013, 02:17:10 PM

Previous topic - Next topic

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?

(query '(einstein minor))




See code below.



(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

#1
I know little about this, but I suspect that (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

#2
Quote from: "jopython"Will it be possible to insert a function in the 'rules' database ...

Lutz talks about that in the manual.
http://www.newlisp.org/downloads/newlisp_manual.html#unify)" url="http://www.newlisp.org/downloads/newlisp_manual.html#unify%29">
Quote from: "Lutz (at http://www.newlisp.org/downloads/newlisp_manual.html#unify)"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)