Page 1 of 1

Propositional logic example in Newlisp documentation

Posted: Fri Mar 29, 2013 9:17 pm
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))


Re: Propositional logic example in Newlisp documentation

Posted: Sat Mar 30, 2013 6:14 pm
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....

Re: Propositional logic example in Newlisp documentation

Posted: Sat Mar 30, 2013 8:28 pm
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.